您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > [C]-用户自定义控件(含源代码)-透明文本框
usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Windows.Forms;usingSystem.Drawing.Imaging;namespaceZBobb{///summary///AlphaBlendTextBox:A.Nettextboxthatcanbetranslucenttothebackground.///(C)2003BobBradley/ZBobb@hotmail.com////summarypublicclassAlphaBlendTextBox:System.Windows.Forms.TextBox{#regionprivatevariablesprivateuPictureBoxmyPictureBox;privateboolmyUpToDate=false;privateboolmyCaretUpToDate=false;privateBitmapmyBitmap;privateBitmapmyAlphaBitmap;privateintmyFontHeight=10;privateSystem.Windows.Forms.TimermyTimer1;privateboolmyCaretState=true;privateboolmyPaintedFirstTime=false;privateColormyBackColor=Color.White;privateintmyBackAlpha=10;///summary///Requireddesignervariable.////summaryprivateSystem.ComponentModel.Containercomponents=null;#endregion//endprivatevariables#regionpublicmethodsandoverridespublicAlphaBlendTextBox(){//ThiscallisrequiredbytheWindows.FormsFormDesigner.InitializeComponent();//TODO:AddanyinitializationaftertheInitializeComponentcallthis.BackColor=myBackColor;this.SetStyle(ControlStyles.UserPaint,false);this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);this.SetStyle(ControlStyles.DoubleBuffer,true);myPictureBox=newuPictureBox();this.Controls.Add(myPictureBox);myPictureBox.Dock=DockStyle.Fill;}protectedoverridevoidOnResize(EventArgse){base.OnResize(e);this.myBitmap=newBitmap(this.ClientRectangle.Width,this.ClientRectangle.Height);//(this.Width,this.Height);this.myAlphaBitmap=newBitmap(this.ClientRectangle.Width,this.ClientRectangle.Height);//(this.Width,this.Height);myUpToDate=false;this.Invalidate();}//SomeoftheseshouldbemovedtotheWndProclaterprotectedoverridevoidOnKeyDown(KeyEventArgse){base.OnKeyDown(e);myUpToDate=false;this.Invalidate();}protectedoverridevoidOnKeyUp(KeyEventArgse){base.OnKeyUp(e);myUpToDate=false;this.Invalidate();}protectedoverridevoidOnKeyPress(KeyPressEventArgse){base.OnKeyPress(e);myUpToDate=false;this.Invalidate();}protectedoverridevoidOnMouseUp(MouseEventArgse){base.OnMouseUp(e);this.Invalidate();}protectedoverridevoidOnGiveFeedback(GiveFeedbackEventArgsgfbevent){base.OnGiveFeedback(gfbevent);myUpToDate=false;this.Invalidate();}protectedoverridevoidOnMouseLeave(EventArgse){//foundthiscodetofindthecurrentcursorlocation//at=Cursor.Position;Formf=this.FindForm();ptCursor=f.PointToClient(ptCursor);if(!this.Bounds.Contains(ptCursor))base.OnMouseLeave(e);}protectedoverridevoidOnChangeUICues(UICuesEventArgse){base.OnChangeUICues(e);myUpToDate=false;this.Invalidate();}//--protectedoverridevoidOnGotFocus(EventArgse){base.OnGotFocus(e);myCaretUpToDate=false;myUpToDate=false;this.Invalidate();myTimer1=newSystem.Windows.Forms.Timer(this.components);myTimer1.Interval=(int)win32.GetCaretBlinkTime();//usuallyaround500;myTimer1.Tick+=newEventHandler(myTimer1_Tick);myTimer1.Enabled=true;}protectedoverridevoidOnLostFocus(EventArgse){base.OnLostFocus(e);myCaretUpToDate=false;myUpToDate=false;this.Invalidate();myTimer1.Dispose();}//--protectedoverridevoidOnFontChanged(EventArgse){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();}protectedoverridevoidOnTextChanged(EventArgse){base.OnTextChanged(e);myUpToDate=false;this.Invalidate();}protectedoverridevoidWndProc(refMessagem){base.WndProc(refm);//needtorewriteasabigswitchif(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();}elseif(m.Msg==win32.WM_HSCROLL||m.Msg==win32.WM_VSCROLL){myUpToDate=false;this.Invalidate();}elseif(m.Msg==win32.WM_LBUTTONDOWN||m.Msg==win32.WM_RBUTTONDOWN||m.Msg==win32.WM_LBUTTONDBLCLK//||m.Msg==win32.WM_MOUSELEAVE///****){myUpToDate=false;this.Invalidate();}elseif(m.Msg==win32.WM_MOUSEMOVE){if(m.WParam.ToInt32()!=0)//shiftkeyorotherbuttons{myUpToDate=false;this.Invalidate();}}//System.Diagnostics.Debug.WriteLine(Pro:+m.Msg.ToString(X));}///summary///Cleanupanyresourcesbeingused.////summaryprotectedoverridevoidDispose(booldisposing){if(disposing){//this.BackColor=Color.Pink;if(components!=null){components.Dispose();}}base.Dispose(disposing);}#endregion//endpublicmethodandoverrides#regionpublicpropertyoverridespublicnewBorderStyleBorderStyle{get{returnbase.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();}}publicnewColorBackColor{get{returnColor.FromArgb(base.BackColor.R,base.BackColor.G,base.BackColor.B);}set{myBackColor=value;base.BackColor=value;myUpToDate=false;}}publicoverrideboolMultiline
本文标题:[C]-用户自定义控件(含源代码)-透明文本框
链接地址:https://www.777doc.com/doc-2830404 .html