首页 > DUI > DUI-获得文件拖动所在路径
2015三月24

DUI-获得文件拖动所在路径

随手代码:(以下针对duirichedit设置)内部调用:

GetContainer()->OnRegisterDragDrop(m_hDUIWnd,pDropTarget);

即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class DropFileTarget : public IDropTarget
{
public:
	DropFileTarget();
	~DropFileTarget();
 
public:
	//IUnkown
	virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, __RPC__deref_out void __RPC_FAR *__RPC_FAR *ppvObject);
	virtual ULONG STDMETHODCALLTYPE AddRef(void);
	virtual ULONG STDMETHODCALLTYPE Release(void);
 
	//IDropTarget
	virtual HRESULT STDMETHODCALLTYPE DragEnter(__RPC__in_opt IDataObject *pDataObj,DWORD grfKeyState,POINTL pt,__RPC__inout DWORD *pdwEffect);
	virtual HRESULT STDMETHODCALLTYPE DragOver(DWORD grfKeyState,POINTL pt,__RPC__inout DWORD *pdwEffect);
	virtual HRESULT STDMETHODCALLTYPE DragLeave(void);
	virtual HRESULT STDMETHODCALLTYPE Drop(__RPC__in_opt IDataObject *pDataObj,DWORD grfKeyState, POINTL pt,__RPC__inout DWORD *pdwEffect);
protected:        
	LONG              m_nRef;
};
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/// DropFileTarget
DropFileTarget::DropFileTarget()
{
	m_nRef = 1;
}
 
DropFileTarget::~DropFileTarget()
{
}
 
//IUnkown
HRESULT STDMETHODCALLTYPE DropFileTarget::QueryInterface(REFIID riid, __RPC__deref_out void __RPC_FAR *__RPC_FAR *ppvObject)
{
	HRESULT hr = S_OK;// E_NOINTERFACE;
	do 
	{
		if (__uuidof(IUnknown) == riid)
		{
			*ppvObject=(IUnknown*) this;
			AddRef();
			break;
		}
		if(__uuidof(IDropTarget) == riid)
		{
			*ppvObject=(IDropTarget*)this;
			AddRef();
			break;
		}
 
		hr = E_NOINTERFACE;
	} while (false);
	return hr;
}
ULONG STDMETHODCALLTYPE DropFileTarget::AddRef(void)
{
	return ++m_nRef;
}
 
ULONG STDMETHODCALLTYPE DropFileTarget::Release(void)
{ 
	ULONG uRet = --m_nRef;
	if (0 == uRet)
	{
		delete this;
	}
	return uRet;
}
 
/// IDropTarget
HRESULT STDMETHODCALLTYPE DropFileTarget::DragEnter(__RPC__in_opt IDataObject *pDataObj,DWORD grfKeyState,POINTL pt,__RPC__inout DWORD *pdwEffect)
{
	*pdwEffect = DROPEFFECT_COPY;
	return S_FALSE;
}
 
HRESULT STDMETHODCALLTYPE DropFileTarget::DragOver(DWORD grfKeyState,POINTL pt,__RPC__inout DWORD *pdwEffect)
{
	*pdwEffect = DROPEFFECT_COPY;
	return S_FALSE;
}
 
HRESULT STDMETHODCALLTYPE DropFileTarget::DragLeave(void)
{
	return S_FALSE;
}
HRESULT STDMETHODCALLTYPE DropFileTarget::Drop(__RPC__in_opt IDataObject *pDataObj,DWORD grfKeyState, POINTL pt,__RPC__inout DWORD *pdwEffect)
{
	HRESULT hr = S_FALSE;
	do 
	{
		// 
		FORMATETC formatEtc;
		formatEtc.cfFormat = CF_HDROP;
		formatEtc.ptd = NULL;
		formatEtc.dwAspect = DVASPECT_CONTENT;
		formatEtc.lindex = -1;
		formatEtc.tymed = (DWORD) -1;
		if (FAILED(pDataObj->QueryGetData(&formatEtc)))
		{
			break;
		}
 
		STGMEDIUM stgMedium;
		SCODE sc = pDataObj->GetData(&formatEtc, &stgMedium);
		if (FAILED(sc))
		{
			break;
		}
		HDROP hDropFiles = (HDROP)GlobalLock((HGLOBAL)stgMedium.hGlobal); //锁定内存块
		if (NULL != hDropFiles)
		{
			int nDropCount = DragQueryFile(hDropFiles, -1, NULL, 0);
			wchar_t szPath[MAX_PATH] = {0};
			for (int i=0;i<nDropCount; i++)
			{
				memset(szPath,0,sizeof(wchar_t)*MAX_PATH);
				DragQueryFileW(hDropFiles,i, (LPWSTR)szPath,MAX_PATH);
			}
		}
		GlobalUnlock(stgMedium.hGlobal);
		if (stgMedium.pUnkForRelease)
		{
			::ReleaseStgMedium(&stgMedium);
		}
	} while (false);
 
	return hr;
}

参考自MFC的COleDropSource::GetFileData 以下面的代码改装而来:

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
BOOL CListCtrlEx::OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
       UINT           nFileCount = 0;
       HDROP          hDropFiles = NULL;
       HGLOBAL        hMemData = NULL;
 
       AfxMessageBox("OnDrop");
       if(pDataObject->IsDataAvailable(CF_HDROP))
       {
              hMemData = pDataObject->GetGlobalData(CF_HDROP);
              hDropFiles = (HDROP)GlobalLock((HGLOBAL)hMemData); //锁定内存块
              if(hDropFiles != NULL)
              {
                     char chTemp[_MAX_PATH+1] = {0};
                     nFileCount = DragQueryFile(hDropFiles, 0xFFFFFFFF, NULL, 0);
                     for(UINT nCur=0; nCur<nFileCount; ++nCur) //遍历取得每个文件名
                     {
                            ZeroMemory(chTemp, _MAX_PATH+1);
                            DragQueryFile(hDropFiles, nCur, (LPTSTR)chTemp, _MAX_PATH+1);
                            AddAllFiles(chTemp);
                     }
              }
              GlobalUnlock(hMemData);
              return TRUE;
       }
       else
       {
              return FALSE;
       }
}


文章作者:hgy413
本文地址:http://hgy413.com/2196.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

11 Responses to “DUI-获得文件拖动所在路径”

  1. #1 minecraft 回复 | 引用 Post:2018-10-05 13:41

    Hi, Neat post. There is an issue together
    with your website in web explorer, may check this?
    IE nonetheless is the marketplace leader
    and a good section of people will miss your great writing due to this problem.

  2. #2 minecraft 回复 | 引用 Post:2018-10-06 13:40

    I’m really inspired along with your writing abilities and also with the structure to
    your blog. Is that this a paid theme or did you customize it your self?

    Anyway stay up the nice quality writing, it’s uncommon to look a great blog like this one nowadays..

  3. #3 minecraft 回复 | 引用 Post:2018-10-07 06:32

    Hello, i think that i saw you visited my web site so i came to “return the favor”.I’m
    attempting to find things to improve my web site!I suppose its ok to use a few of your ideas!!

  4. Hi, i feel that i noticed you visited my website thus i got here to return the prefer?.I am attempting to to find
    issues to improve my website!I guess its adequate to use some of your ideas!!

  5. Wonderful goods from you, man. I’ve take into account your stuff
    previous to and you’re just too fantastic. I really like what you
    have received right here, certainly like what you are stating and
    the best way by which you say it. You’re making it enjoyable and you still take care of to keep it smart.
    I can not wait to learn much more from you. This
    is really a terrific site.

  6. #6 Coconut Oil 回复 | 引用 Post:2018-10-21 07:41

    What’s up, I read your new stuff like every week. Your humoristic style is awesome, keep it up!

  7. #7 quest bars cheap 回复 | 引用 Post:2018-11-03 18:49

    Oh my goodness! Impressive article dude! Thank
    you, However I am having issues with your RSS. I don’t understand why I am unable to join it.

    Is there anyone else getting identical RSS problems?
    Anyone that knows the answer can you kindly respond? Thanks!!

  8. #8 quest bars cheap 回复 | 引用 Post:2018-11-04 16:18

    quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap
    Excellent weblog here! Additionally your site quite a bit up fast!
    What web host are you using? Can I get your associate
    hyperlink on your host? I wish my website loaded up as quickly as yours lol quest
    bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap

  9. It’s appropriate time to make a few plans for the long run and it’s time to be happy.
    I’ve read this publish and if I may just I want to recommend you
    few fascinating things or tips. Perhaps you could write subsequent articles referring to
    this article. I wish to learn even more issues about it!

  10. That is a good tip especially to those fresh to the blogosphere.
    Short but very accurate information… Many thanks for sharing this
    one. A must read article!

  11. Magnificent goods from you, man. I have understand your stuff previous to
    and you are just extremely great. I actually like what you’ve acquired here,
    really like what you’re stating and the way in which you
    say it. You make it enjoyable and you still take care of to keep it smart.
    I can not wait to read much more from you. This is actually a wonderful site.

发表评论