FontFamily 和Font 的区别

本文详细介绍了GDI+中字体系列的概念及其组成部分,包括如何通过字形、字号和单位创建字体对象,并提供了具体的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GDI+ 将字样相同但字形不同的字体分组为字体系列。例如,下面是同一个字样(Arial),不同的字形 :

-------------------------------------------------------- 

Arial Regular 常规

Arial Bold 粗体

Arial Italic 斜体

Arial Bold Italic 粗斜体

-------------------------------------------------

GDI+ 使用四种字形形成字体系列:常规、粗体、倾斜和粗斜体。像 narrow 和 rounded 之类的形容词不被视为字形;而是作为字体系列名的一部分。如下,Arial,Arial Black,Arial Narrow 是三个不同的字样:

--------------------------------------------------------

Arial 

Arial Black

Arial Narrow


------------------------------------------------- 

在GDI+中,字样+字形+字号+单位构成了“字体”,在使用 GDI+ 绘制文本之前,您需要构造一个 FontFamily 对象(指定字样)和一个 Font (“字体”)对象。FontFamily 对象指定字样(例如 Arial,宋体,),而 Font 对象指定字号、字形和单位。

示例
长春电脑维修下面的示例构造一个字号为 48,以像素为单位、字形为“粗斜体”、字样为“宋体”的字体。在下面的代码中,传递给 Font 构造函数的第一个参数是 FontFamily 对象。第二个参数指定字体的大小,第三个参数确定字形,其单位由第四个参数确定(可以指定以像素,点...为单位)。

Pixel 为 GraphicsUnit 枚举的一个成员,Regular 是 FontStyle 枚举的一个成员。



 
  1. uses
  2. GDIPAPI,GDIPOBJ;
  3. procedure TForm1.FormPaint(Sender: TObject);
  4. var
  5. g:TGPGraphics;
  6. font:TGPFont;
  7. fontfamily:TGPFontFamily;
  8. sb:TGPSolidBrush;
  9. begin
  10. g:=TGPGraphics.Create(Canvas.Handle);
  11. fontfamily:=TGPFontFamily.Create('宋体'); //建立“字样”对象
  12. font:=TGPFont.Create(fontfamily,16,FontStyleBoldItalic,UnitPixel); //建立“字体”对象
  13. sb:=TGPSolidBrush.Create(aclRed);
  14. g.DrawString('小侃',-1,font,MakePoint(20.0,10.0),sb);
  15. sb.free;
  16. font.free;
  17. fontfamily.free;
  18. g.free;
  19. end;


### Font Family in Programming and Design Tools Usage In programming environments, especially those involving graphical user interfaces (GUIs), handling fonts safely across multiple threads has been addressed. Using color and font objects in multiple threads is now safe to do[^1]. This advancement ensures that developers can manipulate `FontFamily` properties without encountering thread safety issues. For implementing `FontFamily`, various libraries provide comprehensive support depending on the platform: #### Python with Tkinter Example Tkinter offers a straightforward way to set up different font families within GUI applications. ```python import tkinter as tk from tkinter import font root = tk.Tk() custom_font = font.Font(family="Helvetica", size=12) label = tk.Label(root, text="Sample Text", font=custom_font) label.pack() root.mainloop() ``` #### C# WPF Implementation WPF provides robust mechanisms for defining and applying custom font families through XAML or programmatically. ```csharp using System.Windows; using System.Windows.Media; namespace FontDemoApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var fontFamily = new FontFamily("Arial"); Label label = new Label { Content = "Hello World", FontFamily = fontFamily, FontSize = 20 }; this.Content = label; } } } ``` #### CSS Web Development Integration When working with web technologies, integrating specific font families into stylesheets enhances visual appeal while maintaining cross-browser compatibility. ```css body { font-family: 'Times New Roman', Times, serif; } h1 { font-family: Arial, Helvetica, sans-serif; } ``` ### Handling Fonts Safely Across Threads The ability to use font objects across multiple threads securely means developers no longer need to worry about synchronization when accessing shared resources like `FontFamily`. This improvement significantly reduces complexity in multi-threaded application development where UI elements frequently interact with background processes.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值