troubleshooting Question

How to put a window on the foreground and give it the focus with SetWindowPos ?

Avatar of B_B_Boris
B_B_Boris asked on
Visual Basic Classic.NET Programming
2 Comments1 Solution512 ViewsLast Modified:
Hi there,

I need to make an exe which puts the window "My window" on the foreground and gives the focus to it.
I run my exe from a shell prompt window. Initially, "My window" is open but is behind the shell window.

When I run the program, "My window" is moved to the left top corner of the screen and is resized so
that only the bar is visible. As if SWP_NOSIZE and SWP_NOMOVE were not taken into account.

Moreover, "My window doesn't have the focus.

What did I do wrong ?

Thanks in advance

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    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

    Private Const SWP_NOSIZE = &H1
    Private Const SWP_NOMOVE = &H2
    Private Const HWND_TOPMOST = -1
    Private Const SWP_SHOWWINDOW = &H40

    Dim myHandle As Long

    myHandle = FindWindow(vbNullString, "My window")
    SetWindowPos(myHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOMOVE)


Mike Tomlinson
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros