动态加载和卸载字体

259 篇文章 4 订阅

C#动态加载和卸载字体(以文件的方式)

2009-07-01 17:18

因为是在CE里,所以是用Coredll   PC机用的不是这个,可查MSDN

 

[DllImport("coredll", EntryPoint = "AddFontResource")]

        private static extern int AddFontResource([In,MarshalAs( UnmanagedType.LPWStr)]string fontSource);

 

        [DllImport("coredll", EntryPoint = "SendMessage")]

        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

 

 

int installFont = AddFontResource(@"/SDMEM/MSYH.TTF"); //这是字体的安装 返回不为0即成功

 

SendMessage((IntPtr)0xffff, 0x001d, IntPtr.Zero, IntPtr.Zero); //通知其它正在运行的应用程序,有新字体注册了

 

                //枚举字体

                InstalledFontCollection enumFonts = new InstalledFontCollection();

                FontFamily[] fonts = enumFonts.Families;

                foreach (FontFamily font in fonts)

                {

                    MessageBox.Show(font.Name);

                }

 

如果只是自己的程序使用的话,那么就没必要这么折腾了

 

System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();

 

 

            privateFonts.AddFontFile("fileName");    //D:/aa/abc.ttf

 

 

            System.Drawing.Font font = new Font(privateFonts.Families[0], 12);

 

 

(以文件的方式)

2009-07-01 17:18

 

 

 

Delphi程序中使用自带字体

关键字: delphi

initialization
  AddFontResource(PChar(dirAppRoot + 'XXXXX.TTF'));

finalization
  RemoveFontResource(PChar(dirAppRoot + 'XXXXX.TTF'));

 

 

 

java程序中直接使用中文字体文件。代码中的hb.ttf文件请用你自己的中文字体文件代替
(网上下载吧)。



import java.io.File;
import java.awt.*;

public class testFont {
  private static String fontpath = "c://test//";
  private static java.io.File file = new java.io.File(fontpath + "hb.ttf");
  public testFont() {
  }

  public static void main(String args[]) {
    if (!file.exists()) {
      System.out.println("file not found");
      return;
    }


    try {
      java.io.FileInputStream fi = new java.io.FileInputStream(file);
      java.io.BufferedInputStream fb = new java.io.BufferedInputStream(fi);
      Font nf = Font.createFont(Font.TRUETYPE_FONT, fb);
      nf = nf.deriveFont(Font.BOLD, 10);
      System.out.println(nf.getFontName());
      System.out.println(nf.getSize());
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}


运行结果;
C:/test>java  testFont
 
方正华隶简体
10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值