什么是隐藏C#visual studio中按钮上的对象的代码 [英] What is a code to hide an object on a button in C# visual studio

查看:150
本文介绍了什么是隐藏C#visual studio中按钮上的对象的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio创建一个寻宝游戏,并尝试将图像隐藏在一个按钮中。有人请帮帮我



我尝试过的事情:



我有尝试了一些代码,如:btn.Visible = false;

但没有工作

I am creating a treasure hunt game using visual studio and try to hide the image in a button. Someone please help me

What I have tried:

I have tried to some codes like: btn.Visible = false;
but have not worked

推荐答案

我们真的无法帮到你那么多在这里,因为我们不知道你是如何实现用户正在搜索的对象。但是听到它的声音,你将它们实现为带有图像的按钮 - 这不是一个很好的方法 - 所以将可见属性设置为 false 应该有效,只要它是按钮的正确实例,而不是您刚刚创建的新实例。如果你说

We can't really help you that much here, because we have no idea how you are implementing your objects the user is searching for. But by the sound of it, you are implementing them as buttons with an image on them - which isn't a brilliant way to do it - and so setting the Visible property to false should work, provided it's the right instance of the button, and not a new one you just created. If you are saying
Button btn = new Button();
btn.Visible = false;

然后这不起作用,因为你没有影响用户的实际按钮查看和交互。

具体如何实现这个游戏将取决于您运行的环境:例如,WinForms解决方案与基于Web的解决方案有很大不同。

但是......因为我试着根本不使用按钮。

例如,对于WinForms,我会使用BackgroundImage属性将背景绘制到Panel上,并处理Paint事件用于添加该图像顶部的对象。然后,我将处理面板的MouseClick事件,并检查用户是否在其中一个对象内。如果是这样,请将其从待绘制列表中删除,并使面板无效以强制重绘。通过将 Form.DoubleBuffered 设置为 true ,使用时不会发生任何事情。

有很多方法可以让命中测试变得更容易:有一个GraphicsPath类可以描述你的对象在哪里以及它是什么形状,并且它有一个IsVisible方法来检查Point是否在它内部或者没有。

这是一个更多的工作,但它会产生比按钮更好的结果!

Then that won't work because you aren't affecting the actual button that the user is looking at and interacting with.
Exactly how to implement this game will depend on the environment you are running in: a WinForms solution would be very different from a web-based solution for example.
But...for I'd try not to use buttons at all.
For example, for WinForms, I'd draw the background onto a Panel using the BackgroundImage property, and handle the Paint event to "add" the objects on top of that image. I'd then handle the MouseClick event for the panel and check if the user was inside one of the objects. If so, remove it from the "to be drawn" list and Invalidate the panel to force a redraw. By setting Form.DoubleBuffered to true the use shouldn't see anything happen.
There are a lot of ways to make the "hit testing" easier: there is a GraphicsPath class which can describe where your object is and what shape it is, and it has a IsVisible method which checks a Point to see if it's inside it or not.
It's a bit more work, but it's going to produce a better result than buttons!


这篇关于什么是隐藏C#visual studio中按钮上的对象的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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