3
我想知道如何實現文本框中選定文本的自定義背景色。默認情況下,它使用所選文本的窗口標準顏色(淺藍色)。由於我使用基於WinForms TextBox的皮膚文本編輯器,並且TextBox不公開任何屬性來更改顏色,所以我想知道是否有其他方法可以在應用程序級別更改此係統默認顏色?在文本框中更改選定的文本背景色
感謝,
湯姆
我想知道如何實現文本框中選定文本的自定義背景色。默認情況下,它使用所選文本的窗口標準顏色(淺藍色)。由於我使用基於WinForms TextBox的皮膚文本編輯器,並且TextBox不公開任何屬性來更改顏色,所以我想知道是否有其他方法可以在應用程序級別更改此係統默認顏色?在文本框中更改選定的文本背景色
感謝,
湯姆
也許這有助於...
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);
}
}