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?

Dani AI

Generated

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:

  • Important messages to consider beyond the right‑click are WM_NCHITTEST (to detect whether a point is over the caption), WM_NCLBUTTONDOWN / WM_NCLBUTTONDBLCLK (left click and double‑click), and WM_SYSCOMMAND (system menu via Alt+Space or the icon). Handling these lets a custom menu replace keyboard and mouse invocations as well as clicks on the icon/top‑left corner.
  • When intentionally suppressing the default behavior, return without forwarding the specific message to the base handler; forward everything else. Suppressing the wrong messages will break drag‑to‑move, double‑click‑to‑maximize and other built‑in behaviors, so be explicit about which messages are swallowed.
  • Show the custom menu using screen coordinates so it appears at the right spot on multi‑monitor / high‑DPI setups.

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.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.