Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Get Control Handle


  • Please log in to reply
3 replies to this topic
corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Function to retrieve a control's handle. Tweaks welcome...

ControlGetHWND(WindowHWND, CtrlClass, CtrlInstance)
{
  ; Retrieves the handle of a control
  ; WindowHWND - Handle to the parent window (window that contains the control)
  ; CtrlClass - ClassName of the control
  ; CtrlInstance - Instance number of the control
  Loop, %CtrlInstance%
  {
    tempHWND := DllCall("FindWindowExA", "Uint", WindowHWND, "Uint", tempHWND, "str", CtrlClass, "str", NULL, "Uint")
    If tempHWND = 0
      Break
  }
  Return tempHWND
}

Example:
NULL=
Run, Notepad,,, notepid
WinWait, ahk_pid %notepid%
WinWaitActive, ahk_pid %notepid%
WinHandle := WinExist("Untitled - Notepad")
CtrlHandle := ControlGetHWND(WinHandle, "Edit", "1")

; Test the handle retrieved
if CtrlHandle
{
  EM_REPLACESEL := 0xC2
  teststr := "If you can see this message it means that the ControlGetHWND() function"
  teststr := teststr " was able to retrieve the handle of Notepad's Edit control."
  DllCall("SendMessageA", "Uint", CtrlHandle, "int", EM_REPLACESEL, "str", NULL, "str", teststr) 
}
else
  MsgBox, "Error: Could not retrieve the control's handle"


ControlGetHWND(WindowHWND, CtrlClass, CtrlInstance)
{
  ; Retrieves the handle of a control
  ; WindowHWND - Handle to the parent window (window that contains the control)
  ; CtrlClass - ClassName of the control
  ; CtrlInstance - Instance number of the control
  Loop, %CtrlInstance%
  {
    tempHWND := DllCall("FindWindowExA", "Uint", WindowHWND, "Uint", tempHWND, "str", CtrlClass, "str", NULL, "Uint")
    If tempHWND = 0
      Break
  }
  Return tempHWND
}


corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Updated to use Break to exit Loop.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
There's a variant of this that might be a little faster at the bottom of the DllCall page. It's called GetChildHWND() and it uses WindowFromPoint() instead of a loop.

Also, there is a plan to add this as a built-in feature soon because getting the HWND of a control is a very common need (there have been many requests).

Thanks.

Futurepower(R)
  • Members
  • 38 posts
  • Last active: Oct 11 2014 11:35 PM
  • Joined: 20 Jun 2005
My understanding is that this does not work as a way of identifying a DOS window or a window generated by a 16-bit program.

If so, that should be documented.