GetCharWidth32 和点大小问题

发布于 2024-11-04 06:55:15 字数 1835 浏览 0 评论 0原文

目前,我有以下函数来尝试获取字符串中特定字符的字符宽度。无论磅值如何,它都会返回相同的字体值。我知道这是逻辑单位。我需要考虑什么乘数才能将其从逻辑单元转换为像素?

谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-11-11 06:55:15

您正在尝试将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文