如何在鼠标单击时删除形状? [英] How do I remove a shape on mouse click?

查看:101
本文介绍了如何在鼠标单击时删除形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出如何在鼠标点击时删除形状,但我找不到任何关于如何做的可靠信息。这有可能吗?我将创建多个形状(它们是相同的形状,只是在不同的位置),我想制作它,以便用户可以在必要时删除单个形状。


任何人都可以帮我解决这个问题吗?

解决方案

这是你的代码,你可以做任何你喜欢的事情:P

例如,如果您的形状继承自Control并且您将它们插入到面板上,则可以通过附加到Shape的鼠标事件(无论您喜欢哪个)来轻松删除它们。如果它们被触发,将它们从面板的控制列表中删除。


这只是猜测,你实际上说很少关于你的程序做什么。如果您需要更具体的帮助,您将需要更加详细。告诉我们你要做什么,告诉我们你做了什么以及你尝试了什么。


展开 | 选择 | Wrap | 行号


下次,如果你谈谈你想要做什么,这会有所帮助。给我们一些背景而不是向我们抛出代码;)那就是说,我想我从你发布的内容中得到了想法,所以我们来谈谈它。


你有什么这里看起来只是 一个展示品。你给它行和列,并作为回应,它绘制了一系列的桌子。这意味着你没有任何实际的对象可以与之互动,但确实意味着你知道你的办公桌在任何特定的时间应该在哪里。


你有一个面板,squareContainerPanel,你正在绘制。这意味着你可以为MouseClick事件添加一个监听器,这样你就可以知道该面板上何时何地发生了鼠标交互。您可以使用该信息来查看鼠标单击是否位于其中一个形状的顶部。你已经有了生成矩形的代码,所以你知道它们的界限。 .NET Rectangle还有一个Contains方法,您可以使用它来查看它是否包含一个点。如果确实如此,你可以在某处设置某种标志,而不是画那个特定的桌子。


现在,我认为值得指出的是,架构的变化可以使这个对你来说更容易一些。目前,你根本没有跟踪你的矩形......你每次去绘制它们时都会生成它们而根本不存储它们。也许考虑制作一个Desk类并将它们预生成为List< Desk>类型的成员变量。然后你所要做的就是画它们。它还可以在您查看用户是否与他们进行交互时帮助您。在面板的鼠标单击事件中,您可以遍历列表中的每个桌面,并查看鼠标单击是否与该特定对象进行了交互,然后执行操作。在您的特定情况下,采取行动将从列表中删除它,以便它不会在下次更新时被绘制。


我希望如果您有任何帮助,进一步的问题请问! :)

I''m trying to find out how to remove a shape on mouse click, but I can''t find any solid information on how to do it. Is this possible at all? I''ll be creating multiple shapes (They''re the same shape, just in different locations) and I want to make it so that the user can remove individual shapes if necessary.

Can anyone please help me out with this one?

解决方案

It''s your code, you can do whatever you like :P

For example, if your shapes inherit from Control and you''re inserting them on a panel, you can quite easily remove them by attaching to the Shape''s mouse events (whichever you like). If they are triggered, remove them from the Panel''s control list.

That''s just speculation though, you''ve actually said very little about what your program does. If you need any more specific help you''re going to have to be more detailed. Tell us what you''re trying to do, show us what you''ve done and what you''ve tried.


Expand|Select|Wrap|Line Numbers


Next time, it helps if you talk a bit about what you''re trying to do. Give some background instead of just throwing code at us ;) That said, I think I get the idea from what you''ve posted, so lets talk about it.

What you have here looks to be just a display thing. You give it rows and columns and in response to that, it draws a series of desks. This means you don''t have any actual objects to interact with, but it does mean you know where your desks are supposed to be at any given time.

You''ve got a panel, squareContainerPanel, that you''re drawing in. That means you can add a listener to the MouseClick event so you can tell when, where mouse interaction took place on that panel. You can use that information to see if that mouse click was over top of one of your shapes. You''ve already got the code to generate the rectangles, so you know their bounds. A .NET Rectangle also has a Contains method that you can use to see if it contains a point. If it does, you can set some kind of flag somewhere not to draw that particular desk.

Now, I think it''s worth pointing out that a change in architecture could make this a little easier for you. Currently, you don''t keep track of your rectangles at all... you generate them every time you go to draw them and don''t store them at all. Perhaps consider making a Desk class and pre-generating them into a member variable of type List<Desk>. Then all you have to do is draw them. It also helps you when it''s time to see if the user is interacting with them. On the panel''s mouse click event, you can loop through each Desk in your list and see if the mouse click interacted with that particular object, then take action. In your particular case, taking action would be removing it from the list so that it doesn''t get drawn on the next update.

I hope that helps you, if you have any further questions please ask! :)


这篇关于如何在鼠标单击时删除形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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