¿Por qué el valor devuelto por getextentpoint32 sigue siendo el mismo independientemente del tamaño de fuente actual?

programación


Estoy desarrollando esta aplicación y para garantizar que el contenido se ajuste al tamaño de la página, la aplicación utiliza el tamaño de fuente actual y el valor que devolvió GetTextExtentPoint32 para ajustar el tamaño de la página al introducir la salida en un bucle while. Me sorprendió bastante descubrir que GetExtentPoint32 sigue devolviendo el mismo valor a pesar de reducir el tamaño de fuente. ¿Por qué es así? ¿Cómo puedo resolver este desafío?

Los códigos relevantes se muestran a continuación:

C++
GetTextExtentPoint32(hdc, wstSchoolName11.c_str(), lstrlen(wstSchoolName11.c_str()), &size);
if ((iTextLeft + size.cx) > (iPaperWidth - iPictureWidth - iOffsetX))
	{
		iFontSize--;

		hNewFont = CreateAppFont(iFontSize, FW_BOLD);
		hOldFont = (HFONT)SelectObject(hdc, hNewFont);
		GetTextMetrics(hdc, &tm);
		//int iTextTop11 = iTop + iOffsetY;
		//int iTextLeft11 = iImageLeft + iPictureWidth + 5 * dOneMMX;
		wstring wstSchoolName11 = utf8_decode(stSchoolName);
		CharUpper(&wstSchoolName11[0]);
		//SIZE size11;
		GetTextExtentPoint32(hdc, wstSchoolName11.c_str(), lstrlen(wstSchoolName11.c_str()), &size);
	}
	else
	{
		bMaximumWith = true;
	}
C++
int iWindowExtX = 1000 * GetDeviceCaps(hDC,HORZSIZE);
    int iWindowExtY = 1000 * GetDeviceCaps(hDC, VERTSIZE);

    int iViewPortX = GetDeviceCaps(hDC, HORZRES);
    int iViewPortY = GetDeviceCaps(hDC, VERTRES);

    SetMapMode(hDC, MM_ISOTROPIC);
    SetWindowExtEx(hDC, iWindowExtX, iWindowExtY, nullptr);
    SetViewportExtEx(hDC, iViewPortX, iViewPortY, nullptr);

La definición de la aplicación CreateFont se muestra a continuación. ¿Tiene algo de malo?

C++
HFONT CreateAppFont(int cHeight, int cWeight, DWORD bItalic = 0, int cEscapement = 0, DWORD bUnderline = 0, LPCWSTR pszFaceName = L"Times New Roman");


HFONT CreateAppFont(int cHeight, int cWeight, DWORD bItalic, int cEscapement, DWORD bUnderline, LPCWSTR pszFaceName)
{
	return CreateFont(cHeight, 0, cEscapement, cEscapement, cWeight, bItalic, bUnderline, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
		CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, pszFaceName);

	
}

Lo que he probado:

He dedicado un tiempo razonable a la depuración. El resultado necesitó esta publicación.

Solución 1

El Función GetTextExtentPoint32A (wingdi.h) – Aplicaciones Win32 | Microsoft aprende[^] devuelve el tamaño del texto en unidades lógicas. Las funciones de visualización de texto utilizan ese número multiplicado por el tamaño de fuente para calcular el espacio real requerido para la visualización.

Bueno, acabo de probar esto y los valores de tamaño reflejan la fuente en uso. Creo que necesitas observar más de cerca lo que sucede en tu código, con referencia específica a CreateAppFont función.

コメント

タイトルとURLをコピーしました