简体   繁体   English

如何为表单设置“首选大小”?

[英]How do I set a “preferred size” for a form?

I have some controls on a form, and I have them anchored to the left and right of the form to ensure they grow and shrink with the form. 我在表单上有一些控件,我将它们固定在表单的左侧和右侧,以确保它们随表单增长和缩小。 My form is also set to automatically grow and shrink in order to accommodate some labels that may get long strings. 我的表单也设置为自动增长和缩小,以适应可能获得长字符串的一些标签。 When those labels get their long strings removed, I want my form to go back to the width it was at before. 当这些标签移除长字符串时,我希望我的表单恢复到之前的宽度。 Said controls which are anchored to the left and right on my form appear to be preventing this. 在我的表格上左右固定的所述控件似乎阻止了这种情况。 Can I make it work as I intend? 我可以按照我的意图使它工作吗?

Here's a way that doesn't require you to write any sizing specific code... but it assumes all your buttons are at the bottom. 这是一种不需要你编写任何特定尺寸代码的方法......但它假设你的所有按钮都在底部。

Step 1. Set a minimum size for the dialog. 步骤1.设置对话框的最小大小。

Step 2. Create a panel on the form. 步骤2.在表单上创建一个面板。

Step 3. Move your buttons into the panel. 步骤3.将按钮移动到面板中。

Step 4. Dock the panel to the bottom of the form. 步骤4.将面板停靠在表单底部。

Step 5. Ensure that the dialogs AutoSizeMode is set to ShrinkAndGrow 步骤5.确保AutoSizeMode对话框设置为ShrinkAndGrow

You can dock panels to any of the left,right,top and bottom borders with this technique. 您可以使用此技术将面板停靠到左,右,顶部和底部边框中的任何一个。 and have the dialog resize appropriately. 并使对话框适当调整大小。

Can I suggest you not do this? 我可以建议你要这样做吗? I know you're trying to be helpful for your users, but having a window shrinking randomly (it will likely seem random to the user) is going to be confusing and perhaps frustrating. 我知道你正试图为你的用户提供帮助,但是随机缩小窗口(对用户来说可能是随机的)会让人感到困惑,也许会令人沮丧。 This is probably even more true with the new Windows 7 UI feature Aero Snap ie you dock the window to the left and then it resizes on you. 使用新的Windows 7 UI功能Aero Snap可能更是如此,即您将窗口停靠在左侧,然后调整大小。

Consider the window size a user preference. 考虑用户偏好的窗口大小。 The user has already told you how big they want the thing to be, respect it! 用户已经告诉过你他们想要的东西有多大,尊重它!

Edit: I should also add that there is a bug if you do go down this route. 编辑:我还应该补充一点,如果你沿着这条路走下去就有一个bug。 Consider the form being 1000 pixes wide and the user drags it to (-500, 100) to show it half on their monitor. 考虑到1000像素宽的形式,用户将其拖动到(-500,100)以在显示器上显示一半。 If they hit a button that's visible (or worse, something happens in the background!) and the form resizes down to 450 pixels the window has just disappeared! 如果他们点击了一个可见的按钮(或者更糟糕的是,某些事情发生在背景中!)并且表单大小调整到450像素,那么窗口就会消失! The user sees it in the taskbar, but can't get to it unless they know some fancy alt+space shortcuts. 用户在任务栏中看到它,但除非他们知道一些花哨的alt +空格快捷方式,否则无法访问它。

One suggestion would be to hook the OnPaint event. 一个建议是挂钩OnPaint事件。 Your controls are built by this point and will have their initial location values set on the form. 您的控件是由此点构建的,并且将在表单上设置其初始位置值。 Capture everything you want into an object set private at the form level. 将您想要的所有内容捕获到表单级别的私有对象集中。

During an OnStringsRemoved event, read from your object and set everything back the way it was. 在OnStringsRemoved事件期间,从对象中读取并按原样设置所有内容。 Modifying a control that's anchored should trigger the form resize but it may be that you have to set everything manually. 修改锚定的控件应该触发表单调整大小,但可能是您必须手动设置所有内容。 It may also be that modifying the form will shrink the controls and not the other way around. 也许修改表单会缩小控件而不是相反。

An alternative suggestion would be to just hardcode your initial values and go back to them every time. 另一种建议是硬编码您的初始值并每次都返回它们。 I personally would opt for OnPaint. 我个人会选择OnPaint。 It'll help you handle things like Large Fonts bumping up control size or anything the framework does before the user sees it. 它可以帮助您处理大字体增加控件大小或框架在用户看到之前执行的任何操作。

use System.Drawing.Graphics.MeasureString 使用System.Drawing.Graphics.MeasureString

    public SizeF MeasureString(string text, Font font, int width);

text=yourtext; 文本= yourtext; font=fontofthelabel; 字体= fontofthelabel; width=maxwidthoflabel; 宽度= maxwidthoflabel;

MeasureString in MSDN MSDN中的MeasureString

you can google for more 你可以谷歌更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM