我是这么编的,请问为什么不对?能帮助改改吗?
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;
}
}
c# panel画图 panel1中画一个矩形,然后实现用鼠标拖拽这个矩形移动
答案:2 悬赏:50
解决时间 2021-01-30 17:55
- 提问者网友:樱花树下最美的约定
- 2021-01-30 06:21
最佳答案
- 二级知识专家网友:猎杀温柔
- 2021-01-30 07:59
程序已发送 里面有两个程序 一个是关于Pain中的拖拽 一个是listbox中的拖拽。你参考参考,也许对你要实现的效果有帮助。
全部回答
- 1楼网友:偏爱自由
- 2021-01-30 08:39
<p>为了简单起见,我画在了窗口上,与画在panel中的一样的道理 。</p> <p>下面为效果图,最后为代码</p> <p>
</p> <p></p> <p>
</p> <p>
</p> <p></p> <p></p>
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯