2011-04-21 4 views
3

TextBox에서 선택한 텍스트에 대해 사용자 지정 backcolors를 얻는 방법을 궁금합니다. 기본적으로 선택한 텍스트 (밝은 파란색)에 대해 Windows의 표준 색상을 사용합니다. winforms TextBox 및 TextBox를 기반으로하는 스킨 된 텍스트 편집기를 사용하므로 색상을 변경하는 속성이 노출되지 않으므로 응용 프로그램 수준에서이 시스템 기본 색을 변경할 수있는 다른 방법이 있는지 궁금합니다.TextBox에서 선택한 텍스트 BackColor 변경

감사합니다,

답변

1

어쩌면이 도움이 ...

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); 
     } 
    }