/// <summary> /// 鼠标拖动事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseMove(object sender, HalconDotNet.HMouseEventArgs e) { //响应鼠标左键的动作,右键用于右键快捷菜单,而不响应右键 if (e.Button == System.Windows.Forms.MouseButtons.Left) { double deltaX, deltaY, posX, posY; double zoomZone; //由鼠标按下到抬起(即前次的状态是按下) if (!_mousePressed) { return; } //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI,且有活动ROI if (_ROICtrller != null && _ROIPaintMode == PAINT_MODE_INCLUDE_ROI && _ROICtrller.GetActiveROIIdx() != -1) { //执行ROI管理器对于鼠标移动的处理,并返回活动ROI的索引(有ROI选中时,无法操作[移动]图像) _ROICtrller.MouseMoveAction(e.Y, e.X); } else if (_viewMode == VIEW_MODE_MOVE) { //显示控件的视图状态为MODE_VIEW_MOVE(移动图像) deltaY = (e.Y - _startRow); deltaX = (e.X - _startCol); if ((int)deltaY != 0 || (int)deltaX != 0) { //位置发生变化,移动图像 MoveImage(deltaY, deltaX); } } else if (_viewMode == HWndCtrller.VIEW_MODE_MAGNIFY) { //显示控件的视图状态为MODE_VIEW_MAGNIFY(在鼠标当前位置创建放大窗口) if (_zoomWindow != null) { HalconDotNet.HSystem.SetSystem("flush_graphic", "false"); _zoomWindow.ClearWindow(); posY = ((e.Y - _imgRow1) / (_imgRow2 - _imgRow1)) * _hWndCtrl.Height; posX = ((e.X - _imgCol1) / (_imgCol2 - _imgCol1)) * _hWndCtrl.Width; zoomZone = (_zoomWndSize / 2) * ZoomWndFactor * ZoomAddOn; _zoomWindow.SetWindowExtents((int)posY - (_zoomWndSize / 2), (int)posX - (_zoomWndSize / 2), _zoomWndSize, _zoomWndSize); _zoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); Repaint(_zoomWindow); HalconDotNet.HSystem.SetSystem("flush_graphic", "true"); _zoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } } } }
/// <summary> /// 鼠标抬起事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseUp(object sender, HalconDotNet.HMouseEventArgs e) { //响应鼠标左键的动作,右键用于右键快捷菜单,而不响应右键 if (e.Button == System.Windows.Forms.MouseButtons.Left) { this._mousePressed = false; //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI,且有活动ROI if (_ROICtrller != null && _ROIPaintMode == HWndCtrller.PAINT_MODE_INCLUDE_ROI && _ROICtrller.GetActiveROIIdx() != -1) { //若活动ROI索引非-1,则表示更新ROI(或创建或选择) _ROICtrller.IconicUpdatedDel(ROIManager.EVENT_UPDATE_ROI); } else if (_viewMode == HWndCtrller.VIEW_MODE_MAGNIFY) { if (_zoomWindow != null) { _zoomWindow.Dispose(); } } else if (_viewMode == HWndCtrller.VIEW_MODE_MOVE) { //主要时测试鼠标抬起和移动谁先触发 } } }
/// <summary> /// 鼠标按下事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseDown(object sender, HalconDotNet.HMouseEventArgs e) { //响应鼠标左键的动作,右键用于右键快捷菜单,而不响应右键 if (e.Button == System.Windows.Forms.MouseButtons.Left) { this._mousePressed = true; int activeROIIdx = -1; double scale = 0; //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI if (_ROICtrller != null && _ROIPaintMode == HWndCtrller.PAINT_MODE_INCLUDE_ROI) { //执行ROI管理器对于鼠标按下的处理,并返回活动ROI的索引(有ROI选中时,无法操作[移动]图像) activeROIIdx = _ROICtrller.MouseDownAction(e.Y, e.X); } //活动ROI索引为-1,即没有选中ROI if (activeROIIdx == -1) { switch (_viewMode) { case HWndCtrller.VIEW_MODE_ZOOM: { //不需要单击一次缩放一次,改为通过滚轮来缩放 /* * if (e.Button == System.Windows.Forms.MouseButtons.Left) * scale = 0.8; * else if (e.Button == System.Windows.Forms.MouseButtons.Right) * scale = 1 / 0.8; * * ZoomImage(e.Y, e.X, scale); */ } break; case HWndCtrller.VIEW_MODE_NONE: break; case HWndCtrller.VIEW_MODE_MOVE: //与鼠标移动事件互助,此时记录鼠标的点击位置,在鼠标移动事件时,平移图像 _startRow = e.Y; _startCol = e.X; break; case HWndCtrller.VIEW_MODE_MAGNIFY: //在鼠标点击位置,创建显示窗口,局部放大图像[放大部分待定] CreateZoomWindow(e.Y, e.X); break; } } } }
/// <summary> /// 鼠标按下事件回调 /// [切换多视图与单视图] /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HWndcMain_HMouseDown(object sender, HalconDotNet.HMouseEventArgs e) { if (ConvertHWndCtrlMouseSingleToDoubleClick(sender, e)) { //左键双击,则切换窗口 if (_ownerModule != null) { _ownerModule.ShowSingleViewer(_camerID); } } }
/// <summary> /// 鼠标拖动事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseMove(object sender, HalconDotNet.HMouseEventArgs e) { double deltaX, deltaY, posX, posY; double zoomZone; //由鼠标按下到抬起(即前次的状态是按下) if (!this._mousePressed) { return; } //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI,且有活动ROI if (this._ROICtrller != null && this._ROIDispMode == MODE_INCLUDE_ROI && this._ROICtrller.ActiveROIIndex != -1) { //执行ROI管理器对于鼠标移动的处理,并返回活动ROI的索引(有ROI选中时,无法操作[移动]图像) this._ROICtrller.MouseMoveAction(e.Y, e.X); } else if (this._viewMode == MODE_VIEW_MOVE) { //显示控件的视图状态为MODE_VIEW_MOVE(移动图像) deltaY = (e.Y - this._startRow); deltaX = (e.X - this._startCol); if ((int)deltaY != 0 || (int)deltaX != 0) { //位置发生变化,移动图像 this.MoveImage(deltaY, deltaX); } } else if (this._viewMode == MODE_VIEW_MAGNIFY) { //显示控件的视图状态为MODE_VIEW_MAGNIFY(在鼠标当前位置创建放大窗口) HalconDotNet.HSystem.SetSystem("flush_graphic", "false"); this._zoomWindow.ClearWindow(); posY = ((e.Y - this._imgRow1) / (this._imgRow2 - this._imgRow1)) * this._hWindowControl.Height; posX = ((e.X - this._imgCol1) / (this._imgCol2 - this._imgCol1)) * this._hWindowControl.Width; zoomZone = (this._zoomWndSize / 2) * this._zoomWndFactor * this._zoomAddOn; this._zoomWindow.SetWindowExtents((int)posY - (this._zoomWndSize / 2), (int)posX - (this._zoomWndSize / 2), this._zoomWndSize, this._zoomWndSize); this._zoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); Repaint(this._zoomWindow); HalconDotNet.HSystem.SetSystem("flush_graphic", "true"); this._zoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
/// <summary> /// 鼠标按下事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseDown(object sender, HalconDotNet.HMouseEventArgs e) { this._mousePressed = true; int activeROIIdx = -1; double scale = 0; //while (e.Button != System.Windows.Forms.MouseButtons.Right) //{ //} //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI if (this._ROICtrller != null && this._ROIDispMode == MODE_INCLUDE_ROI) { //执行ROI管理器对于鼠标按下的处理,并返回活动ROI的索引(有ROI选中时,无法操作[移动]图像) activeROIIdx = this._ROICtrller.MouseDownAction(this._hWindowControl.HalconWindow, e.Y, e.X); } //活动ROI索引为-1,即没有选中ROI if (activeROIIdx == -1) { switch (_viewMode) { case MODE_VIEW_ZOOM: //{ // if (e.Button == System.Windows.Forms.MouseButtons.Left) // scale = 0.8; // else if (e.Button == System.Windows.Forms.MouseButtons.Right) // scale = 1 / 0.8; // this.ZoomImage(e.Y, e.X, scale); //} break; case MODE_VIEW_NONE: break; case MODE_VIEW_MOVE: //与鼠标移动事件互助,此时记录鼠标的点击位置,在鼠标移动事件时,平移图像 this._startRow = e.Y; this._startCol = e.X; break; case MODE_VIEW_MAGNIFY: //在鼠标点击位置,创建显示窗口,局部放大图像[放大部分待定] CreateZoomWindow(e.Y, e.X); break; } } }
/// <summary> /// 鼠标滚轮事件的处理函数 /// [满足条件下缩放图像] /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseWheel(object sender, HalconDotNet.HMouseEventArgs e) { int activeROIIdx = -1; double scale = 0; //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI if (this._ROICtrller != null && this._ROIDispMode == MODE_INCLUDE_ROI) { //执行ROI管理器对于鼠标按下的处理,并返回活动ROI的索引(有ROI选中时,无法操作[缩放]图像) activeROIIdx = this._ROICtrller.MouseDownAction(this._hWindowControl.HalconWindow, e.Y, e.X); } //活动ROI索引为-1,即没有选中ROI,则可以响应滚轮事件[在指定鼠标位置缩放图像] if (activeROIIdx == -1) { switch (_viewMode) { case MODE_VIEW_ZOOM: { //设置固定的缩放比例 if (e.Delta > 0) { scale = 0.8; } else { scale = 1 / 0.8; } orgScale *= scale; if (orgScale > 10 || orgScale < 0.1) { return; } this.ZoomImage(e.Y, e.X, scale); } break; case MODE_VIEW_NONE: break; case MODE_VIEW_MOVE: break; case MODE_VIEW_MAGNIFY: break; } } }
private bool ConvertHWndCtrlMouseSingleToDoubleClick(object sender, HalconDotNet.HMouseEventArgs e) { HalconDotNet.HWindowControl hwdctrl = sender as HalconDotNet.HWindowControl; if (hwdctrl != null) { if (e.Button == MouseButtons.Left) { if (_isSingleClick) //左键单击,已记录单击一次 { if (_hWndTag == hwdctrl.Tag.ToString()) //再次单击时,同一个窗体 { TimeSpan span = DateTime.Now - _dateTimeLast; if (span.Milliseconds + 100 <= SystemInformation.DoubleClickTime) //与上次单击记录的时间间隔小于系统定义双击时间,则重置单击记录,判定为双击 { _isSingleClick = false; //左键单击记录复位 return(true); } else { return(false); } } else { return(false); } } //左键单击,未记录单击一次,则当前记录单击一次,判定为非双击 else { _isSingleClick = true; //单击一次 _dateTimeLast = DateTime.Now; _hWndTag = hwdctrl.Tag.ToString(); return(false); } }//非左键单击,判定为非双击 else { return(false); } } else { return(false); } }
/// <summary> /// 鼠标滚轮事件的处理函数 /// [满足条件下缩放图像] /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseWheel(object sender, HalconDotNet.HMouseEventArgs e) { int activeROIIdx = -1; double scale = 0; //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI if (_ROICtrller != null && _ROIPaintMode == PAINT_MODE_INCLUDE_ROI) { //执行ROI管理器对于鼠标按下的处理,并返回活动ROI的索引(有ROI选中时,无法操作[缩放]图像) activeROIIdx = _ROICtrller.MouseDownAction(e.Y, e.X); } //活动ROI索引为-1,即没有选中ROI,则可以响应滚轮事件[在指定鼠标位置缩放图像] if (activeROIIdx == -1) { switch (_viewMode) { case HWndCtrller.VIEW_MODE_ZOOM: { //设置固定的缩放比例 if (e.Delta > 0) { scale = 0.8; } else { scale = 1 / 0.8; } ZoomImage(e.Y, e.X, scale); } break; case HWndCtrller.VIEW_MODE_NONE: break; case HWndCtrller.VIEW_MODE_MOVE: break; case HWndCtrller.VIEW_MODE_MAGNIFY: break; } } }
/// <summary> /// 鼠标抬起事件的处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MouseUp(object sender, HalconDotNet.HMouseEventArgs e) { this._mousePressed = false; //在ROI管理器非空且ROI是否添加绘制的标记为MODE_INCLUDE_ROI,且有活动ROI if (this._ROICtrller != null && this._ROIDispMode == MODE_INCLUDE_ROI && this._ROICtrller.ActiveROIIndex != -1) { //若活动ROI索引非-1,则表示更新ROI(或创建或选择) this._ROICtrller.NotifyIconic(ROICtrller.EVENT_UPDATE_ROI); } else if (this._viewMode == MODE_VIEW_MAGNIFY) { if (this._zoomWindow != null) { this._zoomWindow.Dispose(); } } else if (this._viewMode == MODE_VIEW_MOVE) { //主要时测试鼠标抬起和移动谁先触发 double i = 0.0; } }
private void HWindowControl1_HMouseMove(object sender, HalconDotNet.HMouseEventArgs e) { }