current position:Home>C sharp mouse penetration

C sharp mouse penetration

2022-02-02 21:23:07 CSDN Q & A

C# winform In form How to call the of the parent control through the mouse event in the child control OnMouseMove And other methods to override the mouse event of the parent control , It can call the mouse movement event of the child control and respond to the mouse event of the parent control at the same time . For example, the problem I have now , In a panel There are several buttons inside button, When I move the mouse over the button , It doesn't trigger panel Of MouseMove event . Please give us some guidance , thank !




Refer to the answer 1:

First of all, analyze the thinking : as everyone knows ,window The system is a message driven system , So, if you want to achieve the effect you want, you can actually achieve it by forwarding messages . The specific code is shown below :

using System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Windows.Forms;namespace Test{    public partial class Form3 : Form    {        public Form3()        {            InitializeComponent();            PenetrableButton penetrableButton = PenetrableButton.AddPenetrableButton(this, 100, 100, 200, 200, "PenetrableButton");        }        protected override void OnMouseMove(MouseEventArgs e)        {            this.Text = e.X + "," + e.Y;            base.OnMouseMove(e);        }        protected override void OnMouseDown(MouseEventArgs e)        {            this.Text = "MouseLeftButtonDown:" + e.X + "," + e.Y;            base.OnMouseDown(e);        }    }    public class PenetrableButton : Button    {        private List<int> penetrableMessageList;        private PenetrableButton()        {            penetrableMessageList = new List<int>();            penetrableMessageList.Add(Wind32Util.WM_MOUSEMOVE);            penetrableMessageList.Add(Wind32Util.WM_MOUSELEFTBUTTONDOWN);        }        public static PenetrableButton AddPenetrableButton(Form parent, int top, int left, int width, int height, string text)        {            PenetrableButton penetrableButton = new PenetrableButton();            penetrableButton.Top = top;            penetrableButton.Left = left;            penetrableButton.Width = width;            penetrableButton.Height = height;            penetrableButton.Text = text;            if (parent != null)                parent.Controls.Add(penetrableButton);            return penetrableButton;        }        protected override void OnMouseMove(MouseEventArgs mevent)        {            this.Text = mevent.X + "," + mevent.Y;            base.OnMouseMove(mevent);        }        protected override void OnMouseDown(MouseEventArgs e)        {            this.Text = "MouseLeftButtonDown:" + e.X + "," + e.Y;            base.OnMouseDown(e);        }        protected override void WndProc(ref Message m)        {            //  Forward the message to the parent control             if (this.Parent.Handle != null && penetrableMessageList.Contains(m.Msg))            {                //  Correct the mouse coordinates according to the relative position                 Wind32Util.SendMessage(this.Parent.Handle, m.Msg, m.WParam, (IntPtr)(m.LParam.ToInt32() + ((this.Top << 16) | this.Left)));            }            base.WndProc(ref m);        }    }    public class Wind32Util    {        //  Mouse movement message         public const int WM_MOUSEMOVE = 0x200;        public const int WM_MOUSELEFTBUTTONDOWN = 0x201;        [DllImport("User32.dll", EntryPoint = "SendMessage")]        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);    }}

1. Create your own one derived from Button Class , And rewrite WndProc Message handler , Find the news you are interested in through SendMessage Send this message to the target .
2. Due to the relative position , So you can't forward messages directly , Need to correct the coordinates of the mouse , The specific coordinates of the mouse are through lParam Got , Formula is :y<<16|x.
3. The above example only gives you the example of forwarding MouseMove and MouseLeftButtonDown Examples of these two messages , It should be possible to draw inferences from one instance , Instead, forward any messages you are interested in .
4. The forwarding object in the example only refers to the parent control forwarded to itself , You should also draw inferences from one instance, which can be forwarded to any object you are interested in .
5. When forwarding, you should pay attention to ,Button Whether class and other classes have the processing of this message case, without , Forwarded will not be processed .
Finally, attach the renderings :

img




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022123038708.html

Random recommended