首页 > 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
本文地址:https://hgy413.com/2196.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

33 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.

  12. Right now it sounds like WordPress is the top blogging
    platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  13. #13 descargar facebook 回复 | 引用 Post:2018-11-21 08:02

    Thank you for the good writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you! By the way, how could we communicate?

  14. At this time it sounds like Expression Engine is the best blogging platform available right now.
    (from what I’ve read) Is that what you are using on your blog?

  15. #15 Taruhan QQ Online 回复 | 引用 Post:2020-05-10 15:49

    You actually make it seem so easy with your presentation but I find this matter to be actually something
    that I think I would never understand. It seems too complicated and very
    broad for me. I am looking forward for your next post, I will try
    to get the hang of it!

  16. #16 JOBBOARD FOR LOGISTICS 回复 | 引用 Post:2020-05-14 15:36

    JETZTSTELLENANGEBOT SCHALTEN
    Stellenangebote für, Seefracht, Luftfracht,Lager und
    Kontrakt-/ Bahnlogistik, ddem Import, Export oder Zoll

  17. Fabulous, what a blog it is! This website presents valuable data to us,
    keep it up.

  18. #18 amazon games download 回复 | 引用 Post:2020-05-16 08:25

    Hmm is anyone else having problems with the pictures on this blog loading?
    I’m trying to figure out if its a problem on my end
    or if it’s the blog. Any feedback would be greatly appreciated.

  19. Hurrah! Finally I got a weblog from where I can really
    get helpful information concerning my study and knowledge.

  20. I don’t even know how I ended up here, but I thought this post was good.
    I don’t know who you are but definitely you are going to a famous blogger if you are not already 😉 Cheers!

  21. #21 daftar Judi bola 回复 | 引用 Post:2020-05-18 05:50

    I savour, lead to I found exactly what I used to be looking for.
    You have ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

  22. #22 Pkv Games 回复 | 引用 Post:2020-05-18 16:01

    Hi there would you mind stating which blog platform you’re using?
    I’m looking to start my own blog in the near future but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and
    I’m looking for something unique. P.S My apologies for getting off-topic but I had to ask!

  23. #23 Pkv Terpercaya 回复 | 引用 Post:2020-05-18 17:20

    Aw, this was an incredibly good post. Finding the time and actual
    effort to create a really good article… but what can I say…
    I procrastinate a whole lot and never seem to get nearly anything done.

  24. #24 Judi bola online 回复 | 引用 Post:2020-05-19 04:35

    Just wish to say your article is as astonishing.
    The clarity in your post is just spectacular and i can assume you are an expert on this
    subject. Fine with your permission let me to grab your RSS feed to keep
    up to date with forthcoming post. Thanks a million and please continue the enjoyable work.

  25. I’ve been browsing online more than 3 hours today, yet I never
    found any interesting article like yours. It’s pretty worth enough
    for me. In my view, if all website owners and bloggers made good content as you did, the net will be much more useful than ever before.

  26. #26 hale opole 回复 | 引用 Post:2020-06-03 23:13

    Opolskim. Atrakcyjna lokalizacja. Więcej ofert na.

  27. Hello There. I discovered your weblog the use of msn. That is an extremely smartly
    written article. I’ll be sure to bookmark it and return to learn extra of your helpful info.
    Thank you for the post. I will definitely comeback.

  28. #28 other g 回复 | 引用 Post:2020-06-10 17:02

    This post will assist the internet users for setting up new website
    or even a weblog from start to end.

  29. #29 principles 回复 | 引用 Post:2020-06-11 01:50

    It’s awesome to visit this web site and reading the
    views of all mates regarding this article, while I
    am also zealous of getting know-how.

  30. #30 匿名 回复 | 引用 Post:2020-06-11 02:32

    I blog frequently and I really appreciate your information. This article has really peaked my interest.
    I will take a note of your blog and keep checking for new information about once a week.
    I opted in for your RSS feed too.

  31. #31 nemodno.com 回复 | 引用 Post:2020-06-11 07:05

    En outre, les femmes sont en recherche de solutions pour soigner et éviter les infections
    uro-génitales puisqu’une femme sur deux aura une cystite
    au moins une fois dans sa vie.

  32. #32 give g 回复 | 引用 Post:2020-06-11 14:07

    I enjoy what you guys are usually up too.
    This kind of clever work and exposure! Keep up the
    great works guys I’ve incorporated you guys to blogroll.

  33. #33 www.pasoconya.jp 回复 | 引用 Post:2020-06-14 04:43

    Actually no matter if someone doesn’t know
    then its up to other users that they will assist, so here it takes
    place.

发表评论