devxlogo

Remove Unwanted System Menu Options

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 menusPrivate Declare Function RemoveMenu Lib _	"User32" (ByVal hMenu As Long, _	ByVal nPosition As Long, ByVal wFlags _	As Long) As LongPrivate Declare Function GetSystemMenu Lib _	"User32" (ByVal hWnd As Long, ByVal revert _	As Long) As LongPrivate Declare Function GetWindowLong Lib _	"User32" Alias "GetWindowLongA" (ByVal hWnd _	As Long, ByVal lIndex As Long) As LongPrivate Declare Function SetWindowLong Lib _	"User32" Alias "SetWindowLongA" (ByVal hWnd _	As Long, ByVal lIndex As Long, _	ByVal dwNewLong As Long) As LongPrivate Const MF_BYPOSITION = &H400Private Const MAXIMIXE_BUTTON = &HFFFEFFFFPrivate Const MINIMIZE_BUTTON = &HFFFDFFFFPrivate Const GWL_STYLE = (-16)' enumeration used when calling VBRemoveMenuPublic Enum RemoveMenuEnum	rmMove = 1	rmSize = 2	rmMinimize = 3	rmMaximize = 4	rmClose = 6End EnumPublic 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 SelectEnd 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
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist