0

我有 3 个全局变量:

OPENFILENAME ofn;

TCHAR FileName[1024];

TCHAR Title[1024];

在 WM_CREATE 中,我填写了 ofn 的信息:

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;

在 WM_COMMAND 中case ID_FILE_SAVE:,我有

            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 保存到位图文件(可以打开)可以正常工作。

但是在 WM_CHAR 中wParam == 'p',我有

SaveAsBmp(FileName);
return 0;

我在之前一次调用时使用此函数SaveAsBmpDialog,因此FileName已经是完整路径。

但是我得到一个无法打开的位图文件。(即使我先删除文件,它也会创建文件,但仍然无法打开)。文件大小和以前一样,所以我认为WriteFileSaveAsBmp工作中。

我不明白为什么没有GetSaveFileName(&ofn)in就不能工作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);
4

0 回答 0