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

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

解决方法很简单:

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

 

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

这个稍微麻烦,这里贴上stackoverflow粘过来的答案,经测试,不可使用,但是这个思路应该是可行的。最后我用winform自带的PictureBox显示视频。

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]);
        }
    }
}

 

相关文章: