Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Window Precedence

Posted on 2000-03-10
7
Medium Priority
?
166 Views
Last Modified: 2011-10-03
I have an application which has a main window, and when you press a button on the main window, other applications launch as part of the program.  Now, when the applications launch, they take over focus of the program with the main program window still running in the background (which is what I desired).  However, my problem is that when I select the main program, my launched application falls behind the main window.  What I want to do is leave the launched application on top of the main window after clicking the main window, but I want the launched window to be gray and disabled until the user clicks on the launched window again.

Also.. I want to be able to set precedence for the different windows.. like say if window foo is open.. and window baz is open.. I want to have a way for my application to determine that baz should remain on top of foo.
0
Comment
Question by:ChrisH
  • 4
  • 3
7 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 600 total points
ID: 2603963
Add the following code to a MODULE:

'-------------------------------------------------------------------------------
    Public Const HWND_TOPMOST = -1
    Public Const HWND_NOTOPMOST = -2
    Public Const SWP_NOMOVE = 2
    Public Const SWP_NOSIZE = 1
    Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    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
    Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
    Public Sub SetTopMostWindow(ObjHwnd As Long, Optional Switch As Boolean)
        If Switch = True Then
            SetWindowPos ObjHwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
        Else
            SetWindowPos ObjHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS
        End If
    End Sub
'-------------------------------------------------------------------------------



Then after you launch your external application, do this to make it always on top:

'REPLACE "Untitled - Notepad" with the string in the TitleBar of your launched application
Dim hWnd As Long
hWnd = FindWindow(vbNullString, "Untitled - Notepad")
SetTopMostWindow hWnd, True



And do this to reset it back to normal:

'REPLACE "Untitled - Notepad" with the string in the TitleBar of your launched application
Dim hWnd As Long
hWnd = FindWindow(vbNullString, "Untitled - Notepad")
SetTopMostWindow hWnd, False





Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2603971
By the way, when you make the call:


   hWnd = FindWindow(vbNullString, "Untitled - Notepad")

if hWnd = 0 then the API did not find the window, so don't call the SetTopMostWindow function....



Cheers!®©
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2604617
Is there a way to get the title name in before the findwindow call?  See.. I launch a window using a Call Shell() function call based on a register value.  Any thoughts?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ChrisH
ID: 2604823
Is there a way to get the title name in before the findwindow call?  See.. I launch a window using a Call Shell() function call based on a register value.  Any thoughts?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2604862
The SetTopMostWindow function I gave you requires a Window Handle and Shell returns an Instance handle...

Check out the following microsoft KB article:

HOWTO: Find a Window Handle from an Instance Handle
http://support.microsoft.com/support/kb/articles/Q242/3/08.ASP?LNG=ENG&SA=MSDN&FR=0 




Cheers!®©
0
 
LVL 1

Author Comment

by:ChrisH
ID: 2605990
Thanks for your help..
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2607091
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question