Sign in to follow this  
Followers 0
antmar904

GUI Save/Print Button

4 posts in this topic

hi all,

i am very new to autoit.

i created a small gui that show some system info.

how would i create a button that will save system info to a txt file and a button that will print the system info:

here is what i have so far:

script

#include <GUIConstants.au3>

info()

Func info()

Local $hGUI, $hLABEL, $Button_1, $Button_2, $var, $MyDocsFolder

$hGUI = GUICreate("System Information")

$hLABEL = GUICtrlCreateLabel("* Operating System",10,20)

$hLABEL = GUICtrlCreateLabel("System Information",150,0)

$hLABEL = GUICtrlCreateLabel("Type:",10,40)

$hLABEL = GUICtrlCreateLabel(@OSType,100,40)

$hLABEL = GUICtrlCreateLabel("Version:",10,60)

$hLABEL = GUICtrlCreateLabel(@OSVersion,100,60)

$hLABEL = GUICtrlCreateLabel("Architecture:",10,80)

$hLABEL = GUICtrlCreateLabel(@OSArch,100,80)

$hLABEL = GUICtrlCreateLabel("Service Pack:",10,100)

$hLABEL = GUICtrlCreateLabel(@OSServicePack,100,100)

$hLABEL = GUICtrlCreateLabel("* Computer System",10,130)

$hLABEL = GUICtrlCreateLabel("Computer Name:",10,150)

$hLABEL = GUICtrlCreateLabel(@ComputerName,100,150)

$hLABEL = GUICtrlCreateLabel("IP Address:",10,170)

$hLABEL = GUICtrlCreateLabel(@IPAddress1,100,170)

$hLABEL = GUICtrlCreateLabel("* Logged On User",10,200)

$hLABEL = GUICtrlCreateLabel("User Name:",10,220)

$hLABEL = GUICtrlCreateLabel(@UserName,100,220)

$Button_1 = GUICtrlCreateButton("Print",300,370)

$Button_2 = GUICtrlCreateButton("Save",340,370)

GUISetState()

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $Button_1

Case $msg = $Button_2

FileSaveDialog("Save",$MyDocsFolder, "Text file (*.txt)", 2)

EndSelect

WEnd

EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Look in the help file to see examples for FileWriteLine and _FilePrint. They'll show you how to do what you want for starters.

Edited by wyzzard

Share this post


Link to post
Share on other sites

Here's one way of doing it, it's not the cleanest display, but that can be tweaked how you'd want it to look. You had all the labels reusing the same variable name for the control ID, so I turned it all into one label using one control ID instead. To print the text file, or the label's contents, you can search the forum how to do that.

#include <GUIConstants.au3>
info()
Func info()
     Local $hGUI, $hLABEL, $Button_1, $Button_2, $var, $MyDocsFolder
     $hGUI = GUICreate("System Information")
     $hLABEL = GUICtrlCreateLabel("* Operating System" & @CRLF & _
               "System Information" & @CRLF & "Type:" & @OSType & @CRLF & _
               "Version:" & @OSVersion & @CRLF & "Architecture:" & @OSArch & @CRLF & _
               "Service Pack:" & @OSServicePack & @CRLF & @CRLF & "* Computer System" & @CRLF & @CRLF & _
               "Computer Name:" & @ComputerName & @CRLF & "IP Address:" & @IPAddress1 & @CRLF & _
               "* Logged On User" & @CRLF & "User Name:" & @UserName, 10, 10, 200, 300)
     $Button_1 = GUICtrlCreateButton("Print", 300, 370)
     $Button_2 = GUICtrlCreateButton("Save", 340, 370)
     GUISetState()
     While 1
          $msg = GUIGetMsg()
          Select
               Case $msg = $GUI_EVENT_CLOSE
                    ExitLoop
               Case $msg = $Button_1
               Case $msg = $Button_2
                    Local $File = FileSaveDialog("Save", $MyDocsFolder, "Text file (*.txt)", 2)
                    Local $hFile = FileOpen($File, 2)
                    FileWrite($hFile, GUICtrlRead($hLABEL))
                    FileClose($hFile)
          EndSelect
     WEnd
EndFunc   ;==>info

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

(...)To print the text file, or the label's contents, you can search the forum how to do that.

Or use this snippet:

; #FUNCTION# ====================================================================================================================
; Name ..........: _PrintFileOrText
; Description ...: Simple support printing files or texts!
; Syntax ........: _PrintFileOrText($vValue[, $fWaiting = True])
; Parameters ....: $vValue            - File name or texto!
;                 $fWaiting         - [optional] If True, wait to impression. Default is True.
; Return values .: None
; Author ........: JScript
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _PrintFileOrText($vValue, $fWaiting = True)
    Local $sFileName, $iRandom = 0

    If FileExists($vValue) Then
        $sFileName = $vValue
    Else
        $iRandom = 1
        $sFileName = @TempDir & "" & Random(111111, 999999, 1) & ".tmp" ; Generates a random file name...
        ; Writes the contents to be printed...
        $hFileOpen = FileOpen($sFileName, 2)
        FileWrite($hFileOpen, $vValue)
        FileClose($hFileOpen)
    EndIf

    Switch $fWaiting
        Case True
            RunWait(@SystemDir & "notepad.exe /p " & FileGetShortName($sFileName), "", @SW_HIDE)
        Case False
            Run(@SystemDir & "notepad.exe /p " & FileGetShortName($sFileName), "", @SW_HIDE)
    EndSwitch
    If $iRandom Then FileDelete($sFileName)

    Return 1
EndFunc   ;==>_Print

Regards,

João Carlos.


http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0