Link to home
Start Free TrialLog in
Avatar of LeighWardle
LeighWardleFlag for Australia

asked on

VB6 - Finding a Window using FindWindow from a Form object

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
Avatar of vb_elmar
vb_elmar
Flag of Germany image

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".
Avatar of LeighWardle

ASKER

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
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
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, vb_elmar.

Regards,
Leigh