用vc画图当把图复制到剪贴板时并通过GetTextExtent得到字符长度
以下是用VC画图并获取字符长度的示例代码:
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = _T("MyWindowClass");
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(0, _T("MyWindowClass"), _T("My Window"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap = NULL;
static int cx, cy;
static TCHAR szText[] = _T("Hello, World!");
static int nTextLength = _tcslen(szText);
static SIZE szTextSize;
switch (msg)
{
case WM_CREATE:
{
HDC hDC = GetDC(hWnd);
hBitmap = CreateCompatibleBitmap(hDC, 400, 300);
ReleaseDC(hWnd, hDC);
cx = 400;
cy = 300;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
RECT rc = {0, 0, cx, cy};
FillRect(hMemDC, &rc, (HBRUSH)(COLOR_WINDOW + 1));
HFONT hFont = CreateFont(24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, _T("Arial"));
HFONT hOldFont = (HFONT)SelectObject(hMemDC, hFont);
SetTextColor(hMemDC, RGB(255, 0, 0));
SetBkMode(hMemDC, TRANSPARENT);
TextOut(hMemDC, 100, 100, szText, nTextLength);
SelectObject(hMemDC, hOldFont);
DeleteObject(hFont);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
BitBlt(hDC, 0, 0, cx, cy, hMemDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);
break;
case WM_COPY:
{
if (OpenClipboard(hWnd))
{
EmptyClipboard();
HDC hMemDC = CreateCompatibleDC(NULL);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
BITMAPINFOHEADER bih = {0};
bih.biSize = sizeof(bih);
bih.biWidth = cx;
bih.biHeight = cy;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
bih.biSizeImage = cx * cy * 3;
HGLOBAL hDIB = GlobalAlloc(GHND, sizeof(bih) + bih.biSizeImage);
LPBYTE pDIB = (LPBYTE)GlobalLock(hDIB);
memcpy(pDIB, &bih, sizeof(bih));
LPBYTE pBits = pDIB + sizeof(bih);
GetDIBits(hMemDC, hBitmap, 0, cy, pBits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
GlobalUnlock(hDIB);
SetClipboardData(CF_DIB, hDIB);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
CloseClipboard();
}
// Get text length
HDC hDC = GetDC(hWnd);
HFONT hFont = CreateFont(24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, _T("Arial"));
HFONT hOldFont = (HFONT)SelectObject(hDC, hFont);
GetTextExtentPoint32(hDC, szText, nTextLength, &szTextSize);
SelectObject(hDC, hOldFont);
DeleteObject(hFont);
ReleaseDC(hWnd, hDC);
}
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
在窗口中绘制一段文本,并在复制到剪贴板时获取文本长度。在 WM_COPY 消息中,我们首先创建一个兼容的内存 DC,将位图选入 DC,并将位图复制到内存 DC 中。然后,我们创建一个 DIB 并将其锁定以获取指向位图数据的指针。接下来,我们将位图数据复制到 DIB 中,并将 DIB 放入剪贴板中。最后,我们使用 GetTextExtentPoint32 函数获取文本大小,并将其存储在 szTextSize 变量中
原文地址: https://www.cveoy.top/t/topic/epTA 著作权归作者所有。请勿转载和采集!