Jump to content

How to use all messages in GuiGetMsg()


kcvinu
 Share

Recommended Posts

Hi all,
I am using GuiGetMsg(). Assume that i have a combo box in my gui. And when i use this code - 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CMB_NamesList     ; Handle of my Combo box
            CellSelect()     
    EndSwitch
WEnd

This code will call my "CellSelect()" function only when the selection change event occurred. But assume that i need to call this function right after the user clicks on the combo box. So do i need to use GuiRegisterMsg function for that ? Or can i simply do the task with GUIGetMsg() ? If the second option is possible, then i wonder how to do it. 

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

You need GuiRegisterMsg

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>

Global $g_hCombo

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("(UDF) ComboBox Create", 400, 296)
    Local $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
    GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")
    $g_hCombo = GuiCtrlGetHandle($idComboBox)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode
    $hWndFrom = $lParam
    $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    $iCode = BitShift($wParam, 16) ; Hi Word
    Switch $hWndFrom
        Case $g_hCombo
            Switch $iCode
                Case $CBN_DROPDOWN ; Sent when the list box of a combo box is about to be made visible
                    _DebugPrint("$CBN_DROPDOWN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
             EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _DebugPrint($s_Text , $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text  & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

 

Link to comment
Share on other sites

@mikell , So GuiGetMsg is for a few limited messages. May i know what is the reason behind it ? 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

  • Moderators

kcvinu,

That is a question that only Jon could answer, but I would hazard a guess that it is based on keeping the code simple for the less-experienced AutoIt user. Being able to use a simple ControlID in a loop to detect the most common events occurring to a control rather than having to create relatively complicated event handlers for every case would seem a pretty sensible design decision.

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

@Melba23 ,

Thanks for your clarification. But it is an extra power for a programmer to use such messages with easiness. :)

 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

  • Moderators

kcvinu,

And hence the introduction of GUIRegisterMsg which allows the more experienced coder to access virtually all of the messages associated with a control. But you definitely need to know what you are doing when you get to that level, so it is probably best that the newer coders are somewhat sheltered.

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

Melba23,

Yeah, i agree. New coders needs protection from the internal hazards. Eventhough, i am happy with using GuiRegisterMsg for this purpose, i am searching for easy methods to do these type of tasks. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Then the only way is to use a workaround 
But as GuiGetMsg allows detection of 90%+  of the usual control events  it should not be a great trouble  :)

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Combo", 400, 296)
Local $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            $a = GUIGetCursorInfo()
            If $a[4] = $idComboBox Then ConsoleWrite("combo just clicked" & @crlf)     
        Case $idComboBox
            ConsoleWrite("combo item selected" & @crlf)      
    EndSwitch
WEnd

 

Edited by mikell
Link to comment
Share on other sites

lol forgot about getcursorinfo and just wrote this mess...

Opt("MouseCoordMode", 2)
Local $iLeft = 10, $iTop = 10, $iWidth = 185, $iHeight = 20

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("(UDF) ComboBox Create", 400, 296)
    Local $idComboBox = GUICtrlCreateCombo("Item 1", $iLeft, $iTop, $iWidth, $iHeight)
    GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")
    $g_hCombo = GUICtrlGetHandle($idComboBox)
    GUISetState(@SW_SHOW)

    Local $msg = 0
    Do
        $msg = GUIGetMsg()
        If $msg = 0 Then ContinueLoop

        If $msg = -7 Then
            _PrimaryClick()
        EndIf

    Until $msg = -3
    GUIDelete()
EndFunc   ;==>Example


Func _PrimaryClick()
    $ix = MouseGetPos(0)
    $iy = MouseGetPos(1)
    ConsoleWrite($ix & @LF)
    ConsoleWrite($iy & @LF)
    If ($ix > $iLeft) And ($ix < ($iLeft + $iWidth)) Then
        If $iy > $iTop And $iy < $iTop + $iHeight Then
            ConsoleWrite("Clicked" & @LF)
        EndIf
    EndIf
EndFunc   ;==>_PrimaryClick

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

If you compete to use magic numbers, then please comment the full names  for better reading. And you know @JohnOne , when i see the magic nums, i just skipped your script and went to the next.

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

@mikell Really, that's a nice workaround for my problem. Thanks a lot. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

1 hour ago, kcvinu said:

If you compete to use magic numbers, then please comment the full names  for better reading. And you know @JohnOne , when i see the magic nums, i just skipped your script and went to the next.

Thanks for the heads up, I'll remember not to waste my time in future.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Personally, I appreciate that sort of frankness, and do not take any kind of offense from it.

He does not bother reading a particular style of example, and I do not like wasting my time.

A communication is made, and everyone is happy. :)

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Oops ! I think my half English made a problom.  What i mean to say is - when i load this page, i saw 2 scripts as replys. One is from mikell and one is from johnone. At first glance, i saw the magic numbers in johnone's script and i thought read it later. Then i read mikell's reply. I didn't meant to say that i don't want jo's reply. @JohnOne I am really sorry for to make you think like that. I want your guidance and support in future since you are a senior and experienced member of this forum. I didn't meant to say that i avoid your reply. I just meant to say that i choose mikell's reply first and then yours. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIShellEx.au3>

Opt( "MustDeclareVars", 1 )

#cs
$CBN_SELCHANGE = 1
$CBN_DBLCLK = 2
$CBN_SETFOCUS = 3
$CBN_KILLFOCUS = 4
$CBN_EDITCHANGE = 5
$CBN_EDITUPDATE = 6
$CBN_DROPDOWN = 7
$CBN_CLOSEUP = 8
$CBN_SELENDOK = 9
$CBN_SELENDCANCEL = 10
#ce

Global $idCBN_FIRST
Global $hCombo1, $hCombo2

Example()


Func Example()
  Local $hGui = GUICreate( "(UDF) ComboBox Create", 400, 300 )

  ; Message IDs
  $idCBN_FIRST = GUICtrlCreateDummy() ; $CBN_SELCHANGE
  For $i = 0 To 8
    GUICtrlCreateDummy()              ; $CBN_DBLCLK to $CBN_SELENDCANCEL
  Next

  ; Combo 1
  Local $hChild1 = GUICreate( "", 396, 30, 2, 2, $WS_CHILD, -1, $hGui )
  GUISetState( @SW_SHOW, $hChild1 )
  $hCombo1 = _GUICtrlComboBox_Create( $hChild1, "", 0, 0, 396, 30 )

  ; Add files
  _GUICtrlComboBox_BeginUpdate( $hCombo1 )
  _GUICtrlComboBox_AddDir( $hCombo1, "", $DDL_DRIVES, False )
  _GUICtrlComboBox_EndUpdate( $hCombo1 )

  ; Combo 2
  Local $hChild2 = GUICreate( "", 396, 30, 2, 52, $WS_CHILD, -1, $hGui )
  GUISetState( @SW_SHOW, $hChild2 )
  $hCombo2 = _GUICtrlComboBox_Create( $hChild2, "", 0, 0, 396, 30 )

  ; Add files
  _GUICtrlComboBox_BeginUpdate( $hCombo2 )
  _GUICtrlComboBox_AddDir( $hCombo2, "", $DDL_DRIVES, False )
  _GUICtrlComboBox_EndUpdate( $hCombo2 )

  ; Callback function
  Local $pComboBoxCallback = DllCallbackGetPtr( DllCallbackRegister( "ComboBoxCallback", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )

  ; Subclassing child windows
  _WinAPI_SetWindowSubclass( $hChild1, $pComboBoxCallback, 1, $hCombo1 ) ; $iSubclassId = 1, $pData = $hCombo1
  _WinAPI_SetWindowSubclass( $hChild2, $pComboBoxCallback, 2, $hCombo2 ) ; $iSubclassId = 2, $pData = $hCombo2
  ; Because we are subclassing the child windows, messages from the combo boxes only, will be handled by the callback function.

  ; Show GUI
  GUISwitch( $hGui )
  GUISetState( @SW_SHOW )

  ; Message loop
  Local $aMsg
  While 1
    $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
      Case $hGui
        Switch $aMsg[0]
          Case $idCBN_FIRST + 0 ; $CBN_SELCHANGE = 1
            ConsoleWrite( "$CBN_SELCHANGE notificaton from "    & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 1 ; $CBN_DBLCLK = 2
            ConsoleWrite( "$CBN_DBLCLK notificaton from "       & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 2 ; $CBN_SETFOCUS = 3
            ConsoleWrite( "$CBN_SETFOCUS notificaton from "     & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 3 ; $CBN_KILLFOCUS = 4
            ConsoleWrite( "$CBN_KILLFOCUS notificaton from "    & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 4 ; $CBN_EDITCHANGE = 5
            ConsoleWrite( "$CBN_EDITCHANGE notificaton from "   & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 5 ; $CBN_EDITUPDATE = 6
            ConsoleWrite( "$CBN_EDITUPDATE notificaton from "   & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 6 ; $CBN_DROPDOWN = 7
            ConsoleWrite( "$CBN_DROPDOWN notificaton from "     & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 7 ; $CBN_CLOSEUP = 8
            ConsoleWrite( "$CBN_CLOSEUP notificaton from "      & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 8 ; $CBN_SELENDOK = 9
            ConsoleWrite( "$CBN_SELENDOK notificaton from "     & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )
          Case $idCBN_FIRST + 9 ; $CBN_SELENDCANCEL = 10
            ConsoleWrite( "$CBN_SELENDCANCEL notificaton from " & ( GUICtrlRead( $aMsg[0] ) = $hCombo1 ? "Combo1" : "Combo2" ) & @CRLF )

          Case $GUI_EVENT_CLOSE
            ExitLoop
        EndSwitch
    EndSwitch
  WEnd

  ; Cleanup
  _WinAPI_RemoveWindowSubclass( $hChild1, $pComboBoxCallback, 1 )
  _WinAPI_RemoveWindowSubclass( $hChild2, $pComboBoxCallback, 2 )
  GUIDelete()
EndFunc

; Callback function
Func ComboBoxCallback( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $hCombo )
  If $iMsg <> $WM_COMMAND Then Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]

  Local $iCode = BitShift( $wParam, 16 )
  Switch $iCode
    Case $CBN_SELCHANGE To $CBN_SELENDCANCEL
      GUICtrlSendToDummy( $idCBN_FIRST + $iCode - $CBN_SELCHANGE, $hCombo )
      Return 1 ; Prevents the notifications from being forwarded to other message handlers.
  EndSwitch    ; This includes a user defined GUIRegisterMsg WM_COMMAND message handler.

  ; Call next function in subclass chain (this forwards messages to main GUI)
  Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]
  #forceref $iSubclassId
EndFunc

 

Link to comment
Share on other sites

@LarsJ Thanks for the reply. It seems very helpful to understand a beginner like me. Every block of code is well commented.  Thanks for this great help. I am amused when i see the "_WinAPI_SetWindowSubclass" in this script. Because, i never see such a function in the help file before. May be my negligence. Oh yes, it was in the group of ShellEx Management, that's why i missed it. After reading the help file, my interest in autoit increased. Now i  am reading a book named "Subclassing and Hooking in vb & vb.net". When reading it, i wish to try all the tricks in autoit too. I have wrote some code for it, but didn't complete that program due to the rush. Now, its time to learn something new from your script. I know this script is a treasure for me now. Once i read this, i will sure ask my doubts (if any). I hope you will guide me. Thanks again. :) 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

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

×
×
  • Create New...