自定义控件选择图片框或面板上的坐标?

时间:2013-08-31 14:26:13

标签: controls mouse picturebox

我正在开发一个显示DICOM文件的应用程序,现在我正在进入MPR。是否有一个控件可以让你在图片框或面板中像这样一行:

http://www.vcreatelogic.com/docs/gcf-2.6.0/html/MPRView.jpg 目的是移动那条线并在移动时执行一些东西,所以我基本上需要某种控制或自定义控制(或者可能是自定义图标),这将允许用户在他移动时看到他在图片框中移动的位置鼠标。

谢谢!

更新: 这是我正在使用的代码:

在Picturebox的鼠标按下:

 if (e.Clicks == 2)
        {

            horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
            horizontalend = new Point(picbox_mpr.Width, e.Y);//End point of Horizontal line.
            verticalstart = new Point(e.X, 0);//Start point of Vertical line
            verticalend = new Point(e.X, picbox_mpr.Height);//End point of Vertical line.
 }

然后在MouseMove(我可以平移图片)中,我希望这些线条在绘制的位置保持固定..

更新: 整个想法是在用户双击图像时绘制线条,然后如果图像被移动,则将这些线条与图像一起移动..

2 个答案:

答案 0 :(得分:1)

首先,我给出的方法不需要自定义控件才能工作,它利用PictureBox提供的两个事件来运行逻辑,但是如果你还需要一个自定义控件,你应该是能够通过一些编码将这些方法移植到该控件来实现这一点。

以下两个字段将保存逻辑中的大部分值

private Point horizontalstart, horizontalend;
private Point verticalstart, verticalend;
private bool drawlines;//Specifies a value whether PictureBox should redraw itself.

让我解释一下这些要点是什么;
第一个Point语句保存水平线的起点和终点,以及
第二个Point语句保存垂直线的起点和终点 完成后,请在MouseMoveEvent;

PictureBox中写下此代码
    private void SourcePictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if(drawlines==true)//If the lines should move.
        {
         horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
         horizontalend = new Point(SourcePictureBox.Width, e.Y);//End point of Horizontal line.
         verticalstart = new Point(e.X, 0);//Start point of Vertical line
         verticalend = new Point(e.X, SourcePictureBox.Height);//End point of Vertical line.
         SourcePictureBox.Invalidate();//Force PictureBox to repaint.
        }

        else if(drawlines==false)//To lock the lines at current coordinates.
        {
         //Add any code if needed.
        }
    }

其中SourcePictureBox是我们正在操作的PictureBox。一旦MouseMoveEvent被引发,就会计算两行的新坐标,并将重绘消息发送到PictureBox,后者使用上面声明和计算的坐标。

好的,现在在PictureBox的Paint事件中编写以下代码;

    private void SourcePictureBox_Paint(object sender, PaintEventArgs e)
    {
        if (SourcePictureBox.Image != null)
        {
            e.Graphics.DrawLine(SystemPens.WindowText, horizontalstart, horizontalend);//Draw Horizontal line.
            e.Graphics.DrawLine(SystemPens.WindowText, verticalstart, verticalend);//Draw Vertical line.
        }
    }

我在图片中注意到的另一件事是,图片显示的指南比图片略小,如果是这种情况,则替换MouseMoveEvent事件中除{{1}之外的语句一个有以下内容;

Invalidate()

但是这些陈述中有什么新东西,除了它们通过值10(int)增加两行的起点并减少值为10的终点之外什么都没有,因此在PictureBox的边缘和引导线之间给你一些空间。请注意,如果您想使用您想要的值增加或减少空格替换值10,但请记住,该值不得大于PictureBox大小的一半。
我要解决的另一件事是,如果你想要 horizontalstart = new Point(0+10, e.Y);//Start point of Horizontal line. horizontalend = new Point(SourcePictureBox.Width-10, e.Y);//End point of Horizontal line. verticalstart = new Point(e.X, 0+10);//Start point of Vertical line verticalend = new Point(e.X, SourcePictureBox.Height-10);//End point of Vertical line. ,用change the colour of guiding lines中的任何值替换Paint Event中两个语句的第一个参数。如果没有满足你的课程,你总是可以自由创造。如果你知道怎么做,那么它是最好的选择,如果你需要帮助,请看看SystemPens。创建一支新笔您可以选择为指纹线定义宽度。

<强>更新

`在任何时候,为了阻止线移动,将 drawlines (上面声明的)设置为 false ,要恢复其功能,请将其设置为 true

这不是一个万无一失的方法,但我希望它能够完成你的工作,并且不要忘记将PictureBox和相关事件重命名为PictureBox的名称。

答案 1 :(得分:1)

解决了!

这是代码:

//Variable declaration:
    private Point horizontalstart, horizontalend, verticalstart, verticalend, horizontalstart_initialpoint, horizontalend_initialpoint, verticalstart_initialpoint, verticalend_initialpoint;
    System.Drawing.Pen redline= new System.Drawing.Pen(System.Drawing.Color.Red);
    private Point redlinemovedpoints = Point.Empty;
    private Point redline_panstart = Point.Empty;


 void picbox_mpr_MouseDown(object sender, MouseEventArgs e)
    {
        redline_panstart = new Point(e.X, e.Y); //saves the point from where you pan the picture

 if (e.Clicks == 2) //if the user double clicks, then the red line is drawn in the PictureBox
        {
            horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
            horizontalend = new Point(picbox_mpr.Width, e.Y);//End point of Horizontal line.
            verticalstart = new Point(e.X, 0);//Start point of Vertical line
            verticalend = new Point(e.X, picbox_mpr.Height);//End point of Vertical line.

            //fixed reference points, this is used to store where the line was drawn initially.
            horizontalstart_initialpoint= horizontalstart;
            horizontalend_initialpoint = horizontalend;
            verticalstart_initialpoint = verticalstart;
            verticalend_initialpoint = verticalend;

            picbox_mpr.Invalidate(); // Draw the lines (see paint event at the bottom)
        }


 void picbox_mpr_MouseMove(object sender, MouseEventArgs e)
    {

 if (e.Button == MouseButtons.Left & pan) // don't worry about pan, is a variable I use to determine if the user is dragging the mouse.
        {

            redlinemovedpoints = new Point(e.Location.X - redline_panstart.X,
                    e.Location.Y - redline_panstart.Y); // this is the X and Y points move

            horizontalstart = new Point(horizontalstart_initialpoint.X + redlinemovedpoints.X, horizontalstart_initialpoint.Y+redlinemovedpoints.Y);
            horizontalend = new Point(picbox_mpr.Width, horizontalend_initialpoint.Y + redlinemovedpoints.Y);
            verticalstart = new Point(verticalstart_initialpoint.X + redlinemovedpoints.X, verticalstart_initialpoint.Y + redlinemovedpoints.Y);
            verticalend = new Point(verticalstart_initialpoint.X + redlinemovedpoints.X, picbox_mpr.Height);

            picbox_mpr.Invalidate(); //re draws the lines, this time moved X or Y depending the pan.

        }

}

void picbox_mpr_MouseUp(object sender, MouseEventArgs e)
    {

        //save the line points positions where after panning the image
        horizontalstart_initialpoint = horizontalstart;
        horizontalend_initialpoint = horizontalend;
        verticalstart_initialpoint = verticalstart;
        verticalend_initialpoint = verticalend;
     }

 private void picbox_mpr_Paint(object sender, PaintEventArgs e)
    {
        Image tmp = (Image)img.RenderImage(0);
        e.Graphics.Clear(System.Drawing.Color.Black);
        e.Graphics.DrawImage(tmp, movingPoint);
        tmp.Dispose();
        e.Graphics.DrawLine(redline, horizontalstart, horizontalend);//Draw Horizontal line.
        e.Graphics.DrawLine(redline, verticalstart, verticalend);//Draw Horizontal line.

     }