我有3个全局变量:
OPENFILENAME ofn;
TCHAR FileName[1024];
TCHAR Title[1024];
在WM_CREATE中,我填写了forn的信息:
static TCHAR filter[] = TEXT("Bitmap Files (*.BMP)\0*.bmp\0") \
TEXT("All Files (*.*)\0*.*\0\0");
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = filter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = 1024;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 1024;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT("bmp");
ofn.lCustData = 0;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
在带有case ID_FILE_SAVE:
的WM_COMMAND中,我有
case ID_FILE_SAVE:
if (!SaveAsBmpDialog(hwnd, FileName, Title))
return 0;
SaveAsBmp(FileName);
return 0;
其中SaveAsBmpDialog
是
static bool SaveAsBmpDialog(HWND hwnd, TCHAR *fileName, TCHAR *title)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = fileName;
ofn.lpstrFileTitle = title;
ofn.Flags = NULL;
return GetSaveFileName(&ofn);
}
将HDC保存到Bitmap文件(可以打开)可以正常工作。
但是在wParam == 'p'
的WM_CHAR中,我有
SaveAsBmp(FileName);
return 0;
我在之前调用SaveAsBmpDialog
时使用此功能,因此FileName
已经是完整路径。
但是我得到了一个无法打开的Bitmap文件。 (即使我先删除文件,它也会创建文件,但仍然无法打开)。文件大小与以前一样,因此我认为WriteFile
中的SaveAsBmp
可以正常工作。
我不明白为什么在GetSaveFileName(&ofn)
没有WM_CHAR
时它无效。
我想要的是在没有保存对话框的情况下将HDC保存到现有文件中。
ps:在SaveAsBmp
,我有
HANDLE hFile = CreateFile(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesWritten;
//SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
WriteFile(hFile, &fileHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, &info.bmiHeader, sizeof(BITMAPINFOHEADER) + info.bmiHeader.biClrUsed * sizeof(RGBQUAD), &dwBytesWritten, NULL);
WriteFile(hFile, pPixels, (int) info.bmiHeader.biSizeImage, &dwBytesWritten, NULL);
CloseHandle(hFile);