设计时自定义控制控件大小和移动
2011-01-28 18:21
531 查看
做一个类似于TextBox一样在设计时只能调整位置和宽度,不能修改它的高度,请使用下面的方法:
假定我的控件名称为PopupCheckedList,高度只能是21像素。
首先,需要与控件对应的一个Desinger类,名为<PopupCheckedListDesigner>,代码如下:
第三部,在构造函数中,将高度修改为默认的21.
注意,一定要在InitializeComponent函数调用之后。
这样已经OK了,我们看看在设计时,控件的选中状态吧。
这个控件在选中时,就没有可以上下拖动的锚点,一看就让使用着知道高度是不可更改的。
当然手工代码还是可以修改这个控件的高度,比如修改Control.height属性,那怎么办哪?
你就需要额外的写点东西了,第一覆盖常用的Height属性和Size属性,使用自定义的代码逻辑,禁止外部修改它的高度。
比如下面的代码段,去掉了Height的Set方法。
同样,还需要在Resize事件中进行判断,如果高度不是21的话,就强制修改为21,以防止其他方式修改了控件的Height。
假定我的控件名称为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。
相关文章推荐
- Android UI设计之<十二>自定义View,实现绚丽的字体大小控制控件FontSliderBar
- 小猫统计定制CRUD(增查删改)界面:界面内元素(控件)的位置移动、大小控制
- WPF:从WPF Diagram Designer Part 1学习控件模板、移动、改变大小和旋转
- 主界面设计+自定义按钮实现tab控件功能
- IOS-26-自定义Item控件在横竖屏运行时大小位置紊乱
- 【分享】如何设计WinForm中DataGridView控件的自定义按钮列
- android自定义Dialog大小控制
- android之4.0控件switch自定义开关背景图片和控制宽度
- Winform鼠标拖动改变控件大小、移动控件位置
- 轻松加精确完成水晶报表对象的对齐、移动及大小调整——使用准线设计
- event 事件 自定义滚动条 控制文字大小 透明度
- Android 自定义 一个可以控制子控件是否可以点击的layout
- 【Android自定义View实战】之自定义评价打分控件RatingBar,可以自定义星星大小和间距
- Android_自定义控件之设置文本的大小
- 【分享】如何设计WinForm中DataGridView控件的自定义按钮列
- 使用鼠标拖曳的方式移动、更改panel控件的大小
- android绘制自定义view时,控件大小的测量与确定问题,代码演示
- 移动产品交互设计-屏幕大小
- C#鼠标控制控件移动的示例
- 实现控件的移动,改变大小(DELPHI实现)