views:

406

answers:

3

This is a Windows Forms / .Net C# question.

I have a borderless windows whose transparency key and background color make it completely transparent. Inside the window are a couple of user controls.

I want to be able to move the window. I know how to do this on the parent window, but my problem is that the child controls are the only thing visible and thus the only thing click-able.

The question is: how can I pass certain messages up to the Parent so the Parent can move when the right mouse button is down and the mouse is moving on any one of the child controls?

Or maybe you can suggest another way?

Thanks for the help.

Mark

A: 

You need to call the SendMessage API function to send mouse messages to your parent control.

It would probably be easiest to do this by overriding your control's WndProc method.

SLaks
Mark Wager-Smith
You should capture all of the mouse messages in the link. You'll need to adjust the coordinates to be relative to the parent control. (Call `PointToScreen`, then `PointToScreen`)
SLaks
Thanks. I think I got it. But you say PointToScreen then PointToScreen? Do you mean PointToScreen to get the screen coordinates then PointToClient to transpose those to the client of the parent window?
Mark Wager-Smith
Yes, I did mean that. (typo)
SLaks
A: 

You can achieve your goal even without SendMessage using System.Windows.Forms.Message class. If you have done dragging I guess you are familiar with WM_NCLBUTTONDOWN message. Send it to you parent from your control's MouseDown event.

Here is an example for moving the form clicking on control label1. Note the first line where sender is used to release the capture from clicked control. This way you can set this handler to all controls intended to move your form.

This is complete code to move the form. Nothing else is needed.


public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

private void label1_MouseDown(object sender, MouseEventArgs e)
{
      (sender as Control).Capture = false;
      Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
      base.WndProc(ref msg);
}

Hope this helps.

Petr Havlicek
Thanks for the suggestion. Unfortunalty, the code you provided results in the UserControl moving withing the boundries of the Parent form.Kind of a neat effect, but unfortunately not what I'm after.
Mark Wager-Smith
Just discovered that by changing the Handle in your code to the parent.Handle...it works great. Thanks very much for the help. Now I just need to figure out how to allow the UserControl to process some of its own mouse events and pass only the drag event to the Parent. But I feel I'm pretty close. Thanks again.
Mark Wager-Smith
Hi Mark, you are welcome. If you find my answer usefull please mark it as useful or accepted answer, thanks.Which events do you want to process by your UserControl?
Petr Havlicek
+4  A: 

I think the easiest way is to add this event to your child controls:

/// <summary>
/// The event that you will throw when the mouse hover the control while being clicked 
/// </summary>
public event EventHandler MouseRightClickedAndHoverChildControl;

After, all the parent have to do is to subscribe to those events and make the operations to move the Parent:

ChildControl.MouseRightClickedAndHoverChildControl += OnMouseHoverChildControl;

private void OnMouseHoverChildControl(object sender, EventArgs e)
{
    //do foo...
}
Pierre-Luc Champigny