Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于显示图像的Windows窗体文本框_C#_Html_Winforms - Fatal编程技术网

C# 用于显示图像的Windows窗体文本框

C# 用于显示图像的Windows窗体文本框,c#,html,winforms,C#,Html,Winforms,我目前有一个Windows窗体项目,我在其中创建了一个简单的聊天应用程序。目前聊天是输出到一个多行文本框,但我现在想加强它一点,并添加一些样式。我希望将来能有一些图像,很好地格式化它,也许还会有一些HTML(尽管这并不重要)。我只是想知道我应该用什么来实现这一点。我确实想过更新一个HTML页面,然后用每一条新消息重新加载它,但这并不能提供很好的用户体验。我也看过richtextbox类,但这似乎对我所追求的有点限制。我希望有人能给我指出正确的方向,用什么 我正在努力实现类似于我用红色突出显示的目

我目前有一个Windows窗体项目,我在其中创建了一个简单的聊天应用程序。目前聊天是输出到一个多行文本框,但我现在想加强它一点,并添加一些样式。我希望将来能有一些图像,很好地格式化它,也许还会有一些HTML(尽管这并不重要)。我只是想知道我应该用什么来实现这一点。我确实想过更新一个HTML页面,然后用每一条新消息重新加载它,但这并不能提供很好的用户体验。我也看过richtextbox类,但这似乎对我所追求的有点限制。我希望有人能给我指出正确的方向,用什么

我正在努力实现类似于我用红色突出显示的目标:


虽然其他一些评论表明WPF非常适合这种情况,但在现实世界中,切换并不总是可能的或可取的

常规的所有者绘制列表框非常适合此用途

要创建一个,只需将列表框上的DrawMode设置为OwnerDrawVariable,例如

list.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
然后您只需要提供两个事件处理程序,第一个用于测量项目(告诉列表框项目的高度),另一个用于实际渲染项目。e、 g

this.list.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.list_DrawItem);
this.list.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.list_MeasureItem);
使用GDI+DrawImage(其中g是图形上下文)将图像渲染到列表中相当简单:

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窗体,其中包含系统上所有字体的所有者绘制列表框,生成高度可变的所有者绘制列表项:

使用系统;
使用系统图;
使用System.Windows.Forms;
名称空间字体显示
{
公共类测试:System.Windows.Forms.Form
{
专用字头;
private System.Windows.Forms.ListBox列表;
private System.ComponentModel.Container components=null;
公开考试()
{
初始化组件();
head=新字体(“Arial”,10,GraphicsUnit.Pixel);
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置){
if(组件!=null){
组件。Dispose();
}
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.list=new System.Windows.Forms.ListBox();
这个.SuspendLayout();
// 
//名单
// 
this.list.DrawMode=System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.list.IntegralHeight=false;
this.list.Location=新系统.图纸.点(12,12);
this.list.Name=“list”;
this.list.Size=新系统图纸尺寸(604323);
this.list.TabIndex=0;
this.list.DrawItem+=new System.Windows.Forms.DrawItemEventHandler(this.list\u DrawItem);
this.list.MeasureItem+=新系统.Windows.Forms.MeasureItemEventHandler(this.list\u MeasureItem);
// 
//试验
// 
this.AutoScaleBaseSize=新系统.图纸.尺寸(6,15);
this.ClientSize=新系统.Drawing.Size(520358);
this.Controls.Add(this.list);
this.Name=“Test”;
this.Text=“Display”;
this.Load+=new System.EventHandler(this.Test\u Load);
this.Resize+=new System.EventHandler(this.Display\u Resize);
此选项为.resume布局(false);
}
#端区
[状态线程]
静态void Main()
{
运行(新测试());
}
私有无效测试加载(对象发送方、事件参数)
{
试一试{
//循环所有字体系列
FontFamily[]族=FontFamily.families;
foreach(FontFamily in families){
尝试{list.Items.Add(新字体(family,20,FontStyle.Regular,GraphicsUnit.Pixel));继续;}
捕获{}
}
显示大小(此为EventArgs.Empty);
}
抓住{
}
}
私有无效显示\调整大小(对象发送器,System.EventArgs e)
{
矩形r=this.ClientRectangle;
list.SetBounds(list.Left,
列表。顶部,
r、 宽度-(列表左*2),
r、 高度-(list.Top+list.Left));
}
公共字符串TextValue=“示例字符串”;
公共字符串格式
{
得到
{
StringFormat格式=StringFormat.GenericTyphographic;
format.FormatFlags |=StringFormatFlags.NoWrap;
返回格式;
}
}
私有无效列表\u DrawItem(对象发送者,System.Windows.Forms.DrawItemEventArgs e)
{
刷回=空;
刷前=空;
笔刷htext=null;
矩形r;
试一试{
Font=(Font)列表项[如索引];
//环路
如果((e.State&DrawItemState.Selected)!=0){
back=新的SolidBrush(颜色为深蓝色);
fore=新的SolidBrush(颜色为白色);
htext=新的SolidBrush(颜色为橙色);
}
否则{
背面=新的SolidBrush(颜色为白色);
fore=新的SolidBrush(颜色为黑色);
htext=新的SolidBrush(Color.DarkRed);
}
//填空
e、 图形。填充矩形(背面,e.Bounds);
//获取标题的大小
SizeF szHeader=e.Graphics.MeasureString(font.Name,head,int.MaxValue,Format);
SizeF
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 {
            }
        }
    }
}