Jump to content
Sign in to follow this  
drr

Closing one window closes the other too

Recommended Posts

drr

Hi,

I've script where the flow is as follows:

1. Window is created with custom title and custom window class name (input by user)

2. Second window is created which asks user for input (creates variable with title of another existing window)

Problem:

3. When the OK button (confirming input) is pressed, the window is closed (as planned), but it also closes the other window, which was created first.

Here's the code (I've compiled it from various threads in this forum):

#include <WinAPI.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <guilistview.au3>
#Include <GuiEdit.au3>


Global Const $CS_VREDRAW = 1
Global Const $CS_HREDRAW = 2
Global Const $IDI_APPLICATION = 32512
Global Const $WHITE_BRUSH = 0
Global Const $CW_USEDEFAULT = 0x80000000

Global $hMsgHelloWndProc
Global $WindowClassName

Global $WindowTitle


; Get user input for window and class name
Do
    $WindowClassName = InputBox(Default, 'Enter desired WindowClass name', '')
Until $WindowClassName <> '' Or @error


; Create background window

$hWndMain = GUICreate("My GUI")
GUISetState (@SW_SHOW)

; Creating and registring the class
$iRet = MsgHelloReg($WindowClassName, $hMsgHelloWndProc)

; Creating and activating a class example
$hWndHello = _WinAPI_CreateWindowEx(0, $WindowClassName, $WindowClassName, _
    $WS_OVERLAPPEDWINDOW, $CW_USEDEFAULT, 0, $CW_USEDEFAULT, 0, $hWndMain, 0, _WinAPI_GetModuleHandle(""), 0)
$iRet = _WinAPI_ShowWindow($hWndHello)

WinWait ($WindowClassName)
$var = WinList()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 500, 193, 115)
$List1 = GUICtrlCreateListView("List of Windows          ",10, 10, 600, 400)
$OKbutton = GUICtrlCreateButton ("OK",500,420,70,70)
Dim $list2[$var[0][0] + 1]
For $i = 1 To $var[0][0]
    If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then
        $list2[$i] = GUICtrlCreateListViewItem($var[$i][0], $List1)
        GUISetState(@SW_SHOW)
    EndIf
Next
$edit1 = GUICtrlCreateEdit('',20,420,400,40)
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc ;==>IsVisible

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
     
        Case $Okbutton
            $WindowTitle = _GUICtrlEdit_GetText ($edit1)
            ;MsgBox (4160, "Information", "Item 2 Text: "&$WindowTitle )
            WinClose ("Form1") 
            Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd    

; script flow continues here (left for later)


; Unregistering the class before exit
$iRet = MsgHelloUnReg($WindowClassName, $hMsgHelloWndProc)


; UDF to create and register our class
Func MsgHelloReg($sClassName, ByRef $hWndProc)
    ; selecting icons and cursors (there is many choices)
    Local $aIcon = DllCall("user32.dll", "hwnd", "LoadIcon", "hwnd", 0, "int", $IDI_APPLICATION)
    Local $aCursor = DllCall("user32.dll", "hwnd", "LoadCursor", "hwnd", 0, "int", $IDC_ARROW)
    ; Registering window processing as CallBack-function
    $hWndProc = DLLCallbackRegister ("MsgHelloWndProc", "int", "hwnd;int;wparam;lparam")
    ;Creating and initializing class structures
    Local $tWndClassEx = DllStructCreate("uint cbSize;uint style;ptr lpfnWndProc;int cbClsExtra;int cbWndExtra;hwnd hInstance;hwnd hIcon;hwnd hCursor;hwnd hbrBackground;ptr lpszMenuName;ptr lpszClassName;hwnd hIconSm")
    Local $tClassName = DllStructCreate("char["& StringLen($sClassName)+1 &"]")
    DllStructSetData($tClassName, 1, $sClassName)

    DllStructSetData($tWndClassEx, "cbSize", DllStructGetSize($tWndClassEx) )
    DllStructSetData($tWndClassEx, "style", BitOR($CS_VREDRAW, $CS_HREDRAW) )
    DllStructSetData($tWndClassEx, "lpfnWndProc", DllCallbackGetPtr($hWndProc))
    DllStructSetData($tWndClassEx, "hInstance", _WinAPI_GetModuleHandle(""))
    DllStructSetData($tWndClassEx, "hIcon", $aIcon[0])
    DllStructSetData($tWndClassEx, "hCursor", $aCursor[0])
    DllStructSetData($tWndClassEx, "hbrBackground", _WinAPI_GetStockObject($WHITE_BRUSH))
    DllStructSetData($tWndClassEx, "lpszClassName", DllStructGetPtr($tClassName))
    DllStructSetData($tWndClassEx, "hIconSm", $aIcon[0])
    ; Registering process
    Local $aRet = DllCall("user32.dll", "dword", "RegisterClassExA", "ptr", DllStructGetPtr($tWndClassEx) )
    Return $aRet[0]
EndFunc ; ==> MsgHelloReg

; UDF to unregister the class (there should not be a copy of the class)

Func MsgHelloUnReg($sClassName, ByRef $hWndProc)
    DllCallbackFree($hWndProc)
    Local $aRet = DllCall("user32.dll", "int", "UnregisterClassA", "str", $sClassName, "hwnd", _WinAPI_GetModuleHandle(""))
    Return $aRet[0]
EndFunc ; ==> MsgHelloUnReg

; Window processing of our window class
Func MsgHelloWndProc($hWnd, $uMsg, $wParam, $lParam)
    Local $aRet, $iRet, $hDC, $tRect, $sDrawText = "Hello, World!"
    Switch $uMsg
        Case $WM_PAINT
            $stPaint = DllStructCreate("int;int;long;long;long;long;int;int;byte[32]")
            $ahDC = DllCall("user32.dll", "int", "BeginPaint", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPaint))

            $tRect = _WinAPI_GetClientRect($hWnd)

            _WinAPI_DrawText($ahDC[0], $sDrawText, $tRect, BitOR($DT_SINGLELINE, $DT_CENTER, $DT_VCENTER))

            DllCall("user32.dll", "int", "EndPaint", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPaint))
        Case $WM_CLOSE
            _WinAPI_DestroyWindow($hWnd)
        Case $WM_DESTROY
;           $aRet = DllCall("user32.dll", "none", "PostQuitMessage", "int", 0 )
        Case Else
            Return _WinAPI_DefWindowProc($hWnd, $uMsg, $wParam, $lParam) ; Set default window processing
    EndSwitch
    Return 0
EndFunc ; ==> MsgHelloWndProc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
        Local $tagNMHDR, $event
        $tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
        If @error Then Return
        $event = DllStructGetData($tagNMHDR, 3)
        Select
            Case $wParam = $List1

                Select
                    Case $event = $NM_CLICK
                        Local $tagNMITEMACTIVATE = DllStructCreate("int;int;int;int;int;int;int;int;int", $lParam)
                        $selected = DllStructGetData($tagNMITEMACTIVATE, 4)
                        $selcol = DllStructGetData($tagNMITEMACTIVATE, 5)
            
                        $seltxt = StringSplit(GUICtrlRead(GUICtrlRead($List1)),'|')
                        
                        GUICtrlSetData($Edit1, $seltxt[1 + $selcol])
                EndSelect
        EndSelect
        
        Return $GUI_RUNDEFMSG

EndFunc ;==>WM_Notify_EventsoÝ÷ Ø.¦,zØ^¦ºé¢²Ê&{^­æ«¢éÝêÞjëh×6While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
     
        Case $Okbutton
            $WindowTitle = _GUICtrlEdit_GetText ($edit1)
            ;MsgBox (4160, "Information", "Item 2 Text: "&$WindowTitle )
            WinClose ("Form1")
            Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

but cant figure out what exactly is wrong. Whe using 'ExitLoop' instead of 'Exit' it gives 'unhadled win32 exception'.

Thanks for help :D

Share this post


Link to post
Share on other sites
Monamo

Hi,

I've script where the flow is as follows:

1. Window is created with custom title and custom window class name (input by user)

2. Second window is created which asks user for input (creates variable with title of another existing window)

Problem:

3. When the OK button (confirming input) is pressed, the window is closed (as planned), but it also closes the other window, which was created first.

Here's the code (I've compiled it from various threads in this forum):

#include <WinAPI.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <guilistview.au3>
#Include <GuiEdit.au3>


Global Const $CS_VREDRAW = 1
Global Const $CS_HREDRAW = 2
Global Const $IDI_APPLICATION = 32512
Global Const $WHITE_BRUSH = 0
Global Const $CW_USEDEFAULT = 0x80000000

Global $hMsgHelloWndProc
Global $WindowClassName

Global $WindowTitle


; Get user input for window and class name
Do
    $WindowClassName = InputBox(Default, 'Enter desired WindowClass name', '')
Until $WindowClassName <> '' Or @error


; Create background window

$hWndMain = GUICreate("My GUI")
GUISetState (@SW_SHOW)

; Creating and registring the class
$iRet = MsgHelloReg($WindowClassName, $hMsgHelloWndProc)

; Creating and activating a class example
$hWndHello = _WinAPI_CreateWindowEx(0, $WindowClassName, $WindowClassName, _
    $WS_OVERLAPPEDWINDOW, $CW_USEDEFAULT, 0, $CW_USEDEFAULT, 0, $hWndMain, 0, _WinAPI_GetModuleHandle(""), 0)
$iRet = _WinAPI_ShowWindow($hWndHello)

WinWait ($WindowClassName)
$var = WinList()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 500, 193, 115)
$List1 = GUICtrlCreateListView("List of Windows          ",10, 10, 600, 400)
$OKbutton = GUICtrlCreateButton ("OK",500,420,70,70)
Dim $list2[$var[0][0] + 1]
For $i = 1 To $var[0][0]
    If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then
        $list2[$i] = GUICtrlCreateListViewItem($var[$i][0], $List1)
        GUISetState(@SW_SHOW)
    EndIf
Next
$edit1 = GUICtrlCreateEdit('',20,420,400,40)
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc ;==>IsVisible

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
     
        Case $Okbutton
            $WindowTitle = _GUICtrlEdit_GetText ($edit1)
            ;MsgBox (4160, "Information", "Item 2 Text: "&$WindowTitle )
            WinClose ("Form1") 
            Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd    

; script flow continues here (left for later)


; Unregistering the class before exit
$iRet = MsgHelloUnReg($WindowClassName, $hMsgHelloWndProc)


; UDF to create and register our class
Func MsgHelloReg($sClassName, ByRef $hWndProc)
    ; selecting icons and cursors (there is many choices)
    Local $aIcon = DllCall("user32.dll", "hwnd", "LoadIcon", "hwnd", 0, "int", $IDI_APPLICATION)
    Local $aCursor = DllCall("user32.dll", "hwnd", "LoadCursor", "hwnd", 0, "int", $IDC_ARROW)
    ; Registering window processing as CallBack-function
    $hWndProc = DLLCallbackRegister ("MsgHelloWndProc", "int", "hwnd;int;wparam;lparam")
    ;Creating and initializing class structures
    Local $tWndClassEx = DllStructCreate("uint cbSize;uint style;ptr lpfnWndProc;int cbClsExtra;int cbWndExtra;hwnd hInstance;hwnd hIcon;hwnd hCursor;hwnd hbrBackground;ptr lpszMenuName;ptr lpszClassName;hwnd hIconSm")
    Local $tClassName = DllStructCreate("char["& StringLen($sClassName)+1 &"]")
    DllStructSetData($tClassName, 1, $sClassName)

    DllStructSetData($tWndClassEx, "cbSize", DllStructGetSize($tWndClassEx) )
    DllStructSetData($tWndClassEx, "style", BitOR($CS_VREDRAW, $CS_HREDRAW) )
    DllStructSetData($tWndClassEx, "lpfnWndProc", DllCallbackGetPtr($hWndProc))
    DllStructSetData($tWndClassEx, "hInstance", _WinAPI_GetModuleHandle(""))
    DllStructSetData($tWndClassEx, "hIcon", $aIcon[0])
    DllStructSetData($tWndClassEx, "hCursor", $aCursor[0])
    DllStructSetData($tWndClassEx, "hbrBackground", _WinAPI_GetStockObject($WHITE_BRUSH))
    DllStructSetData($tWndClassEx, "lpszClassName", DllStructGetPtr($tClassName))
    DllStructSetData($tWndClassEx, "hIconSm", $aIcon[0])
    ; Registering process
    Local $aRet = DllCall("user32.dll", "dword", "RegisterClassExA", "ptr", DllStructGetPtr($tWndClassEx) )
    Return $aRet[0]
EndFunc ; ==> MsgHelloReg

; UDF to unregister the class (there should not be a copy of the class)

Func MsgHelloUnReg($sClassName, ByRef $hWndProc)
    DllCallbackFree($hWndProc)
    Local $aRet = DllCall("user32.dll", "int", "UnregisterClassA", "str", $sClassName, "hwnd", _WinAPI_GetModuleHandle(""))
    Return $aRet[0]
EndFunc ; ==> MsgHelloUnReg

; Window processing of our window class
Func MsgHelloWndProc($hWnd, $uMsg, $wParam, $lParam)
    Local $aRet, $iRet, $hDC, $tRect, $sDrawText = "Hello, World!"
    Switch $uMsg
        Case $WM_PAINT
            $stPaint = DllStructCreate("int;int;long;long;long;long;int;int;byte[32]")
            $ahDC = DllCall("user32.dll", "int", "BeginPaint", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPaint))

            $tRect = _WinAPI_GetClientRect($hWnd)

            _WinAPI_DrawText($ahDC[0], $sDrawText, $tRect, BitOR($DT_SINGLELINE, $DT_CENTER, $DT_VCENTER))

            DllCall("user32.dll", "int", "EndPaint", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPaint))
        Case $WM_CLOSE
            _WinAPI_DestroyWindow($hWnd)
        Case $WM_DESTROY
;           $aRet = DllCall("user32.dll", "none", "PostQuitMessage", "int", 0 )
        Case Else
            Return _WinAPI_DefWindowProc($hWnd, $uMsg, $wParam, $lParam) ; Set default window processing
    EndSwitch
    Return 0
EndFunc ; ==> MsgHelloWndProc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
        Local $tagNMHDR, $event
        $tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
        If @error Then Return
        $event = DllStructGetData($tagNMHDR, 3)
        Select
            Case $wParam = $List1

                Select
                    Case $event = $NM_CLICK
                        Local $tagNMITEMACTIVATE = DllStructCreate("int;int;int;int;int;int;int;int;int", $lParam)
                        $selected = DllStructGetData($tagNMITEMACTIVATE, 4)
                        $selcol = DllStructGetData($tagNMITEMACTIVATE, 5)
            
                        $seltxt = StringSplit(GUICtrlRead(GUICtrlRead($List1)),'|')
                        
                        GUICtrlSetData($Edit1, $seltxt[1 + $selcol])
                EndSelect
        EndSelect
        
        Return $GUI_RUNDEFMSG

EndFunc ;==>WM_Notify_EventsƒoÝŠ÷ Ø‹.¦š,zØ^¦º•é¢²Ê&{^­æ«¢éÝ…êÞjëhŠ×6While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
     
        Case $Okbutton
            $WindowTitle = _GUICtrlEdit_GetText ($edit1)
            ;MsgBox (4160, "Information", "Item 2 Text: "&$WindowTitle )
            WinClose ("Form1")
            Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

but cant figure out what exactly is wrong. Whe using 'ExitLoop' instead of 'Exit' it gives 'unhadled win32 exception'.

Thanks for help :D

Try a run in which you change:

WinClose ("Form1")

to:

GUIDelete($Form1)

Edit: Plus when working with multiple GUIs, you may want to look into the "Advanced" parameter of GUIGetMsg().

Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
drr

Try a run in which you change:

WinClose ("Form1")

to:

GUIDelete($Form1)

Edit: Plus when working with multiple GUIs, you may want to look into the "Advanced" parameter of GUIGetMsg().

Thanks, it worked fine! :D

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  

×