各位:
现在用C# 在WinCE平台下写一设备的运动控制程序,要用到button控件中的mousedown mouseup事件,但现发现在WinCE平台下button控件无mousedown mouseup事件。现在想用代码来实现mousedown mouseup事件的功能。网上查些资料,好像要用到消息队列类的知识,对此不熟,望各位能够指点指点,给点资料和代码。谢谢
语言:C#
环境:VS2008
平台:WinCE6.0
现在用C# 在WinCE平台下写一设备的运动控制程序,要用到button控件中的mousedown mouseup事件,但现发现在WinCE平台下button控件无mousedown mouseup事件。现在想用代码来实现mousedown mouseup事件的功能。网上查些资料,好像要用到消息队列类的知识,对此不熟,望各位能够指点指点,给点资料和代码。谢谢
语言:C#
环境:VS2008
平台:WinCE6.0
解决方案 »
- 怎么引用当前文件下。JS文件中的函数
- 谁有API的查看器可以看api的详细解释
- 这段代码有人可以翻译成VB.NET吗?
- vs 2005 serialport 问题
- 求助:关于SelectCommand
- Chart图下载成EXECL~~~~~~~~好看的很
- 怎样得到RichTextBox的右键选中了那个字符,最好能得到如SelectionStart相似的结果。
- vc#将*.doc,*.xls存储到sql中的image字段
- c#里面用来显示隔几秒种换一下的图片的控件应该用哪个呢?
- C++的dll函数在c#中调用获取不到值,请大家帮忙看下,谢谢!
- 字符串转换求助
- C# 如何遍历 26 个字母的 n- m 长度的所有组合??
http://blogs.msdn.com/b/netcfteam/archive/2005/05/20/420551.aspx
然后再加上下面的类: public partial class MouseAwareButton : Button
{
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
Control prevParent = null; public new event MouseEventHandler MouseUp;
public new event MouseEventHandler MouseDown; protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(prevParent, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(prevParent, WM_LBUTTONUP);
}
prevParent = Parent; if (Parent != null)
{
WndProcHooker.HookWndProc(Parent, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(Parent, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
}
base.OnParentChanged(e);
} int WM_LBUTTONDOWN_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDown != null)
MouseDown(this, args);
return 0;
} int WM_LBUTTONUP_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseUp != null)
MouseUp(this, args);
return 0;
}
}然后把你的程序中的Button替换成MouseAwareButton,就可以注册它的MouseUp,MouseDown事件了。
哈哈,又是你,看来你就是我的福星啊。1:你说的把程序中的Button替换成MouseAwareButton,指的是什么地方的的Button,是Form.Designer.cs中的Button吗?? 我改后报错啊,还是改的地方不对???
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.MouseAwareButton();
this.button2 = new System.Windows.Forms.Button();
}2:你说的注册它的MouseUp,MouseDown事件了 ? 是不是就是在Button控件的属性里面的MouseUp MouseDown 事件吗?? 3: 你给的这两个类中有好多方法,我写程序是不用调用这其中有的方法吗??4: 谢谢。 看来我要学的东西还真不少,。
我用的是工业平板电脑,带触摸屏的。我想用 MouseUp,MouseDown事件这两事件的目的1:我写的运动控制程序是用来控制设备的左右移动,也就是用一BUTTON控制,当BUTTON按下时,触发MouseU事件,设备左移(此时按钮一直按着),当BUTTON松开时,触发MouseDown事件,设备停止。2:我的工业平板电脑,有USB接口,听厂家说支持鼠标操作,但没测试过。(一般不会用鼠标的,手指方便)3:谢谢
this.button1 = new System.Windows.Forms.MouseAwareButton();
this.button2 = new System.Windows.Forms.Button();
这个namespace是错的,要看你把MouseAwareButton这个类放在什么namespace下面,比如namespace是 MyApp,这里就改成new MyApp.MouseAwareButton(); 如果外面没有套namespace(直接写在了最外层),就改成 new global::MouseAwareButton();
另外声明定义的地方也要改过来。2。用designer注册事件应该也可以,只要加上MouseAwareButton类后先编译一次正确通过。不过最好直接在后台的Form.cs文件中改:在InitializeComponent()语句下面加上:
this.button1.MouseDown += button1_MouseDown;
你应该尽量学习脱开设计器来工作。3。不需要调用,这是封装好的支持类,所有的工作内部完成,外部只需简单引用。
你好,按照你说的方法,我测试了下,还是会报错,代码如下:
1: 这是在Form1.Designer.cs中修改的代码:
private void InitializeComponent()
{
// this.button1.MouseDown += button1_MouseDown;===========(增加的代码)
this.button1 = new MyAppcation.MouseAwareButton();=========(增加的代码)
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 119);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(133, 44);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_KeyDown); // this.button1.MouseDown += new MyAppcation.MouseAwareButton(this.button1_MouseDown);
// this.button1.MouseDown += new MyAppcation.MouseAwareButton().MouseEventHandler(this.button1_MouseDown);
// this.button1.MouseDown += new MyAppcation.MouseAwareButton().MouseDown();
以上三行代码是我修改的,测试时都是会报错;(看来委托与事件我还是没学好啊)
//
// button2
//
this.button2.Location = new System.Drawing.Point(344, 214);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(133, 44);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(638, 455);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); } #endregion private MyAppcation.MouseAwareButton button1; ==========(这是定义button1)
private System.Windows.Forms.Button button2;
}2:在上次你的问答中,“用designer注册事件应该也可以,只要加上MouseAwareButton类后先编译一次正确通过。不过最好直接在后台的Form.cs文件中改:在加上: this.button1.MouseDown +=button1_MouseDown;”
说可以在Form.cs文件中改,在InitializeComponent()语句下面加上: this.button1.MouseDown += button1_MouseDown;”
是不是打错字了,程序初始化时最终还是进入了Form1.Designer.cs中的InitializeComponent()方法中,也就应该加在Form1.Designer.cs中的InitializeComponent()方法中吧。试了下,加在InitializeComponent()语句下面,会报错,不知你这样说的目的3:谢谢上面你及时回答。
或者也可以直接写成:
this.button1.MouseDown += button1_MouseDown;可以添加在Form.cs中的InitializeComponent()语句下面,程序运行时,会先执行InitializeComponent,接着执行后面的语句。报什么错,button1_MouseDown方法定义了吗?
你好,上午有点事,没及时回帖。不好意思 昨天的报错,是记了定义button1_MouseDown()方法。现重写代码如下:
namespace MotionControlProgram_焊锡机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
// this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
// this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);
} private void MouseAwareButton_MouseDown(object sender, EventArgs e)
{
this.textBox1.Text = "down";
} private void MouseAwareButton_MouseUp(object sender, EventArgs e)
{
this.textBox1.Text = "up";
}
//private void button1_MouseDown(object sender, EventArgs e)
//{
// this.textBox1.Text = "down"; //}
//private void button1_MouseUp(object sender, EventArgs e)
//{ // this.textBox1.Text = "up"; //}现测试时发现两个问题:
1:当我点击button按钮时,不会触发button的mousedown mouseup事件,而当点击button按钮外的区域,反而会触发button的mousedown mouseup事件;(不知是不是点击事件的位置坐标区域设反了???)
2:代码中
this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
// this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
// this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);这两组代码都会出现在上面我提出的问题,不知为何??(第一组代码是我开始测试时,自己猜测着写的)两都之间的差别在那,实际应用该用那组代码?3:你能大概说下,一般解决这种问题的思路和要用到的知道点吗?我担心下次遇到类似的问题??谢谢。
哦,对了,你还是先确认了我的代码吧,如下:1: 这是在Form.designer.cs中代码
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.button1 = new MotionControlProgram_焊锡机.MouseAwareButton();
this.textBox1 = new System.Windows.Forms.TextBox();
private MotionControlProgram_焊锡机.MouseAwareButton button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
}
2: 这是在Form1.cs 中代码:
namespace MotionControlProgram_焊锡机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
// this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
// this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);
} private void MouseAwareButton_MouseDown(object sender, EventArgs e)
{
this.textBox1.Text = "down";
} private void MouseAwareButton_MouseUp(object sender, EventArgs e)
{
this.textBox1.Text = "up";
}
//private void button1_MouseDown(object sender, EventArgs e)
//{
// this.textBox1.Text = "down"; //}
//private void button1_MouseUp(object sender, EventArgs e)
//{ // this.textBox1.Text = "up"; //} }
public partial class MouseAwareButton : Button
{
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
Control prevParent = null; public new event MouseEventHandler MouseUp;
public new event MouseEventHandler MouseDown; protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(prevParent, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(prevParent, WM_LBUTTONUP);
}
prevParent = Parent; if (Parent != null)
{
WndProcHooker.HookWndProc(Parent, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(Parent, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
}
base.OnParentChanged(e);
} int WM_LBUTTONDOWN_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDown != null)
MouseDown(this, args);
return 0;
} int WM_LBUTTONUP_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseUp != null)
MouseUp(this, args);
return 0;
}
}下面还有两个你说的基础类,就不粘了。
Subclassing controls in .NETCF 2.0, part 1
Subclassing controls in .NETCF 2.0, part 2
改成下面这样再试下: protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(this, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(this, WM_LBUTTONUP);
}
prevParent = Parent;
if (Parent != null)
{
WndProcHooker.HookWndProc(this, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(this, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
}
base.OnParentChanged(e);
}或者其实根本不用prevParent也可以。2。我没看出有什么区别,就是方法名不同,用button1_MouseDown好点,因为前半部分是控件名字是约定俗成,当然最好把button1改成有意义的名字。3。你指的是具体什么问题?
警告 1 不应调用设备平台不支持的成员: System.Windows.Forms.Button.add_MouseDown 是此
平台不支持的方法。警告 2 不应调用设备平台不支持的成员: System.Windows.Forms.Button.add_MouseUp 是此平
台不支持的方法。会不会是上面的原因造成的????
检查你的button的类型是否是MyAppcation.MouseAwareButtonMouseAwareButton类中的下面这两句没有改过吧:
public new event MouseEventHandler MouseUp; // new关键字表示重新定义了MouseUp事件
public new event MouseEventHandler MouseDown;
你好,我确认了下你说的这两个地方的的代码,没有错啊。
代码如下:(标有====================表示修改过的代码,方便你查看)
1: 在Form.designer.cs 中代码
namespace MotionControlProgram_焊锡机
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new MotionControlProgram_焊锡机.MouseAwareButton();=================
this.button3 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(385, 198);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(108, 38);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(170, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(159, 23);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(170, 213);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(159, 23);
this.textBox2.TabIndex = 3;
//
// button1
//
this.button1.Location = new System.Drawing.Point(385, 99);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 44);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// button3
//
this.button3.Location = new System.Drawing.Point(385, 317);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(108, 38);
this.button3.TabIndex = 4;
this.button3.Text = "button3";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(170, 317);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(159, 23);
this.textBox3.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(638, 455);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); } #endregion private MotionControlProgram_焊锡机.MouseAwareButton button1; ===================
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox3;
}
}2:Form1.cs中代码:
namespace MotionControlProgram_焊锡机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
// this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
// this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);
}
}
public partial class MouseAwareButton : Button
{
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
Control prevParent = null; public new event MouseEventHandler MouseUp;===(这两行是你上面说的两行代码吧,没修改过)
public new event MouseEventHandler MouseDown;======================
protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(this, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(this, WM_LBUTTONUP);
}
prevParent = Parent;
if (Parent != null)
{
WndProcHooker.HookWndProc(this, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(this, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
}
base.OnParentChanged(e);
} int WM_LBUTTONDOWN_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDown != null)
MouseDown(this, args);
return 0;
} int WM_LBUTTONUP_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseUp != null)
MouseUp(this, args);
return 0;
}
}
下面还有两个类,没写出来。}
你先去吃饭吧。我定位了下,是这两句
this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
但看着好像没有错啊?对了,我对上面两句换成这两句:
this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);
还是会我警告,测试也会出现上述情况。
public new event MouseEventHandler MyMouseUp;
public new event MouseEventHandler MyMouseDown;
然后把外面注册事件的地方也改掉,看看还有错吗?
1: 你先确认下我的代码修改的我错没(我担心我错)??如下:Form1.cs中代码,(这次Form1.designer.cs中代码没修改。)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); // this.button1.MouseDown += new MouseEventHandler(this.MouseAwareButton_MouseDown );
// this.button1.MouseUp += new MouseEventHandler(this.MouseAwareButton_MouseUp);
//this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
//this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp); this.button1.MyMouseUp += new MouseEventHandler(this.button1_MyMouseUp);============
this.button1.MyMouseDown += new MouseEventHandler(this.button1_MyMouseDown);========
} int i = 10;
int j = 10;
int k = 0; private void button1_MyMouseDown(object sender, EventArgs e)
{
this.textBox1.Text = i.ToString();
i--;
} private void button1_MyMouseUp(object sender, EventArgs e)
{
this.textBox2.Text = j.ToString();
j--;
} } public partial class MouseAwareButton : Button
{
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
Control prevParent = null; //public new event MouseEventHandler MouseUp;
//public new event MouseEventHandler MouseDown; public new event MouseEventHandler MyMouseUp;=================增加代码
public new event MouseEventHandler MyMouseDown; //public new event MouseEventHandler MyMouseUp;
//public new event MouseEventHandler MyMouseDown;
protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(this, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(this, WM_LBUTTONUP);
}
prevParent = Parent;
if (Parent != null)
{
WndProcHooker.HookWndProc(this, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(this, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
}
base.OnParentChanged(e);
} int WM_LBUTTONDOWN_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MyMouseDown != null) =================这地方原先是mouseup 要改为MyMouseup
MyMouseDown(this, args);
return 0;
} int WM_LBUTTONUP_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MyMouseUp != null) =================这地方原先是mouseup 要改为MyMouseup
MyMouseUp(this, args);
return 0;
}
}A: public new event MouseEventHandler MyMouseUp;
public new event MouseEventHandler MyMouseDown; 这两句代码中的new关键字要去除,不然会报警告的(
警告 1 成员“MotionControlProgram_焊锡机.MouseAwareButton.MyMouseUp”不会隐藏继承的
成员。不需要关键字 new。)B: 按照你说的修改后,原先报的警告,现不会出现,但测试button按钮时还是会出现上面我所说的少计数的情况啊,真是纠结。是不是我的代码还是没修改完整啊,还是怎么了??
比如WM_LBUTTONDBLCLK表示一个鼠标双击消息,你可以搜索pinvoke WM_LBUTTONDBLCLK查到它对应的消息值:
const int WM_LBUTTONDBLCLK = 0x0203;
如果你想截取这个消息的话,可以模仿WM_LBUTTONDOWN,加上:
WndProcHooker.HookWndProc(this, WM_LBUTTONDBLCLK_Handler, WM_LBUTTONDBLCLK);
底层的支持方法会去“钩住”这个消息,出现这个消息时调用WM_LBUTTONDBLCLK_Handler方法。
然后再模拟WM_LBUTTONDOWN_Handler,写个WM_LBUTTONDBLCLK_Handler
int WM_LBUTTONDBLCLK_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDoubleClick != null)
MouseDoubleClick(this, args);
return 0;
}
最后再加上 MouseDoubleClick的定义:
public new event MouseEventHandler MouseDoubleClick;另外你可以看戏OpenNetCF,这是个现成的类库框架,功能比.net cf强一些,有些功能,比如鼠标事件,就可以直接用了。
http://www.opennetcf.com/Products/SmartDeviceFramework.aspx
代码如下:
namespace MotionControlProgram_焊锡机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new MouseEventHandler(this.button1_MouseUp);
this.button1.MouseDoubleClick += new MouseEventHandler(this.button1_MouseDoubleClick); } int i = 10;
int j = 10;
int k = 0; private void button1_MouseDown(object sender, EventArgs e)
{
this.textBox1.Text = i.ToString();
i--;
} private void button1_MouseUp(object sender, EventArgs e)
{
this.textBox2.Text = j.ToString();
j--;
}
private void button1_MouseDoubleClick(object sender, EventArgs e)
{
this.textBox2.Text = j.ToString();
j--;
}
} public partial class MouseAwareButton : Button
{
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
private const int WM_LBUTTONDBLCLK = 0x0203;
Control prevParent = null;
public new event MouseEventHandler MouseUp;
public new event MouseEventHandler MouseDown;
public new event MouseEventHandler MouseDoubleClick;
protected override void OnParentChanged(EventArgs e)
{
if (prevParent != null)
{
WndProcHooker.UnhookWndProc(this, WM_LBUTTONDOWN);
WndProcHooker.UnhookWndProc(this, WM_LBUTTONUP);
WndProcHooker.UnhookWndProc(this, WM_LBUTTONDBLCLK); }
prevParent = Parent;
if (Parent != null)
{
WndProcHooker.HookWndProc(this, WM_LBUTTONDOWN_Handler, WM_LBUTTONDOWN);
WndProcHooker.HookWndProc(this, WM_LBUTTONUP_Handler, WM_LBUTTONUP);
WndProcHooker.HookWndProc(this, WM_LBUTTONDBLCLK_Handler, WM_LBUTTONDBLCLK);
}
base.OnParentChanged(e);
} int WM_LBUTTONDOWN_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDown != null)
MouseDown(this, args);
return 0;
} int WM_LBUTTONUP_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseUp != null)
MouseUp(this, args);
return 0;
} int WM_LBUTTONDBLCLK_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
int x = lParam & 0xffff, y = lParam >> 16;
MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 0, x, y, 0);
if (MouseDoubleClick != null)
MouseDoubleClick(this, args);
return 0;
}
}早上好。
A: 你先确认下我的代码有错误没?B: 从你昨天的回帖中,我是否可以这样理解:当我连续点击两次button时,会先触发一个mousedown事件,然后再触发一个douleclick事件是吧?如果是这样的话,VS2008 C#内部是怎样区分double click事件,click事件,(不会是通过计时吧,在一定时间范围内,如果再次收到单击事件,就认为是double click事件。)C:如果如B中所说,那我的这种情况,就没法解决了哦,即使我截住double click事件,对我来说也没用啊。D:谢谢
OS内部当然是通过计时的方法来区分。
非常感谢。
1: 关于上面我提到mouseup多计数的问题,是我的严重失误,so so sorry;2: 关于mousedown事件少计数一次,也只能这样了。(少一次没事,只是要多按一次,mousedown控制设备启动,mouseup控制设备停止)3:我还有一个问题要劳烦你一下了,问题如下:
程序中用到一个Listview ,用来显示位置坐标点,但修改其属性view为details Listview 中无法显示栅格(就是横坚栅格线,显示出来界面美观)因为我用的是WINCE系统,可能跟这个有关吧,我也在这上面发有贴,有人告诉我用这个代码可以显示出来
SendMessage(listView的句柄, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)LVS_EX_GRIDLINES);
你能告诉我详细点吗,比如要添加什么类啊,定义 申明什么的,好像又要用到消息类的知识,又是底层类的东西,这是原贴http://bbs.csdn.net/topics/3905852474:THANKS;
你可以搜索c# pinvoke SendMessage找到SendMessage的定义方法,应该是:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
然后搜索LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_GRIDLINES这两个常量的值,加上定义后就能用了(可能需要把int转成uint或IntPtr)。
上面会报错 提示:{"无法找到 PInvoke DLL“user32.dll”。"}
查资料网上有网友说WINCE系统中user32.dll要改为coredll.dll.我测试了下,代码要修改下,如下: [DllImport("coredll.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
还是非常感谢。追加50分。(下次还有问题,我还得找你哦。哈哈)
上面会报错 提示:{"无法找到 PInvoke DLL“user32.dll”。"}
查资料网上有网友说WINCE系统中user32.dll要改为coredll.dll.我测试了下,代码要修改下,如下: [DllImport("coredll.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
还是非常感谢。追加50分。(下次还有问题,我还得找你哦。哈哈)
怎么不能追加分啊,还是我没找对地方啊??,要不你到这个贴子的链接处,随变答下,我把分数追加给你,http://bbs.csdn.net/topics/390585247 或你告诉怎样在这地方追加?????
最后弱弱的问下,你们那代码怎么贴到贴子里很是工整,还带编号,我贴上来的就不带编号,有什么工具软件吗?(我担心下次代码太多,太乱,你又看得眼花了啊)
再次感谢
谢谢了,不用加分了。
贴代码用编辑框的上面的按钮,第12个: