Simple way to use drag and drop

zhaozj2021-02-11  180

Simple way to use drag and drop

If you want to increase the functionality of the Drag & DROP for the window, follow these steps:

1. AdductDrag (TRUE) is called in OnCreate (...).

2, add message mapping

Begin_MESSAGE_MAP (...) // {{AFX_MSG_MAP (CDROPEDIT) ON_WM_CREATE () ON_WM_DROPFILES () / / About DRAG & DROP message //}} AFX_MSG_MAPEND_MESSAGE_MAP ()

3, handle messages

Void cxxxxx :: Ondropfiles (HDrop Dropinfo) {// Get Drag & DROP file number word wnumfilesdropped = DragQueryFile (Dropinfo, -1, null, 0); cstract firstfile = ""; // for (Word x = 0; x < WnumfilesDropped; x ) {// Get the length of the PathName, because the third parameter is null, so the call will fail and return the desired length Word wpathnamesize = DragQueryFile (Dropinfo, X, NULL, 0); // Assign Memory Char * NpszFile = (Char *) Localaloc (LPTR, WPathNameSize = 1); // Assignment error IF (npszfile == null) Continue; // to get filename DragQueryFile (Dropinfo, X, NpszFile, wpathnamesize); firstfile = npszfile;

// If it is shortcut, the conversion is converted to get the correct file name, expandedfile is the last file name cstring expandedfile = expandshrting (firstfile); // Release Memory LocalFree (npszfile);} // Complete DRAGFINISH (Dropinfo);}

4, about expandshortcut (...)

CString CXXXXX :: ExpandShortcut (CString & inFile) {CString outFile = ""; // Make sure we have a pathASSERT (inFile = _T (! "")); IShellLink * psl; HRESULT hres; LPTSTR lpsz = inFile.GetBuffer (MAX_PATH ); // Create instance for shell linkhres = :: CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) & psl); if (SUCCEEDED (hres)) {// Get a pointer to the persist file interfaceIPersistFile * ppf; HRES = PSL-> Queryinterface (IID_IPERSISTFILE, (LPVOID *) & PPF); if (succeeded (hres)) {// make Sure it's ANSIWORD WSZ [MAX_PATH]; :: MultibyTetowideChar (CP_ACP, 0, LPSZ, -1, WSZ, MAX_PATH); // Load shortcuthres = ppf-> Load (wsz, STGM_READ); if (SUCCEEDED (hres)) {WIN32_FIND_DATA wfd; // find the path from that HRESULT hres = psl-> GetPath (outFile.GetBuffer (MAX_PATH) , Max_Path, & WFD, SLGP_UNCPRIORITY; OUTFILE.RELEASEBUFFER ();} PPF-> Release ();} psl-> release ();} infile.releasebuffer (); // if this fails, outfile == "" Return Outfile;} Excerpted from Yushuyuan http://www.mfc2000.yeah.net

转载请注明原文地址:https://www.9cbs.com/read-4007.html

New Post(0)