您的位置:首页 > 其它

设计时自定义控制控件大小和移动

2011-01-28 18:21 531 查看
做一个类似于TextBox一样在设计时只能调整位置和宽度,不能修改它的高度,请使用下面的方法:

假定我的控件名称为PopupCheckedList,高度只能是21像素。

首先,需要与控件对应的一个Desinger类,名为<PopupCheckedListDesigner>,代码如下:

publicclassPopupCheckedListDesigner:System.Windows.Forms.Design.ControlDesigner
{
publicoverrideSystem.Windows.Forms.Design.SelectionRulesSelectionRules
{
get
{
returnSystem.Windows.Forms.Design.SelectionRules.LeftSizeable|
System.Windows.Forms.Design.SelectionRules.RightSizeable
|System.Windows.Forms.Design.SelectionRules.Moveable;
}
}

}


这里面的SelectionRules就是控制了设计时可以执行的选中操作,定义如下,大家一看就明白了:

//Summary:
//Definesidentifiersthatareusedtoindicateselectionrulesforacomponent.
[Flags]
publicenumSelectionRules
{
//Summary:
//Indicatesthecomponentislockedtoitscontainer.OverridestheSystem.Windows.Forms.Design.SelectionRules.Moveable,
//System.Windows.Forms.Design.SelectionRules.AllSizeable,System.Windows.Forms.Design.SelectionRules.BottomSizeable,
//System.Windows.Forms.Design.SelectionRules.LeftSizeable,System.Windows.Forms.Design.SelectionRules.RightSizeable,
//andSystem.Windows.Forms.Design.SelectionRules.TopSizeablebitflagsofthis
//enumeration.
Locked=-2147483648,
//
//Summary:
//Indicatesnospecialselectionattributes.
None=0,
//
//Summary:
//Indicatesthecomponentsupportsresizefromthetop.
TopSizeable=1,
//
//Summary:
//Indicatesthecomponentsupportsresizefromthebottom.
BottomSizeable=2,
//
//Summary:
//Indicatesthecomponentsupportsresizefromtheleft.
LeftSizeable=4,
//
//Summary:
//Indicatesthecomponentsupportsresizefromtheright.
RightSizeable=8,
//
//Summary:
//Indicatesthecomponentsupportssizinginalldirections.
AllSizeable=15,
//
//Summary:
//Indicatesthecomponentsupportsalocationpropertythatallowsittobe
//movedonthescreen.
Moveable=268435456,
//
//Summary:
//Indicatesthecomponenthassomeformofvisibleuserinterfaceandtheselection
//serviceisdrawingaselectionborderaroundthisuserinterface.Ifaselected
//componenthasthisruleset,youcanassumethatthecomponentimplements
//System.ComponentModel.IComponentandthatitisassociatedwithacorresponding
//designerinstance.
Visible=1073741824,
}

第二步,对控件类应用特性标记,标识它的Designer类。

[DesignerAttribute(typeof(PopupCheckedListDesigner))]
publicpartialclassPopupCheckedList:DevExpress.XtraEditors.XtraUserControl


第三部,在构造函数中,将高度修改为默认的21.

publicPopupCheckedList()
{
InitializeComponent();
base.Height=popupContainerEdit1.Height;
base.Width=150;
this.checkedListBoxControl1.ItemCheck+=newDevExpress.XtraEditors.Controls.ItemCheckEventHandler(checkedListBoxControl1_ItemCheck);
}


注意,一定要在InitializeComponent函数调用之后。

这样已经OK了,我们看看在设计时,控件的选中状态吧。





这个控件在选中时,就没有可以上下拖动的锚点,一看就让使用着知道高度是不可更改的。

当然手工代码还是可以修改这个控件的高度,比如修改Control.height属性,那怎么办哪?

你就需要额外的写点东西了,第一覆盖常用的Height属性和Size属性,使用自定义的代码逻辑,禁止外部修改它的高度。

比如下面的代码段,去掉了Height的Set方法。

publicnewintHeight
{
get{returnbase.Height;}
}


同样,还需要在Resize事件中进行判断,如果高度不是21的话,就强制修改为21,以防止其他方式修改了控件的Height。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: