C# [Win32] [GDI+] [API] Load HFONT from Memory
// gdiplusenums.h
//--------------------------------------------------------------------------
// FontStyle: face types and common styles
//--------------------------------------------------------------------------
public enum FontStyle
{
FontStyleRegular = ,
FontStyleBold = ,
FontStyleItalic = ,
FontStyleBoldItalic = ,
FontStyleUnderline = ,
FontStyleStrikeout =
};
//--------------------------------------------------------------------------
// Unit constants
//--------------------------------------------------------------------------
public enum Unit
{
UnitWorld, // 0 -- World coordinate (non-physical unit)
UnitDisplay, // 1 -- Variable -- for PageTransform only
UnitPixel, // 2 -- Each unit is one device pixel.
UnitPoint, // 3 -- Each unit is a printer's point, or 1/72 inch.
UnitInch, // 4 -- Each unit is 1 inch.
UnitDocument, // 5 -- Each unit is 1/300 inch.
UnitMillimeter // 6 -- Each unit is 1 millimeter.
};
// gdiplusgpstubs.h
public unsafe struct GpFont { };
public unsafe struct GpFontCollection { };
public unsafe struct GpFontFamily { };
public unsafe struct GpStatus
{
private Int32 m_val;
};
// gdiplusflat.h
[DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipCloneFontFamily(GpFontFamily* fontFamily, GpFontFamily** clonedFontFamily); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipCreateFont(
/*GDIPCONST*/ GpFontFamily* fontFamily,
float /*REAL*/ emSize,
int /*INT*/ style,
Unit unit,
GpFont** font
); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipDeleteFont(GpFont* font); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipDeletePrivateFontCollection(GpFontCollection** fontCollection); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFamily(GpFont* font, GpFontFamily** family); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontCollectionFamilyCount(
GpFontCollection* fontCollection,
int* /*INT**/ numFound
); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontCollectionFamilyList(
[In] /*_In_*/ GpFontCollection* fontCollection,
[In] /*_In_*/ int /*INT*/ numSought,
[Out] /*_Out_writes_to_(numSought, * numFound)*/ GpFontFamily** gpfamilies/*[]*/,
[Out] /*_Out_*/ int* /*INT**/ numFound
); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontHeight(/*GDIPCONST*/ GpFont* font, /*GDIPCONST*/ GpGraphics* graphics,
float* /*REAL**/ height); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontSize(GpFont* font, float* /*REAL**/ size); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontStyle(GpFont* font, int* /*INT**/ style); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetFontUnit(GpFont* font, Unit* unit); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetLogFontA(GpFont* font, GpGraphics* graphics, LOGFONTA* logfontA); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipGetLogFontW(GpFont* font, GpGraphics* graphics, LOGFONTW* logfontW); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipNewInstalledFontCollection(GpFontCollection** fontCollection); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipNewPrivateFontCollection(GpFontCollection** fontCollection); [DllImport("gdiplus.dll")]
public static extern
int /*GpStatus*/ /*WINGDIPAPI*/
GdipPrivateAddMemoryFont(
GpFontCollection* fontCollection,
/*GDIPCONST*/ void* memory,
int /*INT*/ length
);
// wingdi.h
[DllImport("gdi32.dll")]
public static extern
/*WINGDIAPI*/ void* /*HANDLE*/ /*WINAPI*/ AddFontMemResourceEx([In] /*_In_reads_bytes_(cjSize)*/ void* /*PVOID*/ pFileView,
[In] /*_In_*/ uint /*DWORD*/ cjSize,
/*_Reserved_*/ void* /*PVOID*/ pvResrved,
[In] /*_In_*/ uint* /*DWORD**/ pNumFonts); [DllImport("gdi32.dll")]
public static extern void* CreateFontIndirectW(
[In] LOGFONTW* lplf
); [DllImport("gdi32.dll")]
public static extern int DeleteObject(
[In] void* hObject
); [DllImport("gdi32.dll")]
public static extern /*WINGDIAPI*/ int /*BOOL*/ /*WINAPI*/ RemoveFontMemResourceEx([In] /*_In_*/ void* /*HANDLE*/ h);
// winuser.h
[DllImport("user32.dll")]
public static extern void* GetDC(
[In] void* hWnd
); [DllImport("user32.dll")]
public static extern int ReleaseDC(
[In] void* hWnd,
[In] void* hDC
);
public static bool GpSucceeded(int iGpStatus)
{
return iGpStatus == ;
} public static void* LoadHFontFromMemory(byte* pv, int iLength) {
void* hfont = null;
uint dw;
_hfontres = AddFontMemResourceEx(pv, (uint)iLength, null, &dw);
if (_hfontres != null)
{
int gps;
GdiplusStartupInput gsi; gsi.GdiplusVersion = ;
void* token;
if (GpSucceeded(gps = GdiplusStartup(&token, &gsi, null)))
{
GpFontCollection* gpfc;
if (GpSucceeded(gps = GdipNewPrivateFontCollection(&gpfc)))
{
if (GpSucceeded(gps = GdipPrivateAddMemoryFont(gpfc, pv, iLength)))
{
int f, f2;
GdipGetFontCollectionFamilyCount(gpfc, &f);
GpFontFamily** ppl = (GpFontFamily**)malloc((void*)(f * sizeof(void*)));
GdipGetFontCollectionFamilyList(gpfc, f, ppl, &f2);
GpFont* gpf;
if (GpSucceeded(gps = GdipCreateFont(*ppl, 16f, (int)FontStyle.FontStyleRegular, Unit.UnitPoint, &gpf)))
{
void* hdc = GetDC(null);
GpGraphics* gpg;
if (GpSucceeded(gps = GdipCreateFromHDC(hdc, &gpg)))
{
LOGFONTW lf;
GdipGetLogFontW(gpf, gpg, &lf);
hfont = CreateFontIndirectW(&lf); GdipDeleteGraphics(gpg);
}
ReleaseDC(null, hdc);
GdipDeleteFont(gpf);
}
free(ppl);
}
GdipDeletePrivateFontCollection(&gpfc);
}
GdiplusShutdown(token);
}
}
return hfont;
} void* _hfontres;
void* _hfont;
static void* WndProc(
void* hwnd,
uint uMsg,
void* wParam,
void* lParam
) {
switch (uMsg) {
case WM_CREATE: {
byte* pbuf = null;
//...
// Load Font Type from [File | Resources] to Memory
//...
_hfont = LoadHFontFromMemory(pbuf);
SendMessageW(hwnd, WM_SETFONT, _hfont, null);
}
break;
case WM_DESTROY: {
DeleteObject(_hfont);
RemoveFontMemResourceEx(_hfontres);
//...
}
break;
}
}

C# [Win32] [GDI+] [API] Load HFONT from Memory的更多相关文章
- C#中可直接调用WIN32的API函数--USER32.DLL
Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...
- Delphi的Win32的API调用简单介绍
1. 介绍Win32 API和Win32系统.还要讨论Win32系统的功能以及它与16位系统在功能上的几个主要区别.只是让对Win32系统有一个基本的了解.当已经基本了解Win32操作后,就可 ...
- Win32调试API原理
在Win32中自带了一些API函数,它们提供了相当于一般调试器的大多数功能,这些函数统称为Win32调试API(Win32 Debug API).利用这些API可以做到加载一个程序或捆绑到一个正在运行 ...
- C#调用Win32 的API函数--User32.dll ----转载
Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果. using System; ...
- Windows Graphics Programming Win32 GDI and DirectDraw第六章疑问
<Windows Graphics Programming Win32 GDI and DirectDraw>6.1节中有这样的描述: The Windows NT/2000 graphi ...
- Fielddata is disabled on text fields by default. Set fielddata=true on [gender] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memor
ES进行如下聚合操作时,会报如题所示错误: ➜ Downloads curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "size ...
- Elasticsearch 报错:Fielddata is disabled on text fields by default. Set `fielddata=true` on [`your_field_name`] in order to load fielddata in memory by uninverting the inverted index.
Elasticsearch 报错: Fielddata is disabled on text fields by default. Set `fielddata=true` on [`your_fi ...
- Load PE from memory(反取证)(未完)
Article 1:Loading Win32/64 DLLs "manually" without LoadLibrary() The most important step ...
- Win32 GDI 非矩形区域剪裁,双缓冲技术
传统的Win32通过GDI提供图形显示的功能,包括了基本的绘图功能,如画线.方块.椭圆等等,高级功能包括了多边形和Bezier的绘制.这样app就不用关心那些图形学的细节了,有点类似于UNIX上的X- ...
随机推荐
- 设计模式理解(十)结构型——享元(Flyweight)
最后一个结构型,享元.没有太多的项目经验,对这种模式只有一种概念上的理解,就是为了节约内存等资源,把可重用的东西只申请一次,然后处处调用,同时用Hash进行管理. 直接上图: 代码: /******* ...
- 【Django试图与网址003】
Django中网址是写在 urls.py 文件中,用正则表达式对应 views.py 中的一个函数(或者generic类),我们用一个项目来演示. 一,首先,新建一个项目(project), 名称为 ...
- dt转换List CovertListHelper
public class CovertListHelper { //传递过来的类型必须与数据库类型保持一致问题 public List<T> convertToList<T>( ...
- P4116 Qtree3
思路 可以树剖可以LCT,树剖就是每个重链开一个SET维护一下黑点的深度 非常不优美 使用LCT,在splay上二分找出需要的节点即可 代码 #include <cstdio> #incl ...
- Summary on deep learning framework --- PyTorch
Summary on deep learning framework --- PyTorch Updated on 2018-07-22 21:25:42 import osos.environ[ ...
- 【我的前端自学之路】【HTML5】Web Socket
以下为自学笔记内容,仅供参考. 转发请保留原文链接:https://www.cnblogs.com/it-dennis/p/10508118.html 什么是Web Socket WebSocket ...
- git push时报错:Updates were rejected because the tip of your current branch is behind
出现这样的问题是由于:自己当前版本低于远程仓库版本 有如下几种解决方法: 1.使用强制push的方法: git push -u origin master -f 这样会使远程修改丢失,一般是不可取的, ...
- eval()和JSON.parse()的区别
我们将一个josn字符串解析为对象的时可以使用两种方法: 假设有一个json字符串: '{ "student" : [ {}, {}, {} ] }' 然后解析为对象: 1.eva ...
- UE4外包团队:更新一下UE4和Unity3D案例
全部的贴图都是用出的法线贴图构建的话只用了阳光和天光,都是静态光源,视角是第一人称模板最后的效果嘛就是全4K,120帧,0错误0警告,场景小是小了点但是效果还不错. 工作活有时间更新,欢迎有UE4和U ...
- java代理通俗简单解析
1 代理 1.1 代理的概念和作用 代理的概念很好理解,就像黄牛代替票务公司给你提供票,经纪人代理艺人和别人谈合作.Java的代理是指实现类作为代理类的属性对象, ...