带有_click事件的形状按钮 [英] shaped button with _click event

查看:79
本文介绍了带有_click事件的形状按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码绘制了一个自定义的椭圆形按钮,问题是:我希望在click事件后多次更改其颜色,但它会停止在红色并再次重新绘制,所以它总是达到第一个条件:

Hi,
the below code drawing a custom Oval button, The problem is: I want after the click event to change its color many times but it stops at the red color and repaint again, so it always achieve just the first condition:

展开 | 选择 | Wrap | 行号

推荐答案

个人观点:如果这样是成为一个按钮,大概是以后再重复使用,那么你不应该在Draw方法中有这么多特定的目的代码。为了使其成为可重复使用的项目,您需要为其提供一个真实的Click事件,您可以对其进行响应并为其分配事件处理程序。


如果你给它一个Click事件,你就可以改变Click事件处理程序中的颜色,就像使用工具箱中的标准按钮一样。



至于为什么这只是第一次工作......每次运行Draw方法时,你都会在第31行分配SmokeWhite的画笔颜色。因此,第50行中的if条件是唯一可以为真的条件。
Personal opinion: If this is to become a button, presumably to be re-used later, then you shouldn''t have so much specific purpose code in the the Draw method. In order to make this a re-usable item you will want to give it a true Click event that you can react to and assign event handlers to.

If you give it a Click event, you could then change the color in the Click event handler just as you would with a standard button out of the toolbox.


As for why this is only working the first time... You are assigning the brush color of SmokeWhite in line 31 every time the Draw method is run. Therefore the if condition in line 50 is the only one that can ever be true.


是的我知道为什么但实际上我不知道解决方案,我尝试了你的方式,在演示文稿中设置事件,但我没有工作,我尝试了很多东西来传递颜色,所有这些都没用。每次我点击它总是重新绘制!!!
yeah I know the "Why" but actually I don''t know the solution, and I tried your way, to set the event in the presentation form, but i didn''t work, I tried many thing to pass the color, all of them were useless. it always repaint each time I click on !!!


现在你只是从一个设定的颜色绘画,然后做一个if语句,看看那个颜色是否是任何一个集合颜色,此时你改变颜色和油漆。让我试着简化一下...


你有:

A = 0

如果A == 0然后A = 0

否则如果A == 1则A = 1

否则如果A == 2则A = 2

。 ..


这真的没有意义。你每次经历都是从头开始的。尝试创建一个名为_lastPaintColour的私有成员,然后运行您的代码。当click事件触发时,在WhiteSmoke中启动它并让你的代码继续运行。


此外,这只会在每个绘制周期发生,这不是一个可预测的步伐。我不是百分百肯定,但我认为窗户只在需要的时候画画,所以就像什么东西在窗户上移动一样。


我看到你有一个无效的在那里打电话,但它只在点击事件上。你可能想把它放在鼠标按下事件上,以便在鼠标停止时不断强制重绘。当然,那将是非常非常慢的。


一个更好的解决方案可能是在那里放一个计时器并让它在某种设定的时间间隔内发送给你良好的帧速率。当计时器到期时,将颜色洗牌并强制重新绘制。
Right now you''re only painting from a set colour, then doing an if statement to see if that colour is any of a set of colours, at which point you change the colour and paint. Let me try to simplify that...

You''ve got:
A = 0
if A == 0 then A = 0
else if A == 1 then A = 1
else if A == 2 then A = 2
...

Which doesn''t really make sense. You''re starting from scratch every time you go through. Try making a private member called something like _lastPaintColour and then running your code. When the click event fires, start it off at WhiteSmoke and let your code carry on.

Also, this is only going to happen every paint cycle, which isn''t really a predictable pace. I''m not 100% sure, but I think windows only paints when it needs to, so like when something moves over a window.

I see you''ve got an Invalidate call there, but it''s only on the click event. You might want to put it on the mouse down event to force a repaint constantly while the mouse is down. Of course, that''d be very, very slow.

A better solution might be to put a timer in there and have it go off at some kind of set interval to give you a good frame rate. When the timer expires, shuffle the colours and force a repaint.


这篇关于带有_click事件的形状按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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