How to remove system menu items from a titlebar (Tip)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: How to remove system menu items from a titlebar (Tip)

  1. #1
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    How to remove system menu items from a titlebar (Tip)

    Here's a simple way to choose what items you want removed from the title bar by command.
    For example double clicking on the titlebar icon to close the application.

    Code:
        Const SC_SIZE As Int32 = &HF000
        Const SC_MOVE As Int32 = &HF010
        Const SC_MINIMIZE As Int32 = &HF020
        Const SC_MAXIMIZE As Int32 = &HF030
        Const SC_CLOSE As Int32 = &HF060
        Const SC_RESTORE As Int32 = &HF120
        Const MF_BYCOMMAND As Int32 = &H0
        Const MF_SEPARATOR As Int32 = &H800
        Private Declare Function apiRemoveMenu Lib "user32" Alias "RemoveMenu" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32
        Private Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hwnd As Int32, ByVal bRevert As Int32) As Int32
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_SIZE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_MOVE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_MINIMIZE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_MAXIMIZE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_CLOSE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), SC_RESTORE, MF_BYCOMMAND)
            apiRemoveMenu(apiGetSystemMenu(Me.Handle.ToInt32, False), 0, MF_SEPARATOR)
        End Sub

    Here's how to do it By Position, so you can remove all titlebar menu stuff:
    Code:
        Const MF_BYPOSITION As Int32 = &H400
        Const MF_REMOVE As Int32 = &H1000
        Public Declare Function apiDrawMenuBar Lib "user32" Alias "DrawMenuBar" (ByVal hwnd As Int32) As Int32
        Public Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hWnd As Int32, ByVal bRevert As Boolean) As Int32
        Public Declare Function apiRemoveMenu Lib "user32" Alias "RemoveMenu" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32
        Public Declare Function apiGetMenuItemCount Lib "user32" Alias "GetMenuItemCount" (ByVal hMenu As Int32) As Int32
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim hSysMenu As Int32 = apiGetSystemMenu(Me.Handle.ToInt32, False)
            If hSysMenu Then
                Dim nCnt As Int32 = apiGetMenuItemCount(hSysMenu)
                If nCnt Then
                    Dim i As Int32
                    For i = 0 To nCnt
                        apiRemoveMenu(hSysMenu, nCnt - (i + 1), MF_BYPOSITION Or MF_REMOVE) 'remove all
                    Next
                    apiDrawMenuBar(Me.Handle.ToInt32)
                    Me.Text = "System changed"
                End If
            End If
        End Sub
    Last edited by TT(n); January 19th, 2008 at 02:20 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center