C#语言 第四部分 图形界面编程(五) 布局容器类(4)

6 分隔容器类

分隔容器(SplitContainer)由分隔线和两个面板容器组成,可以通过分隔线将容器所在区域分为两个部分(左右或上下),每个部分里面有一个面板容器(Panel容器类),可以放置其它控件。

通过鼠标拖动分隔线,可以改变容器两个区域的尺寸。

分隔线可以通过SplitterWidth 属性控制,例如:

这里的splitPane是一个SplitContainer类型字段,表示一个分隔容器。上述代码将其分隔线宽度设置为2个单位。

SplitContainer容器的两个面板分别为Panel1属性Panel2属性,也成为1号面板和2号面板。正常情况下,1号面板在左边(分隔线垂直)或者1号面板在上方(分隔线水平),参看示意图:

垂直分隔示意图 水平分割示意图

图1 分隔容器分隔方向示意图

通过SplitContainer类对象的Orientation属性可以设置分隔容器分隔线的方向,例如:

通过SplitContainer类对象Panel1(或者Panel2)属性得到一个Panel类型对象引用,通过其Controls属性的Add方法可以将其它控件增加在这两个面板之一上。从而将控件放置在SplitContainer容器的左边(上边)或右边(下边),例如:

这里的textLabel是一个Label类型字段,表示任意控件。

通过SplitContainer对象的SplitterDistance属性可以控制分隔线和其起始位置的距离(对于分隔线垂直,起始位置在容器最左边;对于分隔线水平,起始位置在容器最顶端),例如:

上述代码设置分隔线距离起始位置120个单位。

通过SplitContainer对象的Panel1MinSize属性可以控制1号面板的最小宽度(对于分隔线垂直)或高度(对于分隔线水平),即拖动分隔线可以使1号面板成为德最小尺寸;同理Panel2MinSize用于设置2号面板,例如:

设置1号面板最小尺寸(宽度或高度,根据分隔线方向决定)为50个单位。

通过SplitContainer对象的Panel1Collapsed属性,可以将1号面板收起(属性值为true)或展开(属性值为false);同理,Panel2Collapsed属性用于2号面板,例如:

将1号面板收起或展开(根据面板的当前状态决定)。

一般来说,分隔容器用于显示侧边栏底边栏,就像我们使用的Visual Studio一样。在不需要的时候收起,并可以随时调整尺寸。

下面通过一个例子来展示SplitContainer的具体使用:

界面布局如下:

界面布局示意图
图3 界面布局示意图

为了让两个按钮放置的更为合理,使用了一个流式布局面板(flowPane字段),将按钮放在流式面板上,再把流式面板放在分隔容器的2号面板中。

代码如下:

Program.cs

本节代码下载

在代码中,通过“切换分隔方向”按钮来切换分隔线的方向(164-200行),切换分隔线方向很简单,设置其Orientation属性即可,但要注意,切换分隔线方向后,要对两个面板内控件的布局方向做必要的调整,因为两个面板的放置方向改变了;

代码中使用了两种方式来收起面板,普通方式和动画方式,普通方式很简单,直接设置Panel1Collapsed(或Panel2Collapsed)属性即可(第243行);动画方式是通过一个定时器控件(即定时呼叫事件委托方法的控件)不断定时改变面板的SplitterDistance属性(收起为减小属性值,展开为增加属性值),直到SplitterDistance属性值达到预定大小(253-288行);注意,SplitterDistance属性不能为负数,代码261行和277行保证了这一点。

定时器在固定时间间隔后呼叫某个委托函数一次,并一直重复直到定时器被关闭。其Interval属性用于设定时间间隔(即多久呼叫委托函数一次,第99行),Tick事件用于指定呼叫的委托函数(第100行),使用Start方法可以启动定时器(第234行),Stop方法可以停止(也可以说是关闭)定时器(第267行)。定时器必须可以在适当的时候被关闭,本例中,如果分隔线达到指定距离后,定时器被关闭。

This Wrox Blox teaches you how to add graphics to C# 2008 applications, explaining fundamental graphics techniques such as: drawing shapes with different colors and line styles; filling areas with colors, gradients, and patterns; drawing text that is properly aligned, sized, and clipped exactly where you want it; manipulating images and saving results in bitmap, JPEG, and other types of files. Also covered are instructions for how to greatly increase your graphics capabilities using transformations. Transformations allow you to move, stretch, or rotate graphics. They also let you work in coordinate systems that make sense for your application. You will also learn how to use all of these techniques in printouts. The author describes the sequence of events that produce a printout and shows how to generate and preview printouts. The final sections describe two powerful new graphic tools that were introduced with .NET Framework 3.0: WPF graphics and FlowDocuments. WPF applications can use XAML graphic commands to declaratively draw and fill the same kinds of shapes that a program can draw by using graphics objects. Finally, a discussion on the FlowDocument object shows you how to define items that should be flowed across multiple pages as space permits. This lets you display text, graphics, controls, and other items that automatically flow across page breaks. FlowDocument viewers make displaying these documents easy for you, and simplifies the user's reading of the documents. This Wrox Blox also contains 35 example programs written in C# 2008, although most of the code works in previous versions of C# as well. The most notable exceptions are WPF graphics and FlowDocuments, both of which require WPF provided in .NET Framework 3.0 and later.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值