在TextBox中更改选定的Text BackColor

时间:2022-07-06 00:00:00

I was wondering how to achieve custom backcolors for selected text in a TextBox. By default, it uses windows's standard color for selected text (light blue). Since I'm using skinned text editors that are based on the winforms TextBox and the TextBox doesn't expose any properties to change the color, I was wondering if there are any other ways to change this system default color on an application level?

我想知道如何在TextBox中为选定的文本实现自定义背景。默认情况下,它使用窗口的标准颜色选择文本(浅蓝色)。由于我使用的是基于winforms TextBox的蒙皮文本编辑器,而TextBox没有公开任何属性来改变颜色,我想知道是否还有其他方法可以在应用程序级别更改此系统默认颜色?

Thanks,

Tom

1 个解决方案

#1


1  

maybe this helps...

也许这有助于......

public class MyTextBox : System.Windows.Forms.TextBox
    {
        private const int WM_PAINT = 0x000F;

        public MyTextBox()
        {
            this.TextChanged += new System.EventHandler(this.myTextBox_TextChanged);

        }

        private void myTextBox_TextChanged(object sender, System.EventArgs e)
        {
        }


        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]    
        protected override void WndProc(ref Message m) 
        {
            base.WndProc(ref m);    
            // Listen for operating system messages.
            switch (m.Msg)
            {
                case WM_PAINT:
                    PaintEventArgs pe = new PaintEventArgs(this.CreateGraphics(),this.RectangleToScreen(this.ClientRectangle));
                    this.OnPaint(pe);
                    break;
            }

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
    // call base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.Clear(this.BackColor);
            string s = this.Text.Substring(0,this.Text.Length/2);
   // provide a object with how to split your string with colors
            string s1 =  this.Text.Substring(this.Text.Length/2);
            SizeF sf = g.MeasureString(s,this.Font);
            g.DrawString(s,this.Font,new SolidBrush(Color.Red),0,0);

            g.DrawString(s1,this.Font,new SolidBrush(Color.Black),sf.Width,0);
        }
    }

#1


1  

maybe this helps...

也许这有助于......

public class MyTextBox : System.Windows.Forms.TextBox
    {
        private const int WM_PAINT = 0x000F;

        public MyTextBox()
        {
            this.TextChanged += new System.EventHandler(this.myTextBox_TextChanged);

        }

        private void myTextBox_TextChanged(object sender, System.EventArgs e)
        {
        }


        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]    
        protected override void WndProc(ref Message m) 
        {
            base.WndProc(ref m);    
            // Listen for operating system messages.
            switch (m.Msg)
            {
                case WM_PAINT:
                    PaintEventArgs pe = new PaintEventArgs(this.CreateGraphics(),this.RectangleToScreen(this.ClientRectangle));
                    this.OnPaint(pe);
                    break;
            }

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
    // call base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.Clear(this.BackColor);
            string s = this.Text.Substring(0,this.Text.Length/2);
   // provide a object with how to split your string with colors
            string s1 =  this.Text.Substring(this.Text.Length/2);
            SizeF sf = g.MeasureString(s,this.Font);
            g.DrawString(s,this.Font,new SolidBrush(Color.Red),0,0);

            g.DrawString(s1,this.Font,new SolidBrush(Color.Black),sf.Width,0);
        }
    }