110,534
社区成员
发帖
与我相关
我的任务
分享
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
UpdateStyles();
}
}
PanelEx panel;
private Point origin = new Point(0,0);
private Point distance = new Point(0, 0);
private bool is_press = false;
private Pen pen = new Pen(Color.SteelBlue, 2);
public Form1()
{
InitializeComponent();
panel = new PanelEx()
{
Size = new Size(600, 600),
Location = new Point(5, 5),
BackColor = Color.White
};
panel.Paint += Panel_Paint;
panel.MouseUp += Panel_MouseUp;
panel.MouseDown += Panel_MouseDown;
panel.MouseMove += Panel_MouseMove;
this.Controls.Add(panel);
}
private void Panel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(pen, origin.X, origin.Y,distance.X,distance.Y);
}
private void Panel_MouseUp(object sender, MouseEventArgs e)
{
if (is_press)
{
Bitmap bmp = new Bitmap(panel.Width, panel.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
var screen = panel.PointToScreen(new Point(0, 0));
gr.CopyFromScreen(new Point(screen.X, screen.Y), Point.Empty, panel.Size);
}
panel.BackgroundImage = bmp;
is_press = false;
}
}
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
is_press = true;
origin = e.Location;
}
private void Panel_MouseMove(object sender, MouseEventArgs e)
{
if (is_press)
{
distance = e.Location;
panel.Invalidate();
}
}