2013-05-03 19 views
5

現在、私は単純なチャットアプリケーションを作成したWindowsフォームプロジェクトを持っています。現在、チャットは複数行のテキストボックスに出力されていますが、現在は少し強化してスタイリングを追加したいと考えています。時間がたつにつれ、私はいくつかのイメージを持っていて、それをうまくフォーマットし、将来的にはいくつかのHTML(これは不可欠ではありませんが)を望みます。私はこれを達成するために何をすべきか考えているだけです。私はHTMLページを更新して新しいメッセージごとにリロードすることを考えましたが、これは非常に良いユーザーエクスペリエンスを与えません。私もrichtextboxクラスを見てきましたが、これは私が何をしているのか少し制限されているようです。私は、何を使うべきかについて、正しい方向に私を指摘できる人がいることを願っています。イメージを表示するWindowsフォームTextBox

私は赤で強調しているものと類似した何かを達成しようとしています

enter image description here

+1

は、[リッチテキストボックス](http://msdn.microsoft.com/en-us/library/system.windowsを使用してください。 forms.richtextbox.aspx)。 –

+1

OPは彼が 'RichTextBox'を試してみて、それが限定されていると言ったので、本当に役に立たない。彼が望むものを達成するために 'RichTextBox'を使う方法をJosephに示すためのコードをいくつか追加できますか? –

+0

winformsのHTMLコントロールhttp://stackoverflow.com/questions/3456787/a-good-html-capable-richedit-replacement-for-winforms – Nick

答えて

0

他のコメントのいくつかは、WPFは、現実の世界では、これに適していることを示しているが、それはスイッチが常に可能であるとは限らない。

この目的には、通常のオーナー描画リストボックスが適しています。

作成するには、リストボックスのDrawModeをOwnerDrawVariableに設定するだけです(例:

list.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; 

次に、あなただけの(項目はなりますどのように背の高いリストボックスを伝える)2つのイベントハンドラ、項目を測定することが最初に提供する必要があり、実際にそれをレンダリングするために別の。例えばリストに画像をレンダリング

this.list.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.list_DrawItem); 
this.list.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.list_MeasureItem); 

は、gはグラフィックスCONTEXT_であるGDI +のdrawImage(と非常に簡単です:

Bitmap bmp = Bitmap.FromFile("test.jpg"); 
Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height); 
Rectangle dest = source; 
g.DrawImage(bmp, dest, source, GraphicsUnit.Pixel); 

これは、すべてのオーナー描画リストボックスを持っているサンプルWindowsフォームで可変高さのオーナー描画リスト項目を生成するシステム上のフォント、: enter image description here

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

namespace Font_Display 
{ 
    public class Test : System.Windows.Forms.Form 
    { 
     private Font head; 
     private System.Windows.Forms.ListBox list; 
     private System.ComponentModel.Container components = null; 

     public Test() 
     { 
      InitializeComponent(); 

      head = new Font("Arial", 10, GraphicsUnit.Pixel); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) { 
       if (components != null) { 
        components.Dispose(); 
       } 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.list = new System.Windows.Forms.ListBox(); 
      this.SuspendLayout(); 
      // 
      // list 
      // 
      this.list.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; 
      this.list.IntegralHeight = false; 
      this.list.Location = new System.Drawing.Point(12, 12); 
      this.list.Name = "list"; 
      this.list.Size = new System.Drawing.Size(604, 323); 
      this.list.TabIndex = 0; 
      this.list.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.list_DrawItem); 
      this.list.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.list_MeasureItem); 
      // 
      // Test 
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); 
      this.ClientSize = new System.Drawing.Size(520, 358); 
      this.Controls.Add(this.list); 
      this.Name = "Test"; 
      this.Text = "Display"; 
      this.Load += new System.EventHandler(this.Test_Load); 
      this.Resize += new System.EventHandler(this.Display_Resize); 
      this.ResumeLayout(false); 

     } 
     #endregion 

     [STAThread] 
     static void Main() 
     { 
      Application.Run(new Test()); 
     } 

     private void Test_Load(object sender, EventArgs e) 
     { 
      try { 
       // Loop all font families 
       FontFamily[] families = FontFamily.Families; 
       foreach (FontFamily family in families) { 
        try { list.Items.Add(new Font(family, 20, FontStyle.Regular, GraphicsUnit.Pixel)); continue; } 
        catch { } 
       } 

       Display_Resize(this, EventArgs.Empty); 
      } 
      catch { 
      } 
     } 

     private void Display_Resize(object sender, System.EventArgs e) 
     { 
      Rectangle r = this.ClientRectangle; 
      list.SetBounds(list.Left, 
       list.Top, 
       r.Width - (list.Left * 2), 
       r.Height - (list.Top + list.Left)); 
     } 

     public string TextValue = "Example String"; 

     public StringFormat Format 
     { 
      get 
      { 
       StringFormat format = StringFormat.GenericTypographic; 
       format.FormatFlags |= StringFormatFlags.NoWrap; 
       return format; 
      } 
     } 

     private void list_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
     { 
      Brush back = null; 
      Brush fore = null; 
      Brush htext = null; 
      Rectangle r; 

      try { 
       Font font = (Font)list.Items[e.Index]; 

       // Loop 
       if ((e.State & DrawItemState.Selected) != 0) { 
        back = new SolidBrush(Color.DarkBlue); 
        fore = new SolidBrush(Color.White); 
        htext = new SolidBrush(Color.Orange); 
       } 
       else { 
        back = new SolidBrush(Color.White); 
        fore = new SolidBrush(Color.Black); 
        htext = new SolidBrush(Color.DarkRed); 
       } 

       // Fill the rect 
       e.Graphics.FillRectangle(back, e.Bounds); 

       // Get the size of the header 
       SizeF szHeader = e.Graphics.MeasureString(font.Name, head, int.MaxValue, Format); 
       SizeF szText = e.Graphics.MeasureString(TextValue, font, int.MaxValue, Format); 

       // Draw the string 
       r = e.Bounds; 
       r.Height = (int)szHeader.Height; 
       e.Graphics.DrawString(font.Name, head, htext, r, Format); 

       // Draw the string 
       r = e.Bounds; 
       r.Y = (int)(e.Bounds.Y + szHeader.Height); 
       r.Height = (int)szText.Height; 
       e.Graphics.DrawString(TextValue, font, fore, r, Format); 
      } 
      catch { 
      } 
      finally { 
       if (fore != null) fore.Dispose(); 
       if (back != null) back.Dispose(); 
       if (htext != null) htext.Dispose(); 
      } 
     } 

     private void list_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) 
     { 
      try { 
       Font font = (Font)list.Items[e.Index]; 
       SizeF szHeader = e.Graphics.MeasureString(font.Name, head, int.MaxValue, Format); 
       SizeF szText = e.Graphics.MeasureString(TextValue, font, int.MaxValue, Format); 

       // Return it 
       e.ItemHeight = (int)(szText.Height + szHeader.Height); 
       e.ItemWidth = (int)Math.Max(szText.Width, szHeader.Width); 
      } 
      catch { 
      } 
     } 
    } 
}