Implementation of the drag-and-drop Function C ++/VC

Source: Internet
Author: User
Implementation of the drag-and-drop Function C ++/VC
I recently compiled a software program that needs to use the drag-and-drop function. I had no idea about it before. I found some information on the Internet, but I did not say it directly. Let's talk about my experiences here. The drag-and-drop function mentioned below is eager for OLE, so the following statement is confirmed during program initialization: If (! Afxoleinit () {return false;} It takes a lot of time for me to drag and drop a source and a purpose. Therefore, two main classes are required for implementation: the source class coledatasource and target class coledroptarget. The source implementation must be derived from the coledatasource class, because the source and destination must have information transmission. Therefore, coledatasource provides several information transmission methods. Here we first introduce onrenderfiledata (lpformatetc, cfile * pfile), which is inherited and implemented from coledatasource. The following is the source code of the class. One of my examples transfers a text to the target control.
Class cmydatasource: Public coledatasource {protected:
// Attributespublic: // operationspublic: public: cstring m_strgiffullpath; virtual ~ Cmydatasource ();
// Generated message map functionsprotected: Virtual bool onrenderfiledata (lpformatetc, cfile *) ;}; bool cmydatasource: onrenderfiledata (lpformatetc, cfile * pfile) {If (lpformatetc-> cfformat = cf_text) {carchive AR (pfile, carchive: Store); ar. writestring (m_strgiffullpath); Ar <(byte) '/0'; // try not to close AR and pfile; otherwise, the target control cannot accept the return true message ;}
Coledatasource: onrenderfiledata (lpformatetc, pfile); Return false;
} Start the drag-and-drop function. I dragged it out of listctrl, so I wrote code in the onbegindragfilelist event. Of course, it can also be started in other events, such as left-click. The Code is as follows: cmydatasource itemdragdrop; itemdragdrop. delayrenderfiledata (cf_text, null); // execute onrenderfiledata to obtain the itemdragdrop data. m_strgiffullpath = "content to be transferred"; int IRES = itemdragdrop. dodragdrop (); // switch (IRES) is returned only when the left button pops up {// case dropeffect_move: break; Case dropeffect_copy: break; Case dropeffect_none: default: break;} The purpose of receiving the function is to test the coledroptarget class. Add a member to your view class, such as coledroptarget m_oledroptarget; and oncc in the View class. Register the member m_oledroptarget.register (this) in the reate function. Generally, the following is the specific receiving code. There are several virtual functions in the View class that need to be rewritten (messages are not understood in many articles ), ondrop: When ondragover is called at last, ondragenter is called at the time of view. When ondragleave is called when the mouse enters view, the following code is called when the mouse leaves view. The ondrop and ondragover functions are all used. The default code is not ondrop. don't call them, it took me a long time to understand. Because the default code ondragover returns dropeffect_none, ondrop does not occur. Therefore, you must rewrite the function and return non-dropeffect_none values. Simply return dropeffect_copy; okay. Now the breakpoint debugging is stopped. I don't understand it either. The onddrop Function Code is as follows: if (pdataobject-> isdataavailable (cf_text) {cfile * pfile = pdataobject-> getfiledata (cf_text ); // at this time, the carchive AR (pfile, carchive: load); cstring STR; ar. readstring (STR); ar. close (); // Delete pfile; // remember to close and analyze the structure. Otherwise, the memory will leak.} return true; // The default return value is not or can be used to execute an empty function. If you don't believe it, follow up and check whether the drag-and-drop process is complete, set a breakpoint at the reading location to see if the data has been transferred. If so, congratulations. If not, check again.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.