【发布时间】:2015-06-13 18:43:19
【问题描述】:
我有需要在矩形内绘制的字符串。
问题在于有时字符串可能太大而无法放入。
如何调整字体大小以使字符串适合里面?
我已阅读 GDI 的文档,但一无所获。我仍然在互联网上不断搜索,希望能找到一些东西或获得自己的想法......
GDI+ 也是一个选项...
以下代码是为了回应用户 Jonathan Potter 的评论而发布的:
#include <windows.h>
#include <windowsx.h>
#include <CommCtrl.h>
#include <stdio.h> // swprintf_s()
#include <math.h>
#include <gdiplus.h>
#include <string>
using namespace Gdiplus;
// enable Visual Styles
#pragma comment( linker, "/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
language='*'\"")
// link with Common Controls library
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "GdiPlus.lib")
//global variables
HINSTANCE hInst;
// main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps = { 0 };
RECT rcClient = { 0 };
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rcClient);
int pageWidth = rcClient.right - rcClient.left,
pageHeight = rcClient.bottom - rcClient.top;
HFONT font = NULL, oldFont = NULL;
// target rectangle, text should fit inside
Rectangle(hdc, 0, 0, pageWidth / 4, pageHeight / 10);
SIZE sz;
GetTextExtentPoint32(hdc, L"This is very long string that might not fit into specified rectangle",
lstrlen(L"This is very long string that might not fit into specified rectangle"), &sz);
if (sz.cx > (pageWidth / 4))
{
// get current font
LOGFONT lf;
GetObject(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
// scale it
lf.lfHeight = MulDiv(lf.lfHeight, (pageWidth / 4), sz.cx);
font = CreateFontIndirect(&lf);
oldFont = SelectFont(hdc, font);
}
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255, 0, 0));
// draw text in test rectangle
RECT rcText = { 0 };
rcText.left = 0;
rcText.top = 0;
rcText.right = pageWidth / 4;
rcText.bottom = pageHeight / 10;
DrawTextEx(hdc,
L"This is very long string that might not fit into specified rectangle",
wcslen(L"This is very long string that might not fit into specified rectangle"),
&rcText, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOCLIP, NULL);
if (font != NULL)
{
SelectFont(hdc, oldFont);
DeleteFont(font);
}
EndPaint(hwnd, &ps);
}
return 0L;
case WM_SIZE:
{
InvalidateRect(hwnd, NULL, TRUE);
}
return 0L;
case WM_CLOSE:
::DestroyWindow(hwnd);
return 0L;
case WM_DESTROY:
{
::PostQuitMessage(0);
}
return 0L;
default:
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
// store hInstance in global variable for later use
hInst = hInstance;
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
// register main window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"Main_Window";
wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// initialize common controls
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_STANDARD_CLASSES;
InitCommonControlsEx(&iccex);
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// create main window
hwnd = CreateWindowEx(0, L"Main_Window", L"Autofit text inside rectangle",
WS_OVERLAPPEDWINDOW, 50, 50, 200, 200, NULL, NULL, hInstance, 0);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
GdiplusShutdown(gdiplusToken);
return Msg.wParam;
}
【问题讨论】:
-
调整字体大小并不是一个好主意。 Windows API 绘图功能支持换行,我记得,在太长的字符串末尾也有“…”(但也许我对后者的看法是错误的,也许那只是在 Windows 标题中)。如果您不希望缩短通常的解决方案是提供滚动功能(使用只读可能禁用的编辑控件)或工具提示或两者兼而有之。但是,如果您确实希望将文本缩放以适合,那么我建议将其绘制在一个适当大的矩形中的位图中,可能是所需大小的两倍,然后按比例缩小。
-
用当前字体测量字符串(例如用 DrawRect(DT_CALCRECT)`),找出你需要缩放字体大小以适应字符串的比例。例如。如果字符串当前是两倍长,则需要一半大小的字体。
-
计算出你需要缩放字体大小的比例 没那么简单。
DrawTextEx和DT_CALCRECT在字符串无法容纳时扩展矩形,这让我的生活更加艰难...... -
GetTextExtentPoint32告诉您字符串的宽度,如果这是您想要的。 -
@JonathanPotter:是的,我在自己的计算中使用了它。它仍然未能“拯救我”。也许我做错了什么,我不排除这个选项,所以如果你能详细说明我会很感激。如果需要,我也可以编写示例代码。