本文實例為大家分享了MFC實現對話框編輯控件上拖拽文件的具體代碼,供大家參考,具體內容如下
步驟
1、重載CEdit類
2、添加編輯框控件
首先,重載CEdit類,定義一個派生類CDragEdit類,并重載它的WM_CREATE方法,在其中添加DragAcceptFile(TRUE); 方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// CDragEdit.h #pragma once class CDragEdit : public CEdit { DECLARE_DYNAMIC(CDragEdit) public : CDragEdit(); virtual ~CDragEdit(); protected : DECLARE_MESSAGE_MAP() public : afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDropFiles( HDROP hDropInfo); }; |
然后,利用類向導為CDragEdit類添加WM_DROPFILE消息相應函數;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//CDragEdit.cpp int CDragEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CEdit::OnCreate(lpCreateStruct) == -1) return -1; // TODO: 在此添加您專用的創建代碼 DragAcceptFiles(TRUE); return 0; } void CDragEdit::OnDropFiles( HDROP hDropInfo) { UINT count; TCHAR filePath[MAX_PATH] = { 0 }; count = DragQueryFile(hDropInfo, -1, NULL, 0); if (1 == count) { DragQueryFile(hDropInfo, 0, filePath, sizeof (filePath)); this ->SetWindowTextW(filePath); UpdateData(FALSE); DragFinish(hDropInfo); //拖放成功后,釋放內存 } else { CString szFilePath; for ( UINT i = 0; i < count; i++) { int pahtLen = DragQueryFile(hDropInfo, i, filePath, sizeof (filePath)); szFilePath = szFilePath + filePath + _T( "\r\n" ); } this ->SetWindowTextW(szFilePath); UpdateData(FALSE); DragFinish(hDropInfo); } |
1
2
3
4
5
6
7
8
9
10
11
|
//MFCDlg.h #program once #include "CDragEdit.h" class CMFDlg : public CDialogEx { …………………… public : CDragEdit m_DragEdit; } |
最后,設置文本框控件屬性[Accept Files]、[multiline]值為True.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_42330311/article/details/103245814