如何在Windows窗体中旋转自定义控件

时间:2022-01-21 15:50:09

I need to rotate my custom control in Windows Form using C# without using third-party libraries.

我需要使用C#在Windows窗体中旋转我的自定义控件,而不使用第三方库。

Don't want to either rotate the text or image of control instead actually need to entirely rotate control.

不想旋转控件的文本或图像而实际上需要完全旋转控件。

4 个解决方案

#1


0  

It is possible if you can get control of the painting, but you have to do a lot of the work yourself. Basically it all occurs on painting. A good example demonstrating how to do it is the Dock Panel Suite for WinForms.

如果你可以控制绘画,你就有可能,但你必须自己做很多工作。基本上这一切都发生在绘画上。演示如何操作的一个很好的例子是用于WinForms的Dock Panel Suite。

In the VS2005AutoHideStrip (found here), there is a GetTransformedRectangle method that uses a System.Drawing.Drawing2D.Matrix class to rotate a rectangle.

在VS2005AutoHideStrip(在此处找到)中,有一个GetTransformedRectangle方法,它使用System.Drawing.Drawing2D.Matrix类来旋转矩形。

It also sets the Transform property on the Graphics class, which will apply transformations automatically when you ask for something to be painted.

它还在Graphics类上设置Transform属性,当您要求绘制某些内容时,它将自动应用转换。

I advise reviewing this code for examples, it does it to draw docked tab strips down the sides of the page as opposed to top / bottom.

我建议您查看此代码的示例,它可以在页面的两侧绘制停靠的标签条,而不是顶部/底部。


Some controls like TextBox are funny in that they borrow heavily from low-level Win32 libraries in their painting / behaviour, I've no idea if it is possible to get enough control to rotate this.

#2


2  

From here:Is it possible to rotate a button control in WinForms? 5 up vote accepted

从这里:是否可以在WinForms中旋转按钮控件?接受5票投票

You can't rotate controls. That's simply not supported by the native API controls that WinForms uses.

您无法旋转控件。 WinForms使用的本机API控件根本不支持这一点。

And one might wonder why it even should be supported. What could you possibly be trying to do that you'd need to rotate a button control? It would be much easier to draw it in a different place with a different shape in the first place, rather than trying to rotate an existing control. (Do note that you can also resize and reposition a control at run-time, if that would fit your needs. Investigate the Size and Location properties.)

人们可能想知道为什么它应该得到支持。您可能想要做什么,您需要旋转按钮控件?首先将它绘制在具有不同形状的不同位置更容易,而不是尝试旋转现有控件。 (请注意,如果符合您的需要,您还可以在运行时调整控件的大小和重新定位。调查大小和位置属性。)

The only workaround is to draw the control's image to a bitmap, hide the control, and draw the bitmap onto the form in the location you want it to appear. Of course, that won't result in a control that the user can interact with. They won't be able to click an image of a button, because it's not a real button. If that's acceptable to you, you should probably be using an image in the first place, rather than a button.

唯一的解决方法是将控件的图像绘制到位图,隐藏控件,并将位图绘制到您希望它出现的位置的窗体上。当然,这不会导致用户可以与之交互的控件。他们将无法点击按钮的图像,因为它不是真正的按钮。如果您可以接受,那么您应该首先使用图像,而不是按钮。

#3


1  

It is not possible to rotate controls. This is not supported by WinForms' controls API. As you are dealing with a custom control, try simply redrawing it to suit your purposes.

无法旋转控件。 WinForms的控件API不支持此功能。在处理自定义控件时,只需重新绘制它以适合您的目的。

#4


0  

I am pretty sure you can not perform this in windows forms at least not easily. However you can perform this in WPF and then bring WPF to your windows Form if you are looking for cool designs or even special effects to your controls.

我很确定你不能在Windows窗体中执行此操作,至少不容易。但是,您可以在WPF中执行此操作,然后将WPF添加到Windows窗体中,如果您正在为控件寻找炫酷的设计甚至特殊效果。

This is FAR more easily done in WPF. In Windows Form, it's a huage pain to pull off.

这在WPF中更容易完成。在Windows窗体中,这是一个令人痛苦的痛苦。

Hope this help

希望这有帮助

#1


0  

It is possible if you can get control of the painting, but you have to do a lot of the work yourself. Basically it all occurs on painting. A good example demonstrating how to do it is the Dock Panel Suite for WinForms.

如果你可以控制绘画,你就有可能,但你必须自己做很多工作。基本上这一切都发生在绘画上。演示如何操作的一个很好的例子是用于WinForms的Dock Panel Suite。

In the VS2005AutoHideStrip (found here), there is a GetTransformedRectangle method that uses a System.Drawing.Drawing2D.Matrix class to rotate a rectangle.

在VS2005AutoHideStrip(在此处找到)中,有一个GetTransformedRectangle方法,它使用System.Drawing.Drawing2D.Matrix类来旋转矩形。

It also sets the Transform property on the Graphics class, which will apply transformations automatically when you ask for something to be painted.

它还在Graphics类上设置Transform属性,当您要求绘制某些内容时,它将自动应用转换。

I advise reviewing this code for examples, it does it to draw docked tab strips down the sides of the page as opposed to top / bottom.

我建议您查看此代码的示例,它可以在页面的两侧绘制停靠的标签条,而不是顶部/底部。


Some controls like TextBox are funny in that they borrow heavily from low-level Win32 libraries in their painting / behaviour, I've no idea if it is possible to get enough control to rotate this.

#2


2  

From here:Is it possible to rotate a button control in WinForms? 5 up vote accepted

从这里:是否可以在WinForms中旋转按钮控件?接受5票投票

You can't rotate controls. That's simply not supported by the native API controls that WinForms uses.

您无法旋转控件。 WinForms使用的本机API控件根本不支持这一点。

And one might wonder why it even should be supported. What could you possibly be trying to do that you'd need to rotate a button control? It would be much easier to draw it in a different place with a different shape in the first place, rather than trying to rotate an existing control. (Do note that you can also resize and reposition a control at run-time, if that would fit your needs. Investigate the Size and Location properties.)

人们可能想知道为什么它应该得到支持。您可能想要做什么,您需要旋转按钮控件?首先将它绘制在具有不同形状的不同位置更容易,而不是尝试旋转现有控件。 (请注意,如果符合您的需要,您还可以在运行时调整控件的大小和重新定位。调查大小和位置属性。)

The only workaround is to draw the control's image to a bitmap, hide the control, and draw the bitmap onto the form in the location you want it to appear. Of course, that won't result in a control that the user can interact with. They won't be able to click an image of a button, because it's not a real button. If that's acceptable to you, you should probably be using an image in the first place, rather than a button.

唯一的解决方法是将控件的图像绘制到位图,隐藏控件,并将位图绘制到您希望它出现的位置的窗体上。当然,这不会导致用户可以与之交互的控件。他们将无法点击按钮的图像,因为它不是真正的按钮。如果您可以接受,那么您应该首先使用图像,而不是按钮。

#3


1  

It is not possible to rotate controls. This is not supported by WinForms' controls API. As you are dealing with a custom control, try simply redrawing it to suit your purposes.

无法旋转控件。 WinForms的控件API不支持此功能。在处理自定义控件时,只需重新绘制它以适合您的目的。

#4


0  

I am pretty sure you can not perform this in windows forms at least not easily. However you can perform this in WPF and then bring WPF to your windows Form if you are looking for cool designs or even special effects to your controls.

我很确定你不能在Windows窗体中执行此操作,至少不容易。但是,您可以在WPF中执行此操作,然后将WPF添加到Windows窗体中,如果您正在为控件寻找炫酷的设计甚至特殊效果。

This is FAR more easily done in WPF. In Windows Form, it's a huage pain to pull off.

这在WPF中更容易完成。在Windows窗体中,这是一个令人痛苦的痛苦。

Hope this help

希望这有帮助