用VS2008開發C#語言wince程序,發現程序裡右鍵捕獲不到,采集器上點也沒反應,上網查好像有個c++版本的,看不懂啊,下面我給出C#實現右鍵效果的解決方案,請各位多多優化。 首先控件ContextMenu 和 Timer 設置contextMenu1 在dataGrid裡實現右鍵點擊,事件dataGrid1_MouseDown 按下鼠標按鈕事件。 上代碼 [csharp] public int _x; public int _y; private void dataGrid1_MouseDown(object sender, MouseEventArgs e) { System.Drawing.Point Pt = new Point(e.X, e.Y); DataGrid.HitTestInfo Ht = dataGrid1.HitTest(Pt.X, Pt.Y); //this.dataGrid1.CurrentCell = new DataGridCell(Ht.Row, Ht.Column); //this.dataGrid1.Select(Ht.Row); if (Ht.Row >= 0 && Ht.Column >= 0) { _x = e.X + 2;//記錄鼠標當前坐標 _y = e.Y + 1;//同上(priorPoint前面已經聲明) SHRGINFO shr = new SHRGINFO(); shr.cbSize = Marshal.SizeOf(typeof(SHRGINFO)); shr.dwFlags = SHRGFLags.SHRG_RETURNCMD; shr.ptDownX = MousePosition.X - 2; shr.ptDownY = MousePosition.Y - 8; shr.hwndClient = GetActiveWindow(); int ret = SHRecognizeGesture(ref shr); if (ret == 1000) { timer1.Enabled = true;//開啟計時器 } } } internal struct SHRGINFO { public int cbSize; public IntPtr hwndClient; public int ptDownX; public int ptDownY; public SHRGFLags dwFlags; } [Flags] internal enum SHRGFLags { SHRG_RETURNCMD = 0x00000001, SHRG_NOTIFYPARENT = 0x00000002, SHRG_LONGDELAY = 0x00000008, SHRG_NOANIMATION = 0x00000010, } [DllImport("aygshell")] extern private static int SHRecognizeGesture(ref SHRGINFO shr); [DllImport("coredll.dll", SetLastError = true)] public static extern IntPtr GetActiveWindow(); DataGrid.HitTestInfo 我也是第一次用,把坐標傳進去就能知道當前左邊是哪列哪行,有沒有數據,下面做出判斷有數據了才顯示。 ret == 1000圓圈正好一圈 [DllImport("aygshell")] extern private static int SHRecognizeGesture(ref SHRGINFO shr); [DllImport("coredll.dll", SetLastError = true)] public static extern IntPtr GetActiveWindow(); 啟用采集器DLL接口程序。 MousePosition.X - 2 當前鼠標-2這個是控制圓圈的,不在鼠標中建,我給調了調, _X _Y 會在時間裡用到。timer1的事件,如果在此彈出右鍵菜單因為資源沒有被釋放,所以會報錯的,只能啟動下一個事件,我寫在了timer1_Tick裡,並使用timer1.Enabled = true啟用事件 [csharp] private void timer1_Tick(object sender, EventArgs e) { string pids = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 0].ToString(); if (pids != "") { contextMenu1.Show(dataGrid1, new Point(_x, _y));//彈出上下文菜單 timer1.Enabled = false; } } 要寫上contextMenu1綁定的控件dataGrid1 this.dataGrid1[this.dataGrid1.CurrentRowIndex, 0].ToString();可獲得當前選中行某列。