case WM_PAINT: { RECT rect; HBRUSH hBrush; HFONT hFont; hdc = BeginPaint(hWnd, &ps); //Logical units are device dependent pixels, so this will create a handle to a logical font that is 48 pixels in height. //The width, when set to 0, will cause the font mapper to choose the closest matching value. //The font face name will be Impact. hFont = CreateFont(48,0,0,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Impact")); SelectObject(hdc, hFont);
//Sets the coordinates for the rectangle in which the text is to be formatted. SetRect(&rect, 100,100,700,200); SetTextColor(hdc, RGB(255,0,0)); DrawText(hdc, TEXT("Drawing Text with Impact"), -1,&rect, DT_NOCLIP); //Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height. //The width, when set to 20, will cause the font mapper to choose a font which, in this case, is stretched. //The font face name will be Times New Roman. This time nEscapement is at -300 tenths of a degree (-30 degrees) hFont = CreateFont(36,20,-300,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Times New Roman")); SelectObject(hdc,hFont);
//Sets the coordinates for the rectangle in which the text is to be formatted. SetRect(&rect, 100, 200, 900, 800); SetTextColor(hdc, RGB(0,128,0)); DrawText(hdc, TEXT("Drawing Text with Times New Roman"), -1,&rect, DT_NOCLIP);
//Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height. //The width, when set to 10, will cause the font mapper to choose a font which, in this case, is compressed. //The font face name will be Arial. This time nEscapement is at 250 tenths of a degree (25 degrees) hFont = CreateFont(36,10,250,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("Arial")); SelectObject(hdc,hFont); //Sets the coordinates for the rectangle in which the text is to be formatted. SetRect(&rect, 500, 200, 1400, 600); SetTextColor(hdc, RGB(0,0,255)); DrawText(hdc, TEXT("Drawing Text with Arial"), -1,&rect, DT_NOCLIP); DeleteObject(hFont);
查找win32的 CreateFont or CreateFontIndirect
查找win32的 CreateFont or CreateFontIndirect
CreateFont用过,好像不能自定义字体类型,比如Arial、Impact、Verdana之类的字体
我就是想要改变字体类型,所以想知道CreatePointFont封装了哪些API
查找win32的 CreateFont or CreateFontIndirect
CreateFont用过,好像不能自定义字体类型,比如Arial、Impact、Verdana之类的字体
我就是想要改变字体类型,所以想知道CreatePointFont封装了哪些API这就是MSDN的解释http://msdn.microsoft.com/en-us/library/windows/desktop/dd183499(v=vs.85).aspx
里面的示例代码如下:LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
{
RECT rect;
HBRUSH hBrush;
HFONT hFont;
hdc = BeginPaint(hWnd, &ps);
//Logical units are device dependent pixels, so this will create a handle to a logical font that is 48 pixels in height.
//The width, when set to 0, will cause the font mapper to choose the closest matching value.
//The font face name will be Impact.
hFont = CreateFont(48,0,0,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Impact"));
SelectObject(hdc, hFont);
//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 100,100,700,200);
SetTextColor(hdc, RGB(255,0,0));
DrawText(hdc, TEXT("Drawing Text with Impact"), -1,&rect, DT_NOCLIP);
//Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height.
//The width, when set to 20, will cause the font mapper to choose a font which, in this case, is stretched.
//The font face name will be Times New Roman. This time nEscapement is at -300 tenths of a degree (-30 degrees)
hFont = CreateFont(36,20,-300,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Times New Roman"));
SelectObject(hdc,hFont);
//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 100, 200, 900, 800);
SetTextColor(hdc, RGB(0,128,0));
DrawText(hdc, TEXT("Drawing Text with Times New Roman"), -1,&rect, DT_NOCLIP);
//Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height.
//The width, when set to 10, will cause the font mapper to choose a font which, in this case, is compressed.
//The font face name will be Arial. This time nEscapement is at 250 tenths of a degree (25 degrees)
hFont = CreateFont(36,10,250,0,FW_DONTCARE,FALSE,TRUE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("Arial"));
SelectObject(hdc,hFont); //Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 500, 200, 1400, 600);
SetTextColor(hdc, RGB(0,0,255));
DrawText(hdc, TEXT("Drawing Text with Arial"), -1,&rect, DT_NOCLIP);
DeleteObject(hFont);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}重申我1楼所说的:查msdn
你稍变通一个, 比如在msdn里面搜索: CreateFont 就会看到很多个API (注意, 请使用索引模式搜索).
然后在VS中可以看到源码, 建议用插件 VAssistX, 看源码比较方便
{
ASSERT(AfxIsValidString(lpszFaceName)); LOGFONT logFont;
memset(&logFont, 0, sizeof(LOGFONT));
logFont.lfCharSet = DEFAULT_CHARSET;
logFont.lfHeight = nPointSize;
Checked::tcsncpy_s(logFont.lfFaceName, _countof(logFont.lfFaceName), lpszFaceName, _TRUNCATE); return CreatePointFontIndirect(&logFont, pDC);
}// pLogFont->nHeight is interpreted as PointSize * 10
BOOL CFont::CreatePointFontIndirect(const LOGFONT* lpLogFont, CDC* pDC)
{
ASSERT(AfxIsValidAddress(lpLogFont, sizeof(LOGFONT), FALSE));
HDC hDC;
if (pDC != NULL)
{
ASSERT_VALID(pDC);
ASSERT(pDC->m_hAttribDC != NULL);
hDC = pDC->m_hAttribDC;
}
else
hDC = ::GetDC(NULL); // convert nPointSize to logical units based on pDC
LOGFONT logFont = *lpLogFont;
POINT pt;
// 72 points/inch, 10 decipoints/point
pt.y = ::MulDiv(::GetDeviceCaps(hDC, LOGPIXELSY), logFont.lfHeight, 720);
pt.x = 0;
::DPtoLP(hDC, &pt, 1);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC, &ptOrg, 1);
logFont.lfHeight = -abs(pt.y - ptOrg.y); if (pDC == NULL)
ReleaseDC(NULL, hDC); return CreateFontIndirect(&logFont);
}
CreatePointFont只是MFC的函数,不是系统API
它是实现对一个 LOGFONT的填充,最后调用CreateFontIndirect来创建字体 (15楼已经列出相关代码,可以参阅)