限制自定义控件的可调整大小(c#.net)(Limit resizable dimensions of a custom control (c# .net))

编程入门 行业动态 更新时间:2024-10-13 18:20:08
限制自定义控件的可调整大小(c#.net)(Limit resizable dimensions of a custom control (c# .net))

我在MS Visual C#中创建一个用户控件,有一件事我找不到答案:

如何在设计视图中限制控件可以调整大小的尺寸?

对于我要问的一个明显的例子,内置的TrackBar控件只能变得更宽,而不是更高,并且只在设计模式中左右显示调整大小的方块。 有没有办法为用户控件复制这个?

我已尝试在设计器中为我的控件设置MinimumSize和MaximumSize值,但这不会给出理想的结果。

I am making a user control in MS Visual C#, and there is one thing that I just can't find an answer to:

How do I limit which dimensions a control can be resized in during design view?

For a clear example of what I'm asking, the built in TrackBar control can only only be made wider, not taller, and only displays the resizing squares on the left and right in design mode. Is there a way to replicate this for a user control?

I have tried setting MinimumSize and MaximumSize values in the designer for my control, but this doesn't give ideal results.

最满意答案

要获得您正在谈论的完整行为(顶部/底部或左/右没有装饰者)和设计时环境中的自定义功能,您可能不得不求助于为您的控件构建自定义控件设计器。

这是一个很大的话题,因为你可以做很多事情。 实际上,您要创建一个继承自ControlDesigner的类,覆盖您需要的任何功能,然后使用DesignerAttribute将其注册到您的用户控件上,为第二个参数指定typeof(IDesigner) (以及您的自定义ControlDesigner派生类型)首先)。

增强设计时支持 定制设计师 ControlDesigner类示例 Visual Studio .NET中的自定义设计时控件功能

现在,在TrackBar的情况下,它有自己的设计器来覆盖ControlDesigner.SelectionRules属性。 此属性只是让您返回一个枚举值(它是一个Flags枚举,因此您可以将它们组合在一起),指示您的设计时选择装饰器如何显示(或不显示)。 一旦你通过一个设计师限制了设计时的大小调整,你只需控制自己就可以限制它自己的大小vai SetBoundsCore 。

To get the full behavior you're talking about (no adorners on top/bottom or left/right) and custom functionality inside the design time environment, you'll probably have to resort to building a custom control designer for your control.

This is a huge topic, as there are a lot of things you can do. Effectively what you'd do is create a class that inherits from ControlDesigner, override whatever functionality you need, then register it on your user control with the DesignerAttribute, specifying typeof(IDesigner) for the 2nd parameter (and your custom ControlDesigner-derived type for the first).

Enhancing Design-time Support Custom Designers ControlDesigner class example Custom Design-time Control Features in Visual Studio .NET

Now, in the case of TrackBar, it has its own designer that overrides the ControlDesigner.SelectionRules property. This property simply lets you return an enumeration value (it's a Flags enum, so you can OR them together) indicating how your design-time selection adorners appear (or not appear). Once you've restricted design-time resizing via a designer, it's simply up to your control itself to constrain its own size vai SetBoundsCore.

更多推荐

本文发布于:2023-08-03 07:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   可调整   大小   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!