VB6 - Finding a Window using FindWindow from a Form object

LeighWardle
LeighWardle used Ask the Experts™
on
Hi Experts,

Assuming I have this code fragment:

Dim frmFormObject As Form
Dim lngHandle As Long

How can I make this code work?

    lngHandle = FindWindow(vbNullString, ????????)

Regards,
Leigh
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If the window you're looking for is a VB6 window the class name is "ThunderRT6FormDC".

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

a=findwindow("ThunderRT6FormDC","[enter the form title here]")

sample:
a=findwindow("ThunderRT6FormDC","Form1")'searching a compiled exe
a=findwindow("ThunderFormDC","Form1")'searching a form in the vb IDE

annotation:
A form running in the vb IDE
has the class name "ThunderFormDC", and a compiled
VB.EXE has the class name "ThunderRT6FormDC".

Author

Commented:
Hi vb_elmar,

Thanks for your suggestions.

I just need some help getting the appropriate title string from the Form object (assume it is frmFormObject).

What do I put as the 2nd argument in findwindow?
I assume it is something like frmFormObject.Caption?

Regards,
Leigh

Commented:
If the title is unknown the 2nd parameter is vbNullString (see below).
To find an (unknown) title try this:

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Sub Form_Load()

h = FindWindow("ThunderRT6FormDC", vbNullString)
Dim LL As Long, aa$
    aa$ = Space$(128)
    result = GetWindowText(h, aa$, 128)
    result = Left(aa, result)
MsgBox result

End Sub
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi vb_elmar,

Sorry, that's not quite what I need.

See my code below.
The line that needs fixing is:

lngHandle = FindWindow(vbNullString, frmFormObject.Caption)  ' <<< doesn't work!!!

What can I replace frmFormObject.Caption with - so the 2nd argument of FindWindow is the string required by FindWindow?

Regards,
Leigh

Public Function FormLoadIntoContainer(frmFormObject As Form, frameFrameObject As Frame) As Boolean
    Dim rec As Rect
    Dim lngHandle As Long
    Dim boolResult As Boolean

    boolResult = FormsUnloadAllExceptThisOne(frmFormObject.Name)
   
    Unload frmFormObject
   
    frmFormObject.WindowState = vbNormal '20120522
    frmFormObject.Top = -10000
    frmFormObject.Show
 
    lngHandle = FindWindow(vbNullString, frmFormObject.Caption)  ' <<< doesn't work!!!

    GetWindowRect lngHandle, rec
    SetParent lngHandle, frameFrameObject.hwnd
    ' The first 5 parameters are required but we only care about the 1st three
    MoveWindow lngHandle, 0, 0, frameFrameObject.Width, frameFrameObject.Height, 1
   
    FormLoadIntoContainer = True
   
End Function

Commented:
Why are you finding your own window using own your caption that is active when shown?
frmFormObject.Show
lngHandle = FindWindow(vbNullString, frmFormObject.Caption)  ' <<< doesn't work!!!

Open in new window


What are you trying to accomplish?

if you want to use your own window use <Me>
like:
Me.ControlBox = False
Me.Size = New Size(465, 60)

if you want to position your window manually:
http://msdn.microsoft.com/en-us/library/aa984420%28v=vs.71%29.aspx

Author

Commented:
I want to be able to move the window, as in:    

     lngHandle = FindWindow(vbNullString, frmFormObject.Caption)  ' <<< doesn't work!!!

    GetWindowRect lngHandle, rec
    SetParent lngHandle, frameFrameObject.hwnd
    ' The first 5 parameters are required but we only care about the 1st three
    MoveWindow lngHandle, 0, 0, frameFrameObject.Width, frameFrameObject.Height, 1
Commented:
The FindWindow function is used to return a hwnd.

If you pass frmFormObject to the "FormLoadIntoContainer" function, you'll get the handle of the form by :

lngHandle = frmFormObject.hwnd

It's not necessary to find the hwnd using ...
lngHandle = FindWindow(vbNullString, frmFormObject.Caption)  ' <<< doesn't work!!!

Instead you can use ...
lngHandle = frmFormObject.hwnd


Public Function FormLoadIntoContainer(frmFormObject As Form, frameFrameObject As Frame) As Boolean
...
    lngHandle = frmFormObject.hwnd
...
End Function

Author

Commented:
Thanks, vb_elmar.

Regards,
Leigh

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial