现在已知一个字符串在客户区输出了,并知道字符串的下面一些属性:字符串文本(CString类型),输出位置(CPoint类型),文字高度,文字宽度(假设与高度相同),文字角度,文字输出的对齐方式(左下,中下,右下,中中对齐),字体名称(如宋体等),如何获得字符串文本在客户区所占的区域(CRgn或HRGN)。CRgn GetTextRgn(CDC *pDC, CString szText, CPoint ptIns, double dHeight, double dWidth, double dRotation, int nAlign, CString szFontface);
希望高手能提供一个函数,高分求解,我的QQ:3426757,也可以在QQ上给我留言,谢谢了。
希望高手能提供一个函数,高分求解,我的QQ:3426757,也可以在QQ上给我留言,谢谢了。
HRGN CNode::TextToRgn(CString strText,CRect& rect)
{
//Init rect
rect.SetRect(0,0,0,0); //
CDC* pDC=GetDC();
CDC memDC;
memDC.CreateCompatibleDC(pDC);
byte buf[1024*30];memset(buf,255,1024*30);
CBitmap b;
b.CreateBitmap(1024,30,1,1,buf);
BITMAP bmp;
b.GetBitmap(&bmp);
HBITMAP hBmpOld=(HBITMAP)memDC.SelectObject(b);
HFONT hFontOld=(HFONT)memDC.SelectObject(m_font);
::TextOut(memDC,0,0,strText,strText.GetLength());
HRGN hRgn=NULL;
for(int i=0;i<bmp.bmWidth;i++)
{
for(int j=0;j<bmp.bmHeight;j++)
{
COLORREF cl=memDC.GetPixel(i,j);
if (hRgn==NULL && cl!=RGB(255,255,255))
{
hRgn=CreateRectRgn(i,j,i+1,j+1);
int iRight=rect.right;
int iBottom=rect.bottom;
rect.SetRect(0,0,max(iRight,i+1),max(iBottom,j+1));
}
else if (cl!=RGB(255,255,255))
{
HRGN h=CreateRectRgn(i,j,i+1,j+1);
CombineRgn(hRgn,hRgn,h,RGN_OR);
DeleteObject(h);
int iRight=rect.right;
int iBottom=rect.bottom;
rect.SetRect(0,0,max(iRight,i+1),max(iBottom,j+1));
}
}
}
memDC.SelectObject(hBmpOld);
memDC.SelectObject(hFontOld);
DeleteDC(memDC.m_hDC);
DeleteDC(pDC->m_hDC);
if (rect.Height()%2==1) rect.InflateRect(0,0,0,1); return hRgn;
}
/////////////////////////////////////////////////////////////////////////////
// CNode message handlers
//Get a mask color image's region.
HRGN CNode::BitmapToRegion(HBITMAP hBmp, COLORREF cTransparentColor, COLORREF cTolerance,CRect& rect)
{
//Init rect;
rect.SetRect(0,0,0,0); HRGN hRgn = NULL;
if (!hBmp)
return NULL; HDC hMemDC = CreateCompatibleDC(NULL);
if (!hMemDC)
return NULL; BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
rect.SetRect(0,0,bm.bmWidth,bm.bmHeight);
//创建一个32位色的位图,并选进内存设备环境
BITMAPINFOHEADER RGB32BITSBITMAPINFO = {
sizeof(BITMAPINFOHEADER), // biSize
bm.bmWidth, // biWidth;
bm.bmHeight, // biHeight;
1, // biPlanes;
32, // biBitCount
BI_RGB, // biCompression;
0, // biSizeImage;
0, // biXPelsPerMeter;
0, // biYPelsPerMeter;
0, // biClrUsed;
0 // biClrImportant;
};
VOID * pbits32;
HBITMAP hbm32 = CreateDIBSection(hMemDC,(BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
if (hbm32)
{
HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);
// Create a DC just to copy the bitmap into the memory DC
HDC hDC = CreateCompatibleDC(hMemDC);
if (hDC)
{
// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
BITMAP bm32;
GetObject(hbm32, sizeof(bm32), &bm32);
while (bm32.bmWidthBytes % 4)
bm32.bmWidthBytes++;
// Copy the bitmap into the memory DC
HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
// For better performances, we will use the ExtCreateRegion() function to create the
// region. This function take a RGNDATA structure on entry. We will add rectangles by
// amount of ALLOC_UNIT number in this structure.
#define ALLOC_UNIT 100
DWORD maxRects = ALLOC_UNIT;
HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
pData->rdh.dwSize = sizeof(RGNDATAHEADER);
pData->rdh.iType = RDH_RECTANGLES;
pData->rdh.nCount = pData->rdh.nRgnSize = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
// Keep on hand highest and lowest values for the "transparent" pixels
BYTE lr = GetRValue(cTransparentColor);
BYTE lg = GetGValue(cTransparentColor);
BYTE lb = GetBValue(cTransparentColor);
BYTE hr = min(0xff, lr + GetRValue(NULL));
BYTE hg = min(0xff, lg + GetGValue(NULL));
BYTE hb = min(0xff, lb + GetBValue(NULL));
// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
for (int y = 0; y < bm.bmHeight; y++)
{
// Scan each bitmap pixel from left to right
for (int x = 0; x < bm.bmWidth; x++)
{
// Search for a continuous range of "non transparent pixels"
int x0 = x;
LONG *p = (LONG *)p32 + x;
while (x < bm.bmWidth)
{
BYTE b = GetRValue(*p);
if (b >= lr && b <= hr)
{
b = GetGValue(*p);
if (b >= lg && b <= hg)
{
b = GetBValue(*p);
if (b >= lb && b <= hb)
// This pixel is "transparent"
break;
}
}
p++;
x++;
}
if (x > x0)
{
// Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
if (pData->rdh.nCount >= maxRects)
{
GlobalUnlock(hData);
maxRects += ALLOC_UNIT;
hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
pData = (RGNDATA *)GlobalLock(hData);
}
RECT *pr = (RECT *)&pData->Buffer;
SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
if (x0 < pData->rdh.rcBound.left)
pData->rdh.rcBound.left = x0;
if (y < pData->rdh.rcBound.top)
pData->rdh.rcBound.top = y;
if (x > pData->rdh.rcBound.right)
pData->rdh.rcBound.right = x;
if (y+1 > pData->rdh.rcBound.bottom)
pData->rdh.rcBound.bottom = y+1;
pData->rdh.nCount++;
// On Windows98, ExtCreateRegion() may fail if the number of rectangles is too
// large (ie: > 4000). Therefore, we have to create the region by multiple steps.
if (pData->rdh.nCount == 2000)
{
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
pData->rdh.nCount = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
}
}
}
// Go to next row (remember, the bitmap is inverted vertically)
p32 -= bm32.bmWidthBytes;
}
// Create or extend the region with the remaining rectangles
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
// Clean up
GlobalFree(hData);
SelectObject(hDC, holdBmp);
DeleteDC(hDC);
}
DeleteObject(SelectObject(hMemDC, holdBmp));
}
DeleteDC(hMemDC); return hRgn;
}
CString szText, //字符串文本
CPoint ptIns, //文本在客户区的位置点
double dHeight, //文本的高度
double dWidth, //文本的宽度(这里可以假设和高度相同)
double dRotation,//文本的角度
int nAlign, //文本的对齐方式
CString szFontface); //文本的字体名称
try path(CDC::beginePath, endpath);
enjoy:)