Solved

keeping a floating form on top

Posted on 1998-10-27
2
160 Views
Last Modified: 2010-04-30
How do I force a form to remain ontop of all other forms running on the windows desktop , even if it does not have focus ?
for example say i was using a form to provide a toolbar for another form and wanted it to alway stay on top of that form.
0
Comment
Question by:spg01
2 Comments
 
LVL 2

Accepted Solution

by:
peterwest earned 50 total points
ID: 1441771
Hi there,

I've always used the code below - it was taken from the VB Knowledge Base.   Put the following code in a module.

      Option Explicit
      Public Const SWP_NOMOVE = 2
      Public Const SWP_NOSIZE = 1
      Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
      Public Const HWND_TOPMOST = -1
      Public Const HWND_NOTOPMOST = -2


      Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (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

      Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) As Long

         If Topmost = True Then 'Make the window topmost
            SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
         Else
            SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,FLAGS)
            SetTopMostWindow = False
         End If
      End Function


To use this function just put the following in the Form_Load event of the form you want on top:

        Dim lRetVal as Long
        lRetVal = SetTopMostWindow(Me.Hwnd,True)

And then - in the Form_Unload event put:

        Dim lRetVal as Long
        lRetVal = SetTopMostWindow(Me.Hwnd,False)

This just stops it from being the topmost window.

Hope it helps,

Pete

0
 

Author Comment

by:spg01
ID: 1441772
just the job ! chears
steve.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

776 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