110,536
社区成员
发帖
与我相关
我的任务
分享
private void pictureBox_Pic_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Blue, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
Rectangle rect = new Rectangle(mPoint1, new Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y));
e.Graphics.DrawRectangle(p, rect);
}
public partial class MyPictureBox : UserControl
{
bool mouseDown;
Point startPoint = Point.Empty;
Rectangle mouseRect = Rectangle.Empty;
public MyPictureBox()
{
InitializeComponent();
}
public Bitmap Image { get; set; }
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.Capture = true;
this.mouseDown = true;
this.startPoint = new Point(e.X, e.Y);
this.mouseRect = new Rectangle(e.X, e.Y, 0, 0);
Cursor.Clip = this.RectangleToScreen(this.ClientRectangle);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.mouseDown)
{
this.ResizeRectangle(e.Location);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (this.mouseDown)
{
this.Capture = false;
this.mouseDown = false;
Cursor.Clip = Rectangle.Empty;
this.DrawReversibleFrame();
this.mouseRect = Rectangle.Empty;
this.DrawReversibleFrame();
}
}
void ResizeRectangle(Point p)
{
this.DrawReversibleFrame();
this.mouseRect.Width = p.X - mouseRect.Left;
this.mouseRect.Height = p.Y - mouseRect.Top;
this.DrawReversibleFrame();
}
void DrawReversibleFrame()
{
Rectangle rect = this.RectangleToScreen(this.mouseRect);
ControlPaint.DrawReversibleFrame(rect, Color.Yellow, FrameStyle.Thick);
}
}