移动图形对象 [英] Move a graphic object

查看:60
本文介绍了移动图形对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过以下代码在form_paint中创建了图形对象:


I''ve created a graphic object in form_paint by this code :


Graphics gr = this.CreateGraphics();
gr.FillEllipse(Brushes.Yellow, 10, 30, 200, 150);



我该如何使该对象像控件(例如按钮)一样对待?;我的意思是用户可以在运行时在窗体上移动此椭圆.
:confused:
就像Powerpack工具一样,在创建对象时,也要移动它.
(请通过使用路径或区域来告诉我....)



how can I make this object to treat like a control(for example a button)?;I mean user be able to move this ellipse on the form at run time.
:confused:
like powerpacks tools ,when you create the object ,you move it.
(please tell me by using path or region .... )

推荐答案

如果您在Form.Paint事件中这样做(如您所愿),那么您不应该使用this.CreateGraphics()-改用e.Graphics.

您将需要处理MouseDown(捕获移动开始),MouseMove(移动椭圆)和MouseUp(移动结束).
在MouseDown中标记您正在移动中,在MouseUp中标记您不在移动中.
在MouseMove中,检查是否在移动,并存储鼠标指针的坐标.使用Invalidate()重绘椭圆.
If you are doing this in the Form.Paint event (as you should), then you should not use this.CreateGraphics() - use e.Graphics instead.

You will need to handle MouseDown (capture start of move) MouseMove (Move your ellipse) and MouseUp (End of move).
Mark that you are moving in a bool in MouseDown, mark you aren''t moving in MouseUp.
In MouseMove, check if you are moving, and store the coordinates of the mouse pointer. Use Invalidate() to redraw your ellipse.


这篇关于移动图形对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆