winform 重繪textbox


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Project
{
[ToolboxItem(true)]
public partial class CTextBox :System.Windows.Forms.TextBox
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public CTextBox() : base() { }
/// <summary>
/// 是否啟用熱點效果
/// </summary>
private bool _HotTrack = true;
private bool _Isempty=false;
/// <summary>
/// 邊框顏色
/// </summary>
private Color _BorderColor = Color.Red;// Color.FromArgb(0xA7, 0xA6, 0xAA);
/// <summary>
/// 熱點邊框顏色
/// </summary>
private Color _HotColor = Color.FromArgb(0x33, 0x5E, 0xA8);
/// <summary>
/// 是否鼠標MouseOver狀態
/// </summary>
private bool _IsMouseOver = false;

[Category("行為"),Description("獲得或設置一個值,指示當鼠標經過控件時控件邊框是否發生變化。只在控件的BorderStyle為FixedSingle時有效"),DefaultValue(true)]
public bool HotTrack
{
get
{
return this._HotTrack;
}
set
{
this._HotTrack = value;
//在該值發生變化時重繪控件,下同
//在設計模式下,更改該屬性時,如果不調用該語句,
//則不能立即看到設計試圖中該控件相應的變化
this.Invalidate();
}
}
[Category("變色"),Description("獲得或設置一個值,當鼠標點擊按鈕的時候判斷文本框是否為空"),DefaultValue(false)]
public bool IsEmpty
{
get
{
return this._Isempty;
}
set
{
this._Isempty = value;
//在該值發生變化時重繪控件,下同 //在設計模式下,更改該屬性時,如果不調用該語句, //則不能立即看到設計試圖中該控件相應的變化
this.Invalidate();
}
}
/// <summary>
/// 邊框顏色
/// </summary>
[Category("外觀"),Description("獲得或設置控件的邊框顏色"),DefaultValue(typeof(Color), "#A7A6AA")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}

/// <summary>
/// 熱點時邊框顏色
/// </summary>
[Category("外觀"),Description("獲得或設置當鼠標經過控件時控件的邊框顏色。只在控件的BorderStyle為FixedSingle時有效"),DefaultValue(typeof(Color), "#335EA8")]
public Color HotColor
{
get
{
return this._HotColor;
}
set
{
this._HotColor = value;
this.Invalidate();
}
}
public void TextBoxXP()
{
}
/// <summary>
/// 鼠標移動到該控件上時
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
//鼠標狀態
this._IsMouseOver = true;
//如果啟用HotTrack,則開始重繪 //如果不加判斷這里不加判斷,則當不啟用HotTrack, //鼠標在控件上移動時,控件邊框會不斷重繪, //導致控件邊框閃爍。下同
if (this._HotTrack)
{
this.Invalidate();//重繪
}
base.OnMouseMove(e);
}
/// <summary>
/// 當鼠標從該控件移開時
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
this._IsMouseOver = false;

if (this._HotTrack)
{
this.Invalidate(); //重繪
}
base.OnMouseLeave(e);
}

/// <summary>
/// 當該控件獲得焦點時
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(EventArgs e)
{
if (this._HotTrack)
{
this.Invalidate();//重繪
}
base.OnGotFocus(e);
}
/// <summary>
/// 當該控件失去焦點時
/// </summary>
/// <param name="e"></param>
protected override void OnLostFocus(EventArgs e)
{
if (this._HotTrack)
{
this.Invalidate();//重繪
}
base.OnLostFocus(e);
}

/// <summary>
/// 獲得操作系統消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0)
{
return;
}
//只有在邊框樣式為FixedSingle時自定義邊框樣式才有效
if (this.BorderStyle == BorderStyle.FixedSingle)
{
if (_Isempty)
{
System.Drawing.Pen pen = new Pen(this._BorderColor,2); //邊框Width為2個像素
if (this._HotTrack)
{
if (this.Focused)
{
pen.Color = this._HotColor;
}
else
{
if (this._IsMouseOver)
{
pen.Color = this._HotColor;
}
else
{
pen.Color = this._BorderColor;
}
}
}
//繪制邊框
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
pen.Dispose();
}
}
m.Result = IntPtr.Zero;//返回結果
ReleaseDC(m.HWnd, hDC);//釋放
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// CTextBox
//
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.ResumeLayout(false);

}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM