C# 做了一个窗体,想要捕获/设置该窗体以外的 所有 鼠标事件
例如:
     1.鼠标离开该窗体后 移动鼠标     窗体上显示鼠标当前坐标(随鼠标移动而变化)
     2.鼠标离开该窗体后 点击鼠标左键 窗体出现相应事件提示
     3.窗体文本框写入坐标点击确定自动鼠标定位到该坐标 -- 已解决调用系统 API SetCursorPos()
     4.窗体下拉框选择事件点击确定自动在鼠标当前位置出发该事件
注:
    是否要用到以下 API 
         GetCursorPos(out Point lpPoint);
         SetCapture(System.IntPtr hwnd);
         ReleaseCapture();
         SetCursorPos(int X, int Y);
         mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);还望大家赐教~~~~~ 在此先谢谢诸位

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication2
    {
    /// <summary>
    /// Form1 的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 設計工具所需的變數。
    /// </summary>
    private System.ComponentModel.Container components = null;        //鼠標事件
            private delegate void MouseEvent(string pt);
            private event MouseEvent OnMouseClick;
    public Form1()
    {
    //
    // Windows Form 設計工具支援的必要項
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
    //
    } /// <summary>
    /// 清除任何使用中的資源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form 設計工具產生的程式碼
    /// <summary>
    /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
                this.ClientSize = new System.Drawing.Size(488, 461);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);            
                this.OnMouseClick+=new MouseEvent(Form1_OnMouseClick);
            }
    #endregion /// <summary>
    /// 應用程式的主進入點。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
                    private void Form1_Load(object sender, System.EventArgs e)
            {
                                   }
            protected override void WndProc(ref Message m)
            {
                switch(m.Msg)
                {
                    case 0x0201:                    
                        OnMouseClick(m.LParam.ToString());
                        break;
                }       
                base.WndProc (ref m);                 
            }        private void Form1_OnMouseClick(string pt)
            {
                //MessageBox.Show(pt);
            }
        }
    }這段代碼你試試。事件中的m.LParam.ToString()你可以改變類型比如傳出一個Point
    當然上面的delegate也要改咯。
    註釋部分打開就可以看到效果.