我在自定义控件上有一个鼠标点击功能:
protected override void OnMouseClick(MouseEventArgs e)
{
double x_pos = e.X;
double y_pos = e.Y;
// ...
}
我需要在自定义控件上发生鼠标单击事件时在主窗体上调用此函数(同时仍然获取相对于自定义控件的坐标)。
非常感谢任何帮助。
答案 0 :(得分:0)
尝试以下方法:
public partial class Form1 : Form
{
public Form1()
{
//InitializeComponent();
var label = new CustomLabel(DoSomething) {
Parent = this, Text = "Test", Top = 50, Left = 50, BackColor = Color.Pink };
this.MouseClick += Form1_MouseClick;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Text = e.Location.ToString();
}
void DoSomething(Point p)
{
Form1_MouseClick(this, new MouseEventArgs(MouseButtons.None, 0, p.X, p.Y, 0));
}
}
public class CustomLabel : Label
{
Action<Point> _action;
public CustomLabel(Action<Point> action)
{
_action = action;
}
protected override void OnMouseClick(MouseEventArgs e)
{
_action(e.Location);
}
}
将委托传递给自定义控件构造函数。在OnMouseClick
中调用此操作。