Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #109
Online Version at http://www.codeoftheweek.com/membersonly/bi/0109.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Removing items from the System Menu

Earn REAL money surfing the web!

If you would like to make some extra cash for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Six months of VB Training for only $49.99

Want to get up to speed on the latest Visual Basic programming? Includes Visual Basic 6 and Visual InterDev 6. Check out our training programs at http://www.codeoftheweek.com/vbtraining.html

Requirements

In this Issue

In this issue we discuss how to remove items from the system menu.

If you have any questions about using this module, let us know at questions@codeoftheweek.com

basFormUtils

The basFormUtils module contains a method which allows you to remove any option from the system menu of a Visual Basic form. It provides these features using two API calls: GetSystemMenu and RemoveMenu. The GetSystemMenu retrieves the handle to the system menu of the specified window. The RemoveMenu call is the one that actually removes the menu item from the menu.

Methods

Public Sub RemoveSystemMenu(ByVal frm As Form, ByVal eMenu As enumMenuType)

This method will remove the system menu item on the form specified by frm. The menu item is defined by the enumerator enumMenuType. The valid choices for eMenu are mtMove, mtSize, mtMinimize, mtMaximize, mtClose, mtRestore. Each enumerator will remove the associated menu option. For example, if you specify the mtMove enumerator it will remove the Move option from the system menu.

Sample Usage

The below sample shows how to call the RemoveSystemMenu method with various options. In this particular example it will remove the maximize, restore and size options from the system menu. You would typically call this method from the Form_Load method within your form.

Private Sub Form_Load()
    RemoveSystemMenu Me, mtMaximize
    RemoveSystemMenu Me, mtSize
    RemoveSystemMenu Me, mtRestore
End Sub

Source Code

Create a new module and paste this source code into it. Change the name of the module to basFormUtils. If you have any questions, email us at help@codeoftheweek.com

'----------------------------------------------------------------------
'
'   Module Name:    basFormUtils
'   Written By:     C&D Programming Corp.
'   Create Date:    11/99
'   Copyright:      Copyright 1999 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal lNewLong As Long) As Long

Private Declare Function GetWindowLong Lib "User32" _
    Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal lIndex As Long) As Long

Private Const GWL_STYLE = (-16)
Private Const MF_BYPOSITION = &H400
Private Const MF_BYCOMMAND = &H0
Private Const MAXIMIZE_BUTTON_MASK = &HFFFEFFFF
Private Const MINIMIZE_BUTTON_MASK = &HFFFDFFFF

' These are the command values for the various options on the system menu.
Enum enumMenuType
    mtMove = &HF010
    mtSize = &HF000
    mtMinimize = &HF020
    mtMaximize = &HF030
    mtClose = &HF060
    mtRestore = &HF120
End Enum

Private Declare Function GetSystemMenu Lib "User32" _
    (ByVal hWnd As Long, ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "User32" _
    (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

'
'   Remove a system menu option, such as close, minimize, size or move.
'
'
Public Sub RemoveSystemMenu(ByVal frm As Form, ByVal eMenu As enumMenuType)

    Dim lSystemMenu As Long
    Dim lWindowStyle As Long
    Dim lRet As Long

    lSystemMenu = GetSystemMenu(frm.hWnd, 0&)
    'Call RemoveMenu(lSystemMenu, eMenu, MF_BYPOSITION)

    Select Case eMenu
        Case mtClose, mtMove, mtSize, mtRestore
            Call RemoveMenu(lSystemMenu, eMenu, MF_BYCOMMAND)
        Case mtMinimize
            Call RemoveMenu(lSystemMenu, eMenu, MF_BYCOMMAND)
            ' also remove the minimize button widget on the top right of the window
            lWindowStyle = GetWindowLong(frm.hWnd, GWL_STYLE)
            lWindowStyle = lWindowStyle And MINIMIZE_BUTTON_MASK
            Call SetWindowLong(frm.hWnd, GWL_STYLE, lWindowStyle)
        Case mtMaximize
            Call RemoveMenu(lSystemMenu, eMenu, MF_BYCOMMAND)
            ' also remove the maximize button widget on the top right of the window
            lWindowStyle = GetWindowLong(frm.hWnd, GWL_STYLE)
            lWindowStyle = lWindowStyle And MAXIMIZE_BUTTON_MASK
            Call SetWindowLong(frm.hWnd, GWL_STYLE, lWindowStyle)
    End Select
End Sub

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com