C. Ross
C. Ross

Reputation: 31858

Get hWnd of the current window/form in VB6?

Ho do I get hWnd of the current window/form in VB6?

Upvotes: 6

Views: 23561

Answers (3)

user65628
user65628

Reputation:

Using Windows API, GetForegroundWindow() will get the handle of the topmost window regardless of which application it is from, and GetActiveWindow() will get the handle of your application's active window. The Declare statements you will need:

Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetActiveWindow Lib "user32.dll" () As Long 

Calling either function will return a window handle as described above.

Upvotes: 8

C-Pound Guru
C-Pound Guru

Reputation: 16368

If you're on the form: Me.hWnd. If you don't know which form is the current form: Screen.ActiveForm.hWnd

Upvotes: 16

Dave
Dave

Reputation: 15016

It's been a long time since I used VB6, but this is what I remember:

You'll want to open the API Viewer, which should be in the Start Menu around the VB6 entry. When you open it, you want to select win32api.txt, and you'll get a list of all of the Win32 API functions. This is the easiest way to not mess up the function signatures. Copy and paste the function declaration into one of your VB6 modules.

I always "cheated" and just looked for my window by caption name, instead of looping over all of the available windows with GetWindow. If you're okay with this, you want to use FindWindow and pass the caption name as the second parameter.

Upvotes: 4

Related Questions