您的位置:首页 > 其它

向ToolStrip、MenuStrip、StatusStrip中添加自定义控件

2012-06-13 21:26 691 查看
新版framework引入了ToolStrip(代替ToolBar)、MenuStrip(代替MenuBar)、StatusStrip(代替StatusBar)这些新控件,虽然旧控件还被支持,但也只是出于兼容性的考虑。

新事物多半会伴随着新问题:这些XxxStrip提供了能够了ToolStripButton、ToolStripLabel、ToolStripDropDownItem等子项,使用它们可以满足大部分的功能需求,剩下的小部分可能需要通过自定义控件来实现了。想要将自定义控件加入到这些XxxStrip中,使用老办法显示是行不通了,下面就进入正题吧。

为了满足上述要求,framework提供了ToolStripControlHost类,它可以承载其他控件,包括两种方式的自定义控件:

1. 使用从 Control 派生的类构造一个 ToolStripControlHost。 要对寄宿的控件和属性进行完全访问,必须重新将 Control 属性强制转换为它所代表的实际类。

2. 扩展 ToolStripControlHost,然后在继承类的默认构造函数中调用基类构造函数,以传递从 Control 派生的类。 此选项允许您对公共控件方法和属性进行包装,以使 ToolStrip 中的访问更加容易。

上面这两条听起来有点晦涩(嗯,很正常,因为这是MSDN的原文 -_-!),那就让我们来看代码吧。

下面是实现一个可以加入到XxxStrip中的自定义月份牌,需要注意的是,在该类的构造函数中调用了ToolStripControlHost构造,并传入了framework自带的MonthCalendar控件;在重载的OnSubscribeControlEvents函数中,对MonthCalendar控件的事件进行处理。

//Declare a class that inherits from ToolStripControlHost.

public class ToolStripMonthCalendar : ToolStripControlHost

{

// Call the base constructor passing in a MonthCalendar instance.

public ToolStripMonthCalendar() : base (new MonthCalendar()) { }

public MonthCalendar MonthCalendarControl

{

get

{

return Control as MonthCalendar;

}

}

// Expose the MonthCalendar.FirstDayOfWeek as a property.

public Day FirstDayOfWeek

{

get

{

return MonthCalendarControl.FirstDayOfWeek;

}

set { value = MonthCalendarControl.FirstDayOfWeek; }

}

// Expose the AddBoldedDate method.

public void AddBoldedDate(DateTime dateToBold)

{

MonthCalendarControl.AddBoldedDate(dateToBold);

}

// Subscribe and unsubscribe the control events you wish to expose.

protected override void OnSubscribeControlEvents(Control c)

{

// Call the base so the base events are connected.

base.OnSubscribeControlEvents(c);

// Cast the control to a MonthCalendar control.

MonthCalendar monthCalendarControl = (MonthCalendar) c;

// Add the event.

monthCalendarControl.DateChanged +=

new DateRangeEventHandler(OnDateChanged);

}

protected override void OnUnsubscribeControlEvents(Control c)

{

// Call the base method so the basic events are unsubscribed.

base.OnUnsubscribeControlEvents(c);

// Cast the control to a MonthCalendar control.

MonthCalendar monthCalendarControl = (MonthCalendar) c;

// Remove the event.

monthCalendarControl.DateChanged -=

new DateRangeEventHandler(OnDateChanged);

}

// Declare the DateChanged event.

public event DateRangeEventHandler DateChanged;

// Raise the DateChanged event.

private void OnDateChanged(object sender, DateRangeEventArgs e)

{

if (DateChanged != null)

{

DateChanged(this, e);

}

}

}

MSDN到这里就结束了,但在使用过程中,我发现了另一个小问题:用户控件(通过各种已有控件进行组装而成,而不是继承某个控件再对其扩展)中使用Dock属性引发的问题,go on.

假设用户控件中有几个控件,其中一个控件A的Dock属性设置为Fill,那么运行程序后,界面上将看不到控件A,解决办法是:将用户控件中的所有控件都放入一个Panel控件B中(不能设置B的Dock属性为Fill,这将使整个用户控件消失,其实ToolStripControlHost在被加到XxxStrip中时,会自动Fill),然后将B的大小设置的与用户控件相同,最后设置B的Anchor属性为上下左右即可,在B中的控件无论怎样设置Dock属性都不会有问题了。

注:以上结论在StatusStrip中实验通过。

最后,good luck for everyone!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: