Jump to content

Recommended Posts

Posted (edited)

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
  Reveal hidden contents

 

Posted

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

 

Posted

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

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

@Melba23 ,

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

 

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

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. 

  Reveal hidden contents

 

Posted (edited)

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
Posted

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.

Posted (edited)

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
  Reveal hidden contents

 

Posted

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

  Reveal hidden contents

 

Posted
  On 2/25/2016 at 7:03 PM, 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.

Expand  

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.

Posted

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. 

  Reveal hidden contents

 

Posted
#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

 

Posted

@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. :) 

  Reveal hidden contents

 

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
×
×
  • Create New...