在 Windows 窗体中旋转 .NET 面板
我们使用 Windows 窗体 和自定义用户控件,我希望能够旋转面板以特定形式托管 userControl。我已经看到 WPF 具有类似的功能,但我暂时无法使用它。是否可以使用内置的 .NET 方法或 实现面板及其子面板的旋转GDI+?
我在游戏开发中看到了一些非常酷的菜单视觉效果,所以我想知道是否可以使用 Windows 窗体创建类似的效果。
We use Windows Forms and custom user controls, and I would like to be able to rotate the panel hosting the userControl in a particular form. I have seen similar functionnalities with WPF, but I can't use it for the moment. Is it possible to achieve the rotation of a panel and its children using possibly built-in .NET methods or GDI+?
I have seen some pretty cool visual effect with menus that are displayed in game development, so I was wondering if it would be possible to create similar effects using Windows Forms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 窗体中旋转面板及其子面板并不受直接支持,我认为这最终会成为一个令人头痛的问题,很容易占用大量时间。想到什么时候您可以在 WPF 中使用零行 C# 代码和只有一点点 XAML。
Rotating a panel and its children in Windows Forms is not something directly supported, and I think it will end up being a buggy headache that could easily suck up lots of time. It's especially painful to think about when you could do this in WPF with zero lines of C# code and only a tiny bit of XAML.
您可以通过调用 RotateTransformGraphics 对象上的 a> 方法。
然而,旋转整个控件并不那么简单,并且在很大程度上取决于控件的实现方式。
如果它是一个复合 UserControl,其中包含其他控件,那么您就不走运了。
如果它是一个自行绘制的单一控件,请尝试继承该控件,重写
OnPaint
方法,并在 Graphics 对象上调用RotateTransform
。但是,您可能会遇到麻烦。特别是,您可能需要重写所有鼠标事件并使用旋转坐标调用基本控件的事件。You can use rotations in GDI+ by calling the RotateTransform method on a
Graphics
object.However, rotating an entire control is not so simple, and will depend heavily on how the control is implemented.
If it's a composite UserControl that has other controls inside of it, you're out of luck.
If it's a sinlge control that paints itself, try inheriting the control, overriding the
OnPaint
method, and callingRotateTransform
on the Graphics object. However, you will probably have trouble with it. In particular, you will probably need to override all of the mouse events and call the base control's events with rotated coordinates.您可以通过在面板上调用 DrawToBitmap 方法,然后旋转位图并将其显示在 PictureBox 中来实现中途:
将位图绘制到另一个位图中,则也可以使用 90 度增量以外的旋转角度:
如果使用GDI 当然,问题是您只显示面板的图像,而不是面板本身,因此不再可能与内部的控件进行交互。
这也许也可以完成,但是您将不得不处理窗口消息,这会变得更加复杂。根据您的需要,您也许还可以处理 PictureBox 上的单击和按键事件、操作面板中的控件,然后更新图像。
You can get halfway there by calling the DrawToBitmap method on your panel, then rotating the bitmap and displaying it e.g. in a PictureBox:
Rotation angles other than 90-degree increments are also possible, if you draw the bitmap into another bitmap using GDI:
The problem of course is that you're only displaying an image of your panel, and not the panel itself, so it's no longer possible to interact with the controls inside.
That could probably be done as well, but you would have to mess with window messages, which gets quite a bit more complicated. Depending on your needs you might also be able to get away with handling click and key events on the PictureBox, manipulating the controls in the panel, and then updating the image.