• 大小: 9.87M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-14
  • 语言: C#
  • 标签: winform  C#  c  ORM  显示  

资源简介

直接读取ttc、oft、ttf格式的字库,显示出来。

本例字库在bin文件夹里面,字库网上很多,自行下载。


static class NativeMethods
		{
			const string GDI32 = "gdi32.dll";
			const string USER32 = "user32.dll";

			struct Rect
			{
				public long Left, Top, Right, Bottom;
				public Rect (Rectangle rect) {
					this.Left = rect.Left;
					this.Top = rect.Top;
					this.Right = rect.Right;
					this.Bottom = rect.Bottom;
				}
			}

			static uint ToGdiColor (Color color) {
				return (uint)(color.R | color.G << 8 | color.B << 16);
			}

			[DllImport (GDI32)]
			internal static extern IntPtr CreateFont (
				int nHeight,
				int nWidth,
				int nEscapement,
				int nOrientation,
				int fnWeight,
				uint fdwItalic,
				uint fdwUnderline,
				uint fdwStrikeOut,
				uint fdwCharSet,
				uint fdwOutputPrecision,
				uint fdwClipPrecision,
				uint fdwQuality,
				uint fdwPitchAndFamily,
				string lpszFace
				);
			[DllImport (GDI32)]
			internal static extern IntPtr SelectObject (HandleRef hdc, IntPtr obj);

			[DllImport (GDI32)]
			internal static extern bool DeleteObject (HandleRef obj);

			[DllImport (USER32, CharSet = CharSet.Auto)]
			static extern int DrawText (HandleRef hDC, string lpchText, int nCount, ref Rect lpRect, uint uFormat);
			internal static int DrawText (HandleRef hDC, string text, Rectangle rect, uint format) {
				var r = new Rect (rect);
				return DrawText (hDC, text, text.Length, ref r, format);
			}

			[DllImport (GDI32)]
			static extern uint SetTextColor (HandleRef hdc, uint crColor);
			internal static uint SetTextColor (HandleRef hdc, Color color) {
				return SetTextColor (hdc, ToGdiColor (color));
			}

			[DllImport (GDI32)]
			internal static extern uint SetBkMode (HandleRef hdc, int mode);
		}

资源截图

代码片段和文件信息

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1 () {
InitializeComponent ();
}

public static void RenderText (IDeviceContext hdc string text string fontFamily Color color Rectangle region int size)
        {
var h = new HandleRef (null hdc.GetHdc ());
var p = new HandleRef (null NativeMethods.CreateFont (size 0 0 0 0 0 0 0 1/*Ansi_encoding*/ 0 0 4 0 fontFamily));
try {
NativeMethods.Selectobject (h p.Handle);
NativeMethods.SetBkMode (h 1);
NativeMethods.SetTextColor (h color);
NativeMethods.DrawText (h text region 0x0100);
}
finally {
NativeMethods.DeleteO

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     30208  2017-05-03 13:08  UserFONT\.vs\GdiTextRendererDemo\v14\.suo

     文件         69  2017-04-28 13:09  UserFONT\app.config

     文件      12800  2017-05-03 13:08  UserFONT\bin\Debug\GdiTextRendererDemo.exe

     文件         69  2017-04-28 13:09  UserFONT\bin\Debug\GdiTextRendererDemo.exe.config

     文件      22016  2017-05-03 13:08  UserFONT\bin\Debug\GdiTextRendererDemo.pdb

     文件      21680  2017-05-03 13:08  UserFONT\bin\Debug\GdiTextRendererDemo.vshost.exe

     文件         69  2017-04-28 13:09  UserFONT\bin\Debug\GdiTextRendererDemo.vshost.exe.config

     文件        490  2016-07-16 19:44  UserFONT\bin\Debug\GdiTextRendererDemo.vshost.exe.manifest

     文件    6589188  2007-11-30 12:47  UserFONT\bin\Debug\琉丽字体.ttc

     文件    3410628  2012-08-28 09:27  UserFONT\bin\Debug\白舟侍書体.ttf

     文件    7347872  2012-11-13 21:55  UserFONT\bin\Debug\良怀行书.otf

     文件       4177  2017-05-03 13:06  UserFONT\Form1.cs

     文件       6981  2017-05-03 11:23  UserFONT\Form1.Designer.cs

     文件       5814  2017-05-03 11:23  UserFONT\Form1.resx

     文件       3472  2017-04-28 13:09  UserFONT\GdiTextRendererDemo.csproj

     文件       1840  2017-04-28 13:09  UserFONT\GdiTextRendererDemo.sln

     文件        374  2013-03-06 17:17  UserFONT\Program.cs

     文件       1392  2013-03-06 14:52  UserFONT\Properties\AssemblyInfo.cs

     文件       2888  2017-04-28 13:09  UserFONT\Properties\Resources.Designer.cs

     文件       5612  2013-03-06 14:52  UserFONT\Properties\Resources.resx

     文件       1124  2017-04-28 13:09  UserFONT\Properties\Settings.Designer.cs

     文件        249  2013-03-06 14:52  UserFONT\Properties\Settings.settings

     目录          0  2017-05-02 14:05  UserFONT\.vs\GdiTextRendererDemo\v14

     目录          0  2017-05-02 13:59  UserFONT\.vs\GdiTextRendererDemo

     目录          0  2017-05-03 13:08  UserFONT\bin\Debug

     目录          0  2017-04-28 13:09  UserFONT\bin\Release

     目录          0  2017-05-03 13:09  UserFONT\obj\Debug

    ...D.H.         0  2017-05-02 13:59  UserFONT\.vs

     目录          0  2017-05-02 13:58  UserFONT\bin

     目录          0  2017-05-02 13:58  UserFONT\obj

............此处省略5个文件信息

评论

共有 条评论