2017-01-27 19 views
1

私は境界線の色をカスタムRichTextBoxを作成しようとしているが、私は問題を抱えている...ここでC#のリッチテキストボックス境界線の色

を示していない 私の境界線の色は、私のコードです:

public partial class AlXRichTextBox : RichTextBox 
{ 
    private RichTextBox textBox; 

    private Color borderColor; 

    public AlXRichTextBox() 
    { 
     InitializeComponent(); 
    } 
    public Color BorderColor 
    { 
     get { return borderColor; } 
     set { borderColor = value; Invalidate(); } 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     Pen p = new Pen(borderColor); 
     Graphics g = e.Graphics; 
     int variance = 3; 
     //g.DrawRectangle(p, new Rectangle(base.Location.X - variance, base.Location.Y - variance, base.Width + variance, base.Height + variance)); 
     ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, borderColor, ButtonBorderStyle.Solid); 
    } 

    private void InitializeComponent() 
    { 
      this.textBox = new System.Windows.Forms.RichTextBox(); 
      this.SuspendLayout(); 
      // 
      // richTextBox1 
      // 
      this.textBox.Location = new System.Drawing.Point(0, 0); 
      this.textBox.Name = "richTextBox1"; 
      this.textBox.Size = new System.Drawing.Size(100, 96); 
      this.textBox.TabIndex = 0; 
      this.textBox.Text = ""; 
      this.textBox.Multiline = true; 
      this.textBox.BorderStyle = BorderStyle.None; 
      // 
      // AlXRichTextBox 
      // 
      this.Size = new System.Drawing.Size(278, 123); 
      this.ResumeLayout(false); 
    } 
} 

この問題?

答えて

1

MSDN articleを参照:

はオーバーライドのOnPaintを使用すると、すべてのコントロールの外観を変更することはできません。すべてのペイントがWindowsによって行われたコントロール(TextBoxなど)は、OnPaintメソッドを呼び出すことはないため、カスタムコードを使用することはありません。 OnPaintメソッドが使用可能かどうかを確認するために変更する特定のコントロールのヘルプドキュメントを参照してください。すべてのWindowsフォームコントロールの一覧については、「Windowsフォームで使用するコントロール」を参照してください。コントロールにOnPaintがメンバーメソッドとしてリストされていない場合は、このメソッドをオーバーライドして外観を変更することはできません。カスタムペイントの詳細については、カスタムコントロールのペイントとレンダリングを参照してください。

しかしあなたは、次のコードを呼び出すことにより、Paintメソッドを呼び出して達成することができ、「ハック」がされています

private const int WM_PAINT = 15; 
protected override void WndProc(ref System.Windows.Forms.Message m) 
{ 
    base.WndProc(ref m); 
    if (m.Msg == WM_PAINT && !inhibitPaint) 
    { 
     // raise the paint event 
     using (Graphics graphic = base.CreateGraphics()) 
      OnPaint(new PaintEventArgs(graphic, 
      base.ClientRectangle)); 
    } 
    } 

    private bool inhibitPaint = false; 

    public bool InhibitPaint 
    { 
     set { inhibitPaint = value; } 
    } 

のSrc:RichTextBox and UserPaint

他のポイントは、あなたが外で描くことができないということです長方形(あなたのRichTBコンポーネントの合計サイズですので、実際には異なるCoordin(小さな内側のもの)を提供したいと思っています)あなたは外側に描画します。

Your 、それはペイントによって変更することには、この制御のために期待されていないとして、あなたは国境などの図面の変更に関しては、「素敵ではない」動作を取得します

public partial class AlXRichTextBox : RichTextBox 
{ 
    private Color borderColor = Color.Red; 

    public Color BorderColor 
    { 
     get { return borderColor; } 
     set { borderColor = value; Invalidate(); } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     int variance = 3; 
     e = new PaintEventArgs(e.Graphics, new Rectangle(e.ClipRectangle.X + variance, e.ClipRectangle.Y + variance, e.ClipRectangle.Width - variance, e.ClipRectangle.Height - variance)); 
     base.OnPaint(e); 

     Pen p = new Pen(borderColor, variance); 
     Graphics g = e.Graphics; 
     g.DrawRectangle(p, new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height)); 
    } 


    private const int WM_PAINT = 15; 
    protected override void WndProc(ref System.Windows.Forms.Message m) 
    { 
     base.WndProc(ref m); 
     if (m.Msg == WM_PAINT && !inhibitPaint) 
     { 
      // raise the paint event 
      using (Graphics graphic = base.CreateGraphics()) 
       OnPaint(new PaintEventArgs(graphic, 
       base.ClientRectangle)); 
     } 

    } 

    private bool inhibitPaint = false; 

    public bool InhibitPaint 
    { 
     set { inhibitPaint = value; } 
    } 
} 

重要

:クラスは次のようになりますWPF - Windows Presentation Foundationのような要素を使用する場合は、を使用することを検討してください。それらはアイテムをテンプレート化してデザインを変更する方がはるかに良いです。

+0

ありがとうございました。 – Al00X

0

少し遅れての答えが、それは私の作品、私はあなたと同じパス上で、これらの日だったし、それはこの溶液に私を得た:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

public class MyRichTextBox : RichTextBox 
{ 
    private const UInt32 WM_PAINT = 0x000F; 
    private const UInt32 WM_USER = 0x0400; 
    private const UInt32 EM_SETBKGNDCOLOR = (WM_USER + 67); 
    private const UInt32 WM_KILLFOCUS = 0x0008; 

    public MyRichTextBox() 
    { 
     this.BorderStyle = System.Windows.Forms.BorderStyle.None; 
    } 

    protected override void WndProc(ref System.Windows.Forms.Message m) 
    { 
     base.WndProc(ref m); 

     Graphics g = Graphics.FromHwnd(Handle); 
     Rectangle bounds = new Rectangle(0, 0, Width - 1, Height - 1); 
     Pen p = new Pen(SystemColors.Highlight, 3); 

     if (m.Msg == WM_PAINT) 
     { 
      if (this.Enabled == true) 
      { 

       if (this.Focused) 
       { 
        g.DrawRectangle(p, bounds); 
       } 

       else 
       { 
        g.DrawRectangle(SystemPens.ControlDark, bounds); 
       } 

      } 
      else 
      { 
       g.FillRectangle(Brushes.White, bounds); 
       g.DrawRectangle(SystemPens.Control, bounds); 
      } 
     } 

     if (m.Msg == EM_SETBKGNDCOLOR) //color disabled background 
     { 
      Invalidate(); 
     } 

     if (m.Msg == WM_KILLFOCUS) //set border back to normal on lost focus 
     { 
      Invalidate(); 
     } 
    } 

} 

このリッチテキストボックスが3つの境界色変更 - 有効になって、集中を無効になった背景で無効になります。ご覧のように、コードはシンプルで短いです。唯一のやり方は、KILL_FOCUSとEM_SETBKGNDCOLORメッセージ(これは無効な背景を変更するためのものです)を上書きすることです。RichTextboxはBorderStyle = Noneにする必要があります。乾杯!

関連する問題