YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

 


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


MFC Source Code: Draw Text with GetCurrentFont, GetTextExtent and GetLogFont and MoveToEx

 
 

Code Snippet

Download demo project - 22.08 KB

Sample Image - SSTextOut.jpg

My function SSTextOut has similar functionality to ExtTextOut. You need to specify the pointer to the device context, the pointer to bounding rectangle, the string itself, and the justification mode.

The string should contain special formatting codes - ^ for superscript and _ for subscript. The next symbol after ^ or _ will be shown as subscript or superscript. If you need to show ^ or _ themselves - just type them twice - ^^ or __.

Justification can be DT_CENTER, DT_LEFT, or DT_RIGHT. It affects only horizontal justification. Vertically, the text will be centered in the bounding rectangle automatically. The text outside the rectangle is clipped. The function uses the current font of the specified device context.

So, to draw the meaningless text in the image above, you need to call:

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

Source Code

The source code of SSTextOut is shown below:

Collapse Copy Code
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);
}

// ARROWSTRUCT
//
// Defines the attributes of an arrow.
typedef struct tARROWSTRUCT {
	int nWidth;		// width (in pixels) of the full base of the arrowhead
	float fTheta;	// angle (in radians) at the arrow tip between the two
					//  sides of the arrowhead
	bool bFill;		// flag indicating whether or not the arrowhead should be
					//  filled
} ARROWSTRUCT;

// ArrowTo()
//
// Draws an arrow, using the current pen and brush, from the current position
//  to the passed point using the attributes defined in the ARROWSTRUCT.
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);

// ArrowTo()
//
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pA) {

	POINT ptTo = {x, y};

	ArrowTo(hDC, &ptTo, pA);
}

// ArrowTo()
//
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pA) {

	POINT pFrom;
	POINT pBase;
	POINT aptPoly[3];
	float vecLine[2];
	float vecLeft[2];
	float fLength;
	float th;
	float ta;

	// get from point
	MoveToEx(hDC, 0, 0, &pFrom);

	// set to point
	aptPoly[0].x = lpTo->x;
	aptPoly[0].y = lpTo->y;

	// build the line vector
	vecLine[0] = (float) aptPoly[0].x - pFrom.x;
	vecLine[1] = (float) aptPoly[0].y - pFrom.y;

	// build the arrow base vector - normal to the line
	vecLeft[0] = -vecLine[1];
	vecLeft[1] = vecLine[0];

	// setup length parameters
	fLength = (float) sqrt(vecLine[0] * vecLine[0] + vecLine[1] * vecLine[1]);
	th = pA->nWidth / (2.0f * fLength);
	ta = pA->nWidth / (2.0f * (tanf(pA->fTheta) / 2.0f) * fLength);

	// find the base of the arrow
	pBase.x = (int) (aptPoly[0].x + -ta * vecLine[0]);
	pBase.y = (int) (aptPoly[0].y + -ta * vecLine[1]);

	// build the points on the sides of the arrow
	aptPoly[1].x = (int) (pBase.x + th * vecLeft[0]);
	aptPoly[1].y = (int) (pBase.y + th * vecLeft[1]);
	aptPoly[2].x = (int) (pBase.x + -th * vecLeft[0]);
	aptPoly[2].y = (int) (pBase.y + -th * vecLeft[1]);

	MoveToEx(hDC, pFrom.x, pFrom.y, NULL);

	// draw we're fillin'...
	if(pA->bFill) {
		LineTo(hDC, aptPoly[0].x, aptPoly[0].y);
		Polygon(hDC, aptPoly, 3);
	}

	// ... or even jes chillin'...
	else {
		LineTo(hDC, pBase.x, pBase.y);
		LineTo(hDC, aptPoly[1].x, aptPoly[1].y);
		LineTo(hDC, aptPoly[0].x, aptPoly[0].y);
		LineTo(hDC, aptPoly[2].x, aptPoly[2].y);
		LineTo(hDC, pBase.x, pBase.y);
		MoveToEx(hDC, aptPoly[0].x, aptPoly[0].y, NULL);
	}
}

 

 

Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net