研究过窗体设计器的都知道,鼠标事件几乎都被屏蔽掉,都被用去做拖动和选择的一些功能。
重新添加鼠标事件的方式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel.Design;
using System.Windows.Forms;
namespace DesignSurfaceExt
{
public enum MyMouseButtons
{
Leave = 0,//鼠标移出
Move,//鼠标移入
LeftDown,//鼠标左键down
LeftUp, //鼠标左键up
DoubleClick,//鼠标双击
RightDown,//鼠标右键down
RightUp,//鼠标右键up
Wheel//鼠标滚轮动
}
public class EventMouse : System.Windows.Forms.Design.ControlDesigner
{
private MyMouseButtons MouseState = 0;
private ISelectionService SelectionService
{
get
{
return this.GetService(typeof(ISelectionService)) as ISelectionService;
}
}
//鼠标动作常见参数:
//鼠标移动:512
//鼠标左键:
//down:513 up:514
//double click:515
//鼠标右键:
//down:516 up:517
//鼠标滚轮:522
/// <summary>
/// 获取鼠标消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
Control t_Control = System.Windows.Forms.Form.FromHandle(m.HWnd) as Control;
if (m.Msg == 515)//鼠标双击
{
MouseState = MyMouseButtons.DoubleClick;
}
//else if (m.Msg == 513) //鼠标左键down
//{
// MouseState = MyMouseButtons.LeftDown;
// //System.Console.WriteLine("鼠标左键down");
//}
//else if (m.Msg == 514) //鼠标左键up
//{
// MouseState = MyMouseButtons.LeftUp;
// //System.Console.WriteLine("鼠标左键up");
//}
//else if (m.Msg == 516)//鼠标右键down
//{
// MouseState = MyMouseButtons.RightDown;
// //System.Console.WriteLine("鼠标右键down");
//}
//else if (m.Msg == 517)//鼠标右键up
//{
// MouseState = MyMouseButtons.RightUp;
// //System.Console.WriteLine("鼠标右键up");
//}
//else if (m.Msg == 522)// 鼠标滚轮
//{
// MouseState = MyMouseButtons.Wheel;
// //System.Console.WriteLine("鼠标滚轮");
//}
//else if (m.Msg == 512)// 鼠标移入
//{
// MouseState = MyMouseButtons.Move;
// //System.Console.WriteLine("鼠标移入");
//}
else
{
MouseState = MyMouseButtons.Leave;
//System.Console.WriteLine("鼠标移出");
}
switch (MouseState)
{
case MyMouseButtons.DoubleClick:
mouseDoubleClick(t_Control.GetType());
break;
default: break;
}
base.WndProc(ref m);
}
private void mouseDoubleClick(Type type)
{
if (type == typeof(UserControlTest))
{
//弹出消息框
System.Console.WriteLine("你好");
}
}
}
}
新建一个类,继承ControlDesigner,然后重写他的鼠标消息函数,再到你需要有这些鼠标事件的控件上加上一句,如下:
[Designer(typeof(EventMouse))]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DesignSurfaceExt
{
//[Designer(typeof(MyDesigner))]//显示右上角三角功能
//[Designer(typeof(EventControlDesigner))]
[Designer(typeof(EventMouse))]
public partial class UserControlTest : UserControl
{
public UserControlTest()
{
InitializeComponent();
}
}
}
这样就可以啦
版权声明:本文为qq996980215原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。