我正在开发一个显示DICOM文件的应用程序,现在我进入了MPR。是否有允许在picturebox或面板中有类似行的控件:
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中(在我可以看到图片的地方),我希望这些线保持固定在所绘制的位置上。
更新:如果用户在图像中双击,则整个想法是绘制线条,如果图像被移动,则将这些线与图像一起移动。
发布于 2013-08-31 16:25:18
首先,我给出的方法不需要一个自定义控件才能工作,它使用PictureBox提供的两个事件来运行逻辑,但是如果您仍然需要一个自定义控件,那么您应该能够通过将这些方法移植到该控件上进行一些编码。
以下两个字段将保存我们逻辑中的大多数值,
private Point horizontalstart, horizontalend;
private Point verticalstart, verticalend;
private bool drawlines;//Specifies a value whether PictureBox should redraw itself.让我解释一下这些要点是什么;
第一个Point语句包含水平线的起点和终点,以及
第二个Point语句保存垂直线的起始点和结束点。
当您完成它时,请用MouseMoveEvent of您的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事件的PictureBox中编写下面的代码;
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事件中的两个语句(不包括Invalidate()事件)替换为下面的语句;
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.但是,这些语句中的新内容是,除了它们通过值10(int)增加两行的起始点和用值10减小端点,从而在PictureBox的边缘和引导的lines.Please注释之间提供了一些空间,如果您想要用您想要的值来替换值10,请记住,这个值不能大于PictureBox大小的一半。
我想说的另一件事是,如果您想要change the colour of guiding lines,将Paint Event中的两个语句的第一个参数替换为类SystemPens.If提供的任何值--类中没有任何值可以满足您的要求,您可以随时为yourself.If创建--您知道如何做,这是最好的选择,如果您需要帮助,请看一看这里.Creating一支新笔,它为您提供了为行会行定义宽度的选项。
更新
“在任何时候,为了阻止线条移动,将上面的drawlines(declared设置为false,恢复其功能,将其设置为true。
这不是一种万无一失的方法,但我希望它足以完成您的工作,并且不要忘记用您的PictureBox名称重命名PictureBox和相关事件。
发布于 2013-09-04 17:49:33
解决了!
下面是代码:
//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.
}https://stackoverflow.com/questions/18549197
复制相似问题