{
CString strText = _T("TEXT");
HDC hdc = ::GetDC(NULL);
ASSERT(hdc);
CRect rect(0, 0, 0, 0);
int iReturn;
::DrawText(hdc, strText, strText.GetLength(), &rect, DT_CALCRECT);
HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc, rect.Width(), rect.Height());
HDC hMemDC = ::CreateCompatibleDC(hdc);
hbitmap = (HBITMAP)::SelectObject(hMemDC, hbitmap);
::PatBlt(hMemDC, rect.left, rect.top, rect.right, rect.bottom, WHITENESS);
::SetBkMode(hMemDC, TRANSPARENT);
::DrawText(hMemDC, strText, strText.GetLength(), &rect, DT_LEFT | DT_BOTTOM);
hbitmap = (HBITMAP)::SelectObject(hMemDC, hbitmap);
::DeleteDC(hMemDC);
long lSizeImage = rect.Height() * (4 * (rect.Width()*24 + 31)/32);
BITMAPINFOHEADER bmih;
BITMAPFILEHEADER bmfh;
::memset(&bmih, 0, sizeof(bmih));
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biWidth = rect.Width();
bmih.biHeight = rect.Height();
bmih.biCompression = BI_RGB;
PBYTE pbits = new BYTE[lSizeImage];
ASSERT(pbits);
iReturn = ::GetDIBits(hdc, hbitmap, 0, rect.Height(), pbits, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);
::ReleaseDC(NULL, hdc);
::DeleteObject(hbitmap);
long lFileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lSizeImage;
bmfh.bfType = *(WORD*)"BM";
bmfh.bfSize = lFileSize;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
HANDLE hFile = ::CreateFile(_T("c://TEXT.bmp"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL, NULL);
ASSERT(hFile != INVALID_HANDLE_VALUE);
DWORD dwNum;
BOOL bReturn;
bReturn = ::WriteFile(hFile, &bmfh, sizeof(bmfh), &dwNum, NULL);
bReturn = ::WriteFile(hFile, &bmih, sizeof(bmih), &dwNum, NULL);
bReturn = ::WriteFile(hFile, pbits, lSizeImage, &dwNum, NULL);
delete []pbits;
CloseHandle(hFile);
}
CString strText = _T("TEXT");
HDC hdc = ::GetDC(NULL);
ASSERT(hdc);
CRect rect(0, 0, 0, 0);
int iReturn;
::DrawText(hdc, strText, strText.GetLength(), &rect, DT_CALCRECT);
HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc, rect.Width(), rect.Height());
HDC hMemDC = ::CreateCompatibleDC(hdc);
hbitmap = (HBITMAP)::SelectObject(hMemDC, hbitmap);
::PatBlt(hMemDC, rect.left, rect.top, rect.right, rect.bottom, WHITENESS);
::SetBkMode(hMemDC, TRANSPARENT);
::DrawText(hMemDC, strText, strText.GetLength(), &rect, DT_LEFT | DT_BOTTOM);
hbitmap = (HBITMAP)::SelectObject(hMemDC, hbitmap);
::DeleteDC(hMemDC);
long lSizeImage = rect.Height() * (4 * (rect.Width()*24 + 31)/32);
BITMAPINFOHEADER bmih;
BITMAPFILEHEADER bmfh;
::memset(&bmih, 0, sizeof(bmih));
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biWidth = rect.Width();
bmih.biHeight = rect.Height();
bmih.biCompression = BI_RGB;
PBYTE pbits = new BYTE[lSizeImage];
ASSERT(pbits);
iReturn = ::GetDIBits(hdc, hbitmap, 0, rect.Height(), pbits, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);
::ReleaseDC(NULL, hdc);
::DeleteObject(hbitmap);
long lFileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lSizeImage;
bmfh.bfType = *(WORD*)"BM";
bmfh.bfSize = lFileSize;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
HANDLE hFile = ::CreateFile(_T("c://TEXT.bmp"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL, NULL);
ASSERT(hFile != INVALID_HANDLE_VALUE);
DWORD dwNum;
BOOL bReturn;
bReturn = ::WriteFile(hFile, &bmfh, sizeof(bmfh), &dwNum, NULL);
bReturn = ::WriteFile(hFile, &bmih, sizeof(bmih), &dwNum, NULL);
bReturn = ::WriteFile(hFile, pbits, lSizeImage, &dwNum, NULL);
delete []pbits;
CloseHandle(hFile);
}
5799

被折叠的 条评论
为什么被折叠?



