c# panel画图 panel1中画一个矩形,然后实现用鼠标拖拽这个矩形移动
我是这么编的,请问为什么不对?能帮助改改吗?privateRectanglem_Rect;publicForm1(){InitializeComponent();this...
我是这么编的,请问为什么不对?能帮助改改吗?
private Rectangle m_Rect;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
m_Rect = new Rectangle(10, 10, 50, 30);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(SystemBrushes.ControlDark, this.m_Rect);
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, this.m_Rect);
}
Point pt;
private void m_Rect_MouseDown(object sender, MouseEventArgs e)
{
pt = Cursor.Position;
}
private void m_Rect_MouseMove(object sender, MouseEventArgs e)
{
int mx = 0;
int my = 0;
int px = 0;
int py = 0;
int x = 0;
int y = 0;
if (e.Button == MouseButtons.Left)
{
x = Cursor.Position.X - this.Location.X - 14;//得到鼠标在窗口中对应的位置
y = Cursor.Position.Y - this.Location.Y - 32;//窗口的左上角与窗口的可用区域的左上角的两个坐标差
px = Cursor.Position.X - pt.X;//取得鼠标相对移动的位置
py = Cursor.Position.Y - pt.Y;//pt是存储上次鼠标移动后的位置
mx = m_Rect.Location.X + px;//把鼠标移动大小复给矩形,让矩形也移动鼠标移动的大小
my = m_Rect.Location.Y + py;
m_Rect.Location = new Point(mx, my);
pt = Cursor.Position;
}
} 展开
private Rectangle m_Rect;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
m_Rect = new Rectangle(10, 10, 50, 30);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(SystemBrushes.ControlDark, this.m_Rect);
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, this.m_Rect);
}
Point pt;
private void m_Rect_MouseDown(object sender, MouseEventArgs e)
{
pt = Cursor.Position;
}
private void m_Rect_MouseMove(object sender, MouseEventArgs e)
{
int mx = 0;
int my = 0;
int px = 0;
int py = 0;
int x = 0;
int y = 0;
if (e.Button == MouseButtons.Left)
{
x = Cursor.Position.X - this.Location.X - 14;//得到鼠标在窗口中对应的位置
y = Cursor.Position.Y - this.Location.Y - 32;//窗口的左上角与窗口的可用区域的左上角的两个坐标差
px = Cursor.Position.X - pt.X;//取得鼠标相对移动的位置
py = Cursor.Position.Y - pt.Y;//pt是存储上次鼠标移动后的位置
mx = m_Rect.Location.X + px;//把鼠标移动大小复给矩形,让矩形也移动鼠标移动的大小
my = m_Rect.Location.Y + py;
m_Rect.Location = new Point(mx, my);
pt = Cursor.Position;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询