使用多选打开文件对话框,工作示例
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
OPENFILENAME ofn={0};
char szFile[MAX_PATH + 1] = {0};
char szDirect[MAX_PATH + 1] = {0};
int fCount=0;
switch(iMsg)
{
case WM_CREATE:
return 0;
case WM_KEYDOWN:
//(int)wParam - virtual key code, lParam - key data
switch((int)wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_F1:
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szDirect;
*(ofn.lpstrFile) = 0;
ofn.nMaxFile = MAX_PATH;
//file type filter, \0 - null character (end of string)
ofn.lpstrFilter = "All Files\0*.*\0Compressed Zip\0*.ZIP\0Text .txt\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = szFile;
*(ofn.lpstrFileTitle) = 0;
ofn.nMaxFileTitle = sizeof(szFile);
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT;
//show dialog Open
if (GetOpenFileName(&ofn)==TRUE)
{
printf("files selected\n");
char* ptr = ofn.lpstrFile;
ptr[ofn.nFileOffset-1] = 0;
printf("Directory path: %s\n", ptr);
ptr += ofn.nFileOffset;
while (*ptr)
{
fCount++;
printf("File: %i %s\n", fCount, ptr);
ptr += (lstrlen(ptr)+1);
}
printf("\n");
printf("selected %i files\n", fCount);
}
else
{
printf("no files selected\n");
}
break;
case VK_F2:
//GetSaveFileName() for example
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
//class name
static char szAppName[] = "HelloWin";
HWND hwnd;
MSG msg;
//declare class variable wc
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
//register class variable wc
RegisterClass(&wc);
//create window
hwnd = CreateWindow
(szAppName, "Standard Window", WS_OVERLAPPEDWINDOW,
100, 100, 600, 400, NULL, NULL, hInstance, NULL);
//show window
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}