C 中的字型

2021-06-28 19:24:09 字數 3806 閱讀 7382

c#中本來已經封裝了通用字型font,但是筆者認為這個font在使用過程中很不方便。

1) font的很多屬性都是唯讀的,不能通過更改屬性的值而對font進行操作;必須重新初始化font物件才行。例如:font.size=12;是不允許的。

2) font總歸是用來寫字的,但是c#中的font類與string類沒有什麼聯絡,這給font的使用帶來了一些麻煩。比如我想要知道乙個string物件使用了制定字型和字型大小以後,列印在螢幕上會占有多少畫素?或者我要知道乙個字型和字型大小,使得string中的內容顯示在螢幕上的長度剛好適合200個畫素?

3) font和string必須的使用另外的函式方法才能顯示在螢幕上,這對於經常要列印、顯示text的程式設計師來說,實在是太繁瑣了。

為了使這些問題能夠比較簡單解決,請看下面的這個類:

using system;

using system.collections.generic;

using system.drawing;

using system.text;

namespace coolcontrols

public coolfont(string fontname,single size)

public coolfont(font f)

public coolfont(string fontname, single size,fontstyle style)

public coolfont(font f, fontstyle style)

public coolfont(font f, single size)

#endregion

#region [ 屬性 ]

public string text

set

}public font font

set

}public string fontname

set}

public single fontsize

set}

public fontstyle style

set

}public int height

}public float sizeinpoints

}#endregion

#region [ 公有函式 ]

public void fitwidth(graphics g, float width)

public void fitheight(graphics g, float height)

public void fitrect(graphics g, rectangle rt)

public void drawstringfit(graphics g, brush brush, rectangle rt)

public void drawstringfit(graphics g, brush brush, rectangle rt, stringalignment alignment, stringalignment alignmentv)

#endregion

#region [ 靜態函式 ]

public static sizef getstringsize(string s, font f,graphics g)

public static sizef getstringsize(coolfont cf, graphics g)

public static float fitstringwidth(string s, font f, graphics g, float width)

}else if(sf.width < width)

cf.fontsize -= 0.1f;

}return cf.fontsize;

}public static float fitstringwidth(coolfont cf, graphics g, float width)

public static float fitstringheight(string s, font f, graphics g, float height)

}else if (sf.height < height)

cf.fontsize -= 0.1f;

}return cf.fontsize;

}public static float fitstringheight(coolfont cf, graphics g, float height)

public static float fitstringrect(string s, font f, graphics g, rectangle rt)

}else if (sf.width < rt.width)

cf.fontsize -= 0.1f;

}if (sf.height > rt.height)

}return cf.fontsize;

}public static float fitstringrect(coolfont cf, graphics g, rectangle rt)

public static void drawstringfit(string s, font f, graphics g, brush brush, rectangle rt)

}else if (sf.width < rt.width)

cf.fontsize -= 0.1f;

}if (sf.height > rt.height)

}stringformat format = new stringformat();

format.formatflags = stringformatflags.noclip;

format.alignment = stringalignment.center;

format.linealignment = stringalignment.center;

g.drawstring(cf.text, cf.font, brush, rt, format);

}public static void drawstringfit(coolfont cf, graphics g, brush brush, rectangle rt)

public static void drawstringfit(string s, font f, graphics g, brush brush, rectangle rt, stringalignment alignment, stringalignment alignmentv)

}else if (sf.width < rt.width)

cf.fontsize -= 0.1f;

}if (sf.height > rt.height)

}stringformat format = new stringformat();

format.formatflags = stringformatflags.noclip;

format.alignment = alignment;

format.linealignment = alignmentv;

g.drawstring(cf.text, cf.font, brush, rt, format);

}public static void drawstringfit(coolfont cf, graphics g, brush brush, rectangle rt, stringalignment alignment, stringalignment alignmentv)

#endregion

}}

這個類允許靜態或者動態訪問,並且實現了string,font和drawing的聯合,使得字串的列印、顯示操作變得很容易。 

C中字型顏色

1.改變整個控制台的顏色 用 system color 0a 其中color後面的0是背景色代號,a是前景色代號。各顏色 如下 0 黑色 1 藍色 2 綠色 3 湖藍色 4 紅色 5 紫色 6 黃色 7 白色 8 灰色 9 淡藍色 a 淡綠色 b 淡淺綠色 c 淡紅色 d 淡紫色 e 淡黃色 f 亮...

Visio中的字型對應Word中字型字型大小

最近在使用visio做流程圖時,發現其中的字型大小與word不一樣。visio中的字型大小使用pt表示。pt是字型單位。全稱為point,中文為 點 常見於軟體設計 排版印刷行業。在常規的排版中,字型大小以 點 來單位。活字的大小稱為 字型大小 而字母的寬度稱作 字寬 1點 0.376公釐 1.07...

Visio中的字型對應Word中字型字型大小

最近在使用visio做流程圖時,發現其中的字型大小與word不一樣。visio中的字型大小使用pt表示。pt是字型單位。全稱為point,中文為 點 常見於軟體設計 排版印刷行業。在常規的排版中,字型大小以 點 來單位。活字的大小稱為 字型大小 而字母的寬度稱作 字寬 1點 0.376公釐 1.07...