[C#] - 用户自定义控件(含源代码)-透明文本框
透明文本框
由于在www.csdn.net只能下载*.dll的文件,没有代码,所以特意发出源代码。
using System;
using System.Collections;
using System.ComponentModel; using System.Drawing; using System.Data;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace ZBobb {
///
/// AlphaBlendTextBox: A .Net textbox that can be translucent to the background. /// (C) 2003 Bob Bradley / ZBobb@hotmail.com ///
public class AlphaBlendTextBox : System.Windows.Forms.TextBox {
#region private variables
private uPictureBox myPictureBox; private bool myUpToDate = false;
private bool myCaretUpToDate = false; private Bitmap myBitmap;
private Bitmap myAlphaBitmap;
private int myFontHeight = 10;
private System.Windows.Forms.Timer myTimer1;
private bool myCaretState = true;
private bool myPaintedFirstTime = false;
private Color myBackColor = Color.White; private int myBackAlpha = 10;
///
/// Required designer variable. ///
private System.ComponentModel.Container components = null;
#endregion // end private variables
#region public methods and overrides
public AlphaBlendTextBox() {
// This call is required by the Windows.Forms Form Designer. InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
this.BackColor = myBackColor;
this.SetStyle(ControlStyles.UserPaint, false);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true);
myPictureBox = new uPictureBox(); this.Controls.Add(myPictureBox); myPictureBox.Dock = DockStyle.Fill; }
protected override void OnResize(EventArgs e) {
base.OnResize(e);
this.myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height);
this.myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height); myUpToDate = false; this.Invalidate(); }
//Some of these should be moved to the WndProc later
protected override void OnKeyDown(KeyEventArgs e) {
base.OnKeyDown(e); myUpToDate = false; this.Invalidate(); }
protected override void OnKeyUp(KeyEventArgs e) {
base.OnKeyUp(e); myUpToDate = false; this.Invalidate();
}
protected override void OnKeyPress(KeyPressEventArgs e) {
base.OnKeyPress(e); myUpToDate = false; this.Invalidate(); }
protected override void OnMouseUp(MouseEventArgs e) {
base.OnMouseUp(e); this.Invalidate(); }
protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
base.OnGiveFeedback(gfbevent); myUpToDate = false; this.Invalidate(); }
protected override void OnMouseLeave(EventArgs e) {
//found this code to find the current cursor location
//at http://www.syncfusion.com/FAQ/WinForms/FAQ_c50c.asp#q597q
Point ptCursor = Cursor.Position;
Form f = this.FindForm();
ptCursor = f.PointToClient(ptCursor); if (!this.Bounds.Contains(ptCursor)) base.OnMouseLeave(e); }
protected override void OnChangeUICues(UICuesEventArgs e) {
base.OnChangeUICues(e); myUpToDate = false; this.Invalidate(); }
//--
protected override void OnGotFocus(EventArgs e) {
base.OnGotFocus(e);
myCaretUpToDate = false; myUpToDate = false; this.Invalidate();
myTimer1 = new System.Windows.Forms.Timer(this.components);
myTimer1.Interval = (int)win32.GetCaretBlinkTime(); // usually around 500;
myTimer1.Tick += new EventHandler(myTimer1_Tick); myTimer1.Enabled = true;
}
protected override void OnLostFocus(EventArgs e) {
base.OnLostFocus(e);
myCaretUpToDate = false; myUpToDate = false; this.Invalidate();
myTimer1.Dispose(); }
//--
protected override void OnFontChanged(EventArgs e) {
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, false);
base.OnFontChanged(e);
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, true);
myFontHeight = GetFontHeight();
myUpToDate = false; this.Invalidate(); }
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e); myUpToDate = false; this.Invalidate(); }
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
// need to rewrite as a big switch
if (m.Msg == win32.WM_PAINT) {
myPaintedFirstTime = true;
if (!myUpToDate || !myCaretUpToDate) GetBitmaps(); myUpToDate = true;
myCaretUpToDate = true;
if (myPictureBox.Image != null) myPictureBox.Image.Dispose(); myPictureBox.Image = (Image)myAlphaBitmap.Clone();
}
else if (m.Msg == win32.WM_HSCROLL || m.Msg == win32.WM_VSCROLL) {
myUpToDate = false; this.Invalidate(); }
else if (m.Msg == win32.WM_LBUTTONDOWN || m.Msg == win32.WM_RBUTTONDOWN || m.Msg == win32.WM_LBUTTONDBLCLK
// || m.Msg == win32.WM_MOUSELEAVE ///**** ) {
myUpToDate = false; this.Invalidate(); }
else if (m.Msg == win32.WM_MOUSEMOVE) {
if (m.WParam.ToInt32() != 0) //shift key or other buttons {
myUpToDate = false; this.Invalidate(); } }
//System.Diagnostics.Debug.WriteLine(\
}
///
/// Clean up any resources being used. ///
protected override void Dispose(bool disposing) {
if (disposing) {
//this.BackColor = Color.Pink; if (components != null) {
components.Dispose(); } }
base.Dispose(disposing); }
#endregion //end public method and overrides
#region public property overrides
public new BorderStyle BorderStyle {
get { return base.BorderStyle; } set {
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, false);
base.BorderStyle = value;
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, true);
this.myBitmap = null;
this.myAlphaBitmap = null; myUpToDate = false; this.Invalidate(); } }
public new Color BackColor {
get {
return Color.FromArgb(base.BackColor.R, base.BackColor.G, base.BackColor.B); } set {
myBackColor = value; base.BackColor = value; myUpToDate = false; } }
public override bool Multiline {
get { return base.Multiline; } set {
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, false);
base.Multiline = value;
if (this.myPaintedFirstTime)
this.SetStyle(ControlStyles.UserPaint, true);
this.myBitmap = null;
this.myAlphaBitmap = null; myUpToDate = false; this.Invalidate(); } }
#endregion //end public property overrides
#region private functions and classes
private int GetFontHeight() {
Graphics g = this.CreateGraphics();