补充一下另一种方法。
1,在头文件中添加shellapi.h,或者直接添加如下代码:
#ifndef _INC_SHELLAPI
#define _INC_SHELLAPI
//
// Define API decoration for direct importing of DLL references.
//
#ifndef WINSHELLAPI
#if !defined(_SHELL32_)
#define WINSHELLAPI DECLSPEC_IMPORT
#else
#define WINSHELLAPI
#endif
#endif // WINSHELLAPI
#ifndef SHSTDAPI
#if !defined(_SHELL32_)
#define SHSTDAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHSTDAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHSTDAPI STDAPI
#define SHSTDAPI_(type) STDAPI_(type)
#endif
#endif // SHSTDAPI
#ifndef SHDOCAPI
#if !defined(_SHDOCVW_)
#define SHDOCAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHDOCAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHDOCAPI STDAPI
#define SHDOCAPI_(type) STDAPI_(type)
#endif
#endif // SHDOCAPI
#if ! (defined(lint) || defined(_lint) || defined(RC_INVOKED))
#if ( _MSC_VER >= 800 ) || defined(_PUSHPOP_SUPPORTED)
#pragma warning(disable:4103)
#if !(defined( MIDL_PASS )) || defined( __midl )
#pragma pack(push)
#endif
#pragma pack(1)
#else
#pragma pack(1)
#endif
#endif // ! (defined(lint) || defined(_lint) || defined(RC_INVOKED))
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
DECLARE_HANDLE(HDROP);
WINSHELLAPI UINT APIENTRY DragQueryFileA(HDROP,UINT,LPSTR,UINT);
WINSHELLAPI UINT APIENTRY DragQueryFileW(HDROP,UINT,LPWSTR,UINT);
#ifdef UNICODE
#define DragQueryFile DragQueryFileW
#else
#define DragQueryFile DragQueryFileA
#endif // !UNICODE
WINSHELLAPI BOOL APIENTRY DragQueryPoint(HDROP,LPPOINT);
WINSHELLAPI VOID APIENTRY DragFinish(HDROP);
WINSHELLAPI VOID APIENTRY DragAcceptFiles(HWND,BOOL);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#if ! (defined(lint) || defined(_lint) || defined(RC_INVOKED))
#if ( _MSC_VER >= 800 ) || defined(_PUSHPOP_SUPPORTED)
#pragma warning(disable:4103)
#if !(defined( MIDL_PASS )) || defined( __midl )
#pragma pack(pop)
#else
#pragma pack()
#endif
#else
#pragma pack()
#endif
#endif // ! (defined(lint) || defined(_lint) || defined(RC_INVOKED))
#endif /* _INC_SHELLAPI */
2,对话框属性中勾选 接收文件(Accept files)
3,添加消息处理:
case WM_DROPFILES:
HDROP hDropInfo = (HDROP) wParam; //从WM_DROPFILES消息中获取所拖放文件的数据结构的指针
DragQueryFile(hDropInfo,0,szFilePath,_MAX_PATH); //获取文件路径
DragFinish(hDropInfo); //拖放结束后,释放内存
SendDlgItemMessage(hWnd,IDC_FILEPATH_EDIT,WM_SETTEXT,MAX_PATH,(LPARAM)szFilePath);
下面是完整代码:
#include <windows.h>
#include <shellapi.h>
#include <commdlg.h>
#include "resource.h"
LRESULT CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL OpenFileDlg(HWND);
char szFilePath[MAX_PATH];
HINSTANCE hInst;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInst = hInstance;
DialogBox(hInstance, (LPCTSTR)IDD_MAIN_DLG, NULL, (DLGPROC)MainDlgProc);
return 0;
}
LRESULT CALLBACK MainDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDC_OPEN_BTN:
if(!OpenFileDlg(hWnd))
return FALSE;
SendDlgItemMessage(hWnd,IDC_FILEPATH_EDIT,WM_SETTEXT,MAX_PATH,(LPARAM)szFilePath);
return TRUE;
}
break;
case WM_CLOSE:
EndDialog(hWnd, 0);
break;
case WM_DROPFILES:
HDROP hDropInfo = (HDROP) wParam; //从WM_DROPFILES消息中获取所拖放文件的数据结构的指针
DragQueryFile(hDropInfo,0,szFilePath,MAX_PATH); //获取文件路径
DragFinish(hDropInfo); //拖放结束后,释放内存
SendDlgItemMessage(hWnd,IDC_FILEPATH_EDIT,WM_SETTEXT,MAX_PATH,(LPARAM)szFilePath);
return TRUE;
break;
}
return 0;
}
BOOL OpenFileDlg(HWND hwnd)
{
OPENFILENAME ofn;
memset(szFilePath,0,MAX_PATH);
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize =sizeof(ofn);
ofn.hwndOwner =hwnd;
ofn.hInstance =GetModuleHandle(NULL);
ofn.nMaxFile =MAX_PATH;
ofn.lpstrInitialDir =".";
ofn.lpstrFile =szFilePath;
ofn.lpstrTitle ="Open...";
ofn.Flags =OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrFilter ="*.*\0*.*\0";
if(!GetOpenFileName(&ofn))
{
ZeroMemory(&ofn,sizeof(OPENFILENAME)); //释放内存
return FALSE;
}
ZeroMemory(&ofn,sizeof(OPENFILENAME)); //释放内存
return TRUE;
}