简体   繁体   English

在Windows窗体应用程序上停靠和锚定

[英]Docking and Anchoring on a Windows Form application

I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2 . 我正在使用C#.NET Compact Framework 2.0 SP2Windows Mobile 5.0及更高版本开发应用程序。

I have a WinForm with two panels inside (upperPanel and bottomPanel). 我有一个内部有两个面板的WinForm(upperPanel和bottomPanel)。 I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. 我希望upperPanel总是填充表单高度的2/3,而bottomPanel填充表单高度的1/3。 Both panels will fill completly form's width. 两个面板都将填充完整形式的宽度。

I've used this: 我用过这个:

upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;

But upperPanel fills the form completly. 但是upperPanel完整地填充了表单。

How can I do this? 我怎样才能做到这一点? I want, more o less, the same gui on differents form factors and on landscape or protrait mode. 我想要的不仅仅是 不同的形状因素风景或者模式的gui。

Thank you. 谢谢。

What you need to do is to put the bottom panel on first and set its Dock property to Bottom . 您需要做的是先将底部面板放在第一位,然后将其Dock属性设置为Bottom Then set the panel's height to be 1/3 of the form's height. 然后将面板的高度设置为窗体高度的1/3。 Finally, add a second panel and set its Dock property to Fill . 最后,添加第二个面板并将其Dock属性设置为Fill The key here is that you want to add the control that will fill the remaining area to be added last. 这里的关键是你要添加控件,它将填充最后添加的剩余区域。 Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate. 或者,您可以使用Visual Studio中的Bring to Front和Send to Back命令来让设计人员进行合作。

You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. 您可能还需要挂钩表单的OnSizeChanged事件,并重新设置底部面板的高度以考虑布局更改。 It's been a little while since I did compact framework programming, so I'm not sure. 自从我做了紧凑的框架编程以来已经有一段时间了,所以我不确定。

Set both panels to "not anchored". 将两个面板设置为“未锚定”。 That is: Remove Dock-Value and clear the Anchor property. 即:删除Dock-Value并清除Anchor属性。 Then, move the controls so they are sized the way you'd like them to be sized. 然后,移动控件,使它们的大小与您希望它们的大小相同。

After that, upon resizing the form, they should resize relatively. 之后,在调整表单大小时,他们应该相对调整大小。

EDIT 编辑
Oops, just tried it and sure it doesn't work. 哎呀,只是试了一下,确定它不起作用。 I mixed this up with a solution that automatically keeps controls centered within the window... 我把它与一个自动将控件集中在窗口中心的解决方案混合在一起......

Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized. 好吧,我猜你必须为表单的Resize事件创建一个处理程序,并在Resize表单Resize后手动对齐控件。

Go to Tools, Other Windows, Document Outline. 转到工具,其他Windows,文档大纲。 Find the two panels, and swap the order of them. 找到两个面板,并交换它们的顺序。 The control that has DockStyle.Fill has to come first for it to be docked correctly. 具有DockStyle.Fill的控件必须首先使其正确停靠。 (or last.. never sure which one it is, but it is one of them :p) (或者最后......永远不确定它是哪一个,但它是其中之一:p)

This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). 这不会解决总是1/3和2/3的问题,但是......因为底部面板将具有固定的高度(除非我弄错了)。 I think maybe the TableLayoutPanel supports this though... 我想也许TableLayoutPanel支持这个......

Update: As noted in the comments, that panel doesn't exist in the compact framework. 更新:如评论中所述,该小组在紧凑框架中不存在。 So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes. 所以,我想这个问题最简单的解决方案就是尝试使用对接,但只要表单大小发生变化,就会更新底部面板的高度。

Right click on the upperPanel and select Bring To Front. 右键单击upperPanel并选择Bring To Front。 However, I don't think this will give you the result you want. 但是,我不认为这会给你想要的结果。 When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form. 调整大小时,底部面板将保持相同的高度,而上部面板将拉伸以填充表单。

Using your docking settings, with this code might do the trick: 使用您的停靠设置,使用此代码可能会做到这一点:

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);

        this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
    }

If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize. 如果你想让它完美地工作,你需要在Form的Resize事件中添加一些代码,然后专门计算出相对大小,并在调整大小后将控件放在正确的位置。

If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. 如果你不担心失去精确度并且表格不会移动很多,你可以通过使用一些相对聪明的锚点来避免这种情况。 Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). 基本上你将不得不选择一个“种植者”(形式的一部分越大,形式越大)。 In this scenario I would probably anchor the top part to Top | 在这种情况下,我可能会将顶部锚定到Top | Left | 左| Right and the bottom part to Top | 右上角和上半部分 Left | 左| Right | 对| Bottom. 底部。 This would mean that the lower part of the form will get bigger if the form is expanded. 这意味着如果表单扩展,表单的下半部分将变大。 In most cases this is acceptable. 大多数情况下这是可以接受的。 If it isn't use the Resize event and some code. 如果不使用Resize事件和一些代码。

You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps: 1) Add the Form and put a Panel1 on it. 您可以使用嵌套面板以及使用锚定和停靠属性进行少量设置来获得所需的设计。遵循以下步骤:1)添加表单并在其上放置Panel1。 Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'. 将其Dock属性设置为“Fill”,将ResizeMode设置为“Grow&Shrink”。 2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'. 2)添加第二个panel2并将其Dock属性设置为'Bottom',设置高度并将Anchor属性设置为'Top,Left'。 3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'. 3)添加第三个面板并将其Dock属性设置为“None”,设置高度并将Anchor属性设置为“Top,Bottom,Left,Right”。

Save and Compile. 保存并编译。 Now all the panels Would maintain their relative Positioning With resizing. 现在所有面板都将通过调整大小来保持相对定位。

The easiest way to do this is to nest panels. 最简单的方法是嵌套面板。 Just set up panels for top bottom and fill. 只需设置顶部面板和填充面板。 Then use panels within those panels to do the same. 然后使用这些面板中的面板来做同样的事情。 The only issues I've had therein are datagrid resizing, which is always a pain anyway. 我在其中遇到的唯一问题是数据网格调整大小,无论如何总是很痛苦。 in that case, you have to use some code to resize the datagrid control on the form resize event. 在这种情况下,您必须使用一些代码来调整表单resize事件上的datagrid控件。

I would like to add a point to @jasonh answer. 我想补充一点@jasonh的答案。

For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true. 对于占用表单2/3的面板,您必须将面板的AutoScroll属性设置为true。

This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height. 这将使面板在控制尺寸超出用户可见度时显示滚动,并确保较小面板的可见性为形状高度的1/3。

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

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