C# 高仿腾讯QQ (TextBox控件美化)(附源码)

接上一篇《C# 高仿腾讯QQ (Bottom控件美化)

这篇来说一下QQ皮肤TextBox控件的美化:

  (1).已修正 每次窗口最小化以后再还原会发现窗口底部往下移了几十个像素(但还不十分完善,在设计模式时有BUG,请高手支招)

 

  效果图如下:

 

 

说明:

     (1)新增一个自定义控件(ALTextBox.cs),在上面放一个TextBox控件,把控件的边框属性设为无 (BorderStyle==BorderStyle.None);

           

  (2)准备一张带有5个状态的按钮图片如

        

    分别对应5种状态,通过重写OnPaint方法把相应的状态图画进去(其实有几种状态用不到,大家自己去修正吧)

    //枚举按钮的状态
     public enum State
      {
         Normal = 1,//按钮默认时
         MouseOver = 2,//鼠标移上按钮时
         MouseDown = 3,//鼠标按下按钮时
         Disable = 4,//当不启用按钮时(也就是按钮属性Enabled==Ture时)
         Default = 5//控件得到Tab焦点时
      }

 

ALTextBox.cs类源码

 

代码

   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AlSkin.AlClass;

namespace AlSkin.AlControl.AlTextBox
{
public partial class AlTextBox : UserControl
{
#region 声明
private Bitmap _TextBoxBackImg = ImageObject.GetResBitmap( " AlSkin.AlSkinImg.AlTextBoxImg.Textbox.png " );
private State state = State.Normal;
private bool _Isico = false ;
private Bitmap _Ico;
private Padding _IcoPadding = new Padding( 3 , 3 , 0 , 0 );
// 枚鼠标状态
private enum State
{
Normal
= 1 ,
MouseOver
= 2 ,
MouseDown
= 3 ,
Disable
= 4 ,
Default
= 5
}
#endregion

#region 构造
public AlTextBox()
{
InitializeComponent();
this .SetStyle(ControlStyles.UserPaint, true );
this .SetStyle(ControlStyles.DoubleBuffer, true );
this .SetStyle(ControlStyles.AllPaintingInWmPaint, true );
this .SetStyle(ControlStyles.SupportsTransparentBackColor, true );
this .SetStyle(ControlStyles.StandardDoubleClick, false );
this .SetStyle(ControlStyles.Selectable, true );
this .BackColor = Color.Transparent;
}
#endregion

#region 属性
[Category(
" 阿龙自定义属性 " ), Description( " 与控件关联的文本 " )]
public string text
{
get
{
return BaseText.Text;
}
set
{
BaseText.Text
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 输入最大字符数 " )]
public int MaxLength
{
get { return BaseText.MaxLength; }
set { BaseText.MaxLength = value; }

}

[Category(
" 阿龙自定义属性 " ), Description( " 与控件关联的文本 " )]
public new string Text
{
get
{
return BaseText.Text;
}
set
{
BaseText.Text
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 将控件设为密码显示 " )]
public bool IsPass
{
get
{
return BaseText.UseSystemPasswordChar;
}
set
{
BaseText.UseSystemPasswordChar
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 密码显示字符 " )]
public char PassChar
{
get
{
return BaseText.PasswordChar;
}
set
{
BaseText.PasswordChar
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 将控件设为多行文本显示 " )]
public bool Multiline
{
get
{
return BaseText.Multiline;
}
set
{
BaseText.Multiline
= value;
if (value)
{ BaseText.Height
= this .Height - 6 ; }
else
{
base .Height = 22 ;
BaseText.Height
= 16 ;
this .Invalidate();
}

}
}

[Category(
" 阿龙自定义属性 " ), Description( " 将控件设为多行文本显示 " )]
public Font font
{
get
{
return BaseText.Font;
}
set
{
BaseText.Font
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 将控件设为只读 " )]
public bool ReadOnly
{
get
{
return BaseText.ReadOnly;
}
set
{
BaseText.ReadOnly
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 多行文本的编辑行 " )]
public String[] lines
{
get
{
return BaseText.Lines;
}
set
{
BaseText.Lines
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 是否显示图标 " )]
public bool Isico
{
get
{
return _Isico;
}
set
{
_Isico
= value;
if (value)
{
if (_Ico != null )
{
BaseText.Location
= new Point(_IcoPadding.Left + _Ico.Width, 3 );
BaseText.Width
= BaseText.Width - _IcoPadding.Left - _Ico.Width;
}
else
{
BaseText.Location
= new Point( 25 , 3 );
BaseText.Width
= BaseText.Width - 25 ;
}
}
this .Invalidate();
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 图标文件 " )]
public Bitmap Ico
{
get
{
return _Ico;
}
set
{
_Ico
= value;
}
}

[Category(
" 阿龙自定义属性 " ), Description( " 图标文件Padding " )]
public Padding IcoPadding
{
get { return _IcoPadding; }
set {
_IcoPadding
= value;
this .Invalidate();
}
}
#endregion

#region 委托
public event EventHandler IcoOnclick;
#endregion

#region 方法
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc
= this .ClientRectangle;
Graphics g
= e.Graphics;
ImageDrawRect.DrawRect(g, _TextBoxBackImg, rc, Rectangle.FromLTRB(
10 , 10 , 10 , 10 ), ( int )state, 5 );
if (_Isico)
{
if (_Ico != null )
{
g.DrawImage(_Ico,
new Point(_IcoPadding.Left,_IcoPadding.Top));
}
}
base .OnPaint(e);
}

private void AlTextBox_Resize( object sender, EventArgs e)
{
if ( this .Height > 22 )
{
Multiline
= true ;
}
else
{
this .Height = 22 ;
Multiline
= false ;
}
}

private void NotifyIcoOnclick()
{
if (IcoOnclick != null )
{
IcoOnclick(
this , EventArgs.Empty);
}
}

public void AppendText( string ss)
{
BaseText.AppendText(ss);
}

private void BaseText_MouseEnter( object sender, EventArgs e)
{
state
= State.MouseOver;
this .Invalidate();
}

private void BaseText_MouseLeave( object sender, EventArgs e)
{
state
= State.Normal;
this .Invalidate();
}

private void AlTextBox_MouseUp( object sender, MouseEventArgs e)
{
if (_Ico != null )
{
if ( new Rectangle(_IcoPadding.Left, _IcoPadding.Top, _Ico.Width, _Ico.Height).Contains(e.X,e.Y))
{
NotifyIcoOnclick();
}
}
}

private void AlTextBox_MouseEnter( object sender, EventArgs e)
{
state
= State.MouseOver;
this .Invalidate();
}

private void AlTextBox_MouseLeave( object sender, EventArgs e)
{
state
= State.Normal;
this .Invalidate();
}
#endregion
}
}

 

 自定义属性(通过自定义属性设置或获取该控件上的,TextBox控件的各种属性值)

 

 详细看源码

 

源码下载  Sorry ,发现网站的服务器出了问题上,空间里面的数据全没了,链过来的文件已全失效,请等待恢复。如果大家愿意,请进

QQ群 57218890 里面都有备份,以后有更新也会传上去,在这里跟大家说一下!

转载于:https://www.cnblogs.com/Along729/archive/2010/12/11/AlTextBox.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值