我如何画一条线,然后用C#把它转换成一条直线?面板A是我的绘画,面板B是结果
我希望程序理解我画了一条线,然后在面板B中将其转换为直线。或者如果我画一个正方形,它就变成了一个完美的正方形
like this
密码
public partial class frmPaint : Form
{
public Point x = new();
public Point y = new();
public Pen penA = new(Color.Red, 2);
public Pen Eraser = new(Color.White, 10);
public Graphics graphics;
public frmPaint()
{
InitializeComponent();
graphics = panelDraw.CreateGraphics();
}
private void panelDraw_MouseDown(object sender, MouseEventArgs e)
{
y = e.Location;
if (rbLineWidth5.Checked)
penA.Width = 5;
if (rbLineWidth10.Checked)
penA.Width = 10;
if (rbLineWidth15.Checked)
penA.Width = 15;
}
private void panelDraw_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.Location;
graphics.DrawLine(penA, x, y);
y = e.Location;
}
if (e.Button == MouseButtons.Right)
{
x = e.Location;
graphics.DrawLine(Eraser, x, y);
y = e.Location;
}
}
private void btnColor_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new();
if (colorDialog.ShowDialog() == DialogResult.OK)
penA.Color = colorDialog.Color;
}
}
1条答案
按热度按时间oug3syen1#
此代码使用MouseLeave事件来确定绘制的结束,它还有一个sizeDiff变量来确定开始绘制的点与结束点之间的距离,从而确定它是正方形还是直线。在确定之后,如果是直线,则在起始点和终点之间绘制直线,如果是正方形,则绘制从图形的最低点开始并延伸到图形的最高点的正方形。
希望这对我有帮助,如果你有任何问题,请告诉我。