tags:

views:

551

answers:

3

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

+1  A: 

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.

Dave
+9  A: 

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

C-Pound Guru
+5  A: 

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.

Heather
+1. Obviously the currently active window may not be a form.
MarkJ
+1 Good answer as well.
C. Ross