为了调试方便,将CRgn rgn对象以图片形式输出,便于查看
{
CRgn rgn
int w = m_lpShapePoints[4].x+4;
int h = m_lpShapePoints[4].y+4;
HWND hDesk = GetDesktopWindow();
HDC hDC = ::GetDC(hDesk);
CMemDC MemDC(hDC, CSize(w, h), 0);
for (int x=0; x<w; x++)
{
for (int y=0; y<h; y++)
{
CPoint pt(x, y);
if (rgn.PtInRegion(pt))
{
CDC *pDC = CDC::FromHandle(MemDC.GetMemDC());
pDC->SetPixel(x, y, RGB(255, 0, 0));
}
}
}
SaveBitmap(MemDC.GetHBITMAP(), _T("c:\\rgnoutput.bmp");
::ReleaseDC(hDesk, hDC);
}
BOOL SaveBitmap(HBITMAP hBitmap, LPCTSTR ptzFileName)
{
HDC hDC;
DWORD dwSize;
BOOL bReturn;
LPBYTE pBits;
HANDLE hFile;
BITMAP bmBitmap;
BITMAPFILEHEADER bfhFile;
BITMAPINFOHEADER bihInfo;
// 返回值初始化为 FALSE
bReturn = FALSE;
// 取得屏幕兼容位图的对象信息
if (GetObject(hBitmap, sizeof(BITMAP), &bmBitmap))
{
// 设置 BITMAPINFOHEADER 结构
bihInfo.biSize = sizeof(BITMAPINFOHEADER);
bihInfo.biWidth = bmBitmap.bmWidth;
bihInfo.biHeight = bmBitmap.bmHeight;
bihInfo.biPlanes = 1;
bihInfo.biBitCount = 24;
bihInfo.biCompression = BI_RGB;
bihInfo.biSizeImage = _WidthPitch(bmBitmap.bmWidth) * bmBitmap.bmHeight;
bihInfo.biXPelsPerMeter = 0;
bihInfo.biYPelsPerMeter = 0;
bihInfo.biClrUsed = 0;
bihInfo.biClrImportant = 0;
// 设置 BITMAPFILEHEADER 结构
bfhFile.bfType = 0x4D42; // "BM"
bfhFile.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bihInfo.biSizeImage;
bfhFile.bfReserved1 = 0;
bfhFile.bfReserved2 = 0;
bfhFile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// 分配内存
pBits = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, bihInfo.biSizeImage);
if (pBits)
{
// 获取屏幕设备场景
hDC = CreateIC(_T("DISPLAY"), NULL, NULL, NULL);
// 获取 DIB 数据
if (GetDIBits(hDC, hBitmap, 0, bmBitmap.bmHeight,
pBits, (LPBITMAPINFO) &bihInfo, DIB_RGB_COLORS))
{
// 创建文件
hFile = CreateFile(ptzFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// 写入 BITMAPFILEHEADER 信息
bReturn = WriteFile(hFile, &bfhFile, sizeof(BITMAPFILEHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 BITMAPINFOHEADER 信息
bReturn = WriteFile(hFile, &bihInfo, sizeof(BITMAPINFOHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 DIB 数据
bReturn = WriteFile(hFile, pBits, bihInfo.biSizeImage, &dwSize, 0);
}
}
// 关闭文件句柄
CloseHandle(hFile);
}
}
// 删除设备场景,释放内存
DeleteDC(hDC);
HeapFree(GetProcessHeap(), 0, (LPVOID) pBits);
}
}
return bReturn;
return 0;
}