Solved

How do you force a form to stay on top?

Posted on 2001-06-26
3
220 Views
Last Modified: 2011-09-20
How do you force a form to stay on top of all forms in the same program, or on top of every other program. Are both possible?

thanks
0
Comment
Question by:kristofer
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 10 total points
Comment Utility
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
Public Sub StayOnTop(frmForm As Form, fOnTop As Boolean)
 
  Const HWND_TOPMOST = -1
  Const HWND_NOTOPMOST = -2
 
  Dim lState As Long
  Dim iLeft As Integer, iTop As Integer, iWidth As Integer, iHeight As Integer
   
  With frmForm
    iLeft = .Left / Screen.TwipsPerPixelX
    iTop = .Top / Screen.TwipsPerPixelY
    iWidth = .Width / Screen.TwipsPerPixelX
    iHeight = .Height / Screen.TwipsPerPixelY
  End With
 
  If fOnTop Then
    lState = HWND_TOPMOST
  Else
    lState = HWND_NOTOPMOST
  End If
  Call SetWindowPos(frmForm.hwnd, lState, iLeft, iTop, iWidth, iHeight, 0)
End Sub
Private Sub Command1_Click()
    StayOnTop Me, True
End Sub
Private Sub Command2_Click()
    StayOnTop Me, False
End Sub
0
 
LVL 2

Author Comment

by:kristofer
Comment Utility
thanks a lot, I was in a rush and that was just what I needed!
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
ya, it's ok!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now