static void savetobmp(char * filename, BITMAPINFOHEADER* bmih, void * buffer, int lBufferLen) { BITMAPFILEHEADER bmpFileHeader; ZeroMemory(&bmpFileHeader, sizeof(bmpFileHeader)); bmpFileHeader.bfType = 'MB'; bmpFileHeader.bfSize=sizeof(bmpFileHeader)+lBufferLen+sizeof(BITMAPINFOHEADER); bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); FILE * fout = fopen(filename, "wb"); fwrite(&bmpFileHeader, 1, sizeof(bmpFileHeader), fout); fwrite(bmih, 1, sizeof(*bmih), fout); fwrite(buffer, 1, lBufferLen, fout); fclose(fout); }
static void CreateClockBitmap(const char * filename, int width, int height) { HDC hDc = CreateCompatibleDC(NULL);//创建空的CDC BITMAPINFO bmpInfo; memset(&bmpInfo, 0, sizeof(bmpInfo)); //创建位图 bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = (int)width;//宽度 bmpInfo.bmiHeader.biHeight = (int)height;//高度 bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 24; // 24bit色深 bmpInfo.bmiHeader.biCompression = BI_RGB; void* pArray = NULL; HBITMAP hbmp = CreateDIBSection(NULL, &bmpInfo, DIB_RGB_COLORS, &pArray, NULL, 0); // 行对齐 UINT uiTotalBytes = ((width * 24 + 31) & ~31)/8 * height; // 初始化成黑色 memset(pArray, 0x00, uiTotalBytes); HBITMAP hOldBmp = (HBITMAP)SelectObject(hDc, hbmp); //选入位图 // 设置字体 HFONT hFont; LOGFONT lFont; memset(&lFont, 0, sizeof(LOGFONT)); // 计算字体高度与磅值 int nNumerator = ::GetDeviceCaps(hDc, LOGPIXELSY); lFont.lfHeight = ::MulDiv( 25, -nNumerator, 72); lFont.lfWeight = 25; // 创建字体 hFont = ::CreateFontIndirect(&lFont); // 选择字体 HGDIOBJ hOldFont = ::SelectObject(hDc, hFont); RECT rc; rc.left = 0; rc.top = 0; rc.right = width; rc.bottom = height; char timestamp [100] = ""; SYSTEMTIME st; GetLocalTime(&st); sprintf(timestamp, "%02hu:%02hu:%02hu", st.wHour, st.wMinute, st.wSecond); // 设置字体颜色 SetTextColor(hDc, RGB(0xff, 0xFF, 0xFF)); // 设置字体背景 SetBkColor(hDc, RGB(0x00, 0x00, 0x00)); // 绘制 DrawTextA(hDc, timestamp, strlen(timestamp), &rc, DT_CENTER|DT_WORDBREAK); // 保存到文件 savetobmp(filename, &bmpInfo.bmiHeader, pArray, uiTotalBytes); // 清理资源 SelectObject(hDc, hOldBmp); SelectObject(hDc, hOldFont); DeleteObject(hFont); DeleteObject(hbmp); DeleteDC(hDc); return ; }