65.9K
CodeProject 正在变化。 阅读更多。
Home

在设备上下文中显示文本时使用下标和上标

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (13投票s)

2006 年 1 月 10 日

CPOL
viewsIcon

62374

downloadIcon

772

ExtTextOut 的扩展版本,支持下标和上标

Sample Image - SSTextOut.jpg

引言

最近我在 Code Project 上搜索如何在设备上下文中显示下标和上标(在使用 TextOutExtTextOut 函数时)。但我找不到任何相关内容。所以最终我决定自己编写这个函数。

特性和使用方法

我的函数 SSTextOut 具有与 ExtTextOut 类似的功能。您需要指定设备上下文的指针、边界矩形的指针、string 本身以及对齐模式。

string 应该包含特殊的格式代码 - ^ 表示上标,_ 表示下标。^_ 之后的下一个符号将显示为下标或上标。如果您需要显示 ^_ 本身 - 只需键入它们两次 - ^^__

对齐方式可以是 DT_CENTERDT_LEFTDT_RIGHT。它仅影响水平对齐。垂直方向上,文本将自动在边界矩形中居中。矩形外部的文本将被裁剪。该函数使用指定设备上下文的当前字体。

因此,要绘制上图中的无意义文本,您需要调用

SSTextOut(pDC,"ms^2/Hz+H_2O-mc^^2__4",&rect,DT_CENTER);

源代码

SSTextOut 的源代码如下所示

void SSTextOut(CDC* pDC, CString data , CRect* drawRect, int justification)
{
 //Necessary initializations
 pDC->SaveDC();
 
 CSize sz;
 CRect outRect(0,0,0,0);

 CFont* pFont = pDC->GetCurrentFont();
 CFont* oldFont;
 pDC->SetTextAlign(TA_BOTTOM|TA_LEFT);

 LOGFONT lf;
 pFont->GetLogFont(&lf);

 CPoint sub,sup,subofs,supofs;

 // Calculate subscript/superscript size and offsets
 sub.x=lf.lfWidth/2;
 sup.x=lf.lfWidth/2;
 sub.y=lf.lfHeight/3*2;
 sup.y=lf.lfHeight/3*2;

 subofs.x=lf.lfWidth/2;
 supofs.x=lf.lfWidth/2;
 subofs.y=lf.lfHeight/6;
 supofs.y=lf.lfHeight/3;

 lf.lfWidth=sub.x;
 lf.lfHeight=sub.y;
 CFont SubFont;
 SubFont.CreateFontIndirect(&lf);
 
 lf.lfWidth=sup.x;
 lf.lfHeight=sup.y;
 CFont SupFont;
 SupFont.CreateFontIndirect(&lf);
 
 CString temp = data;
 TCHAR c;
 
 // Calculate the size of the text that needs to be displayed
 do
 {
  int x=0;
  CString s = "";
  c=' ';
  bool bFind=true;

  // Find the first "^" or "_", indicating the sub- or superscript
  while (bFind)
  {
   x=data.FindOneOf("^_");
   if (x==-1) 
   {
    bFind=false;
    x=data.GetLength();
   }
   else if (x==data.GetLength()-1) bFind=false;
   else if (data[x]!=data[x+1]) 
   {
    bFind=false; 
    c=data[x];
   }
   else x++;
   s=s+data.Left(x);
   data.Delete(0,min(x+1,data.GetLength()));
  }
  sz = pDC->GetTextExtent(s);
  outRect.right+=sz.cx;
  if (outRect.Height()<sz.cy) outRect.top=outRect.bottom-sz.cy;
  
  switch (c) 
  {
  case '^':
   oldFont = pDC->SelectObject(&SupFont);
   sz = pDC->GetTextExtent(data[0]);
   outRect.right+=sz.cx+supofs.x;
   data.Delete(0);
   pDC->SelectObject(oldFont);
   break;
  case '_':
   oldFont = pDC->SelectObject(&SubFont);
   sz = pDC->GetTextExtent(data[0]);
   outRect.right+=sz.cx+subofs.x;
   data.Delete(0);
   pDC->SelectObject(oldFont);
   break;
  }
 }
 while (c!=' ');
 
 // Adjust text position
 outRect.bottom+=2*subofs.y;
 outRect.top-=2*subofs.x;
 CPoint Origin;
 Origin.y = drawRect->Height()/2+outRect.Height()/2+drawRect->top;

 switch (justification)
 {
 case DT_CENTER:
  Origin.x = drawRect->Width()/2-outRect.Width()/2+drawRect->left;
  break;
 case DT_LEFT:
  Origin.x = drawRect->left;
  break;
 case DT_RIGHT:
  Origin.x = drawRect->right-outRect.Width();
 }

 CPoint pnt = Origin;

 data = temp;

 // Draw text
 do
 {
  int x=0;
  CString s = "";
  c=' ';
  bool bFind=true;

  // Find the first "^" or "_", indicating the sub- or superscript
  while (bFind)
  {
   x=data.FindOneOf("^_");
   if (x==-1) 
   {
    bFind=false;
    x=data.GetLength();
   }
   else if (x==data.GetLength()-1) bFind=false;
   else if (data[x]!=data[x+1]) 
   {
    bFind=false; 
    c=data[x];
   }
   else x++;
   s=s+data.Left(x);
   data.Delete(0,min(x+1,data.GetLength()));
  }
  // Draw main text
  pDC->ExtTextOut(pnt.x,pnt.y,ETO_CLIPPED,drawRect,s,NULL);
  sz = pDC->GetTextExtent(s);
  pnt.x+=sz.cx;
  
  // Draw subscript or superscript
  switch (c) 
  {
  case '^':
   oldFont = pDC->SelectObject(&SupFont);
   pDC->ExtTextOut(pnt.x+supofs.x,pnt.y-supofs.y,ETO_CLIPPED,drawRect,data[0],NULL);
   sz = pDC->GetTextExtent(data[0]);
   pnt.x+=sz.cx+supofs.x;
   data.Delete(0);
   pDC->SelectObject(oldFont);
   break;
  case '_':
   oldFont = pDC->SelectObject(&SubFont);
   pDC->ExtTextOut(pnt.x+subofs.x,pnt.y+subofs.y,ETO_CLIPPED,drawRect,data[0],NULL);
   sz = pDC->GetTextExtent(data[0]);
   pnt.x+=sz.cx+supofs.x;
   data.Delete(0);
   pDC->SelectObject(oldFont);
   break;
  }
 }
 while (c!=' ');

 // Done, restoring the device context
 pDC->RestoreDC(-1);
}
© . All rights reserved.