我想在Panel控件上画不同颜色和不同字体的字.请问怎么实现?
解决方案 »
- 怎么查询数据库新添加的消息,然后让notifyIcon1闪烁
- 请教这样的数据层接口应该怎么写,有相关经验的请进来看看,谢谢了
- 为什么会算错????一个神奇的问题
- 关于Crystalreport的翻页、显示问题
- ORA-00907:缺少右括号 请各位仔细看看 谢 在线等
- 字符串问题
- [assembly: log4net.Config.XmlConfigurator()] 是在什么时候运行的?
- 自己用C#写的一个dll使用问题?dll我写好了不会用,怎么用他,我里面说的详细,请进来帮帮我,新手第一次。。
- C#中怎样用 回车键 代替 Tab 键?????
- Chart 周期显示
- socket中accept的问题
- winform从数据库导出成excel,页居页脚
{
e.Graphics.FillRectangle(......)
e.Graphics.DrawString(.......);
}
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);
}
}
}
还有个问题.我是把一个字符串 转换成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;
}
第一列 第二列 第三列 第四列
测试小区I:房屋 377 间,其中已接房: 11/2.92%
用DataGrid或DataGridView是非常方便的