我想在按住右键单击按钮的同时移动鼠标,在面板上画一条线。这是我到目前为止所尝试的内容:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Point point = panel1.PointToClient(Cursor.Position);
Point cordinatedClikedPoint = new Point(xPanelPosition, yPanelPositon);
DrawPoint((point.X), (point.Y), Color.Navy);
}
public void DrawPoint(int x, int y, Color color)
{
Graphics g = this.panel1.CreateGraphics();
Pen pen = new Pen(color);
g.DrawRectangle(pen, x, y, 3, 3);
}
答案 0 :(得分:3)
只需使用MouseDown
和MouseUp
事件设置bool变量,在MouseMove
EventHandler中进行绘制。由于您直接调用图形对象,面板上的任何失效或重绘操作都会导致它被清除,另外两个答案将解决这个问题。
public partial class Form1 : Form
{
bool mouseIsDown;
public Form1()
{
InitializeComponent();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button== System.Windows.Forms.MouseButtons.Right)
mouseIsDown = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
Point point = panel1.PointToClient(Cursor.Position);
DrawPoint((point.X), (point.Y), Color.Navy);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
public void DrawPoint(int x, int y, Color color)
{
Graphics g = this.panel1.CreateGraphics();
Pen pen = new Pen(color);
g.DrawRectangle(pen, x, y, 3, 3);
}
}
答案 1 :(得分:2)
另一个......这次使用ControlPaint.DrawReversibleLine():
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point StartPoint, EndPoint;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
Point pt = Cursor.Position;
StartPoint = pt;
EndPoint = pt;
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black);
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line
EndPoint = Cursor.Position;
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // draw new line
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line
// ... do something with StartPont and EndPoint in here ...
// possibly add them to a class level structure to use in the Paint() event to make it persistent?
}
}
}
答案 2 :(得分:1)
试试这个,你需要在每次抽奖时使面板无效。
private IList<Point> _pointList = new List<Point>();
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
_pointList.Add( e.Location );
panel1.Invalidate(); //force a repaint
}
private void panel1_Paint( object sender, PaintEventArgs e )
{
e.Graphics.DrawLines( Pens.Black, _pointList );
}
答案 3 :(得分:0)
据我所知,当您按下按钮时,MouseDown事件会触发一次。并且WindowsForms面板/表单或其他任何内容每隔几毫秒重绘一次。因此,您需要将该代码放入OnPaint
方法中。
看起来应该是这样的:
//global variable
bool isDragging = false;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void form_Paint(object sender, EventArgs e)
{
if(isDragging)
{
Point point = panel1.PointToClient(Cursor.Position);
Point cordinatedClikedPoint = new Point(xPanelPosition, yPanelPositon);
DrawPoint((point.X), (point.Y), Color.Navy);
}
}
答案 4 :(得分:0)
我认为这是您尝试做的完整解决方案:
public partial class Form1 : Form
{
private Graphics g;
private readonly Pen pen = new Pen(Color.Navy, 7);
private Point oldCoords;
private readonly GraphicsPath graphicsPaths = new GraphicsPath();
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (oldCoords.IsEmpty)
graphicsPaths.StartFigure();
else
{
graphicsPaths.AddLine(oldCoords, new Point(e.X, e.Y));
g.DrawPath(pen, graphicsPaths);
}
oldCoords= new Point(e.X,e.Y);
}
else
oldCoords = Point.Empty;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
g.DrawPath(pen, graphicsPaths);
}
}
干杯:)