GetCharWidth32 和点大小问题
目前,我有以下函数来尝试获取字符串中特定字符的字符宽度。无论磅值如何,它都会返回相同的字体值。我知道这是逻辑单位。我需要考虑什么乘数才能将其从逻辑单元转换为像素?
谢谢!
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;
}
使用测量字符串进行标记的新代码。
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;
}
Currently I have the following function to attempt to get the character widths for specific characters in a string. It returns the same values for a font regardless of point size. I know this is in logical units. What multiplier do I need to take into account to get it out of logical units and into pixels?
Thanks!
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;
}
New code for Mark using 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将 GDI+ 字体选择到 GDI DC 中。我很确定那是行不通的。您需要该字体的 GDI 句柄。
You're trying to select a GDI+ Font into a GDI DC. I'm pretty sure that's not going to work. You need a GDI handle to the font.