C# Windows 窗体.图片框上的可移动区域 [英] C# Windows Forms. Moveable areas on the PictureBox

查看:68
本文介绍了C# Windows 窗体.图片框上的可移动区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理以下图像:http://imgur.com/a2VKb

我已设法在扫描图像的页面之间找到​​一条垂直线.但有时会出现一些错误,我需要为用户提供一个选项来改变这条线的位置和角度.我认为这在 PictureBox 中会很好.

I've managed to find a vertical line between pages on scanned image. But sometimes there are some errors and I need to make an option for user to change this line position and angle. I think this would be nice within a PictureBox.

我需要以某种方式在带有当前图像的图片框上的两个可移动点之间画一条线.当我移动一个点时,必须正确改变线的位置和角度.

I need somehow to draw a line between two movable points on a picturebox with current image. When I move a point, position of line and it's angle have to be changed properly.

推荐答案

这是您可以根据需要使用的示例代码.它基本上使用 4 个事件:
- 油漆
- 鼠标按下
- 鼠标移动
- 鼠标向上

Here is a sample code you can use for your need. It basically uses 4 events :
- Paint
- MouseDown
- MouseMove
- MouseUp

您可以将此代码复制粘贴到名为 Form1 的表单中,并带有一个名为pictureBox1 的图片框

You can copy-paste this code to a form called Form1, with a picture box called pictureBox1

<代码>

    int handleRadius = 3;
    int mPointMoveInProgress = 0;
    Point mPoint1, mPoint2;

    public Form1()
    {
        InitializeComponent();

        mPoint1 = new Point(50, 50); // Set correct default values
        mPoint1 = new Point(50, 300); // Set correct default values
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        // Draw line
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mPoint1, mPoint2);

        Rectangle rectangle;

        // Draw first handle
        rectangle = new Rectangle(mPoint1.X - handleRadius, mPoint1.Y - handleRadius, handleRadius * 2, handleRadius * 2);
        e.Graphics.FillRectangle(Brushes.White, rectangle);
        e.Graphics.DrawRectangle(Pens.Black, rectangle);

        // Draw second handle
        rectangle = new Rectangle(mPoint2.X - handleRadius, mPoint2.Y - handleRadius, handleRadius * 2, handleRadius * 2);
        e.Graphics.FillRectangle(Brushes.White, rectangle);
        e.Graphics.DrawRectangle(Pens.Black, rectangle);
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        // Determine if a point is under the cursor. If so, declare that a move is in progress
        if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) mPointMoveInProgress = 1;
        else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) mPointMoveInProgress = 2;
        else mPointMoveInProgress = 0;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mPointMoveInProgress == 1) // If moving first point
        {
            mPoint1.X = e.X ;
            mPoint1.Y = e.Y ;
            Refresh();
        }
        else if (mPointMoveInProgress == 2) // If moving second point
        {
            mPoint2.X = e.X ;
            mPoint2.Y = e.Y ;
            Refresh();
        }
        else // If moving in the PictureBox: change cursor to hand if above a handle
        {
            if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) Cursor.Current = Cursors.Hand;
            else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) Cursor.Current = Cursors.Hand;
            else Cursor.Current = Cursors.Default;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        // Declare that no move is in progress
        mPointMoveInProgress = 0;
    }

这篇关于C# Windows 窗体.图片框上的可移动区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆