用VS2008开发C#语言wince程序,发现程序里右键捕获不到,采集器上点也没反应,上网查好像有个c++版本的,看不懂啊,下面我给出C#实现右键效果的解决方案,请各位多多优化。
首先控件ContextMenu 和 Timer
设置contextMenu1
在dataGrid里实现右键点击,事件dataGrid1_MouseDown 按下鼠标按钮事件。
上代码
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启用事件
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();可获得当前选中行某列。