//窗体所需参数
private HTuple mouseDowmRowLT1, mouseDownColLT1, mouseDownRowLT2, mouseDownColLT2;
private HTuple currentRowLT1, currentColLT1, currentRowLT2, currentColLT2;
// 鼠标指针坐标值及对应像素点的灰度值
private HTuple mouseDownPosRowLT, mouseDownPosColLT;
private HTuple currentPosRowLT, currentPosColLT;//, currentGrayValLT;
private Point mouseDownPointLT;// 记录鼠标按下时的点
private bool isMouseDownLT = false; // 鼠标左键是否按下
public static HObject ho_ImageLT; // 当前图像
public static HObject ho_ImageLTLable;//带结果图
public bool MouseFalseLT = false;//鼠标是否按下标志
private bool HalconWindowbigLT = false;//窗体是否放大标志
private int countLT = 0;//点击鼠标计数
Stopwatch timeDoubleLT = new Stopwatch();//双击鼠标计时
//鼠标单击事件
private void hWindowControlLT_HMouseDown(object sender, HMouseEventArgs e)
{
HObject ho_emptyObject;
HTuple isEqualLT, hv_Width, hv_Height, button;
//判断是否有图片
HOperatorSet.GenEmptyObj(out ho_emptyObject);
HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out isEqualLT);
if (isEqualLT == true) return;
//鼠标左键,记录当前位置及显示图像的部分
if (e.Button == MouseButtons.Left)
{
if (countLT == 0) //鼠标第一次按下
{
countLT = 1;
timeDoubleLT.Restart();
timeDoubleLT.Start();
HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out mouseDowmRowLT1, out mouseDownColLT1, out mouseDownRowLT2, out mouseDownColLT2);
HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out mouseDownPosRowLT, out mouseDownPosColLT, out button);
mouseDownPointLT.X = Cursor.Position.X;
mouseDownPointLT.Y = Cursor.Position.Y;
isMouseDownLT = true;
}
else if (countLT == 1)//鼠标第二次按下
{
timeDoubleLT.Stop();
if (timeDoubleLT.ElapsedMilliseconds < 300)//双击鼠标间隔
{
if (HalconWindowbigLT == false)//准备放大窗体
{
tableLayoutPanel.Controls.Add(hWindowControlLT, 0, 0);
this.tableLayoutPanel.SetRowSpan(hWindowControlLT, 3);
this.tableLayoutPanel.SetColumnSpan(hWindowControlLT, 3);
HalconWindowbigLT = true;
}
else //准备缩小窗体
{
tableLayoutPanel.Controls.Add(hWindowControlLT, 0, 0);
this.tableLayoutPanel.SetRowSpan(hWindowControlLT, 1);
this.tableLayoutPanel.SetColumnSpan(hWindowControlLT, 1);
HalconWindowbigLT = false;
}
}
countLT = 0;
}
}
else if (e.Button == MouseButtons.Right) //右击--自适应窗体
{
HOperatorSet.GetImageSize(ho_ImageLT, out hv_Width, out hv_Height);
DispImageLT(hWindowControlLT, hv_Width, hv_Height);
HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
}
}
//鼠标移动事件-----图片跟着鼠标移动
private void hWindowControlLT_HMouseMove(object sender, HMouseEventArgs e)
{
HObject ho_emptyObject;
HTuple isEqualLT, button;
//判断是否有图片
HOperatorSet.GenEmptyObj(out ho_emptyObject);
HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out isEqualLT);
if (isEqualLT == true) return;
//if (ho_ImageLT == null) return;
if (isMouseDownLT) //鼠标按下拖动图像移动
{ HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out currentPosRowLT, out currentPosColLT, out button);
double rowMove = currentPosRowLT - mouseDownPosRowLT;
double colMove = currentPosColLT - mouseDownPosColLT;
HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out currentRowLT1, out currentColLT1, out currentRowLT2, out currentColLT2);
HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 - rowMove, currentColLT1 - colMove, currentRowLT2 - rowMove, currentColLT2 - colMove);
HOperatorSet.ClearWindow(hWindowControlLT.HalconWindow);
HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
}
//else //实时显示像素坐标及灰度值
//{
// getPosAndGrayVal(out currentPosRow, out currentPosCol, out currentGrayVal);
// tsslMousePosition.Text = "坐标:(" + currentPosRow.ToString() + "," + currentPosCol.ToString() + ")";
// tsslGrayVal.Text = "值:" + currentGrayVal.ToString();
//}
}
//鼠标松开事件
private void hWindowControlLT_HMouseUp(object sender, HMouseEventArgs e)
{
isMouseDownLT = false;
}
// 图像以鼠标指针所指位置为中心缩放
private void hWindowControlLT_HMouseWheel(object sender, HMouseEventArgs e)
{
HObject ho_emptyObject;
HTuple isEqualLT, button;
HOperatorSet.GenEmptyObj(out ho_emptyObject);
HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out isEqualLT);
if (isEqualLT == true) return;
//if (ho_ImageLT == null) return;
HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out currentPosRowLT, out currentPosColLT, out button);
HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out currentRowLT1, out currentColLT1, out currentRowLT2, out currentColLT2);
double rowMove1 = (currentRowLT1 - currentPosRowLT) * 0.2;
double colMove1 = (currentColLT1 - currentPosColLT) * 0.2;
double rowMove2 = (currentRowLT2 - currentPosRowLT) * 0.2;
double colMove2 = (currentColLT2 - currentPosColLT) * 0.2;
if (e.Delta > 0)
{
HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 + rowMove1, currentColLT1 + colMove1, currentRowLT2 + rowMove2, currentColLT2 + colMove2);
}
else if (e.Delta < 0)
{
HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 - rowMove1, currentColLT1 - colMove1, currentRowLT2 - rowMove2, currentColLT2 - colMove2);
}
HOperatorSet.ClearWindow(hWindowControlLT.HalconWindow);
HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
}