Avatar of LeighWardle
LeighWardle
Flag 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
Visual Basic Classic

Avatar of undefined
Last Comment
LeighWardle

8/22/2022 - Mon
vb_elmar

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".
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
vb_elmar

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
LeighWardle

ASKER
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
systan

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
LeighWardle

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
vb_elmar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
LeighWardle

ASKER
Thanks, vb_elmar.

Regards,
Leigh