读书人

C#string,字体,该如何处理

发布时间: 2013-02-25 10:23:36 作者: rapoo

C#,string,字体
在winformk中,
如何把string str="基本生活";
转化为 str="基本生活"(粗体);
[解决办法]
你设置字体不就行了,动态的设置,需要 new System.Drawing.Font(this.Font, FontStyle.Bold)
[解决办法]
comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
然后在comboBox1_DrawItem里自己画
[解决办法]
internal System.Windows.Forms.ComboBox ComboBox1;
private string[] animals;

// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list. The drop-down style is set to
// ComboBox.DropDown, which requires the user to click on the arrow to
// see the list.
private void InitializeComboBox()
{
this.ComboBox1 = new ComboBox();
this.ComboBox1.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.ComboBox1.Location = new System.Drawing.Point(10, 20);
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(100, 120);
this.ComboBox1.DropDownWidth = 250;
this.ComboBox1.TabIndex = 0;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
animals = new string[]{"Elephant", "c r o c o d i l e", "lion"};
ComboBox1.DataSource = animals;
this.Controls.Add(this.ComboBox1);

// Hook up the MeasureItem and DrawItem events
this.ComboBox1.DrawItem +=
new DrawItemEventHandler(ComboBox1_DrawItem);
this.ComboBox1.MeasureItem +=
new MeasureItemEventHandler(ComboBox1_MeasureItem);
}

// If you set the Draw property to DrawMode.OwnerDrawVariable,
// you must handle the MeasureItem event. This event handler
// will set the height and width of each item before it is drawn.
private void ComboBox1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{

switch(e.Index)
{
case 0:
e.ItemHeight = 45;
break;
case 1:
e.ItemHeight = 20;
break;
case 2:
e.ItemHeight = 35;
break;
}
e.ItemWidth = 260;

}

// You must handle the DrawItem event for owner-drawn combo boxes.
// This event handler changes the color, size and font of an
// item based on its position in the array.
private void ComboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{

float size = 0;
System.Drawing.Font myFont;
FontFamily family = null;

System.Drawing.Color animalColor = new System.Drawing.Color();


switch(e.Index)
{
case 0:
size = 30;
animalColor = System.Drawing.Color.Gray;
family = FontFamily.GenericSansSerif;
break;
case 1:
size = 10;
animalColor = System.Drawing.Color.LawnGreen;
family = FontFamily.GenericMonospace;
break;
case 2:
size = 15;
animalColor = System.Drawing.Color.Tan;
family = FontFamily.GenericSansSerif;
break;
}

// Draw the background of the item.
e.DrawBackground();

// Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2,
e.Bounds.Height, e.Bounds.Height-4);
e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

// Draw each string in the array, using a different size, color,
// and font for each item.
myFont = new Font(family, size, FontStyle.Bold);
e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}

msdn的例子。
[解决办法]
你看你的控件属性里面有没有foreground这种属性,或者font之类的,改成bold就行了
[解决办法]

侯捷<MFC...> 讲的经典啊,  勿在阜沙筑高台

.Net ,就是忽悠新手, 在流沙搭积木, 危险啊

还是好好去学学 基础吧,  新手别一开始,就沉溺于玩玩GUI,玩玩漂亮WPF界面 带来的快感..这种东西带来的编程的快感,虚假的成就感,容易让新手走邪路,..

拖拖控件,被当成宝贝,以石为玉而炫之又炫...
像陈年佳酿的多年没大变化的TCP/IP,算法,数据结构,却被他们束之高阁.

读书人网 >C#

热点推荐