• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Show a window

This is not just showing a window but showing one that's inside an MDI form. I can get the handle of the window without a problem, but using the SetForeGroundWindow or the ShowWindow API will not actually set the desired window to the forefront inside the MDI. I will also like to use the other constants such as SHOWMINIMIZED and SHOWMAXIMIZED. Anyone have any ideas?
0
microfleet
Asked:
microfleet
  • 4
  • 3
1 Solution
 
Erick37Commented:
This is not your application, but an external MDI application, correct?
0
 
Erick37Commented:
Option Explicit
Const SW_MAXIMIZE = 3
Const SW_MINIMIZE = 6
Const SW_NORMAL = 1
Const SW_RESTORE = 9
Private Declare Function BringWindowToTop Lib "user32" _
    (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" _
    (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Sub Command1_Click()
Dim lhWnd As Long
'Grab the MDI child window handle you want
lhWnd = &HCC8
'Maximize the window
Call MyShowWindow(lhWnd, SW_MAXIMIZE)
End Sub

Function MyShowWindow(ByVal lhWnd As Long, _
        Optional ByVal cmdShow As Long = SW_RESTORE) As Long
    Dim lRet As Long
    lRet = ShowWindow(lhWnd, cmdShow)
    MyShowWindow = lRet
    lRet = BringWindowToTop(lhWnd)
End Function
0
 
microfleetAuthor Commented:
Erick37, I realized earlier that it was an internal error: I was grabbing the EDIT box handle by mistake, but I do like the BringWindowToTop API. I never heard of that one. I will give you the points just because anyone who looks at the question will get a good answer, but I would like to add one thing to my question: Do you have any idea why, after the window is "on top," the SendKeys function sends text only to the window that was previously at the forefront?

It doesnt matter if you know why, I'll still give the points.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ozcustomCommented:
The BringWindowToTop call is a good API to call to merely show the window but it doesn't transfer the focus to that window...

A better way to show a target window - inside the MDI form is to use the SetWindowPos API call using the HWND_TOPMOST constant  (Page 243 of the Visual Basic Programmers Guide to the Win32 API)  - another way to show the desired window is to use the SetActiveWindow however this doesn't set the focus so really a combination of the two API functions

SetWindowPos

and

SetFocusAPI

will ensure that the window you want is on top AND that it has the focus
0
 
Erick37Commented:
SetFocusAPI will not work on windows outside of the calling process.

If you have been able to use this API to set the focus on an external program's window, I would love to see your code.  This point has been raised before in another question.
0
 
Erick37Commented:
If you want to activate the MDI child window and give it focus, use this code.  Please note that the question is still locked by ozcustom.

Option Explicit

Const SW_MAXIMIZE = 3
Const SW_MINIMIZE = 6
Const SW_NORMAL = 1
Const SW_RESTORE = 9
Private Declare Function SetActiveWindow Lib "user32" _
    (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" _
    (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As Long) As Long

Private Sub Command1_Click()
Dim lhWnd As Long
'Grab the MDI child window handle you want
lhWnd = &HE54
'Maximize the window
Call MyShowWindow(lhWnd, SW_RESTORE)
End Sub

Function MyShowWindow(ByVal lhWnd As Long, _
        Optional ByVal cmdShow As Long = SW_RESTORE) As Long
    Dim lRet As Long
    lRet = ShowWindow(lhWnd, cmdShow)
    MyShowWindow = lRet
    lRet = SetActiveWindow(lhWnd)
    lRet = SetForegroundWindow(lhWnd)
End Function
0
 
microfleetAuthor Commented:
sorry ozcustom but as I said before I already figured out the trouble and because Erick gave a good answer I was giving the points to him. I wish you would have just left a comment so I wouldnt have to reject your answer but sorry
0
 
microfleetAuthor Commented:
thanks again
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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