我想在Panel控件上画不同颜色和不同字体的字.请问怎么实现?

解决方案 »

  1.   

     private void panel1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.FillRectangle(......)
                e.Graphics.DrawString(.......);
            }
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.panel1.Paint+=new PaintEventHandler(panel1_Paint);
            }        void panel1_Paint(object sender, PaintEventArgs e)
            {
                Font myfont=System.Drawing.SystemFonts.DefaultFont;
                Brush bh = System.Drawing.Brushes.Red;            Rectangle rg = new Rectangle(0, 0, 50, 50);
                //画红色的字,字体是系统默认的,你修改这些参数就可以实现你的要求了
                e.Graphics.DrawString("abc", myfont, bh, rg);
                       }
        }
    }
      

  3.   


    还有个问题.我是把一个字符串 转换成Char[]  再循环这个数组一个一个画出来但是字符间距很别扭,请问怎么解决.        private void panelControl1_Paint(object sender, PaintEventArgs e)
            {
                //Font myfont = System.Drawing.SystemFonts.DefaultFont;
                //Brush bh = System.Drawing.Brushes.Red;
                //string text = "测试小区I:房屋#377#间,其中 已接房:#11/2.92%# 已装修:#3/0.80%# 已入住:#11/2.92%# 空置房:#365/96.82%# 接房装修比:#27.27%# 接房入住比:#100.00%#。车位#377#个,其中 已售出:#2/33.33%# 已出租:#0/0.00%# 闲置:#4/66.67%# 出租:#0/0.00%#。";
                //Rectangle rg = new Rectangle(0, 5, this.panelControl1.Width, this.panelControl1.Height);
                ////画红色的字,字体是系统默认的,你修改这些参数就可以实现你的要求了
                //e.Graphics.DrawString(text, myfont, bh, rg);            Graphics graphics = e.Graphics;
                Font myFont = new Font("宋体", 9.0f);
                SolidBrush brush;
                //设置文本输出格式:在当前窗口中居中显示
                StringFormat fmt = new StringFormat();
                fmt.Alignment = StringAlignment.Near;
                fmt.LineAlignment = StringAlignment.Near;
                //输出文本
                string text = "测试小区I:房屋#377#间,其中 已接房:#11/2.92%# 已装修:#3/0.80%# 已入住:#11/2.92%# 空置房:#365/96.82%# 接房装修比:#27.27%# 接房入住比:#100.00%#。车位#377#个,其中 已售出:#2/33.33%# 已出租:#0/0.00%# 闲置:#4/66.67%# 出租:#0/0.00%#。";
                //string text = "测试小区I:房屋#377#间";
                char[] charStr = text.ToCharArray();
                bool b = false;
                int widht = 0;
                for (int i = 0; i < charStr.Length; i++)
                {
                    if (charStr[i].ToString() == "#")
                    {
                        if (b)
                            b = false;
                        else
                            b = true;
                    }
                    if (b)
                        brush = new SolidBrush(Color.Red);
                    else
                        brush = new SolidBrush(Color.Black);
                    if (charStr[i].ToString() != "#")
                        graphics.DrawString(charStr[i].ToString(), myFont, brush, new Rectangle(widht += GetTextSize(charStr[i].ToString(), myFont).Width, 5, this.Width, this.Height), fmt);
                }
            }        private Size GetTextSize(string text, Font font)
            {
                Size TextSize = System.Windows.Forms.TextRenderer.MeasureText(text, font);
                if (TextSize.Width == 17)
                    TextSize.Width -= 5;
                return TextSize;
            }
      

  4.   

    你如果只是输出文字的话就不要用GDI了,用GridView或其他控件来做也是比较好控制的
      

  5.   

    用其他控件怎么实现?  前提是不用 webbrowser
      

  6.   

    你想规整地输出的话可以用DataGridView,把文字往单元格里填就好了,然后设置单元格的文字样式
      

  7.   

    可以设置一个四列的GridView,设置第二列和第四列为红色字体就可以了
    第一列           第二列   第三列         第四列
    测试小区I:房屋   377   间,其中已接房:  11/2.92%
      

  8.   

    GDI 排列太麻烦啦
    用DataGrid或DataGridView是非常方便的
      

  9.   

    最终没能用GDI实现我的需求.还是使用webbrowser+HTML解决问题.谢谢各位提供帮助.