namespacemytextboxone{//You must use the BorderStyle of the text box as FixedSingle to use it.//Some controls (such as TextBox, Button, and so on) are drawn by the system process, and the overloaded OnPaint method will not work. //All this does not use the overloaded OnPaint method to draw a TextBox border. Public classTextboxtwo:textbox {//gets the current process in order to redraw the control[System.Runtime.InteropServices.DllImport ("user32.dll")] Static externIntPtr GETWINDOWDC (IntPtr hWnd); [System.Runtime.InteropServices.DllImport ("user32.dll")] Static extern intReleaseDC (IntPtr hWnd, IntPtr HDC); //whether to enable hotspot effects Private BOOL_hottrack =true; //Border Color PrivateColor _bordercolor =Color.gray; //hotspot Border Color PrivateColor _hotcolor = Color.FromArgb ( -,0,255,255); //whether the mouse passes Private BOOL_ismouseover =false;
#regionProperty/// <summary> ///whether to enable hotspot effects/// </summary>[Category ("Behavior"), Description ("Gets or sets a value that indicates whether the border of the control changes when the current mouse passes over the control. Only valid when the control's BorderStyle is fixedstring"), DefaultValue (true)] Public BOOLHottrack {Get{return_hottrack;} Set { This. _hottrack =value; //When the value changes, the control is redrawn when the property is changed in design mode, and if the statement is not called, you cannot immediately see the corresponding change in the control in the design attempt This. Invalidate (); } } /// <summary> ///Border Color/// </summary>[Category ("appearance"), Description ("Gets or sets the border color of the control"), DefaultValue (typeof(Color),"Black")] PublicColor bordercolor {Get{return This. _bordercolor;} Set { This. _bordercolor =value; This. Invalidate (); } } /// <summary> ///border color when hot spots/// </summary>[Category ("appearance"), Description ("Gets or sets the border color of the control when the mouse passes over it. Only valid when the control's BorderStyle is fixedsing"), DefaultValue (typeof(Color),"Red")] PublicColor Hotcolor {Get{return This. _hotcolor;} Set { This. _hotcolor =value; This. Invalidate (); } } #endregion
View Code---properties
PublicTextboxtwo ():Base() { This. BorderStyle =BorderStyle.FixedSingle; This. Width = $; This. Height = -; } /// <summary> ///when the mouse moves over the control/// </summary> protected Override voidOnMouseMove (MouseEventArgs e) { This. _ismouseover =true; //if Hottrack is enabled, the redraw starts,//without judgment, when the mouse moves over the control, the border of the control is redrawn continuously, causing the control border to blink. Similarly if( This. _hottrack) { This. Invalidate (); } Base. OnMouseMove (e); } /// <summary> ///When the mouse moves away from the control/// </summary> protected Override voidOnMouseLeave (EventArgs e) { This. _ismouseover =false; if( This. _hottrack) { This. Invalidate (); } Base. OnMouseLeave (e); } /// <summary> ///when the control receives focus/// </summary> protected Override voidOnGotFocus (EventArgs e) {if( This. _hottrack) { This. Invalidate (); } Base. OnGotFocus (e); } protected Override voidOnLostFocus (EventArgs e) {if( This. _hottrack) { This. Invalidate (); } Base. OnLostFocus (e); } /// <summary> ///get operating system messages/// </summary> protected Override voidWndProc (refMessage m) { Base. WndProc (refm); if(M.msg = =0xf|| M.msg = =0x133) { //intercepts the system message and obtains the current control process for redrawing. // //MSDN: Overriding OnPaint will prevent the appearance of all controls from being modified//controls that complete all drawings by Windows (such as a TextBox) never call his OnPaint method//Therefore, the custom code will never be used. See the documentation for the specific control you want to modify .//See if the OnPaint method is available. If a control does not list OnPaint as a member method,//You cannot change the appearance by overriding this method. //MSDN: To understand the available Message.msg,message.lparam and Message.wparam values,//please refer to the Platform SDK documentation reference located in the Msnd Library. Available in the Platform SDK ("Core SDK" section)//The actual constant value is found in the Windows.h header file included in the download, which can also be found on MSDN.IntPtr HDC =GETWINDOWDC (M.hwnd); if(Hdc.toint32 () = =0) return; //custom Border style is only valid when border is FixedSingle if( This. BorderStyle = =borderstyle.fixedsingle) {//border width is 2 pixelsPen p =NewPen ( This. _bordercolor,2); if( This. Hottrack) {if( This. Focused) {P.color= This. _hotcolor; } } Else { if( This. _ismouseover) {P.color= This. _hotcolor; } Else{P.color= This. _bordercolor; } } //Draw BorderGraphics g =Graphics.fromhdc (HDC); G.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias; G.drawrectangle (P,0,0, This. Width-2, This. Height-2); P.dispose (); } //return ResultsM.result =IntPtr.Zero; //ReleaseReleaseDC (M.hwnd, HDC); } } }}
WinForm Change textbox border color (GO)