Use getopenfilename to select a folder

Source: Internet
Author: User
Keyword: getopenfilename cfiledialog SDK folder I have never liked the default folder selection. on the contrary, I like the cfiledialog dialog box in MFC. how to Use the SDK and implement the cfiledialog dialog box to select a folder is what I want to explain and give an example. 1. cfiledialog implementation. cfiledialog is an MFC class that enables customization of open, save, and common dialog. getopenfilename is implemented internally. cfiledialog has a m_ofn structure, which is of the openfilename type. its detailed implementation is in dlgfile. CPP. interestingly, in my vc9, cfiledialog has special treatment for Vista. if the system is Vista and vistastyle is set, cfiledialog uses the ifiledialog interface instead of getopenfilename, this has little to do with us, and I am not planning to complicate the simple problem. generally, the flag of m_ofn in MFC includes ofn_enablehook | ofn_explorer. In this way, the hookproc function pointer can be specified and various messages can be accepted. the MFC is written as follows: m_ofn.lpfnhook = (commdlgproc) _ afxcommdlgproc; this is the default Common Dialog processing function. 2. our implementation. as mentioned above, we basically know how to implement the selection dialog box requirements. (1 ). first set m_ofn.lpstrfile wchar wcsfolderpath [max_path] = {0};: wcscpy (wcsfolderpath, l "*.. * "); m_ofn.lpstrfile = wcsfolderpath; m_ofn.nmaxfile = sizeof (wcsfolderpath)/sizeof (wchar );"*.. * "all files will be blocked and only the effect of the dialog box will be displayed (2 ). set m_ofn.hinstance = (hmodule) getcurrentprocess (); // do not use null, which may cause problems that cannot be customized (3 ). set hookproc: m_ofn.lpfnhook = (commdlgproc) myfolderproc. After completing these tasks, you try to intercept the wm_command idok and idcancel messages in myfolderproc. The result is: you cannot intercept anything. unfortunately, hookproc processes a handle similar to a "pseudo" dialog box. With spy ++, you will find that the parent handle of the dialog box handle in hookproc is actually processed. so we should do this:
  1. Long g_loriwndproc = NULL;
  2. Bool g_breplaced = false;
  3. Uint_ptr static _ stdcall myfolderproc (hwnd hdlg, uint uimsg, wparam, lparam)
  4. {
  5. If (hdlg! = NULL & g_breplaced = false)
  6. {
  7. Wchar wcsclassname1 [max_path];
  8. Wchar wcsclassname2 [max_path];
  9. Hwnd hparent =: getparent (hdlg );
  10. : Getclassnamew (hparent, wcsclassname1, sizeofwcar (wcsclassname1 ));
  11. : Getclassnamew (hdlg, wcsclassname2, sizeofwcar (wcsclassname2 ));
  12. If (0 =: wcscmp (wcsclassname1, wcsclassname2 ))
  13. {
  14. G_breplaced = true;
  15. G_loriwndproc =: setwindowlongw (: getparent (hdlg), gwl_wndproc, (long) _ wndproc );
  16. }
  17. }
  18. If (uimsg = wm_policy)
  19. {
  20. Lpofnotify = (lpofnotify) lparam;
  21. If (lpofnotify-> HDR. Code = cdn_folderchange)
  22. {
  23. // Your codes here
  24. }
  25. }
  26. }
  27. Return false;
  28. }
  29. Lresult static _ stdcall _ wndproc (hwnd, uint umsg, wparam, lparam)
  30. {
  31. If (umsg = wm_command)
  32. {
  33. If (wparam = idok)
  34. {
  35. // Your codes
  36. }
  37. }
  38. Return CALLWNDPROC (wndproc) g_loriwndproc, hwnd, umsg, wparam, lparam );
  39. }

The code above reveals how to customize the filedialog in the Common Dialog. You can intercept the idok.

However, if you select a folder and click "open", you will find that it opens this dialog box by default, instead of closing the dialog box, and return to the folder path selected in the dialog box. how can this problem be solved? It is easy to send the message cdm_getfolderpath, get the folder path, write it to the target cache, and then send the message idcancel when the idok is intercepted. the dialog box is closed. this is how I implement it. I encapsulated getopenfilename into a simple class and set a _ Bok to identify whether the "OK" button was clicked or the "cancel" button was clicked (because I finally sent the idcancel message, if _ Bok is not set, you cannot identify whether it is OK or cancel ). cfolderdialog is a simple class on the Internet, but it is an MFC class. For me, it is still in the throat, so here we use the SDK method to implement it. I hope you will have a better understanding of windows.

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.