Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

How do I bring the focus back to a form while the program is running - VB6?

Currently, we have a 3rd party app (written in VB6) that loses focus while it's running in the middle of the night.  When it loses focus, processing stops.  

I have a small VB6 app that runs throughout the night, checking for error dialogs using the FindWindow function.  When my app finds an error dialog by its caption/title and classname (#32770), and gets the handle to the dialog box, I use   SendMessage handle, WM_CLOSE, 1, 0 to close the error dialog.  That process works great.

My problem is that now we have the 3rd party app losing focus on a small form - and again, processing stops.  I have tried using FindWindow to get the handle of the small form - but with no success.  While I am searching for the caption that is on the form at the EXACT time of the lost focus - it turns out that the form caption is actually Form1 when it's compiled.  During processing, the caption for Form1 is changed on the fly.

I need a way to find the handle of the form in VB6, so I can send a command to it to bring focus back to it.  I need to be able to add this to my small VB6 app that runs during the night - we can't make changes to the 3rd party app (of course).

Just as an FYI, if I remote into the server while this 3rd party app has lost focus on the small form - all I have to do is click the mouse on the lost focus form - and the application takes off running.

I'd really appreciate some expert help here.  Thanks!


0
CynthiaZCar
Asked:
CynthiaZCar
  • 2
1 Solution
 
TrueGeekSGFCommented:
Do you know the hwnd of the form?  If so, you could call the function below to set it to always be on top...  This may need to be set up in a timer event.  Be careful with this since it can cause message boxes needing attention to shop up behind the form you want to be on top.


Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
 
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_SHOWWINDOW = &H40
Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1
 
 
'Set a form always on the top.
'The form can be specified as a Form or object or through its hWnd property.
'If OnTop=False the always on the top mode is de-activated.
Sub SetAlwaysOnTopMode(hWndOrForm As Variant, Optional ByVal OnTop As Boolean = True)
    Dim hWnd As Long
    ' get the hWnd of the form to be move on top
    If VarType(hWndOrForm) = vbLong Then
        hWnd = hWndOrForm
    Else
        hWnd = hWndOrForm.hWnd
    End If
    SetWindowPos hWnd, IIf(OnTop, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
End Sub

Open in new window

0
 
CynthiaZCarAuthor Commented:
Thank you for your help - however, I do not know the hWnd of the form.  I need a way to find the handle of the form in VB6, so I can send a command to it to bring focus back to it.  I have tried

handle = FindWindow(vbNullString, "Caption of the form")

This does not work for that particular form.  This form is out of focus.  Please advise more again.  Thanks.
0
 
CynthiaZCarAuthor Commented:
I was able to solve my own problem by running my VB6 application as the SAME user the 3rd party application was running as.  It found the correct handle, brought focus back to the form and continued along.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now