C# implements some convenient functions on HWindowControl - (zooming and dragging images)

C# implements some convenient functions on HWindowControl - (zooming and dragging images)


1. About the part displayed in the Hwindow form

First of all, the size of the HWindowControl control is fixed. When we are in this fixed size, compared with the originally displayed complete image, now only a part of the image is displayed to achieve the magnification effect.
That is, when we want to enlarge, we reduce the ImagePart; when we want to reduce, we enlarge the ImagePart.
For example: for an image of 800 600,
we initially set the ImagePart to: upper left corner (0, 0), lower right corner (800
600);
now we set it to: upper left corner (0, 0), lower right corner ( 400, 300),
before using the entire image to fill the HWindowControl control, now using half of the image to fill it, obviously this part of the image will be enlarged. The magnification factor is 2.
And
now we set it to: upper left corner (100, 100), lower right corner (500, 400),
which is equivalent to dragging the picture to the upper left corner.
The parameters we need to set the part are the coordinate values ​​​​of the upper left corner and lower right corner of the rectangle, or the upper left corner and length and width values.

2. Mouse-centered zoom

Now that you can zoom, let’s see how to zoom with the current position of the mouse as the center?
To ensure the center position of the mouse, ensure that the relative position of the image pointed by the mouse remains unchanged.
insert image description here
As shown in the figure, the black box is the original image. When
we want to enlarge the image (make the original image smaller and then fill it proportionally into the HWindowControl window), it should be transformed into red; when
we want to reduce the image (make the original image bigger) Then compress it proportionally into the HWindowControl window), so it should be transformed into blue.
The red point is the coordinate of the mouse in the HWindowControl form control.

We can obtain the part when the original image is displayed as LUPointX, LUPointY;
the obtained coordinates of the mouse are MouseX, MouseY;
the magnification ratio Zoom is 0.5;
then the coordinates of the upper left corner of the red rectangle are:

				LUPointY2 = (MouseY - (Zoom * (MouseY - LUPointY))); 
                LUPointX2 = (MouseX - (Zoom * (MouseX - LUPointX)));

Note here that the image coordinates increase from top to bottom and from left to right.
There are also the coordinates of the lower right corner:

                //Ht,Wt为原图像的右下角与左上角计算出来的长宽。
                RBPointY2 = LUPointY2 + (Ht * Zoom);
                RBPointX2 = LUPointX2 + (Wt * Zoom);

At this point, you can set the display area through the part attribute.
The full code is as follows:

You need to bind the following events to HMouseWheel of HWindowControl

        public void Scale(object sender, HMouseEventArgs e) 
        {
    
    
            double Zoom = 0.5;//缩放比例
            HTuple MouseX, MouseY;//鼠标位于图片窗体的坐标
            //左上角与右下角坐标
            HTuple LUPointY, LUPointX, RBPointY, RBPointX;
            HTuple LUPointY2, LUPointX2, RBPointY2, RBPointX2;
            //当前显示的界面大小
            HTuple Ht, Wt;
            HTuple WindowID = hWindowControl1.HalconWindow;

            MouseX = e.X;
            MouseY = e.Y;
            Zoom = e.Delta > 0 ? Zoom : (1 + Zoom);
            HOperatorSet.GetPart(WindowID, out LUPointY, out LUPointX, out RBPointY, out RBPointX);
            Ht = RBPointY - LUPointY;
            Wt = RBPointX - LUPointX;

            if ((Ht * Wt) < 32000 * 32000)
            {
    
    
                LUPointY2 = (MouseY - (Zoom * (MouseY - LUPointY))); 
                LUPointX2 = (MouseX - (Zoom * (MouseX - LUPointX)));
                RBPointY2 = LUPointY2 + (Ht * Zoom);
                RBPointX2 = LUPointX2 + (Wt * Zoom);
                HOperatorSet.SetPart(WindowID, LUPointY2, LUPointX2, RBPointY2, RBPointX2);
                hWindowControl.HalconWindow.ClearWindow();
                hWindowControl.HalconWindow.DispObj(hImage);
            }
        }

3. Move the picture by dragging it with the mouse

Movement is relatively simple.
First, we record the mouse coordinate position (startPointX, startPointY) when the left button is pressed. The
coordinate position (endPointX, endPointY) when the left mouse button is released
can obtain the interpolation move_c, move_r in the x direction and y direction;
Then we can get:
the coordinates of the upper left corner after the move are: (originalImageUL_c + move_c, originalImageUL_r + move_r)
The coordinates of the lower right corner after the move are: (originalImageDR_c + move_c, originalImageDR_r + move_r)

The full code is as follows:

        Point StartPoint = new Point();
        Point EndPoint = new Point();

        private void HMouseDown(object sender, HMouseEventArgs e) 
        {
    
    
            if (e.Button == MouseButtons.Left)
            {
    
    
                StartPoint.X = Convert.ToInt32(e.X);
                StartPoint.Y = Convert.ToInt32(e.Y);
                hWindowControl1.HMouseMove += HMouseMove;
            }
        }
        private void HMouseUp(object sender, HMouseEventArgs e)
        {
    
    
            hWindowControl1.HMouseMove -= HMouseMove;
        }

        private void HMouseMove(object sender, HMouseEventArgs e)
        {
    
    
            EndPoint.X = Convert.ToInt32(e.X);
            EndPoint.Y = Convert.ToInt32(e.Y);
            HWindow_MoveImage(StartPoint, EndPoint, hWindowControl1, hImage);
        }

        public void HWindow_MoveImage(Point startPoint, Point endPoint, HWindowControl hWindow, HImage hImage)
        {
    
    
            int originalImageUL_r;
            int originalImageUL_c;

            int originalImageDR_r;
            int originalImageDR_c;

            hWindow.HalconWindow.GetPart(out originalImageUL_r, out originalImageUL_c, out originalImageDR_r, out originalImageDR_c);

            int move_r = startPoint.Y - endPoint.Y;
            int move_c = startPoint.X - endPoint.X;

            hWindow.HalconWindow.SetPart(originalImageUL_r + move_r, originalImageUL_c + move_c, originalImageDR_r + move_r, originalImageDR_c + move_c);
            hWindow.HalconWindow.ClearWindow();
            hWindow.HalconWindow.DispObj(hImage);
        }

When the source code moves quickly, the high refresh rate may cause image flickering. Make the following changes before and after the hWindow.HalconWindow.ClearWindow(); function in the HWindow_MoveImage function to effectively solve the flickering problem.

                HOperatorSet.SetSystem("flush_graphic", "false");
                hWindow.HalconWindow.ClearWindow();
                HOperatorSet.SetSystem("flush_graphic", "true");

Guess you like

Origin blog.csdn.net/qq_42504097/article/details/129231042