如何在vb.net中旋转按钮 [英] How to rotate button in vb.net

查看:214
本文介绍了如何在vb.net中旋转按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中旋转按钮......这可能吗?

I want to rotate button in my project... Is this possible??

推荐答案

我不确定你是否需要旋转控制(按钮) )...在大多数情况下,足以旋转文本。如果我错了,请告诉我。



请参考此链接:如何:对齐绘制的文本 [ ^ ]
I''m not sure that you need to rotate control (button)... In most cases is enough to rotate text on it. If i''m wrong, please, let me know.

Please, refer this link: How to: Align Drawn Text[^]


在Windows窗体中,这是一个巨大的痛苦。在WPF中,它非常简单!



我认为你使用了错误的应用程序类型......
In Windows Forms it''s a giant pain in the ass. In WPF, it''s incredibly easy!

I think you''re using the wrong application type...


目前为止最好的想法是解决方案4.



System.Windows.Forms 的正确解决方案是:创建一个自定义控件,其行为类似于按钮并实现旋转属性。如果要旋转到任意角度,还应设置属性 System.Windows.Forms.Control.Region

http://msdn.microsoft.com/en-us/library/system.windows .forms.control.region.aspx [ ^ ]。







由于Maciej表达了他对第一个解决方案的担忧,我会提供一些模式细节。



如果是任意角度,它有点复杂,并且我一开始并不想花那么多时间来处理更复杂的代码。



基本上,你需要一个更大的控件才能适应全套按钮各个角度;它将是一个圆形区域,你应该在圆形周围画一个矩形。你能想象这样的人物吗?在此区域内,您可以以任何角度绘制矩形按钮。现在,使用 System.Drawing.Graphics.Transform 渲染包括边框在内的所有细节中的整个按钮,具体取决于当前角度。



现在,您需要删除控件的未使用部分。为此,您计算表示倾斜按钮的矩形区域,并将此区域指定给控件的 Region 属性。



现在,如何改变角度? Angle 属性应该有 setter 来执行更改值的所有副作用。如果该值与前一个值不同,则应该做两件事:1)以不同的角度创建一个矩形区域,并重新分配控件的属性 Region ; 2)调用无效以重新绘制控件。



[END EDIT#2]







如果你只想让它只有90°,问题就特别简单了。从 System.Windows.Forms.ButtonBase 中派生按钮类。首先,从水平位置切换到垂直位置完成视图交换宽度高度值。它将正确呈现按钮的非客户区域。



在重写方法 System.Windows.Forms.Control中渲染客户区。的OnPaint 。您需要渲染文本旋转。要进行轮换,请使用 System.Drawing.Graphics.Transform (您将从事件参数参数中获取 Graphics 的实例):

http://msdn.microsoft。 com / en-us / library / system.windows.forms.buttonbase.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ system.windows.forms.control.onpaint.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.painteventargs.graphics.aspx [ ^ ],

http:// msdn .microsoft.com / zh-cn / library / system.drawing.graphics.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx [ ^ ]。



-SA
The best idea so far is the Solution 4.

The right solution for System.Windows.Forms is: create a custom control which behaves like a button and implement rotation property. If you want to rotate to an arbitrary angle, you should also set the property System.Windows.Forms.Control.Region:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx[^].



As Maciej expressed his concerns about first solution, I''ll provide some mode detail.

In case of arbitrary angle, it''s a bit complicated, and I didn''t want to spend so much time for more complicated code at first.

Basically, you need to have a bigger control which fits the full set of buttons at all angles; it would be a round area, and you should draw a rectangle around the round. Can you picture such figure? Inside this area, you can draw a rectangular button at any angle. Now, you render the whole button in all the detail, including borders, using System.Drawing.Graphics.Transform, depending on the current angle.

Now, you need to cut out unused part of the control. To do so, you calculate the rectangular Region representing the tilted button and assign this region to Region property of the control.

Now, how to change the angle? The Angle property should have a setter to do all the side effects of changing the value. If the value is different from the previous value, you should to two things 1) create a rectangular region at different angle and re-assign the property Region of the control; 2) call Invalidate to cause re-drawing of the control.

[END EDIT #2]



If you only want to allow only 90°, the problem is especially simple. Derive the button class from System.Windows.Forms.ButtonBase. First, switch from horizontal to vertical position is done view swapping Width and Height values. It will correctly render non-client area of the button.

Render the client area in the overridden method System.Windows.Forms.Control.OnPaint. You need to render the text rotated. For rotation, use System.Drawing.Graphics.Transform (you will get the instance of Graphics from an event arguments parameter):
http://msdn.microsoft.com/en-us/library/system.windows.forms.buttonbase.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.painteventargs.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[^].

—SA


这篇关于如何在vb.net中旋转按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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