我有一个简单的Windows窗体程序,该程序允许用户在图片框中绘制直线。有一条线,但是它超出了图片框,在其中不可见(就像我所附的图片一样)。我怎样才能使行仅出现在图片框中。这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
namespace LinePOD
{
public partial class LineTest : Form
{
public LineTest()
{
InitializeComponent();
}
Point p1 = new Point();
Point p2 = new Point();
Pen pen = new Pen(Color.Magenta, 10);
private void LineTest_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
p1 = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p2 = e.Location;
Graphics g = this.CreateGraphics();
g.DrawLine(pen, p1, p2);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Aqua;
}
}
}
答案 0 :(得分:1)
您可以创建一个与PictureBox大小相同的位图,并在PictureBox的Paint事件中绘制该位图。然后在鼠标事件中绘制线条以绘制位图。这将在Windows中保留行以最小化/还原事件。为了方便起见,我放置了整个代码:
npm test