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: Intermediate
Nov 15, 1999

Remove Unwanted System Menu Options

You've probably wanted to limit the normal operations of a form, such as resizing it, preventing it from being minimized or maximized, or allowing it to be closed only when you say so. The trick is to remove the control menu that corresponds to the functionality you want to limit. For example, when you remove the Size menu from the Control menu, the user won't be able to resize your form. The same goes for Minimize, Maximize, and Move. Add this code to a BAS module:
 
' declarations necessary to work with the Control 
' Box menus
Private Declare Function RemoveMenu Lib _
	"User32" (ByVal hMenu As Long, _
	ByVal nPosition As Long, ByVal wFlags _
	As Long) As Long
Private Declare Function GetSystemMenu Lib _
	"User32" (ByVal hWnd As Long, ByVal revert _
	As Long) As Long
Private Declare Function GetWindowLong Lib _
	"User32" Alias "GetWindowLongA" (ByVal hWnd _
	As Long, ByVal lIndex As Long) As Long
Private Declare Function SetWindowLong Lib _
	"User32" Alias "SetWindowLongA" (ByVal hWnd _
	As Long, ByVal lIndex As Long, _
	ByVal dwNewLong As Long) As Long

Private Const MF_BYPOSITION = &H400
Private Const MAXIMIXE_BUTTON = &HFFFEFFFF
Private Const MINIMIZE_BUTTON = &HFFFDFFFF
Private Const GWL_STYLE = (-16)

' enumeration used when calling VBRemoveMenu
Public Enum RemoveMenuEnum
	rmMove = 1
	rmSize = 2
	rmMinimize = 3
	rmMaximize = 4
	rmClose = 6
End Enum

Public Sub VBRemoveMenu(ByVal TargetForm _
	As Form, ByVal MenuToRemove As RemoveMenuEnum)
	' VBRemoveMenu
	' this routine removes the specified menu item 
	' from the control menu and the corresponding 
	' functionality from the form
	'
	' Parameters
	'	TargetForm - the form to perform the 
	'	operation on
	'	MenuToRemove - Enum specifying which menu 
	'	to remove
	Dim hSysMenu As Long
	Dim lStyle As Long
	hSysMenu = GetSystemMenu(TargetForm.hWnd, 0&)
	RemoveMenu hSysMenu, MenuToRemove, _
		MF_BYPOSITION

	Select Case MenuToRemove
		Case rmClose
			' when removing the Close menu, also 
			' remove the separator over it
			RemoveMenu hSysMenu, MenuToRemove - 1, _
				MF_BYPOSITION
		Case rmMinimize, rmMaximize
			' get the current window style
			lStyle = GetWindowLong( _
				TargetForm.hWnd, GWL_STYLE)

			If MenuToRemove = rmMaximize Then
				' turn off bits for Maximize arrow 
				' button
				lStyle = lStyle And MAXIMIXE_BUTTON
			Else
				' turn off bits for Minimize arrow 
				' button
				lStyle = lStyle And MINIMIZE_BUTTON
			End If

			' set the new window style
			SetWindowLong TargetForm.hWnd, _
				GWL_STYLE, lStyle
	End Select
End Sub
Then, when you load your form, call VBRemoveMenu with one of the menu enumerations:
 
	VBRemoveMenu Me, rmMaximize
Not only will the menu be removed from the Control menu, but the corresponding functionality of the form will be removed as well. Because the menus are removed by ordinal position, remove them in descending order if you want to remove more than one. For example:
 
Private Sub Form_Load()
	Call VBRemoveMenu(Me, rmClose)
	Call VBRemoveMenu(Me, rmMaximize)
End Sub
Earl Damron
 
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