Add Items to the ControlBox Menu?

J

Joe Cool

You know how when you minimize an application, and you then right
click on the application's task bar icon, a popup menu appears that
has by default the items Restore, Move, Size, Minimize, Maximize and
Close? It is also known as the ControlBox menu since this is the same
menu you see if you click in the application's ControlBox (upper left
corner icon).

Using VB2005, how to I add my own custom items to that menu?
 
N

Newbie Coder

Joe,

You need to use the GetSystemMenu API, AppendMenu & DeleteMenu then handle
the events in the WndProc sub.

I write this example in 2002 & should change the function declarations to
the DllImport...

This just leaves minimise & close on the system menu:

Declations:

Private Declare Function GetSystemMenu Lib "user32" Alias
"GetSystemMenu" _
(ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer

Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
(ByVal hMenu As Integer, ByVal wFlags As Integer, _
ByVal wIDNewItem As Integer, ByVal lpNewItem As String) As Integer

Private Declare Function DeleteMenu Lib "user32" Alias "DeleteMenu" _
(ByVal hMenu As Integer, ByVal nPosition As Integer, _
ByVal wFlags As Integer) As Integer

Private Const MF_BYPOSITION As Integer = &H400&
Private Const MF_SEPARATOR As Integer = &H800&
Private Const MF_STRING As Integer = &H0&
Private Const WM_SYSCOMMAND As Integer = &H112


Form Load:

' Get the system menu
Dim iMenu As Integer = GetSystemMenu(Me.Handle.ToInt32, 0)
' Delete all system menu item except minimize
DeleteMenu(iMenu, 6, MF_BYPOSITION)
DeleteMenu(iMenu, 5, MF_BYPOSITION)
DeleteMenu(iMenu, 4, MF_BYPOSITION)
DeleteMenu(iMenu, 2, MF_BYPOSITION)
DeleteMenu(iMenu, 1, MF_BYPOSITION)
DeleteMenu(iMenu, 0, MF_BYPOSITION)
' Add a seperator
AppendMenu(iMenu, MF_SEPARATOR, 1000, "")
' Add the 'close' menu item
AppendMenu(iMenu, MF_STRING, 1001, "Close")


WndProc:

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND Then
Select Case m.WParam.ToInt32
Case 1001
' User clicked close - show a copyright message
MessageBox.Show("This application is © 2007 Newbie
Coder. All Rights Reserved", _
Me.Text, MessageBoxButtons.OK,
MessageBoxIcon.Information)
' Exit the application
Application.Exit()
End Select
End If
MyBase.WndProc(m)
End Sub

Remember you use the Append or Delete (menu API) & the index (By Position)
or by command...

I hope this helps,
 
J

Joe Cool

Thanks, I'll look into that. Crap, I hate to use APIs. I wonder why
Microsoft hasn't written .NET classes to handle this stuff?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top