Jump to content

Save a gui and saving its handle and then recalling it


Recommended Posts

I am completely stumped on this. I am trying to "save" a gui/window and recall it later. I have three windows and they have text in each of them, one saying Window X, the other saying Window Y, and the last saying Window Z. What I'm trying to do is for example, I right click on the window that says Window X. and in the context menu there is an option for me to "Save" it. what i want to happen is when i click that option it will throw up and inputbox for me to type 'Window X'. It would then write in an INI file Window X as the key and the handle as the value, etc. Later when I use my program I would go into my main program's window and type in an edit: "Window X" and press a button. I would want it to recall or create the same window I was using before. I hope this is not too confusing.

Here's an example with edits instead but basically i hope you get the idea. Help?

Here is exactly what I need my program to do.

I am using my program to assist me with another application. For example a poker client. I have many people sitting at the table and I want to assign images to them. I do this by right clicking on the images and in the context menu I click 'Assign'. Then an inputbox comes up where I type the players name that I want to assign that image to, in this case 'John'. I press ok and it then writes in my ini 'John' as the key and the value would be the controlID of the image. My problem is how would my program know which image to assign to the name 'John' when I may have many images and the controlID is never the same? Help would be great.

#include <GuiConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <GUIMENU.au3>

Global $idSave[6]
For $i = 0 To 5
    $idSave[$i] = 1000 + $i
Next
Global $wProcOld
Global $lastdragIP = -1
Dim $hInput_GUI[6], $Input[6], $wProcsOld[6], $InputMenu[6], $hMenu[6], $idSave[6]
Dim $button
$gui = GUICreate("", 400, 400)

$button = GUICtrlCreateButton("Recall", 100, 100, 50, 50)
GUISetState()

$wProcHandle = DllCallbackRegister("_WindowProc", "ptr", "hwnd;uint;wparam;lparam")

$toDelete = 0
$iW = -1
Global $totCreated = 0
While 1
    $msg = GUIGetMsg(1)
    $iW = -1
    For $i = 0 To 5
        If $msg[1] = $hInput_GUI[$i] Then
            $iW = $i

            If $msg[0] = $GUI_EVENT_PRIMARYDOWN Then
                $sel = -1

                GUISwitch($hInput_GUI[$iW])
                $aCursorInfo = GUIGetCursorInfo()

                If Not IsArray($aCursorInfo) Then ContinueLoop
                $aMouse_Pos = MouseGetPos()
                $aInputGUI_Pos = WinGetPos($hInput_GUI[$iW])
                While IsArray($aCursorInfo) And $aCursorInfo[2] = 1
                    $aCursorInfo = GUIGetCursorInfo()
                    $aCurrent_Mouse_Pos = MouseGetPos()

                    WinMove($hInput_GUI[$iW], "", _
                            $aInputGUI_Pos[0] - $aMouse_Pos[0] + $aCurrent_Mouse_Pos[0], _
                            $aInputGUI_Pos[1] - $aMouse_Pos[1] + $aCurrent_Mouse_Pos[1])
                WEnd

            EndIf

            ExitLoop
        EndIf
    Next
        Switch $msg[0]
            Case $GUI_EVENT_CLOSE
                Exit
            Case $button
                If $totCreated < 6 Then
                    For $i = 0 To 5
                        If $hInput_GUI[$i] = 0 Then
                            createNextdragIP($i)
                            $totCreated += 1
                            ExitLoop
                        EndIf
                    Next
                Else
                    MsgBox(262144, "MAX Reached", "You are omnly allowed 6 Edits!")
                EndIf


        EndSwitch

WEnd
DllCallbackFree($wProcHandle)

Func createNextdragIP($nw)

    $start = WinGetPos($gui)
    $hInput_GUI[$nw] = GUICreate("No. " & $nw + 1, 120, 22, 300 + 25 * $nw, 300 + 25 * $nw, BitOR($WS_POPUP, $WS_SIZEBOX), $WS_EX_TOOLWINDOW)
    $Input[$nw] = GUICtrlCreateEdit("", 0, 0, 120, 22, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN))
    GUICtrlSetResizing(-1, $GUI_DOCKAUTO)

    $hMenu = _GUICtrlMenu_CreatePopup()
    $InputMenu[$nw] = $hMenu

    _GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave[$nw])
    ConsoleWrite("90" & @CRLF)
    GUISetState()

    $wProcsOld[$nw] = _WinAPI_SetWindowLong(GUICtrlGetHandle($Input[$nw]), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))
EndFunc;==>createNextdragIP


Func _WindowProc($hWnd, $msg, $wParam, $lParam)
    Local $wProcOld

    For $i = 0 To UBound($Input) - 1
        If GUICtrlGetHandle($Input[$i]) = $hWnd Then
            Switch $msg
                Case $WM_CONTEXTMENU
                    _GUICtrlMenu_TrackPopupMenu($InputMenu[$i], $wParam)
                    Return 0
                Case $WM_COMMAND
                    Switch $wParam
                        Case $idSave[$i]
                          ; DONT KNOW WHAT TO PUT HERE?
                    EndSwitch
            EndSwitch
            $wProcOld = $wProcsOld[$i]
            ExitLoop
        EndIf
    Next

    Local $aRet = DllCall("user32.dll", "int", "CallWindowProc", "ptr", $wProcOld, _
            "hwnd", $hWnd, "uint", $msg, "wparam", $wParam, "lparam", $lParam)
    Return $aRet[0]
EndFunc;==>_WindowProc
Edited by coder09
Link to comment
Share on other sites

  • Moderators

coder09,

Do you have to save the GUI info to an INI file? If your main program stays running all the time, why not just hide the secondary GUI you have created until you need to show it again?

I have quickly modded this code I found in my Snippets folder (credit to original author if only I knew who it was!) to show how this might be implemented. Just create a number of children and then use the input to decide which to Delete/Hide/Recall:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

Global $gui[100000]
$x = 1

$Mother_GUI = GUICreate("Multiple box", 500, 500, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$Input_1 = GUICtrlCreateInput("1", 5, 5, 70, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button_1 = GUICtrlCreateButton("New Box", 5, 30, 70, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button_2 = GUICtrlCreateButton("Delete Box", 5, 60, 70, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button_3 = GUICtrlCreateButton("Hide Box", 5, 90, 70, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$Button_4 = GUICtrlCreateButton("Recall Box", 5, 120, 70, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)

GUISetState(@SW_SHOW, $Mother_GUI)

$Main_GUI = GUICreate("", 220, 300, 80, 5, $WS_POPUPWINDOW)
GUISetBkColor(0xCCCCCC)
GUISetState(@SW_SHOW, $Main_GUI)
DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($Main_GUI), "hwnd", WinGetHandle($Mother_GUI))
Makebox()
While 1
    setsize()
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_1
            Makebox()
        Case $Button_2
            Deletebox()
        Case $Button_3
            Hidebox()
        Case $Button_4
            Recallbox()
    EndSwitch
    Sleep(10)
WEnd

Func Makebox()
    If $x < 100000 Then
        $gui[$x] = GUICreate("", 200, 90, 100 + $x * 5, 100 + $x * 5, $WS_POPUPWINDOW, $WS_EX_TOOLWINDOW)
        $label = GUICtrlCreateLabel("  Box number: " & $x, 0, 0, 200, 20, -1, $GUI_WS_EX_PARENTDRAG)
        GUICtrlSetFont(-1, 12, 14, 0, "Comic Sans MS")
        GUICtrlCreateEdit("", 0, 20, 200, 70, $WS_VSCROLL)
        GUISetState(@SW_SHOW, $gui[$x])
        DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($gui[$x]), "hwnd", WinGetHandle($Main_GUI))
        $x = $x + 1
    EndIf
EndFunc  ;==>Makebox

Func Deletebox()
    $n = GUICtrlRead($Input_1)
    If $n <= $x Then GUIDelete($gui[$n])
EndFunc  ;==>Deletebox

Func HideBox()
    $n = GUICtrlRead($Input_1)
    If $n <= $x Then GUISetState(@SW_HIDE, $gui[$n])
EndFunc  ;==>HideBox

Func Recallbox()
    $n = GUICtrlRead($Input_1)
    If $n <= $x Then GUISetState(@SW_SHOW, $gui[$n])
EndFunc  ;==>Recallbox

Func setsize()
    $Motherpos = WinGetPos($Mother_GUI)
    $Mainpos = WinGetPos($Main_GUI)
    If $Mainpos[2] <> $Motherpos[2] - 98 Or $Mainpos[3] <> $Motherpos[3] - 40 Then WinMove($Main_GUI, "", 85, 5, $Motherpos[2] - 98, $Motherpos[3] - 45)
EndFunc  ;==>setsize

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The thing is my main program doesn't run all the time. I may run it in sessions. It's hard to explain, I need the ini file to know which edit (pretend they were all different) was saved (in this case Window X) and then whenever I want I can recall it I type Window X in the input, which then searches that strings value in the ini and then finds it to be window x's control Id. All using window x's specific control ID in the ini to recall it.

Edited by coder09
Link to comment
Share on other sites

  • Moderators

coder09,

If your program does not run all the time then ControlIDs are useless as an ID method as they are dynamically allocated by AutoIt as it runs.

What you could do is to save the location and size of the GUI (determined by WinGetPos) and the content of the edit (read by GUICtrlRead). Then you could recreate a copy of the GUI when you needed - but the ControlID would be whatever this instance of AutoIt gave it - almost certainly not the same as the original. However, you could set the new ControlID to the same variable , which would be transparent to the code - i.e. you would still refer to it as $hGUI# and your script would automatically use the appropriate ControlID.

You can use the Ini section header to define the data, so you should have no problem recalling a specific GUI using that value as the ID. Something like this:

You have 3 GUI/edits active. Save them as GUI1, GUI2, GUI3 in an ini, which would look like this:

[GUI1]
X=567
Y=345
Width=456
Height=123
Edit=I am the content of GUI1
[GUI2]....

Then when you want to recreate the GUI you use IniReadSectionNames to populate a combobox from which you can choose a GUI to recreate using GUICreate and GUICtrlSetData - the parameters are the ini values for the section with the correct title.

Perhaps that might work for you.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you for your help melba. Your idea on the ini usage has helped me. But I believe I failed to make myself clear. Here is exactly what I need my program to do.

I am using my program to assist me with another application. For example a poker client. I have many people sitting at the table and I want to assign images to them. I do this by right clicking on the images and in the context menu I click 'Assign'. Then an inputbox comes up where I type the players name that I want to assign that image to, in this case 'John'. I press ok and it then writes in my ini 'John' as the key and the value would be the controlID of the image. My problem is how would my program know which image to assign to the name 'John' when I may have many images and the controlID is never the same? Help would be great.

Again thank you Melba for your suggestion on the INI usage. Now I just need a way to make my program figure which images is which.

Edited by coder09
Link to comment
Share on other sites

@MDCT:

I have pictures in my gui that i created in autoit that i want to assign to players names in the poker client. i just need a basic idea on how to do this...

Why can't you just save the path of the image along with the control ID and the name. If the controls are not always created in the same order then, as Melba23 says, the ID's will not be the same each time the script is run, but maybe you can get round that by using Eval.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

hmm. sounds interesting martin. and yes the controls wont always be the same and the pictures wont be created in the same order. off to look at Eval.

Edit: I'm reading Eval in the helpfile and it says that in most cases Assign() should be used with it. Could I use Assign() to "assign" a specific number to one of my pictures in my gui then use Eval to recall it? am i on the right track?

Edit2: Scratch the above, Assign() can't be used on arrays. Looks like I'm stuck again...I could always screen scrape but that's too desperate right now.

Edited by coder09
Link to comment
Share on other sites

I'm not sure as I have never done it before. But, I was thinking maybe you can use Control Text which can always be the same instead Control ID, assuming you could assign text on it.

Or perhaps you can rename the picture file according to name when you assign the name? So, the script will know what picture belongs to who.

Link to comment
Share on other sites

hmm. sounds interesting martin. and yes the controls wont always be the same and the pictures wont be created in the same order. off to look at Eval.

Edit: I'm reading Eval in the helpfile and it says that in most cases Assign() should be used with it. Could I use Assign() to "assign" a specific number to one of my pictures in my gui then use Eval to recall it? am i on the right track?

Edit2: Scratch the above, Assign() can't be used on arrays. Looks like I'm stuck again...I could always screen scrape but that's too desperate right now.

Maybe I was a bit off course.

Can you do it by saving the positions of the pictures as well as the picture details and names? Then when you read the ini file, either assign the correct image and name to the picture at the specified position or if there is no picture there then create one.

Possibly I don't understand enough of what you need to do to make a good suggestion.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I'm not sure as I have never done it before. But, I was thinking maybe you can use Control Text which can always be the same instead Control ID, assuming you could assign text on it.

Or perhaps you can rename the picture file according to name when you assign the name? So, the script will know what picture belongs to who.

This is it! It's brilliant yet so simple. I can just set a tooltip identifying which image is which so when I want to recall the image I can simply use the tooltip that was set according to its picture along with the name and that picture will be spawned.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...