C++ Win32 API GetTextExtentPoint32 函数演示 - 计算文本字符串的宽度和高度
#include <windows.h> #include <tchar.h> #include 'stdafx.h'
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szAppName[] = TEXT("GetTextExtentDemo"); HWND hwnd; MSG msg; WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("GetTextExtent Demo111"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static TCHAR szText[] = TEXT('Hello, World!'); static int cxChar, cyChar; HDC hdc; PAINTSTRUCT ps; SIZE size;
HDC hDC;
HDC hMemDC;
HBITMAP hBitmap;
HFONT hFont;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextExtentPoint32(hdc, TEXT('A'), 1, &size);
cxChar = size.cx;
cyChar = size.cy;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, szText, _tcslen(szText));
// 创建剪贴板的设备上下文
hDC = GetDC(NULL);
hMemDC = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap(hDC, 1, 1);
SelectObject(hMemDC, hBitmap);
// 计算字符串长度
hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SelectObject(hMemDC, hFont);
GetTextExtentPoint32(hMemDC, szText, _tcslen(szText), &size);
TextOut(hdc, 0, cyChar, TEXT('Width: '), 7);
TextOut(hdc, 7 * cxChar, cyChar, TEXT('Height: '), 8);
TCHAR szWidth[32], szHeight[32];
_stprintf_s(szWidth, TEXT('%d'), size.cx);
_stprintf_s(szHeight, TEXT('%d'), size.cy);
TextOut(hdc, 14 * cxChar, cyChar, szWidth, _tcslen(szWidth));
TextOut(hdc, 23 * cxChar, cyChar, szHeight, _tcslen(szHeight));
//// 创建剪贴板的设备上下文
//HDC hDC = GetDC(NULL);
//HDC hMemDC = CreateCompatibleDC(hDC);
//HBITMAP hBitmap = CreateCompatibleBitmap(hDC, 1, 1);
//SelectObject(hMemDC, hBitmap);
//// 计算字符串长度
//SIZE size;
//HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
//SelectObject(hMemDC, hFont);
//GetTextExtentPoint32(hMemDC, szText, _tcslen(szText), &size);
//// 释放资源
//DeleteObject(hBitmap);
//DeleteDC(hMemDC);
//ReleaseDC(NULL, hDC);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
原文地址: https://www.cveoy.top/t/topic/gEq5 著作权归作者所有。请勿转载和采集!