各位:
         现在用C#  在WinCE平台下写一设备的运动控制程序,要用到button控件中的mousedown mouseup事件,但现发现在WinCE平台下button控件无mousedown  mouseup事件。现在想用代码来实现mousedown mouseup事件的功能。网上查些资料,好像要用到消息队列类的知识,对此不熟,望各位能够指点指点,给点资料和代码。谢谢
语言:C#
环境:VS2008
平台:WinCE6.0

解决方案 »

  1.   

    调用API函数http://bbs.csdn.net/topics/350264632
      

  2.   

    先到下面这个地址把两个基础工具类WndProcHooker和Win32都复制到你的程序中:
    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事件了。
      

  3.   

    Windows CE?你确定你的设备有鼠标么?如果是触摸屏,怎么操作?
      

  4.   


    哈哈,又是你,看来你就是我的福星啊。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: 谢谢。  看来我要学的东西还真不少,。
      

  5.   


    我用的是工业平板电脑,带触摸屏的。我想用 MouseUp,MouseDown事件这两事件的目的1:我写的运动控制程序是用来控制设备的左右移动,也就是用一BUTTON控制,当BUTTON按下时,触发MouseU事件,设备左移(此时按钮一直按着),当BUTTON松开时,触发MouseDown事件,设备停止。2:我的工业平板电脑,有USB接口,听厂家说支持鼠标操作,但没测试过。(一般不会用鼠标的,手指方便)3:谢谢
      

  6.   

    1。可以在Form.Designer.cs中改。
       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。不需要调用,这是封装好的支持类,所有的工作内部完成,外部只需简单引用。
      

  7.   


    你好,按照你说的方法,我测试了下,还是会报错,代码如下:
    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:谢谢上面你及时回答。
      

  8.   

    this.button1.MouseDown += new MouseEventHandler(this.button1_MouseDown);
    或者也可以直接写成:
    this.button1.MouseDown += button1_MouseDown;可以添加在Form.cs中的InitializeComponent()语句下面,程序运行时,会先执行InitializeComponent,接着执行后面的语句。报什么错,button1_MouseDown方法定义了吗?
      

  9.   


    你好,上午有点事,没及时回帖。不好意思  昨天的报错,是记了定义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:你能大概说下,一般解决这种问题的思路和要用到的知道点吗?我担心下次遇到类似的问题??谢谢。
      

  10.   


    哦,对了,你还是先确认了我的代码吧,如下: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;
            }
        }下面还有两个你说的基础类,就不粘了。
      

  11.   

    1。呵呵,这个应该是我写错了。代码是照上面给的链接的Part2的部分改的,而Part2里的例子是TreeView,应该和Button的情况不一样。
    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。你指的是具体什么问题?
      

  12.   

    Quote: 引用 12 楼 jshi123 的回复:

    1。呵呵,这个应该是我写错了。代码是照上面给的链接的Part2的部分改的,而Part2里的例子是TreeView,应该和Button的情况不一样。
    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。你指的是具体什么问题?你好,
    A: 把你上面重新给的代码替换掉原先的代码中的一部分就可以用了,但我仔细测试了下,发现一个新问题,如下:
    我是这样测试的:在mousedown mouseup事件中各进行一个加计数,然后将计数值赋给textbox,按正常情况当点击一次button按钮就会触发一次mousedown mouseup事件,也就数值应同时都加1才对。但实际测试时发现:当一次点击一次button按钮时,各数值计数正常,也就是每次都会加1 但如如果连续点击两次button按钮,mousedown事件会少计数一次,而mouseup事件计数正常(会加2),当连续点击三次mousedown事件也会少计数一次,(也就是只会加2),mouseup事件正常。??
    自己分析了下:1:我用的是工业平板电脑,开始担心是它的问题,换了另一个测试也会出现相同情况。
                  2:当我用button的click事件来计数时,无论连续点击两次还是三次,计数都是正常的。
     B:上面你说的具体问题是指什么?  我当时是想问一般解决这种问题的思路,方法,还有要用到的知识点,我担心下次遇到类似的问题??
     
    C: 谢谢。
      

  13.   

    哦,不好意思,忘了,编译时我会有如下警告提示:
    警告 1 不应调用设备平台不支持的成员: System.Windows.Forms.Button.add_MouseDown 是此
    平台不支持的方法。警告 2 不应调用设备平台不支持的成员: System.Windows.Forms.Button.add_MouseUp 是此平
    台不支持的方法。会不会是上面的原因造成的????
      

  14.   

    是的,不应当出现这样的警告。
    检查你的button的类型是否是MyAppcation.MouseAwareButtonMouseAwareButton类中的下面这两句没有改过吧:
    public new event MouseEventHandler MouseUp; // new关键字表示重新定义了MouseUp事件
    public new event MouseEventHandler MouseDown;
      

  15.   


    你好,我确认了下你说的这两个地方的的代码,没有错啊。
    代码如下:(标有====================表示修改过的代码,方便你查看)
    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;
            }
        }
    下面还有两个类,没写出来。}
      

  16.   


    你先去吃饭吧。我定位了下,是这两句   
                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);
    还是会我警告,测试也会出现上述情况。
      

  17.   

    这个不知道了。那你把MouseUp,MouseDown的名字改掉,比如改成:
    public new event MouseEventHandler MyMouseUp;
    public new event MouseEventHandler MyMouseDown;
    然后把外面注册事件的地方也改掉,看看还有错吗?
      

  18.   


    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按钮时还是会出现上面我所说的少计数的情况啊,真是纠结。是不是我的代码还是没修改完整啊,还是怎么了??
      

  19.   

    整页都是代码,看得眼都花了,都没有注意到你上面的问题。是这样的,连续点击鼠标,第二次点击会变成WM_LBUTTONDBLCLK,也就是双击,WM_LBUTTONDOWN就少一次。因为.net compact framework功能比较精简,所以有时候会需要直接去调用win32系统的原生功能,会用到p/invoke技术,还需要了解一下windows编程的基础知识,包括消息,窗口,句柄什么的。
    比如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
      

  20.   

    你好,早上来测试下,发现mousedown事件还是跟原来一样还是会少计数一次(连续点击3次),而现在mouseup事件会多计数一次(连续点击2次,会计数3;连续点击3次,会计数4);这是什么情况呢?
    代码如下:
    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:谢谢
      

  21.   

    MouseDown你是用i变量计数,MouseDoubleClick和MouseUp都是用j计数,也就是双击算到了up头上,up当然就多一次。第二次down“变成”dblclick,即双击的话:down-〉up-〉dblclick->up
    OS内部当然是通过计时的方法来区分。
      

  22.   


    非常感谢。
    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;
      

  23.   

    listView的句柄就是你的listView名字.Handle。
    你可以搜索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)。
      

  24.   


    上面会报错 提示:{"无法找到 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分。(下次还有问题,我还得找你哦。哈哈)
      

  25.   


    上面会报错 提示:{"无法找到 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   或你告诉怎样在这地方追加?????
    最后弱弱的问下,你们那代码怎么贴到贴子里很是工整,还带编号,我贴上来的就不带编号,有什么工具软件吗?(我担心下次代码太多,太乱,你又看得眼花了啊)
    再次感谢
      

  26.   

    是的,一般的pinvoke还跟wince下有些小区别。
    谢谢了,不用加分了。
    贴代码用编辑框的上面的按钮,第12个: