首页 > 其他 > 详细

1、获取相对于控件的鼠标坐标;2、去掉控件的EventHandler的所有绑定事件;

时间:2015-12-18 12:57:33      阅读:187      评论:0      收藏:0      [点我收藏+]

最近使用Emgu开发计算机视觉相关软件,但是里面的ImageBox控件自定义事件比较多,很烦人,而且winform不支持直接从控件获取相对于控件的鼠标坐标。这两点就导致我要想在ImageBox上绘制一些东西比较麻烦,解决如下:

1、获取相对于控件的鼠标坐标;

解决方法很简单:

Point x = imageBox1.PointToClient(Form1.MousePosition);
label2.Text = x.X.ToString() + " | " + x.Y.ToString();

 

2、去掉控件的EventHandler的所有绑定事件;

这个稍微麻烦,这里贴上stackoverflow粘过来的答案,尚未测试:

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();

            button1.Click += button1_Click;
            button1.Click += button1_Click2;
            button2.Click += button2_Click;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello");
        }

        private void button1_Click2(object sender, EventArgs e)
        {
            MessageBox.Show("World");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RemoveClickEvent(button1);
        }

        private void RemoveClickEvent(Button b)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick", 
                BindingFlags.Static | BindingFlags.NonPublic);
            object obj = f1.GetValue(b);
            PropertyInfo pi = b.GetType().GetProperty("Events",  
                BindingFlags.NonPublic | BindingFlags.Instance);
            EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
            list.RemoveHandler(obj, list[obj]);
        }
    }
}

 

1、获取相对于控件的鼠标坐标;2、去掉控件的EventHandler的所有绑定事件;

原文:http://www.cnblogs.com/wrajj/p/5056611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!