| |

VerySource

 Forgot password?
 Register
Search
View: 649|Reply: 6

Save me, how to redraw the text in a TextBox

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-5 07:30:01
| Show all posts |Read mode
I rewritten the TextBox's OnPaint function but it didn't work.
Just want to redraw the Text in the TextBox
Give an example
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-29 15:45:01
| Show all posts
// Intercept system messages and get the current control process for redrawing.
                // Some controls (such as TextBox, Button, etc.) are drawn by the system process, overloading the OnPaint method will not work.
                // All here does not use the overloaded OnPaint method to draw the TextBox border.
                //
                // MSDN: Overriding OnPaint will prohibit modifying the appearance of all controls.
                // Those controls that complete all their drawing (such as Textbox) by Windows never call their OnPaint method,
                // So custom code will never be used. See the documentation for the specific control you want to modify,
                // Check if the OnPaint method is available. If a control does not list OnPaint as a member method,
                // then you cannot change its appearance by overriding this method.
                //
                // MSDN: To understand the available Message.Msg, Message.LParam, and Message.WParam values,
                // Refer to the Platform SDK documentation reference located in the MSDN 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.
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-29 17:15:01
| Show all posts
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace BenSoftCN.WinForms.UI
{

    [ToolboxItem (true)]
    public class TextBoxXP: System.Windows.Forms.TextBox
    {
        /// <summary>
        /// Get the current process to redraw the control
        /// </ summary>
        /// <param name = "hWnd"> </ param>
        /// <returns> </ returns>
        [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);

        /// <summary>
        /// whether to enable hotspot effect
        /// </ summary>
        private bool _HotTrack = true;

        /// <summary>
        /// border color
        /// </ summary>
        private Color _BorderColor = Color.FromArgb (0xA7,0xA6,0xAA);

        /// <summary>
        /// hotspot border color
        /// </ summary>
        private Color _HotColor = Color.FromArgb (0x33,0x5E, 0xA8);

        /// <summary>
        /// Whether the mouse MouseOver state
        /// </ summary>
        private bool _IsMouseOver = false;
         
        #region attributes
        /// <summary>
        /// whether to enable hotspot effect
        /// </ summary>
        [Category ("behavior"),
        Description ("Gets or sets a value indicating whether the control border changes when the mouse passes over the control. Only valid when the BorderStyle of the control is FixedSingle")
        DefaultValue (true)]
        public bool HotTrack
        {
            get
            {
                return this._HotTrack;
            }
            set
            {
                this._HotTrack = value;
                // Redraw the control when the value changes, the same below
                // In design mode, when changing this property, if the statement is not called,
                // you cannot immediately see the corresponding changes 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), "# A7A6AA")]
        public Color BorderColor
        {
            get
            {
                return this._BorderColor;
            }
            set
            {
                this._BorderColor = value;
                this.Invalidate ();
            }
        }
        /// <summary>
        /// border color when hotspot
        /// </ summary>
        [Category ("Appearance"),
        Description ("Gets or sets the border color of the control when the mouse passes over the control. Only valid when the BorderStyle of the control is FixedSingle"),
        DefaultValue (typeof (Color), "# 335EA8")]
        public Color HotColor
        {
            get
            {
                return this._HotColor;
            }
            set
            {
                this._HotColor = value;
                this.Invalidate ();
            }
        }
        #endregion attributes

        /// <summary>
        ///
        /// </ summary>
        public TextBoxXP (): base ()
        {
        }
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-29 17:30:02
| Show all posts
/// <summary>
        /// When the mouse moves over the control
        /// </ summary>
        /// <param name = "e"> </ param>
        protected override void OnMouseMove (MouseEventArgs e)
        {
            // mouse status
            this._IsMouseOver = true;
            // If HotTrack is enabled, start redrawing
            // If no judgment is made here, when HotTrack is not enabled,
            // When the mouse moves over the control, the control border will be continuously redrawn,
            // Cause the control border to flicker. Same as below
            // who has a better way? Please tell me, Thanks.
            if (this._HotTrack)
            {
                // Redraw
                this.Invalidate ();
            }
            base.OnMouseMove (e);
        }
        /// <summary>
        /// When the mouse moves away from the control
        /// </ summary>
        /// <param name = "e"> </ param>
        protected override void OnMouseLeave (EventArgs e)
        {
            this._IsMouseOver = false;

            if (this._HotTrack)
            {
                // Redraw
                this.Invalidate ();
            }
            base.OnMouseLeave (e);
        }
         
        /// <summary>
        /// When the control gets focus
        /// </ summary>
        /// <param name = "e"> </ param>
        protected override void OnGotFocus (EventArgs e)
        {

            if (this._HotTrack)
            {
                // Redraw
                this.Invalidate ();
            }
            base.OnGotFocus (e);
        }
        /// <summary>
        /// When the control loses focus
        /// </ summary>
        /// <param name = "e"> </ param>
        protected override void OnLostFocus (EventArgs e)
        {
            if (this._HotTrack)
            {
                // Redraw
                this.Invalidate ();
            }
            base.OnLostFocus (e);
        }

        /// <summary>
        /// get operating system messages
        /// </ summary>
        /// <param name = "m"> </ param>
        protected override void WndProc (ref Message m)
        {

            base.WndProc (ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                // Intercept system messages and get the current control process for redrawing.
                // Some controls (such as TextBox, Button, etc.) are drawn by the system process, overloading the OnPaint method will not work.
                // All here does not use the overloaded OnPaint method to draw the TextBox border.
                //
                // MSDN: Overriding OnPaint will prohibit modifying the appearance of all controls.
                // Those controls that complete all their drawing (such as Textbox) by Windows never call their OnPaint method,
                // So custom code will never be used. See the documentation for the specific control you want to modify,
                // Check if the OnPaint method is available. If a control does not list OnPaint as a member method,
                // then you cannot change its appearance by overriding this method.
                //
                // MSDN: To understand the available Message.Msg, Message.LParam, and Message.WParam values,
                // Refer to the Platform SDK documentation reference located in the MSDN 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 only works when the border style is FixedSingle
                if (this.BorderStyle == BorderStyle.FixedSingle)
                {
                    // Border Width is 1 pixel
                    System.Drawing.Pen pen = new Pen (this._BorderColor, 1) ;;
                 
                    if (this._HotTrack)
                    {
                        if (this.Focused)
                        {
                            pen.Color = this._HotColor;
                        }
                        else
                        {
                            if (this._IsMouseOver)
                            {
                                pen.Color = this._HotColor;
                            }
                            else
                            {
                                pen.Color = this._BorderColor;
                            }
                        }
                    }
                    // Draw the border
                    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 ();
                }
                // return result
                m.Result = IntPtr.Zero;
                //freed
                ReleaseDC (m.HWnd, hDC);
            }
        }

    }
}
Reply

Use magic Report

0

Threads

23

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-3-30 15:30:02
| Show all posts
Want to redraw the Text in the TextBox
-------------------
Then inherit this control and rewrite his Render
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-11 08:15:01
| Show all posts
Redraw the text in the TextBox
-------------------
Then inherit this control and rewrite his Render
---------
  Upstairs, I didn't find this function. Is there a TextBox, not a TextBox of ToolStrip?
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-11 14:00:01
| Show all posts
Tochali88:

Thanks for your answer, your example works, but I added a line after DrawRectangle
g.DrawString (this.Text, this.Font, new System.Drawing.SolidBrush (Color.Red), this.Bounds); no effect, can you help?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list