Jump to content

Recommended Posts

Posted

Hi all,

How can i get notified when user clicks on a combo box's edit area ? This is my code so far. 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <ComboConstants.au3>
#include <ListBoxConstants.au3>



Global $btn
; creates a window

Global $Window_0 = GUICreate("My Window", 800, 500, -1, -1, BitOR($WS_SIZEBOX, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX))
$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)
GUICtrlSetFont(-1,12,400)
Global $lstbx = GUICtrlCreateList("FirstItem", 400,50, 200, 300)
GUICtrlSetFont(-1,12,400)


GUIRegisterMsg($WM_COMMAND, "MyEventCallback")
GUIRegisterMsg($WM_SIZE, "MyEventCallback")
GUIRegisterMsg($WM_NOTIFY, "NotifyManager")

GUISetState(@SW_SHOW)
GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx,  "Apple|Orange|Pineapple|Grape|Lemon")
Do
    $Event = GUIGetMsg( )

Until $Event = $GUI_EVENT_CLOSE

Func MyEventCallback($hwnd, $message, $wParam, $lParam)
    Select

        Case $message = $WM_COMMAND
            ;----------------------------------------------------------------
            If LoWord($wParam) = $cmb Then ; if control id is combox's Then
                Local $Notification = HiWord($wParam)
                Select
                    Case $Notification = $CBN_DROPDOWN   ; here we check the notification code.
                        ConsoleWrite("$CBN_DROPDOWN Worked " & @MIN & ":" & @SEC & @CRLF)
                    Case $Notification = $CBN_EDITCHANGE   ; here we check the notification code.
                        ConsoleWrite("$CBN_EDITCHANGE Worked " & @MIN & ":" & @SEC & @CRLF)
                    
                    ; **** Here i want add the code for combo box clicking.
                    
                EndSelect
            ;---------------------------------------------------------------------  
            ElseIf LoWord($wParam) = $lstbx Then
                Local $Notification = HiWord($wParam)
                Select
                    Case $Notification = $LBN_SELCHANGE
                        ConsoleWrite("$LBN_SELCHANGE" & @CRLF)
                EndSelect
            EndIf
            ;------------------------------------------------------------------

        Case $message = $WM_SIZE

    EndSelect
    Return $GUI_RUNDEFMSG
EndFunc   ;==>ProGUI_EventCallback

Func NotifyManager($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ;Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If DllStructGetData($tagNMHDR, 3) = $NM_LDOWN Then
        ConsoleWrite("Notify Worked" & @CRLF)
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc

Func LoWord($Variable)
    Return BitAND($Variable, 0xFFFF)
EndFunc

Func HiWord($Variable)
    Return BitShift($Variable, 16)
EndFunc

 

  Reveal hidden contents

 

Posted

Get a handle to the edit control and subclass the edit control:

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

Global $g_idMemo

Example()

Func Example()
  Local $tInfo, $idCombo

  ; Create GUI
  GUICreate("ComboBox Get ComboBox Info", 400, 296)
  $idCombo = GUICtrlCreateCombo("", 2, 2, 396, 296)
  $g_idMemo = GUICtrlCreateEdit("", 2, 32, 396, 266, 0)
  GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
  GUISetState(@SW_SHOW)

  ; Add files
  _GUICtrlComboBox_BeginUpdate($idCombo)
  _GUICtrlComboBox_AddDir($idCombo, @WindowsDir & "\*.exe")
  _GUICtrlComboBox_EndUpdate($idCombo)

  If _GUICtrlComboBox_GetComboBoxInfo($idCombo, $tInfo) Then
    MemoWrite("Handle to the ComboBox .....: " & DllStructGetData($tInfo, "hCombo"))
    MemoWrite("Handle to the Edit Box .....: " & DllStructGetData($tInfo, "hEdit"))
    MemoWrite("Handle to the drop-down list: " & DllStructGetData($tInfo, "hList"))
  EndIf

  Local $hEdit = DllStructGetData($tInfo, "hEdit")
  Local $pMsgHandler = DllCallbackGetPtr( DllCallbackRegister( "MsgHandler", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )
  _WinAPI_SetWindowSubclass( $hEdit, $pMsgHandler, 1000, 0 ) ; $iSubclassId = 1000, $pData = 0

  ; Loop until the user exits.
  Do
  Until GUIGetMsg() = $GUI_EVENT_CLOSE

  ; Cleanup
  _WinAPI_RemoveWindowSubclass( $hEdit, $pMsgHandler, 1000 ) ; Unregister message handler
  GUIDelete()
EndFunc   ;==>Example

; Write a line to the memo control
Func MemoWrite($sMessage)
  GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

; Message handler based on subclassing
Func MsgHandler( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData )
  Switch $iMsg
    Case $WM_LBUTTONDOWN
      ConsoleWrite( "$WM_LBUTTONDOWN" & @CRLF )
  EndSwitch

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

 

Posted (edited)

@Earthshine Thank you. Will check it.

@LarsJ Thank you for the reply. Before reading your code, let me ask you a question. In vb.net we have an event named "ComboBox_Mouseclick"  So I assumed that there is a notification message is coming from combo box to parent window if we click the combo. If there is no such messages are coming, then vb.net's mouse click event is using a subclassing techiniq like you did in your reply ? 

Edit note :- Without subclassing, is it impossible ?

Edited by kcvinu
  Reveal hidden contents

 

Posted

I don't know much about VB.NET, but I don't think standard controls and VB.NET controls are comparable with regard to code. And event messages and notifications are also not comparable.

I think "ComboBox_Mouseclick" is a standard message. It's not implemented through subclassing.

In AutoIt it's impossible without subclassing, because the internal message handler would "eat" all the mouse clicks.

Posted (edited)

@LarsJ Thanks for the reply. Well, i was playing with some AutoIt code to use all events in AuotIt like we did in any dot net language.  I can use most common notification messages with GuiRegisterMsg.  But this one was very hard. I am glad to know that it is possible with atleast subclassing. I wish if AutoIt's internal msg handler doesn't swallow these notifications. :) 

Edit Note - By the way, i can't find $tagCOMBOBOXINFO structure in AutoIt help file. But i found one in MSDN.  I think although, AutoIt is not allowing us to code in the straight forward method, it will allow us to do these kind of tasks in complex ways. 

Edited by kcvinu
  Reveal hidden contents

 

  • Moderators
Posted

kcvinu,

Less elegant but seemingly effective:

#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>

Global $tInfo, $bFocus = False

Global $Window_0 = GUICreate("My Window", 800, 500)

$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)

Global $lstbx = GUICtrlCreateList("FirstItem", 400,50, 200, 300)

GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx,  "Apple|Orange|Pineapple|Grape|Lemon")

GUISetState(@SW_SHOW)

_GUICtrlComboBox_GetComboBoxInfo($cmb, $tInfo)
$hComboEdit = DllStructGetData($tInfo, "hEdit")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If _WinAPI_GetFocus() = DllStructGetData($tInfo, "hEdit") And $bFocus = False Then
        $bFocus = True
        ConsoleWrite("True" & @CRLF)
    ElseIf _WinAPI_GetFocus() <> DllStructGetData($tInfo, "hEdit") And $bFocus = True Then
        $bFocus = False
        ConsoleWrite("False" & @CRLF)
    EndIf

WEnd

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 Wow ! Thank you for the code. This is amazing. I thought this is impossible without complex stuff like subclassing. But you just proved that it is possible with some api funcs. Although, it won't fire consoleWrite when we clicked second time, because the focus is still on combo's edit control, it is an evidence of the power of win32 api . Anyhow, this aproach is great. 

  Reveal hidden contents

 

Posted (edited)

Hello. Another way...

#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>



Global $tInfo
Global $Window_0 = GUICreate("My Window", 800, 500)

$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)
Global $lstbx = GUICtrlCreateList("FirstItem", 400, 50, 200, 300)

GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx, "Apple|Orange|Pineapple|Grape|Lemon")
GUISetState(@SW_SHOW)
_GUICtrlComboBox_GetComboBoxInfo($cmb, $tInfo)
Global $hComboEdit = DllStructGetData($tInfo, "hEdit")



While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            _CheckComBoxClickTextArea()

    EndSwitch

WEnd


Func _CheckComBoxClickTextArea()
    Local $g_tStruct = DllStructCreate($tagPOINT)
    DllStructSetData($g_tStruct, "x", MouseGetPos(0))
    DllStructSetData($g_tStruct, "y", MouseGetPos(1))
    Local $hHandleFromPoint=_WinAPI_WindowFromPoint($g_tStruct)
    If $hHandleFromPoint = $hComboEdit Then
    ConsoleWrite("Clicked" & @CRLF)
    EndIf
EndFunc

Saludos

Edited by Danyfirex
  • Moderators
Posted

kcvinu,

  Quote

 it won't fire consoleWrite when we clicked second time

Expand  

Then try this:

#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>

Global $tInfo

Global $Window_0 = GUICreate("My Window", 800, 500)

$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)

Global $lstbx = GUICtrlCreateList("FirstItem", 400,50, 200, 300)

GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx,  "Apple|Orange|Pineapple|Grape|Lemon")

GUISetState(@SW_SHOW)

_GUICtrlComboBox_GetComboBoxInfo($cmb, $tInfo)
$hComboEdit = DllStructGetData($tInfo, "hEdit")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            If _WinAPI_GetFocus() = DllStructGetData($tInfo, "hEdit") Then
                ConsoleWrite("Hit" & @CRLF)
            EndIf

    EndSwitch

WEnd

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

M32 when you open and close the drop down the event is raised too Also when you select alList Item.

 

Saludos

  • Moderators
Posted

Danyfirex,

 I see - it is because those actions give focus to the edit. time for another think.....

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

 

  • Moderators
Posted

Et voila!

#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>

Global $tInfo

Global $Window_0 = GUICreate("My Window", 800, 500)

$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)

Global $lstbx = GUICtrlCreateList("FirstItem", 400,50, 200, 300)

GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx,  "Apple|Orange|Pineapple|Grape|Lemon")

GUISetState(@SW_SHOW)

_GUICtrlComboBox_GetComboBoxInfo($cmb, $tInfo)
$hComboEdit = DllStructGetData($tInfo, "hEdit")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            If _WinAPI_GetFocus() = DllStructGetData($tInfo, "hEdit") And _GUICtrlComboBox_GetDroppedState($cmb) = False Then
                ConsoleWrite("Hit" & @CRLF)
            EndIf
    EndSwitch
WEnd

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
  • Moderators
Posted

Danyfirex,

Annoyingly correct! Back to thinking mode.....

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

Another way.

#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include <WinAPIGdi.au3>
Global $tInfo

Global $Window_0 = GUICreate("My Window", 800, 500)

$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)
Global $cmb = GUICtrlCreateCombo("Sample", 50, 150, 300, 50)

Global $lstbx = GUICtrlCreateList("FirstItem", 400, 50, 200, 300)
GUICtrlSetData($cmb, "Item 2|Item 3", "Item 2")
GUICtrlSetData($lstbx, "Apple|Orange|Pineapple|Grape|Lemon")

GUISetState(@SW_SHOW)
_GUICtrlComboBox_GetComboBoxInfo($cmb, $tInfo)
Global $hComboEdit = DllStructGetData($tInfo, "hEdit")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            _CheckComBoxClickTextArea()
    EndSwitch
WEnd

Func _CheckComBoxClickTextArea()
    Local $iX = MouseGetPos(0)
    Local $iY = MouseGetPos(1)
    Local $tRect = _WinAPI_GetWindowRect($hComboEdit)
    Local $iLeft = DllStructGetData($tRect, 1)
    Local $iTop = DllStructGetData($tRect, 2)
    Local $iRight = DllStructGetData($tRect, 3)
    Local $iBottom = DllStructGetData($tRect, 4)
    If _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iRight, $iBottom) Then
        ConsoleWrite("Clicked" & @CRLF)
    EndIf
EndFunc   ;==>_CheckComBoxClickTextArea

Saludos

Posted (edited)

This is can be another way

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

$Window_0 = GUICreate("My Window", 800, 500, -1, -1, BitOR($WS_SIZEBOX, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX))
$btn = GUICtrlCreateButton("Click Me", 50, 50, 120, 50)

$cmb1 = GUICtrlCreateCombo("Sample1", 50, 150, 300, 50)
$cmb2 = GUICtrlCreateCombo("Sample2", 50, 200, 300, 50)

GUISetState(@SW_SHOW)

Do
    $event = GUIGetMsg()
    Switch $event
        Case $GUI_EVENT_PRIMARYDOWN
            If MouseGetCursor() = 5 Then ConsoleWrite(ControlGetFocus($Window_0) & @CRLF)
    EndSwitch
    Sleep(50)
Until $event = $GUI_EVENT_CLOSE

 

Edited by Deye
Posted (edited)

 @Melba23, @Danyfirex, @Deye Thanks to all. Each of your code is great and amazing. So, what i learn from this post is --

 If you want to provide mouse click event for your controls like combo, list box etc..

You should...

1. Get the handle of that particular control.

2. Trap the primary down event

3. Check the mouse position. If that is your control's position, call your event handler.

And Deye's technique is little bit different. He checked for mouse cursor. (Brilliant !  ) 

Well, i am in search of how to implement all type of event handling in AutoIt. Thanks for all your inputs. They helped me a lot to learn. :) 

 

Edited by kcvinu
  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...