Article ID: 203099
Article Last Modified on 2/22/2007
/********************************************************************
* *
* FUNCTION: GetTextBoundingRect() *
* *
* PURPOSE: Get a bounding rectangle for a string of text output *
* to a specified coordinate in a DC using the currently *
* selected font *
* *
* NOTES: The reference DC must have a True Type font selected *
* *
*********************************************************************/
BOOL GetTextBoundingRect(HDC hDC, // Reference DC
int x, // X-Coordinate
int y, // Y-Coordinate
LPSTR lpStr, // The text string to evaluate
DWORD dwLen, // The length of the string
LPRECT lprc) // Holds bounding rectangle
{
LPPOINT lpPoints;
LPBYTE lpTypes;
int i, iNumPts;
// Draw the text into a path
BeginPath(hDC);
i = SetBkMode(hDC, TRANSPARENT);
TextOut(hDC, x, y, lpStr, dwLen);
SetBkMode(hDC, i);
EndPath(hDC);
// How many points are in the path
iNumPts = GetPath(hDC, NULL, NULL, 0);
if (iNumPts == -1) return FALSE;
// Allocate room for the points
lpPoints = (LPPOINT)GlobalAlloc(GPTR, sizeof(POINT) * iNumPts);
if (!lpPoints) return FALSE;
// Allocate room for the point types
lpTypes = GlobalAlloc(GPTR, iNumPts);
if (!lpTypes) {
GlobalFree(lpPoints);
return FALSE;
}
// Get the points and types from the current path
iNumPts = GetPath(hDC, lpPoints, lpTypes, iNumPts);
// Even more error checking
if (iNumPts == -1) {
GlobalFree(lpTypes);
GlobalFree(lpPoints);
return FALSE;
}
// Initialize the rectangle
SetRect(lprc, 0xFFFFF, 0xFFFFF, 0, 0);
// Get the maximum/minimum extents from the path data
for (i=0; i<iNumPts; i++) {
if (lpPoints[i].x > lprc->right) lprc->right = lpPoints[i].x;
if (lpPoints[i].y > lprc->bottom) lprc->bottom = lpPoints[i].y;
if (lpPoints[i].x < lprc->left) lprc->left = lpPoints[i].x;
if (lpPoints[i].y < lprc->top) lprc->top = lpPoints[i].y;
}
GlobalFree(lpTypes);
GlobalFree(lpPoints);
return TRUE;
}
Additional query words: kbDSupport
Keywords: kbdswgdi2003swept kbhowto KB203099