VC++菜鸟问题 拖拽文件到文本框获取文件路径简单问题

我是以文本框为对象的,严格按照网站http://blog.csdn.net/wwwzys/article/details/7663946二、文本框实现WM_DROPFILES消息这个方法来,但是花了一个一个多小时愣是没搞出啦,本人新学的以前没学过,这么简单问题还要麻烦网友不好意思,,这是源码http://pan.baidu.com/s/1jG6fq4Y,修改一下说说错在哪

补充一下另一种方法。

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;


}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-05-13

因为你的系统是WIN7以上系统,所以UAC引起了此问题。


将如下函数添加到DLG.cpp:

BOOL ChangeWndMessageFilterOk(UINT nMessage, BOOL bAllow)
{
    typedef BOOL (WINAPI * ChangeWindowMessageFilterOkFn)(UINT, DWORD);
    
    HMODULE hModUser32 = NULL;
    hModUser32 = LoadLibrary(_T("user32.dll"));
    if (hModUser32 == NULL) {
        return FALSE;
    }
    
    ChangeWindowMessageFilterOkFn pfnChangeWindowMessageFilter = (ChangeWindowMessageFilterOkFn) GetProcAddress(hModUser32, "ChangeWindowMessageFilter");
    if (pfnChangeWindowMessageFilter == NULL)
    {
        FreeLibrary(hModUser32);
        return FALSE;
    }
    
    FreeLibrary(hModUser32);
    
    return pfnChangeWindowMessageFilter(nMessage, bAllow ? 1 : 2);
}


然后让DLG对话框响应WM_INITDIALOG消息,在OnInitDialog函数中添加如下代码:

BOOL DLG::OnInitDialog() 
{
    CDialog::OnInitDialog();
    
    ChangeWndMessageFilterOk(WM_DROPFILES, TRUE);
    ChangeWndMessageFilterOk(0x0049, TRUE);       // 0x0049 == WM_COPYGLOBALDATA

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

本回答被提问者采纳
第2个回答  2015-10-12
// DLG.cpp :
#include "drag.h"
#include "fileedit.h" //添加这一行
#include "DLG.h" //要添加在这一行之前

在两个对话框类的cpp文件中,包含自定义类的头文件,注意要保证在对话框类头文件之前。

// dragDlg.cpp : 
#include "stdafx.h"
#include "drag.h"
#include "fileedit.h" //同理
#include "dragDlg.h"