How to pass messages from a child user control to a parent

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

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

I want to be able to move the window. I know how to do this in the parent window, but my problem is that the child controls are the only thing visible and therefore the only thing that can be clicked.

Question: how can I send certain messages to the parent so that the parent can move when the right mouse button is lowered and the mouse moves over any of the child controls?

Or maybe you can suggest another way?

Thanks for the help.

Mark

+3
source share
4 answers

You can achieve your goal even without SendMessage using the class System.Windows.Forms.Message. If you did a drag and drop, I think you are familiar with the message WM_NCLBUTTONDOWN. Send it to your parents from your MouseDown control event.

The following is an example of moving a form by clicking on the control shortcut1. Notice the first line in which the sender is used to release the captured control. Thus, you can set this handler for all controls designed to move your form.

This is the complete code for moving the form. Nothing more 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.

+7

, - :

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

, :

ChildControl.MouseRightClickedAndHoverChildControl += OnMouseHoverChildControl;

private void OnMouseHoverChildControl(object sender, EventArgs e)
{
    //do foo...
}
+7
0

I had exactly this question ... but came up with a different answer. If you have a message in your WndProc, you can simply change the handle to your parent handle, and then pass it.

I needed to do this in our derived TextBox ... TextBox eats scroll wheel events, even if ScrollBars is set to None. I wanted them to spread on the Form. So, I just put this inside WndProc for my derived TextBox:

            case 0x020A: // WM_MOUSEWHEEL
            case 0x020E: // WM_MOUSEHWHEEL
                if (this.ScrollBars == ScrollBars.None && this.Parent != null)
                    m.HWnd = this.Parent.Handle; // forward this to your parent
                base.WndProc(ref m);
                break;

            default:
                base.WndProc(ref m);
                break;
0
source

Source: https://habr.com/ru/post/1727703/


All Articles