Hi
I want to capture mouse events when the mouse is positioned on the title bar of a form. In particular I want to be able to replace the standard context menu (to move, size, maximize, minimize, close) with one of my own.
Can anyone help please?
Hi
I want to capture mouse events when the mouse is positioned on the title bar of a form. In particular I want to be able to replace the standard context menu (to move, size, maximize, minimize, close) with one of my own.
Can anyone help please?
A good, practical approach is already shown by : the title bar is part of the window "non‑client" area, so intercepting non‑client messages in the form's WndProc is the right place to capture mouse activity there. That technique cleanly catches the usual right‑click on the caption and lets a ContextMenuStrip be shown at the cursor instead of the default menu.
A few additions and caveats that fill gaps left by the example above:
If full control is required (no system menu at all), two common options are: modify the system menu via the Win32 GetSystemMenu/ModifyMenu/RemoveMenu APIs, or remove the standard chrome entirely (FormBorderStyle = None) and implement a custom caption area and hit‑testing. Both approaches require extra work for keyboard accessibility and for preserving expected shortcuts (Alt+Space, keyboard focus, screen readers), so plan to re‑implement equivalents where needed.
Quick troubleshooting checklist: verify the form still moves and resizes after changes; test Alt+Space and the icon click; confirm correct placement on different DPI/monitor setups; and only swallow the exact non‑client messages intended. This preserves expected window behavior while replacing the title‑bar menu.
Jump to Post— selvaganapathy 31Hi, You have to override Window Procedure for the particular form. I give example that shows Context menu when user right click on the Title bar
Public Const WM_NCRBUTTONDOWN = &HA4 Public Const WM_NCRBUTTONUP = &HA5 Dim ContMenu As ContextMenuStrip Private Sub Form1_Load(ByVal sender As System.Object, ByVal …
Hi, You have to override Window Procedure for the particular form. I give example that shows Context menu when user right click on the Title bar
Public Const WM_NCRBUTTONDOWN = &HA4
Public Const WM_NCRBUTTONUP = &HA5
Dim ContMenu As ContextMenuStrip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ContMenu = New ContextMenuStrip()
ContMenu.Items.Add("Open")
ContMenu.Items.Add("Exit")
End Sub
Protected Overrides Sub WndProc(ByRef WndMsg As System.Windows.Forms.Message)
If WndMsg.Msg = WM_NCRBUTTONDOWN Then
ContMenu.Show(Control.MousePosition)
Return ' Prevent to process this Message to the Base Class
End If
MyBase.WndProc(WndMsg)
End Sub This example just override the Context menu, but still if user click on the Top Left corner or Alt + Space it will show the context menu. If you want to change this menu you have to go for Win32 API.
Many thanks for your reply. I can now do exactly what I wanted to.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.