In the Win32 program, it is common to use files for drag and drop (not to deny that some people like to use a button to open)
In fact, the use of drag-and-drop in the program, it is very simple, just need to create a window when the ws_ex_acceptfiles identifier, and then use a message function to deal with (of course, this is a window extension style, so you need to use CreateWindowEx to create):
HWnd = CreateWindowEx (Ws_ex_acceptfiles, Szwindowclass, SzTitle, Ws_overlappedwindow, cw_usedefault, 0, CW_ Usedefault, 0, NULL, NULL, HINSTANCE, NULL);
This allows us to respond to the corresponding message type:
Case Wm_dropfiles:ondropfiles (HWnd, (Hdrop) wParam);
The response function is actually quite simple:
VOID Ondropfiles (HWND hwnd, Hdrop hdropinfo) {UINT Nfilecount =::D ragqueryfile (Hdropinfo, (UINT)-1, NULL, 0); TCHAR Szfilename[_max_path] = _t ("");D word dwattribute;//get dragged in Files and folders for (UINT i = 0; i < Nfilecount; i++) {::D ragqueryfile (Hdropinfo, I, szFileName, sizeof (szFileName));d Wattribute =:: GetFileAttributes (szFileName);// is the folder if (Dwattribute & file_attribute_directory) { :: SetCurrentDirectory (szFileName); Enumeratefiles ();} else{//files can be played directly cout << szfilename << Endl; MessageBox (0, szFileName, "", MB_OK);}::D ragfinish (hdropinfo);}
Enumeratefiles () is a function that enumerates sub-files in a folder:
VOID Enumeratefiles () {win32_find_data fd; HANDLE hfind =:: FindFirstFile (_t ("*. *"), &FD); if (hfind! = Invalid_handle_value) {do {///If for directory if ( Fd.dwfileattributes & File_attribute_directory) {if (_tcscmp (Fd.cfilename, _t (".")) && _tcscmp ( Fd.cfilename, _t (".."))) {:: SetCurrentDirectory (fd.cfilename); Enumeratefiles ();:: SetCurrentDirectory (_t (".."));} } If the file is else{string Strdir; TCHAR lpdir[max_path];::getcurrentdirectory (MAX_PATH, lpdir); strdir = lpdir;//if (strdir.right (1)! = _T ("\ \"))// {//Strdir + = _t ("\ \");//}strdir + = Fd.cfilename; MessageBox (0, Strdir.c_str (), "", MB_OK);}} while (:: FindNextFile (Hfind, &FD));:: FindClose (Hfind);}}
It's as simple as that, and it's going to work.
Use the corresponding example to download the Portal:
http://download.csdn.net/detail/zengraoli/7864689
Win32 using File dragging