原文:ENABLING Drag-and-Drop without OLE

ENABLING Drag-and-Drop without OLE
Digitally Urs (view profile)
May 21, 2003

来源:http://www.codeguru.com/Cpp/Cpp/cpp_mfc/dragdrop/article.php/c4059/

The drag-and-drop can be obtained in two ways; the first is OLE and the second is using the MFC. MFC supports the functionality of drag-and-drop without using OLE. To enable drag-and-drop in your application, you'll have to perform the following tasks:

  • The CWnd::DragAcceptFiles () enables you to accept the drag-and-drop functionality for your application. The only parameter to the function is of the BOOL type and which, by default, is set to FALSE to disable the drag-and-drop functionality. Setting it to TRUE will enable your application to handle WM_DROPFILES message for the CWND class.
  • The DragAcceptFiles function can be called of any CWnd derived class; handling the WM_DROPFILES will enable your application for drag-and-drop.

The steps involved are the following (I'm writing the code segment for an SDI application):

  1. Go in the CMainFrame::OnCreate function of your SDI application and make a call to DragAcceptFiles:
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  2. {    // MFC code goes here 
  3.    DragAcceptFile(true) // call the Drag Accept files  
  4.    // rest of MFC code generated by ClassWizard 
  5. } 
  6. You must keep in mind that you must call this function after the CWnd object has been created.

  7. Map the WM_DROPFILES message for your CMainFrame class and modify the code like the following:
    void CMainFrame::OnDropFiles(HDROP hDropInfo) 
  8. { UINT i = 0; 
  9. UINT nFiles = ::DragQueryFile(hDropInfo, (UINT) -1, NULL, 0);
  10.  for (i = 0; i < nFiles; i++) 
  11. { TCHAR szFileName[_MAX_PATH]; 
  12. ::DragQueryFile(hDropInfo, i, szFileName, _MAX_PATH); 
  13. ProcessMyFile(szFileName); 
  14. }  
  15. ::DragFinish(hDropInfo); 
  16. CFrameWnd::OnDropFiles(hDropInfo); 
  17. } 

    The first call to DragQueryFile will return you the total number of files dropped at your application. Each next call, the DragQueryFiles will return the name of the file to you in the szFileName parameter.

    After that, you have the name of the file. You can process it any way you like.

  18. The next important thing is to call the DragFinish function; otherwise, your application will leak a little bit of memory every time some drag-and-drop operation is made.

Written by:

Danish Qamar is a student of Computer Sciences in Lahore, Pakistan. He's been developing Web applications for three years and started working with Visual C++ in February, 2002. Other interests includes playing games, messing up 3d models integration in OpenGL, and is a badminton freak. He can be reached at:

sahil_2@hotmail.com or chicken_in_the_kitchen@yahoo.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值