views:

171

answers:

1

I have a windows form that can be moved around by clicking and dragging on any portion of the form. I used the method of overriding WndProc, and setting the result of the NCHITTEST function to be HTCAPTION, in order to fool the form into thinking I clicked the caption - so it enables dragging.

The code for this works great, and is below:

protected override void WndProc(ref Message msg)
 {
   if (msg.Msg == (int)WinAPI.NCHITTEST)
   {
    DefWndProc(ref msg);
    if ((int)msg.Result == (int)MousePositionCodes.HTCLIENT)
    {
       msg.Result = (IntPtr)MousePositionCodes.HTCAPTION;
       return;
    }
   }
  }
  base.WndProc(ref msg);
 }

The problem occurs when I dock a ToolStripPanel into the form (this is acting as a draggable toolbar). I need any portion of the ToolStripPanel that is not covered by a ToolStrip to pass up the messages necessary to cause the whole form to enter drag mode.

I have created my own ToolStripContainer class to override the WndProc function and have tried using the same function as above, but it causes the ToolStripContainer to enter drag mode within the form, which is not the desired functionality.

I have also tried passing up NCHITTEST messages to the parent, as well as constructing a new message with the current mouse coordinates and sending it to the parent using the WinAPI and the parent's window handle.

I have to be missing something simple here... Anyone have any ideas?

+3  A: 

Try in WndProc of your own ToolStripContainer where you have testing for WM_NCHITTEST returning HTTRANSPARENT (-1) for the area where you want drag to happen. This will cause the message to go up in chain to your form where you handle it and return HTCAPTION so drag happens.

Hope this helps.

DevComponents - Denis Basaric
That was PERFECT! Thank you!!I have been struggling over this for over a week. You just saved my deadline ;)
Glad I could help :-)
DevComponents - Denis Basaric