ComBobox、TextBox控件扩展(Enable=false,灰色)


Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace pcd.Tools


{
public class TextBoxEnable : System.Windows.Forms.TextBox

{

构造函数#region 构造函数
public TextBoxEnable()

{
this.BorderStyle = BorderStyle.FixedSingle;
}
#endregion

//重写OnEnabledChanged
protected override void OnEnabledChanged(EventArgs e)

{
if (Enabled == false)

{
SetStyle(ControlStyles.UserPaint, true);
}
else

{
SetStyle(ControlStyles.UserPaint, false);
this.Font = new Font(“宋体“, 9F);
}
base.OnEnabledChanged(e);
}
//重写OnPaint
protected override void OnPaint(PaintEventArgs pe)

{
base.OnPaint(pe);
if (Enabled == false)

{
pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
pe.ClipRectangle);
//文字描画
float x = 0, y = 0;
Size s = pe.Graphics.MeasureString(Text, Font).ToSize();
if (this.TextAlign == HorizontalAlignment.Left)

{
x = 1;//←
}
else if (this.TextAlign == HorizontalAlignment.Right)

{
x = Width – s.Width;//→
}
else if (this.TextAlign == HorizontalAlignment.Center)

{
x = (Width – s.Width) / 2;
}
y = 5.5F;
if (this.Multiline)
pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, new RectangleF(x, y, this.Width, this.Height));
else
pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, new RectangleF(x, y, 1000, this.Height));//单行是,width可以设置成无限大。
pe.Graphics.DrawRectangle(new Pen(Color.Black, 2), new Rectangle(0, 0, this.Width, this.Height));//pe.ClipRectangle

}
}
}
}


Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace pcd.Tools


{
// 2.声明一个委托。
public delegate void ReadOnlyEventHandler(object sender, ReadOnlyEventArgs args);
public class ComboboxReadOnly : ComboBox

{

ReadOnly#region ReadOnly
private bool _ReadOnle;
[Bindable(true),
Category(“Data“),
DefaultValue(false),
Description(“获取或者设置是否只读“),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool ReadOnly

{
get

{
return this._ReadOnle;
}
set

{

ReadOnlyEventArgs roea = new ReadOnlyEventArgs(this._ReadOnle != value);
this._ReadOnle = value;
OnReadOnlyChanged(roea);
}
}
#endregion


定义事件readonlychanged#region 定义事件readonlychanged
// 3.定义事件
[
Description(“在更改控件的只读状态时发生。“),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public event ReadOnlyEventHandler readonlychanged;
// 4.事件发生后,通知其他对象的方法
//这个方法是受保护的虚方法
protected virtual void OnReadOnlyChanged(ReadOnlyEventArgs args)

{

if (ReadOnly)

{
SetStyle(ControlStyles.UserPaint, true);
}
else

{
SetStyle(ControlStyles.UserPaint, false);
this.Font = new Font(“宋体“, 9F);
}

if (readonlychanged != null)

{
readonlychanged(this, args);
}
//***
Invalidate();//强制一个控件触发onPaint事件
}
#endregion


构造函数#region 构造函数
public ComboboxReadOnly()

{
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.TabStop = false;
}
#endregion


override#region override
//重写WndProc
protected override void WndProc(ref Message m)

{
if (this._ReadOnle && (m.Msg == 0xa1 || m.Msg == 0x201 || m.Msg == 0x203))

{
return;
}
base.WndProc(ref m);
}

//重写OnPaint
protected override void OnPaint(PaintEventArgs pe)

{
base.OnPaint(pe);
if (this.ReadOnly)

{
pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
pe.ClipRectangle);
//文字描画
int x = 0, y = 0;
Size s = pe.Graphics.MeasureString(Text, Font).ToSize();
y = (Height – s.Height) / 2;
x = 1;//←
pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, x, y);
pe.Graphics.DrawRectangle(new Pen(Color.Black, 2), new Rectangle(0, 0, this.Width, this.Height));//pe.ClipRectangle
}
}


重写OnEnabledChanged也可以。不过用readonly更好#region 重写OnEnabledChanged也可以。不过用readonly更好
//重写OnEnabledChanged
//protected override void OnEnabledChanged(EventArgs e)
//{
// if (Enabled == false)
// {
// SetStyle(ControlStyles.UserPaint, true);
// }
// else
// {
// SetStyle(ControlStyles.UserPaint, false);
// }
// base.OnEnabledChanged(e);
//}

//重写OnPaint
//protected override void OnPaint(PaintEventArgs pe)
//{
//base.OnPaint(pe);
//if (Enabled == false)
//{

// pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
// pe.ClipRectangle);
// //文字描画
// int x = 0, y = 0;
// Size s = pe.Graphics.MeasureString(Text, Font).ToSize();
// // x = Width – s.Width;//→
// y = (Height – s.Height) / 2;
// x = 1;//←
// pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, x, y);
// pe.Graphics.DrawRectangle(new Pen(Color.Black, 2), pe.ClipRectangle);

//}
// }
#endregion
#endregion
}


1.定义一个携带事件信息的类。#region 1.定义一个携带事件信息的类。
public class ReadOnlyEventArgs : EventArgs

{
private bool readvalue;
public bool ReadValue

{

get
{ return readvalue; }
}
public ReadOnlyEventArgs(bool readonlyvalue)

{
this.readvalue = readonlyvalue;
}

}
#endregion
}

注:
1.ComBoboxReadOnly控件主要是在ComBobox基础上扩展了ReadOnly属性、OnReadOnlyChanged事件。
2.通过这两个控件可以熟悉控件开发,掌握自定义控件属性、事件的处理。
3.控件源码如下:
TextBoxEnable.cs


Code

























































































ComBoboxReadOnly.cs


Code







































































































































































































转载于:https://www.cnblogs.com/jdmei520/archive/2009/01/05/1369344.html