How can I get the handle of the active control on the screen? Not just my form. With the GetForegroundWindow I can get the window but not the control... Any ideas?
Thanks :)
Printable View
How can I get the handle of the active control on the screen? Not just my form. With the GetForegroundWindow I can get the window but not the control... Any ideas?
Thanks :)
I can't think of an answer right now, but I did find this on Google. It's to a thread that [I'm supposing] asks the same question, but you have to register for the site. I don't wanna do that.
http://www.vbcity.com/forums/topic.asp?tid=31361
Thanks. I'll have a look :)
No luck with that. It just sais to enumerate the child controls of the window and look for some flag that'll show if it's active. I listed all the controls but I have no idea for a flag :(
This may work: Form1.ActiveControl
That would work if I was working on my form... But that and Screen.ActiveControl only show for the controls of the VB project. I need for other apps also :(
That's what I was afraid of ;)
Well, time to hunt through the API guide again :)
Here's a C++ version that may work: http://www.experts-exchange.com/Prog..._10302522.html. It uses threads, so you never know how it would do in VB.
I have searched but it seems there is nothing :cry:Quote:
Originally posted by jemidiah
That's what I was afraid of ;)
Well, time to hunt through the API guide again :)
Try this...
VB Code:
Option Explicit Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, _ ByVal idAttachTo As Long, _ ByVal fAttach As Long) _ As Long Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Declare Function GetFocus Lib "user32" () As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _ lpdwProcessId As Long) _ As Long Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long Private Sub Timer1_Timer() Dim hFore As Long, hFocus As Long hFocus = GetFocus If hFocus = 0 Then hFore = GetForegroundWindow() Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, True) hFocus = GetFocus Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, False) End If Me.Caption = hFocus End Sub
I put it in a timer to make sure it actually worked. It seems to.
I'll test it...
I wonder what else this could get accomplished. Cool function...jeez, I'm a geek.Quote:
Originally posted by crptcblade
VB Code:
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, _ ByVal idAttachTo As Long, _ ByVal fAttach As Long) _ As Long
Once again, you have found my solution...
Thanks :thumb: