限制自定义控件的可调整尺寸 (c# .net) [英] Limit resizable dimensions of a custom control (c# .net)

查看:33
本文介绍了限制自定义控件的可调整尺寸 (c# .net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MS Visual C# 中制作用户控件,但有一件事我找不到答案:

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?

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

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?

我尝试在设计器中为我的控件设置 MinimumSize 和 MaximumSize 值,但这并没有给出理想的结果.

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

推荐答案

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

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.

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

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).

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

现在,就 TrackBar 而言,它拥有自己的设计器,可以覆盖 ControlDesigner.SelectionRules 属性.此属性只是让您返回一个枚举值(它是一个 Flags 枚举,因此您可以将它们 OR 在一起)指示您的设计时选择装饰器如何出现(或不出现).一旦您通过设计器限制了设计时调整大小,就完全取决于您的控件本身来限制其自己的大小 vai SetBoundsCore.

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.

这篇关于限制自定义控件的可调整尺寸 (c# .net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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