简体   繁体   中英

Drag file on dialog with MFC

I want to drag file on dialog and get the file's path. So I searched web and tried that.

MyDlg.cpp

KmCdmMakeMultiProjectDlg::KmCdmMakeMultiProjectDlg(CWnd* pParent)
: CDialog (KmCdmMakeMultiProjectDlg::IDD, pParent)
{
}

void KmCdmMakeMultiProjectDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_MULTIPART, lst_AddList);
    DDX_Control(pDX, IDC_BTN_ADD_PROJECT, btn_AddList);
    DDX_Control(pDX, IDC_BTN_ADDLIST_CSV, btn_AddList_CSV);
    DDX_Control(pDX, IDC_BTN_DEL_PROJECT, btn_DelList);
    DDX_Control(pDX, IDC_BTN_TARGET_SELECT, btn_ReferFolder);
    DDX_Control(pDX, IDC_BTN_FILE_SELECT, btn_ReferCSV);
    DDX_Control(pDX, IDC_BTN_EXECUTE, btn_Execute);
    DDX_Control(pDX, IDC_BTN_EDIT_NAME, btn_EditName);
    DDX_Control(pDX, IDC_BTN_EDIT_DESCRIPTION, btn_EditDescription);
    DDX_Control(pDX, ID_CLOSE, btn_Close);
}

BEGIN_MESSAGE_MAP(KmCdmMakeMultiProjectDlg, CDialog)
    ON_BN_CLICKED(IDC_BTN_ADD_PROJECT, &KmCdmMakeMultiProjectDlg::AddList)
    ON_BN_CLICKED(IDC_BTN_ADDLIST_CSV, &KmCdmMakeMultiProjectDlg::AddListCSV)
    ON_BN_CLICKED(IDC_BTN_DEL_PROJECT, &KmCdmMakeMultiProjectDlg::DelList)
    ON_BN_CLICKED(IDC_BTN_TARGET_SELECT, &KmCdmMakeMultiProjectDlg::SelectPath)
    ON_BN_CLICKED(IDC_BTN_FILE_SELECT, &KmCdmMakeMultiProjectDlg::SelectCSV)
    ON_BN_CLICKED(IDC_BTN_EXECUTE, &KmCdmMakeMultiProjectDlg::MakeExecute)
    ON_BN_CLICKED(IDC_BTN_EDIT_NAME, &KmCdmMakeMultiProjectDlg::EditName)
    ON_BN_CLICKED(IDC_BTN_EDIT_DESCRIPTION, &KmCdmMakeMultiProjectDlg::EditDescription)
    ON_BN_CLICKED(ID_CLOSE, &KmCdmMakeMultiProjectDlg::CloseDialog)
    ON_WM_DROPFILES()
END_MESSAGE_MAP()

BOOL KmCdmMakeMultiProjectDlg::OnInitDialog() 
{   
    CDialog::OnInitDialog();
    CDialog::DragAcceptFiles();
}

void KmCdmMakeMultiProjectDlg::OnDropFiles(HDROP hDropInfo)
{
    CString csfile = "Why don't come this break point!";
    CDialog::OnDropFiles(hDropInfo);
}

I thought it is not difficult. Just set message ON_WM_DROPFILES() in dialog, And Set CDialog::DragAcceptFiles(); in dialog's OnInitDialog() method.

I expected that OnDropFiles(HDROP hDropInfo) is run if I drag a file on dialog. I have tested with debug mode, and checked break point in OnDropFiles method. But didn't occur anything even though I dropped a file.

Have you any some idea?, waiting your teaching. Thank you.

If application run as administrator, must include this 2 line before DragAcceptFiles();

For example.

BOOL KmCdmMakeMultiProjectDlg::OnInitDialog() 
{   
    CDialog::OnInitDialog();
    ChangeWindowMessageFilter(0x0049, MSGFLT_ADD);
    ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
    CDialog::DragAcceptFiles();
}

Points are ChangeWindowMessageFilter.

Thank you.

In the name of the C++ an C, do not use ChangeWindowMessageFilter. This deals with Privilege Isolation (UIPI) message filter and has nothing to do with drag and drop support.

You have to call DragAcceptFiles for any windows object that is going to accept drag and drop by processing the WM_DROPFILES message. It has to be called when the object is attached to a window (valid m_hWnd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM