1

In my .net winform application's title bar, I have put up a Panel control, which has a lot of other controls. Due to this, I don't get a context menu or the drag functionality when I click on the Panel. But the same appears when I click outside the Panel in the title bar.

P.S - Obviously for the users, the whole bar on the top of the application is known as the "Title bar", and they want the drag and context menu everywhere on the title bar(which for them, includes the Panel also.)

So is there any way to achieve this. I want the context menu to appear when I right-click on the Panel or any control in the Panel. I also want to drag my whole application window, when I try to click and drag on the Panel or any of the control in the Panel.

Appreciate any kind of help. Thanks!

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Sandeep
  • 5,581
  • 10
  • 42
  • 62
  • 1
    The best way to do this is listed in the answer at http://stackoverflow.com/questions/4577141/move-window-without-border – David Thielen May 24 '12 at 13:21

1 Answers1

2

You have to tunnel messages to the WndProc as the "normal" title bar would do.

Declare this:

private const int WM_SYSCOMMAND = 0x112;
private const int SC_MOUSEMOVE = 0xf012;
private const int SC_MOUSEMENU = 0xf090;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg,
    IntPtr wParam, IntPtr lParam);

Then on your Panel MouseDown handler do this for the left click:

SendMessage(Handle, WM_SYSCOMMAND, new IntPtr(SC_MOUSEMOVE), IntPtr.Zero);

And this for the right click:

SendMessage(Handle, WM_SYSCOMMAND, new IntPtr(SC_MOUSEMENU), IntPtr.Zero);
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208