WinForms AutoScroll和AutoSize [英] WinForms AutoScroll and AutoSize

查看:103
本文介绍了WinForms AutoScroll和AutoSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当窗口小到该尺寸时,是否有任何方法可以使用滚动条来实现控件的最小尺寸,并且如果窗口大于最小尺寸,同时具有该控件的自动尺寸?
锚定适用于将大小调整为更大但不较小的情况(不显示自动滚动),停靠可禁用自动滚动条.

我发现它的唯一方法是将控件停靠在父级中,并在背景中以固定大小添加不可见的面板,并将父级上的自动滚动设置为true.当用户将窗口的大小调整为小于隐藏面板滚动条的固定大小时,出现.如果窗口的尺寸更大,则停靠的控件将调整为父级的尺寸.

有更好的非黑客"解决方案吗?

Is there any way to achieve minimum size of control with scroll bar when window is to small for that size AND at the same time have autosize of that control if window is greater than minimum size?
Anchors works great for resizing to greater size but not smaller (auto scroll don''t appear), docking disables auto scroll bars.

Only way I have found it''s to dock controls in parent, and add non visible panel in background with fixed size, and set auto scroll on parent to true. Than when user resize window to size smaller than fixed size of hidden panel scroll bars appears. Whe size of window is greater, docked controls resize to parent size.

Is there better ''non-hacking'' solution?

推荐答案

您的解决方案实际上是绝对不需要的黑客".

真正的解决方案是子对象和父面板的属性的组合,该属性应该是从System.Windows.Forms.ScrollableControl派生的面板类型,例如"regular" System.Windows.Forms.Panel.

首先,这是不正确的,因为您不能将Dock与孩子一起使用.您可以使用DockAnchor,我建议您最好使用对接,因为锚定在开发和支持中更加手动,并且更可能导致闪烁.因此,我将展示具有对接功能的代码示例.我只关心水平尺寸:

Your solution is really a "hack" which is absolutely not needed.

The real solution is a combination of properties of the child object and a parent panel, which should be of the the panel type derived from System.Windows.Forms.ScrollableControl, such as "regular" System.Windows.Forms.Panel.

First of all, this is not true that you cannot use Dock with chilren. You can use either Dock or Anchor, and I would advise to prefer docking, as anchoring is more manual in development and support and more likely causes flicker. So, I will show the code sample with docking. I will take care only about horizontal dimension:

namespace AutoScrollApplication {
    using System.Windows.Forms;

    public class AutoScrollForm : Form {
        
        internal AutoScrollForm() {
            Panel pad = new Panel();
            pad.Dock = DockStyle.Fill;
            this.Controls.Add(pad);
            TextBox textBox = new TextBox();
            textBox.Dock = DockStyle.Top;
            pad.Controls.Add(textBox);
            pad.AutoScroll = true;
            // now, select 300 for limiting factor;
            // I hope in real code in won't be an immediate constant I put just for illustration:
            pad.AutoScrollMinSize = new System.Drawing.Size(300, 0);
        } //AutoScrollForm
    
        [System.STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new AutoScrollForm());
        } //Main

    } //class AutoScrollForm

} //namespace AutoScrollApplication



为了演示的目的,我避免使用设计器,而是将所有应用程序放在一个文件中.如果您构建它,它将显示您想要的确切效果.

就这样.



请参阅有关我为说明该技术而提出的不合适的立即常数 300的警告.通过使用当前的窗口大小,您可以做得更好.为此,请使用例如:



For the purpose of the demo, I avoided using the designer and put all the application in just one file. If you build it, it will show the exact effect you wanted.

That''s it.



Please see my warning about the inappropriate immediate constant 300 I put just for illustration of the technique. You could do better by using the current size of the window. For this purpose, use, for example:

pad.AutoScrollMinSize =
                new System.Drawing.Size(this.ClientRectangle.Width, 0);

在这种情况下,将在两种模式之间的边界上精确设置滚动:第一次显示窗口时,滚动条不可见.从这一点来看,如果您将窗体变宽,则子级TextBox的宽度将增大,如果您使它变窄,则将出现滚动条.

那不是很好吗?


祝你好运,

—SA

In this case, the scrolling will be set exactly on the boundary between two modes: when a window is first shown, the scroll bar is not visible. From this point, if you make your form wider, the child TextBox will grow in width, if you make if narrower, the scroll bar appears.

Isn''t that nice?


Good luck,

—SA


这篇关于WinForms AutoScroll和AutoSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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