Solved

How do you force a form to stay on top?

Posted on 2001-06-26
3
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 10 total points
ID: 6227192
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
ID: 6227220
thanks a lot, I was in a rush and that was just what I needed!
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6227230
ya, it's ok!
0

Featured Post

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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

734 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