Visual Studio WinForms 是否支持无窗口控件?

发布于 2024-12-17 02:23:09 字数 960 浏览 2 评论 0原文

Visual Studio WinForms 工具箱中的每个控件都必须源自 Control 吗?

Visual Studio 是否支持无窗口控件?


添加到 Visual Studio 工具箱中的每个控件:

在此处输入图像描述

必须1 源自 Control,它是 windowed< 的包装器/strong> 控制。

不幸的是,窗口控件非常“重”;有很多,尤其是嵌套的,会导致 WinForms 的性能受到影响。

过去我通过创建聚合自定义控件来处理该问题。自定义控件内部包含其他无窗口控件:

  • 图像(PictureBox 的无窗口版本)
  • 标题标签(Label 的无窗口版本)
  • 副标题标签(Label 的无窗口版本 ) Label
  • 边框(Panel的无窗口版本)

这些对于缓解 WinForms 中的性能问题很有用,但它们被困在代码中。

我想做其他开发环境允许的事情,即不创建 Windows 窗口的 Control 版本。我希望 Visual Studio 工具箱能够接受**无窗口*控件。

我知道如果我真的想要无窗口控件:我应该切换到 WPF。但这太过分了。

Visual Studio WinForms 是否支持无窗口控件?

1 或不

Must every control in the Visual Studio WinForms toolbox descend from Control?

Does Visual Studio support window-less controls?


Every control you add to the Toolbox in Visual Studio:

enter image description here

must1 descend from Control, which is a wrapper around a windowed control.

Unfortunately, Windowed controls are very "heavy"; having a lot of them, especially nested, causes performance in WinForms to suffer.

In the past i've dealt with the problem by creating aggregate custom controls. The custom control internally contains other window-less controls:

  • an image (windowless version of a PictureBox)
  • title label (windowless version of a Label)
  • subtitle label (windowless version of a Label)
  • border (windowless version of a Panel)

These are useful to mitigate performance problems in WinForms, but they're stuck inside code.

i would like to do what other development environments allow, is a version of Control that doesn't create a Windows window. i would like the ability for the Visual Studio toolbox to accept **window-less* controls.

i know that if i really wanted window-less controls: i should switch to WPF. But that's overkill.

Does Visual Studio WinForms support window-less controls?

1 or not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-12-24 02:23:09

是和否。

首先,请查看可敬的 Raymond Chen 的这篇文章:http://blogs.msdn.com/b/oldnewthing/archive/2005/02/11/371042.aspx

是的。欢迎您创建不是从 Control 派生的“控件”。我在我的应用程序中创建了几个本机支持单击、分层等的无窗口控件,我将它们绘制到屏幕外缓冲区,然后将它们直接绘制在某些父 FormControl (例如 PictureBox)。这很简单,但并不简单,因为您需要自己用代码管理一切。

否。 对于任何 Control 派生的控件设计器(例如将它们放置在面板或窗体上),Windows 窗体设计器中将不支持任何无窗口控件,因此您不会有拖放表单设计。

正如 Hans 所指出的,ToolStripMenuStrip(无窗口控件)就是这样的例子。请注意,当您在 Form 上创建新的 MenuStrip 时,MenuStrip 会放置在窗体下方的组件托盘中。 MenuStrip 有一组与其关联的自定义设计器类,以支持自定义绘制和“在此处键入”功能,以及用于添加和删除菜单项的对话框。请注意,“子”无窗口控件(例如 ToolStripButton)在工具箱中不提供直接拖放到表单上的支持 - 只有自定义设计器知道这一点。 MenuStrip 的自定义设计器还支持选择子无窗口控件,以便您可以在“属性”窗口中编辑每个项目的属性。

我无法想象这适合您的情况(除非您正在创建一些用于转售的控件),但如果您非常坚定,您可以以大致相同的方式为您的一组创建设计器支持无窗口控件:

  1. 创建一个派生自 Component 的类,用于管理无窗口控件。例如,您可以调用此类WindowlessWidgetManager。编译后,该控件将位于您的工具箱中。 WindowlessWidgetManager 可以包含无窗口控件的集合,并为画布(例如 FormPictureBox)提供绘画和其他 UI 支持。
  2. 创建一个派生自 ComponentDesigner 的设计器类,该类支持在设计时添加和删除自定义控件等操作。有关详细信息,请参阅 http ://msdn.microsoft.com/en-us/library/system.componentmodel.design.componentdesigner(v=VS.90).aspx

这是一个非常漫长的过程,有许多注意事项,但如果就是你想实现的,功能就有了。

Yes and No.

First, check out this article from the venerable Raymond Chen: http://blogs.msdn.com/b/oldnewthing/archive/2005/02/11/371042.aspx

Yes. You are welcome to create "controls" that do not derive from Control. I have created several windowless controls in my application that natively support clicking, layering, etc., I draw them to an offscreen buffer, and then paint them directly on some parent Form or Control (such as a PictureBox). This is straightforward to do but not simple, as you will need to manage everything yourself in code.

No. Any windowless controls will not be supported in the Windows Forms designer for any of the Control-derived controls designers (such as placing them on a Panel or Form) so you won't have drag-and-drop form design.

As Hans has pointed out, the ToolStrip and MenuStrip (a windowless control) are such examples. Notice that when you create a new MenuStrip on a Form, the MenuStrip is placed in the Component tray underneath the form. The MenuStrip has a custom set of Designer classes associated with it to support the custom painting and "Type Here" functionality, as well as the dialog boxes to add and remove menu items. Note that the "child" windowless controls, such as the ToolStripButton, are not available in the ToolBox for drag-and-drop support directly onto the form - only the custom designer knows about it. The custom designer for the MenuStrip also supports selecting the child windowless controls so that you can edit the properties of each item in the Properties window.

I can't imagine this is suitable for your situation (unless you are creating some controls for resale), but if you are very determined, you can create designer support in much the same way for your set of windowless controls:

  1. Create a class that derives from Component that will be used to manage your Windowless controls. For example, you could call this class WindowlessWidgetManager. After you compile, this control will be in your toolbox. The WindowlessWidgetManager can contain a collection of your windowless controls, and provide painting and other UI support for a canvas such as a Form or PictureBox.
  2. Create a designer class that derives from ComponentDesigner that supports things such adding and removing your custom controls at design time. For more information, see http://msdn.microsoft.com/en-us/library/system.componentmodel.design.componentdesigner(v=VS.90).aspx

This is a very lengthy process with a number of caveats, but if that is what you wish to achieve, the functionality is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文