C#WinForm⾃定义控件样式(textBox,comboBox,form,radiob。。。
1.⾃定义comboBox控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestFromStyleChange
{
/// <summary>
/// ⾃定义combobox控件
/// 增加边框,
/// </summary>
#region 需要注意的地⽅
//DrawMode=OwnerDrawVariale
//DropDownStyle=DropDownList
#endregion
class MyComboBox : ComboBox
{
#region ⾃定义属性
private Color _BorderColor = Color.FromArgb(87, 100, 108);
private Color _BackColor = Color.FromArgb(232, 233, 237);
private ButtonBorderStyle _BorderStyle = ButtonBorderStyle.Solid;
private int _ArrowWidth = 2;
private Color _ArrowColor = Color.FromArgb(87, 100, 108);
private bool _ArrowFlag = true;
private Color _FontColor = Color.FromArgb(0, 0, 0);
private Color _BoxBgEnColor = Color.FromArgb(225, 224, 224);
private Color _BoxBgUnColor = Color.FromArgb(187, 187, 187);
[Description("边框颜⾊"), Category("Set")]
public Color BorderColor
{
set { this._BorderColor = value; }
get { return _BorderColor; }
}
[Description("背景颜⾊"), Category("Set")]
public Color BoxBackColor
{
set { this._BackColor = value; }
get { return this._BackColor; }
}
[Description("边框样式"), Category("Set")]
public ButtonBorderStyle BoxBorderStyle
{
set { this._BorderStyle = value; }
get { return this._BorderStyle; }
}
[Description("下拉箭头宽度"), Category("Set")]
public int ArrowWidth
{
set { this._ArrowWidth = value; }
get { return this._ArrowWidth; }
}
[Description("下拉箭头颜⾊"), Category("Set")]
public Color ArrowColor
{
set { this._ArrowColor = value; }
get { return this._ArrowColor; }
}
}
[Description("是否绘制下拉箭头"), Category("Set")]
public bool ArrowEnabled
{
set { this._ArrowFlag = value; }
get { return this._ArrowFlag; }
}
[Description("字体颜⾊"), Category("UserSet")]
public Color FontColor
{
set { this._FontColor = value; }
get { return this._FontColor; }
}
[Description("下拉框可⽤时的背景颜⾊"), Category("UserSet")]
public Color BoxBgEnColor
{
set { this._BoxBgEnColor = value; }
get { return this._BoxBgEnColor; }
}
[Description("下拉框不可⽤时的背景颜⾊"), Category("UserSet")]
public Color BoxBgUnColor
{
set { this._BoxBgUnColor = value; }
get { return this._BoxBgUnColor; }
}
#endregion
public MyComboBox()
{
//SetStyle(ControlStyles.UserPaint /*| ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | Co ntrolStyles.SupportsTransparentBackColor*/, true);
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
}
//导⼊API函数
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern IntPtr GetWindowDC(IntPtr hWnd);//返回hWnd参数所指定的窗⼝的设备环境。
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //函数释放设备上下⽂环境(DC)
protected override void OnDrawItem(DrawItemEventArgs e)
{
int ItemsCount = Items.Count;
Rectangle rect = new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height * ItemsCount - 2);
e.Graphics.FillRectangle(new SolidBrush(_BackColor), rect);
e.Graphics.DrawRectangle(new Pen(new SolidBrush(_BorderColor)), rect);
for (int i = 0; i < Items.Count; i++)
{
if (SelectedItem != null)
{
if (SelectedItem.ToString() == Items[i].ToString())
{
Rectangle rectSel = new Rectangle(1, e.Bounds.Height * (SelectedIndex), e.Bounds.Width - 2, e.Bounds.Height - 2);
e.Graphics.FillRectangle(new SolidBrush(_BoxBgUnColor), rectSel);
}
}
e.Graphics.DrawString(Items[i].ToString(), Font, new SolidBrush(_FontColor), new PointF(0, i *(e.Bounds.Height == 0 ? 16 : e.Bounds.Height)));            }
}
protected override void WndProc(ref Message m)
{
//if (m.Msg == 0x0014) // 禁掉清除背景消息
//    return;
//    return;
base.WndProc(ref m);
//WM_PAINT = 0xf; 要求⼀个窗⼝重画⾃⼰,即Paint事件时
//WM_CTLCOLOREDIT = 0x133;当⼀个编辑型控件将要被绘制时发送此消息给它的⽗窗⼝;
//通过响应这条消息,所有者窗⼝可以通过使⽤给定的相关显⽰设备的句柄来设置编辑框的⽂本和背景颜⾊
//windows消息值表,可参考:hi.baidu/dooy/blog/item/0e770a24f70e3b2cd407421b.html
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) //如果取设备上下⽂失败则返回
{
return;
}
//建⽴Graphics对像
Graphics g = Graphics.FromHdc(hDC);
Rectangle rect = new Rectangle(0, 0, Width, Height);
Rectangle rect2 = new Rectangle(1, 1, Width - 2, Height - 2);
//画边框的
ControlPaint.DrawBorder(g, rect, _BorderColor, _BorderStyle);
//画坚线
//ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid);
//g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height));
Color backColor = Enabled ? _BoxBgEnColor : _BoxBgUnColor;
string selValue = SelectedItem == null ? "" : SelectedItem.ToString();
string Text = Enabled ? selValue : "";
g.FillRectangle(new SolidBrush(backColor), rect2);
//填充背景
if (_ArrowFlag)
{
//g.DrawLine(new Pen(_ArrowColor, _ArrowWidth), new PointF(this.Width - this.Height + 6, 6), new PointF(this.Width - this.Height + 11, 11));                    //g.DrawLine(new Pen(_ArrowColor, _ArrowWidth), new PointF(this.Width - this.Height + 11, 11), new PointF(this.Width - this.Height + 16, 6));
g.DrawLines(new Pen(_ArrowColor, _ArrowWidth), new PointF[] { new PointF(this.Width - this.Height + 6, 6), new PointF(this.Width - this.Heig ht + 11, 11), new PointF(this.Width - this.Height + 16, 6) });
}
g.DrawString(Text, Font, new SolidBrush(_FontColor), 1, 2);
//释放DC
ReleaseDC(m.HWnd, hDC);
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
}
2. ⾃定义TextBox(增加边框)
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestFromStyleChange
{
///
/// ⾃定义输⼊框控件
/// 增加了边框颜⾊属性
/
//
#region 使⽤时需要注意的地⽅
//⽆
#endregion
public partial class MyTextBox : TextBox
{
private Rectangle m_rcClient = Rectangle.Empty;
private Color _coBorder = Color.Black;
//protected override CreateParams CreateParams
//{
//    get
/
/    {
//        CreateParams cparams = base.CreateParams;
//        BorderStyle border = this.BorderStyle;
//        if (!(border == BorderStyle.Fixed3D))
borderbox//        {
//            cparams.ExStyle &= ~NativeMethods.WS_EX_CLIENTEDGE;
//            cparams.Style &= ~8388608;
//            switch (border)
//            {
//                // Unlike other controls, text box doesn't draw its single border in the NC area!
//                case BorderStyle.Fixed3D:
/
/                case BorderStyle.FixedSingle:
//                    // 使⼀个视窗具有凹陷边框
//                    cparams.ExStyle = cparams.ExStyle | NativeMethods.WS_EX_CLIENTEDGE | NativeMethods.WS_EX_WINDOWEDGE;    //                    break;
//            }
//        }
//        return cparams;
//    }
//}
[Description("边框颜⾊"),Category("UserSet")]
public Color BorderColor
{
get { return this._coBorder; }
set { this._coBorder = value; }
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case NativeMethods.WM_PAINT:
this.OnWmNcPaint(ref m);
break;
case NativeMethods.WM_NCPAINT:
this.OnWmNcPaint(ref m);
return;
}
base.WndProc(ref m);
base.WndProc(ref m);
}
private void OnWmNcPaint(ref Message m)
{
NativeMethods.RECT rcWnd = new NativeMethods.RECT();
NativeMethods.GetWindowRect(m.HWnd, ref rcWnd);
int intWidth = rcWnd.Width;
int intHeight = rcWnd.Height;
int width = rcWnd.Width;
int height = rcWnd.Height;
IntPtr hRgn = NativeMethods.CreateRectRgn(0, 0, width, height);
if (hRgn != IntPtr.Zero)
{
IntPtr hDc = NativeMethods.GetWindowDC(m.HWnd);
if (hDc != IntPtr.Zero)
{
IntPtr memDc = NativeMethods.CreateCompatibleDC(hDc);
if (memDc != IntPtr.Zero)
{
IntPtr hBmp = NativeMethods.CreateCompatibleBitmap(hDc, width, height);
if (hBmp != IntPtr.Zero)
{
IntPtr hObj = NativeMethods.SelectObject(memDc, hBmp);
if (m.WParam.ToInt32() != 1)
{
IntPtr tmpRgn = NativeMethods.CreateRectRgn(0, 0, 0, 0);
NativeMethods.CombineRgn(tmpRgn, m.WParam, IntPtr.Zero, NativeMethods.RGN_COPY);
NativeMethods.OffsetRgn(tmpRgn, -rcWnd.left, -p);
NativeMethods.CombineRgn(hRgn, hRgn, tmpRgn, NativeMethods.RGN_AND);
NativeMethods.DeleteObject(tmpRgn);
}
using (Graphics g = Graphics.FromHdc(memDc))
{
RenderNCArea(g, width, height, true);
}
NativeMethods.SelectClipRgn(hDc, hRgn);
NativeMethods.ExcludeClipRect(hDc, m_rcClient.X, m_rcClient.Y, m_rcClient.Right, m_rcClient.Bottom);
NativeMethods.BitBlt(hDc, 0, 0, rcWnd.Width, rcWnd.Height, memDc, 0, 0, 0x00CC0020/*SRCCOPY*/ );
NativeMethods.SelectObject(memDc, hObj);
NativeMethods.DeleteObject(hBmp);
}
NativeMethods.DeleteDC(memDc);
}
NativeMethods.DeleteDC(hDc);
}
IntPtr hTmp = NativeMethods.CreateRectRgn(m_rcClient.Left, m_rcClient.Top, m_rcClient.Right, m_rcClient.Bottom);            if (hTmp != IntPtr.Zero)
{
NativeMethods.CombineRgn(hRgn, hRgn, hTmp, NativeMethods.RGN_AND);
NativeMethods.OffsetRgn(hRgn, rcWnd.left, p);
NativeMethods.DeleteObject(hTmp);
Message msg = new Message();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。