C# [Win32] [GDI+] [API] Load HFONT from Memory

 

 

 1     // gdiplusenums.h
 2 
 3     //--------------------------------------------------------------------------
 4     // FontStyle: face types and common styles
 5     //--------------------------------------------------------------------------
 6 
 7     public enum FontStyle
 8     {
 9         FontStyleRegular = 0,
10         FontStyleBold = 1,
11         FontStyleItalic = 2,
12         FontStyleBoldItalic = 3,
13         FontStyleUnderline = 4,
14         FontStyleStrikeout = 8
15     };
16 
17     //--------------------------------------------------------------------------
18     // Unit constants
19     //--------------------------------------------------------------------------
20 
21     public enum Unit
22     {
23         UnitWorld,      // 0 -- World coordinate (non-physical unit)
24         UnitDisplay,    // 1 -- Variable -- for PageTransform only
25         UnitPixel,      // 2 -- Each unit is one device pixel.
26         UnitPoint,      // 3 -- Each unit is a printer's point, or 1/72 inch.
27         UnitInch,       // 4 -- Each unit is 1 inch.
28         UnitDocument,   // 5 -- Each unit is 1/300 inch.
29         UnitMillimeter  // 6 -- Each unit is 1 millimeter.
30     };

 

 1     // gdiplusgpstubs.h
 2 
 3     public unsafe struct GpFont { };
 4 
 5     public unsafe struct GpFontCollection { };
 6 
 7     public unsafe struct GpFontFamily { };
 8 
 9     public unsafe struct GpStatus
10     {
11         private Int32 m_val;
12     };

 

 1     // gdiplusflat.h
 2     [DllImport("gdiplus.dll")]
 3     public static extern
 4     int /*GpStatus*/ /*WINGDIPAPI*/
 5     GdipCloneFontFamily(GpFontFamily* fontFamily, GpFontFamily** clonedFontFamily);
 6 
 7     [DllImport("gdiplus.dll")]
 8     public static extern
 9     int /*GpStatus*/ /*WINGDIPAPI*/
10     GdipCreateFont(
11         /*GDIPCONST*/ GpFontFamily* fontFamily,
12         float /*REAL*/ emSize,
13         int /*INT*/ style,
14         Unit unit,
15         GpFont** font
16     );
17 
18     [DllImport("gdiplus.dll")]
19     public static extern
20     int /*GpStatus*/ /*WINGDIPAPI*/
21     GdipDeleteFont(GpFont* font);
22 
23     [DllImport("gdiplus.dll")]
24     public static extern
25     int /*GpStatus*/ /*WINGDIPAPI*/
26     GdipDeletePrivateFontCollection(GpFontCollection** fontCollection);
27 
28     [DllImport("gdiplus.dll")]
29     public static extern
30     int /*GpStatus*/ /*WINGDIPAPI*/
31     GdipGetFamily(GpFont* font, GpFontFamily** family);
32 
33     [DllImport("gdiplus.dll")]
34     public static extern
35     int /*GpStatus*/ /*WINGDIPAPI*/
36     GdipGetFontCollectionFamilyCount(
37         GpFontCollection* fontCollection,
38         int* /*INT**/ numFound
39     );
40 
41     [DllImport("gdiplus.dll")]
42     public static extern
43     int /*GpStatus*/ /*WINGDIPAPI*/
44     GdipGetFontCollectionFamilyList(
45         [In] /*_In_*/ GpFontCollection* fontCollection,
46         [In] /*_In_*/ int /*INT*/ numSought,
47         [Out] /*_Out_writes_to_(numSought, * numFound)*/ GpFontFamily** gpfamilies/*[]*/,
48         [Out] /*_Out_*/                                 int* /*INT**/ numFound
49     );
50 
51     [DllImport("gdiplus.dll")]
52     public static extern
53     int /*GpStatus*/ /*WINGDIPAPI*/
54     GdipGetFontHeight(/*GDIPCONST*/ GpFont* font, /*GDIPCONST*/ GpGraphics* graphics,
55                       float* /*REAL**/ height);
56 
57     [DllImport("gdiplus.dll")]
58     public static extern
59     int /*GpStatus*/ /*WINGDIPAPI*/
60     GdipGetFontSize(GpFont* font, float* /*REAL**/ size);
61 
62     [DllImport("gdiplus.dll")]
63     public static extern
64     int /*GpStatus*/ /*WINGDIPAPI*/
65     GdipGetFontStyle(GpFont* font, int* /*INT**/ style);
66 
67     [DllImport("gdiplus.dll")]
68     public static extern
69     int /*GpStatus*/ /*WINGDIPAPI*/
70     GdipGetFontUnit(GpFont* font, Unit* unit);
71 
72     [DllImport("gdiplus.dll")]
73     public static extern
74     int /*GpStatus*/ /*WINGDIPAPI*/
75     GdipGetLogFontA(GpFont* font, GpGraphics* graphics, LOGFONTA* logfontA);
76 
77     [DllImport("gdiplus.dll")]
78     public static extern
79     int /*GpStatus*/ /*WINGDIPAPI*/
80     GdipGetLogFontW(GpFont* font, GpGraphics* graphics, LOGFONTW* logfontW);
81 
82     [DllImport("gdiplus.dll")]
83     public static extern
84     int /*GpStatus*/ /*WINGDIPAPI*/
85     GdipNewInstalledFontCollection(GpFontCollection** fontCollection);
86 
87     [DllImport("gdiplus.dll")]
88     public static extern
89     int /*GpStatus*/ /*WINGDIPAPI*/
90     GdipNewPrivateFontCollection(GpFontCollection** fontCollection);
91 
92     [DllImport("gdiplus.dll")]
93     public static extern
94     int /*GpStatus*/ /*WINGDIPAPI*/
95     GdipPrivateAddMemoryFont(
96         GpFontCollection* fontCollection,
97         /*GDIPCONST*/ void* memory,
98         int /*INT*/ length
99     );

 

 1     // wingdi.h
 2     [DllImport("gdi32.dll")]
 3     public static extern
 4     /*WINGDIAPI*/ void* /*HANDLE*/ /*WINAPI*/ AddFontMemResourceEx([In] /*_In_reads_bytes_(cjSize)*/ void* /*PVOID*/ pFileView,
 5                                          [In] /*_In_*/ uint /*DWORD*/ cjSize,
 6                                          /*_Reserved_*/ void* /*PVOID*/ pvResrved,
 7                                          [In] /*_In_*/ uint* /*DWORD**/ pNumFonts);
 8 
 9     [DllImport("gdi32.dll")]
10     public static extern void* CreateFontIndirectW(
11       [In] LOGFONTW* lplf
12     );
13 
14     [DllImport("gdi32.dll")]
15     public static extern int DeleteObject(
16       [In] void* hObject
17     );
18 
19     [DllImport("gdi32.dll")]
20     public static extern /*WINGDIAPI*/ int /*BOOL*/ /*WINAPI*/ RemoveFontMemResourceEx([In] /*_In_*/ void* /*HANDLE*/ h);

 

 1     // winuser.h
 2     [DllImport("user32.dll")]
 3     public static extern void* GetDC(
 4       [In] void* hWnd
 5     );
 6 
 7     [DllImport("user32.dll")]
 8     public static extern int ReleaseDC(
 9       [In] void* hWnd,
10       [In] void* hDC
11     );

 

 1     public static bool GpSucceeded(int iGpStatus)
 2     {
 3         return iGpStatus == 0;
 4     }
 5 
 6     public static void* LoadHFontFromMemory(byte* pv, int iLength) {
 7         void* hfont = null;
 8         uint dw;
 9         _hfontres = AddFontMemResourceEx(pv, (uint)iLength, null, &dw);
10         if (_hfontres != null)
11         {
12             int gps;
13             GdiplusStartupInput gsi; gsi.GdiplusVersion = 2;
14             void* token;
15             if (GpSucceeded(gps = GdiplusStartup(&token, &gsi, null)))
16             {
17                 GpFontCollection* gpfc;
18                 if (GpSucceeded(gps = GdipNewPrivateFontCollection(&gpfc)))
19                 {
20                     if (GpSucceeded(gps = GdipPrivateAddMemoryFont(gpfc, pv, iLength)))
21                     {                    
22                         int f, f2;
23                         GdipGetFontCollectionFamilyCount(gpfc, &f);
24                         GpFontFamily** ppl = (GpFontFamily**)malloc((void*)(f * sizeof(void*)));
25                         GdipGetFontCollectionFamilyList(gpfc, f, ppl, &f2);
26                         GpFont* gpf;
27                         if (GpSucceeded(gps = GdipCreateFont(*ppl, 16f, (int)FontStyle.FontStyleRegular, Unit.UnitPoint, &gpf)))
28                         {                    
29                             void* hdc = GetDC(null);
30                             GpGraphics* gpg;
31                             if (GpSucceeded(gps = GdipCreateFromHDC(hdc, &gpg)))
32                             {
33                                 LOGFONTW lf;
34                                 GdipGetLogFontW(gpf, gpg, &lf);
35                                 hfont = CreateFontIndirectW(&lf);                        
36 
37                                 GdipDeleteGraphics(gpg);
38                             }
39                             ReleaseDC(null, hdc);
40                             GdipDeleteFont(gpf);
41                         }
42                         free(ppl);
43                     }
44                     GdipDeletePrivateFontCollection(&gpfc);
45                 }
46                 GdiplusShutdown(token);
47             }
48         }
49         return hfont;
50     }
51 
52     void* _hfontres;
53     void* _hfont;
54     static void* WndProc(
55       void* hwnd,
56       uint uMsg,
57       void* wParam,
58       void* lParam
59     ) {
60         switch (uMsg) {
61             case WM_CREATE: {
62                 byte* pbuf = null;
63                 //...
64                 // Load Font Type from [File | Resources] to Memory
65                 //...
66                 _hfont = LoadHFontFromMemory(pbuf);
67                 SendMessageW(hwnd, WM_SETFONT, _hfont, null);
68             }
69             break;
70             case WM_DESTROY: {
71                 DeleteObject(_hfont);
72                 RemoveFontMemResourceEx(_hfontres);
73                 //...
74             }
75             break;
76         }
77     }

 

 

 

转载于:https://www.cnblogs.com/shitekudasai/p/9562417.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值