目前我有以下函数来尝试获取字符串中特定字符的字符宽度。无论点大小如何,它都会返回相同的字体值。我知道这是逻辑单位。我需要考虑什么乘数才能将其从逻辑单位和像素中分离出来?
谢谢!
double Utils::GetFormattedCharWidth(char theChar, Gdiplus::Font* pFont, RectF& rectArc, Graphics& graphics)
{
double textWidth = 0;
HDC hdc = NULL;
DWORD outLine = 0; //= GetLastError();
hdc = graphics.GetHDC();
outLine = GetLastError();
LPINT lpBuffer = new int;
/*ABC *abc = new ABC[256];
for(int iCon = 0; iCon < 256; iCon++)
{
(&abc[iCon])->abcA = 0;
(&abc[iCon])->abcB = 0;
(&abc[iCon])->abcC = 0;
}*/
DWORD dSize = 0;
SetMapMode(hdc,MM_TEXT);
HGDIOBJ hOldFont = SelectObject(hdc, pFont);
outLine = GetLastError();
//outLine = GetLastError();
//DWORD d = GetGlyphOutline(hdc, theChar, GGO_METRICS, lpg, dSize, lpvBuffer, LPM);
DWORD d = GetCharWidth32(hdc, theChar, theChar, lpBuffer);
//lpABC = (LPABC)GlobalAllocPtr( GHND, 256*sizeof(ABC) );
//d = GetCharABCWidthsA(hdc, 0, 255, abc);
outLine = GetLastError();
//DWORD d = GetCharABCWidthsA(hdc, theChar, theChar, abc);
int iExtraSpacing = GetTextCharacterExtra(hdc);
outLine = GetLastError();
graphics.ReleaseHDC(hdc);
textWidth = (double)(*lpBuffer) ;
//delete abc;
delete lpBuffer;
graphics.ReleaseHDC(hdc);
return textWidth + iExtraSpacing;
}
Mark使用Measure String的新代码。
double Utils::GetFormattedCharWidth(char theChar, Gdiplus::Font* pFont, RectF& rectArc, Graphics& graphics)
{
double textWidth = 0;
char charBuff[4];
memset(&charBuff, 0, 4);
charBuff[0] = theChar;
RectF rectTemp;
WCHAR* pChar = (WCHAR*)charBuff;
graphics.MeasureString(pChar, 1, pFont, rectArc, &rectTemp);
textWidth = rectTemp.Width;
return textWidth;
}
答案 0 :(得分:0)
您正在尝试将GDI +字体选择为GDI DC。我很确定这不起作用。你需要一个字体的GDI句柄。