C# - C# 中是否有(类似).AddChild 和 .AddEventListeners,它们可以在图形上使用吗?
我是 C# 新手,但不是 OOP。
我想制作一个“画布”面板,用户可以在其上通过鼠标单击绘制形状,但也可以删除它们(没有什么花哨的,固定尺寸之类的,普通的旧钢笔对象)。就像我说的,我希望用户能够删除他按住 Alt 键单击的任何对象。
我不确定我到底该怎么做。如果我使用 Flash,我可能会这样做:
my_circle_object = new disc-or-whatever-etc;
canvas.addChild(my_circle_object);
my_circle_object.AddEventListener(MouseClickEvent, function_to_remove_child);
现在,由于编译语言在涉及简单的前端 UI 相关内容时是魔鬼,我确信编写此代码需要 20 倍的代码C#。但是,有类似我的例子吗?
我花了整个下午的时间阅读 GraphicsContainers、SmoothingPaint、使用位图的图形持久性等内容,但我从未找到简单的添加事件方法。
谢谢
I'm new to C# but not to OOP.
I'd like to make a "canvas" panel on which a user can draw shapes by mouseClick-ing but also delete them (nothing fancy, fixed sizes and whatnot, plain old pen objects). Like I said, I want the user to be able to delete whatever objects he alt-clicks on.
I'm not sure how exactly could I go about doing this. If I were using Flash, I'd probably do something like:
my_circle_object = new disc-or-whatever-etc;
canvas.addChild(my_circle_object);
my_circle_object.AddEventListener(MouseClickEvent, function_to_remove_child);
Now, since compiled languages are the devil when it comes to simple front-end UI related stuff, I'm sure It'll take 20 times more code to write this in C#. But, is there anything similar to my example?
I've spent all afternoon reading on things like GraphicsContainers, SmoothingPaint, Graphics Persistence using bitmaps etc. but I never found a simple add event method..
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Graphics
对象上的 shape 方法绘制的对象(例如DrawLine
、DrawEllipse
、DrawRect
等) .) 就图形 API 而言,不表示概念对象。调用这些函数只是将项目作为位图绘制到图形表面。完成后,就没有任何东西可以附加事件了。您需要创建自己的形状类型并让它们将自己绘制到图形对象上。您必须在您使用的任何控件(我假设是一个
Panel
)上附加适当的鼠标事件,并进行自己的碰撞检测。The objects that you draw using the shape methods on a
Graphics
object (e.g.DrawLine
,DrawEllipse
,DrawRect
, etc.) do not represent conceptual objects as far as the graphics API is concerned. Calling those functions simply draws the item to the graphics surface as a bitmap. Once that's done, there's nothing there to attach an event to.You'll need to create your own shape types and have them draw themselves to the graphics object. You'll have to attach to the appropriate mouse events on whatever control you're using (I'm assuming a
Panel
) and do your own collision detection.