Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 26, 1999

Make a Form Stay on Top Redux

Many routines use the SetWindowPos API to always keep a form on top. Most require the user to remember several nonintuitive arguments. I'll not only show you how to simplify the arguments, but I'll also illustrate the usefulness of the new Enum function. Enums have several advantages: Possible argument values are listed for you in the IDE using Microsoft's IntelliSense, the arguments are listed in the Object Browser, and Enums are included automatically in the type library when used in a class module. This all translates into easier programming and more code reuse:

 
'Paste this code into a module
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_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOACTIVATE = &H10

Public Enum WindowPos
	vbTopMost = -1&
	vbNotTopMost = -2&
End Enum

Public Sub SetFormPosition(hWnd As Integer, _
	Position As WindowPos)
	Const wFlags As Long = SWP_NOMOVE Or _
		SWP_NOSIZE Or SWP_SHOWWINDOW Or _
		SWP_NOACTIVATE 
	If Position = vbTopMost or Position = _
		vbNotTopMost Then
		SetWindowPos hWnd, Position, 0, 0, 0, 0, _
			wFlags
	End If
End Sub

'Add two command buttons to a form; then paste in 'this code

 
Private Sub Command1_Click()
	'Makes form topmost
	SetFormPosition Me.hWnd, vbTopMost
End Sub

Private Sub Command2_Click()
	'Restore form to normal position
	SetFormPosition Me.hWnd, vbNotTopMost
End Sub
Jared A.
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date