Windows 窗体应用程序,使用窗体自动缩放控件 [英] Windows forms app, autoscale controls with form

查看:50
本文介绍了Windows 窗体应用程序,使用窗体自动缩放控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手.设计一个可以调整大小的表单,我想让我的文本框、标签和按钮随着表单调整大小,有人能告诉我怎么做吗?

I'm a newbie. Designing a form that can be resized, and I want my textboxes, labels and buttons to resize with the form, can someone tell me how to do this?

推荐答案

这取决于您需要的布局类型.您必须执行的基本工具"包括以下属性:AnchorDock.

It depends on the type of layout you need. The "basic tools" you have to do that are following properties: Anchor and Dock.

使用 Anchor 属性,您可以将元素的一侧附加"到其容器的一侧.例如,如果您在窗口的右下角放置一个按钮并将底部,右侧"设置为锚点,那么当您调整表单大小时,该按钮将保持其相对于该角的位置.现在假设您在表单中放置了一个多行文本框,根据需要调整大小(例如距顶部、左右边框 4 px 和 128 px 高度)并将 Anchor 属性设置为左、上、右".当您调整表单的大小时,该控件将保持其高度但它会调整大小以保持其边距(因此,如果您使表单更宽,其宽度将增加).

With the Anchor property you "attach" a side of an element to a side of its container. For example if you place a button in the bottom-right corner of a window and you set "Bottom, Right" as Anchor then when you'll resize the form the button will keep its relative position to that corner. Now imagine you place a multiline text-box in the form, resize as needed (for example 4 px from top, left and right border and 128 px height) and set the Anchor property to "Left, Top, Right". When you'll resize the form that control will keep its height but it'll resize to keep its margins (so if you'll make the form wider its width will be increased).

码头是不同的.通过停靠,您可以向布局管理器说"以在一个方向上使用所有可用空间.例如,如果您设置为 Left,那么您的控件将保持其宽度,但它将使用所有可用高度,并且其位置将尽可能靠左.您可能有多个控件停靠在一个容器中,假设您有 5 个文本框与顶部停靠在一个窗体中.它们将使用所有宽度(和调整大小)堆叠到表单的顶部.另一个示例:顶部停靠控件(作为横幅)和填充"停靠控件(作为主要内容).请记住,停靠控件的顺序很重要(如果您首先放置填充"控件,它将使用所有可用空间,而顶部"停靠控件将重叠).

Dock is different. With docking you "say" to the Layout Manager to use all available space in one direction. For example if you set to Left then your control will keep its width but it'll use all the available height and its location will be most left as possible. You may have more than one control docked in a container, imagine you have 5 textbox with Top docking inside a form. They'll be stacked to the top of the form using all the width (and resizing). Another example: a Top docked control (as a banner) and a "Fill" docked control (as main content). Remember that with docking the order of controls matters (if you first place the "Fill" control it'll use ALL the available space and the "Top" dock control will overlap).

此外,您还有一些布局控件(表格和堆栈).它们非常易于使用,并且 30 分钟的实验"比长文本更能说明问题.

Moreover you have some layout controls too (tables and stacks). They're really easy to use and a 30 minutes of "experiments" will clarify much better than a long text.

这篇关于Windows 窗体应用程序,使用窗体自动缩放控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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