AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Get Control Handle

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
corrupt



Joined: 29 Dec 2004
Posts: 2421

PostPosted: Sun May 29, 2005 12:36 am    Post subject: Get Control Handle Reply with quote

Function to retrieve a control's handle. Tweaks welcome...

Code:
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:
Code:
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
}
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2421

PostPosted: Sun May 29, 2005 2:06 am    Post subject: Reply with quote

Updated to use Break to exit Loop.
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10471

PostPosted: Sun May 29, 2005 11:10 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
Futurepower(R)



Joined: 20 Jun 2005
Posts: 35

PostPosted: Mon Jun 20, 2005 8:28 pm    Post subject: DOS Windows? 16-bit programs? Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group