zoukankan      html  css  js  c++  java
  • 浏览文件夹对话框初始化时选中特定目录

    在Windows编程时,我们有时需要设定初始目录,希望浏览文件夹对话框在弹出时,选中用户所指的一个目录,而不是千篇一律地选中“我的电脑”。

    如下:弹出“浏览文件夹”对话框时,选中“E:\Programs\VC\MFC\DataFiles\

    具体可通过BROWSEINFO结构体中lpfn所指向的回调函数来实现。【另一个成员lParam传递初始路径】

    代码实现如下:

     1 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)   
    2 {
    3 if (uMsg == BFFM_INITIALIZED )
    4 {
    5 ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,lpData);
    6 }
    7 return 0;
    8 }
    9
    10 CString CTestApp::GetPath(const CString& strInitPath, HWND wnd)
    11 {
    12 CString strPath;
    13 BROWSEINFO bi;
    14 char name[MAX_PATH];
    15 LPITEMIDLIST pidl;
    16
    17 ZeroMemory(&bi,sizeof(BROWSEINFO));
    18 bi.hwndOwner = wnd;
    19 bi.pszDisplayName = name;
    20 bi.lpszTitle = "选择文件夹";
    21 bi.ulFlags = BIF_RETURNONLYFSDIRS;
    22 bi.lpfn = BrowseCallbackProc;
    23 bi.lParam = (LPARAM)(const char*)strInitPath;
    24
    25 CoInitialize(NULL);
    26 pidl = SHBrowseForFolder(&bi);
    27
    28 if(pidl == NULL)
    29 {
    30 return "";
    31 }
    32
    33 SHGetPathFromIDList(pidl, strPath.GetBuffer(MAX_PATH));
    34 strPath.ReleaseBuffer();
    35 if (strPath != "")
    36 {
    37 if(strPath.GetAt(strPath.GetLength() - 1) != '\\')
    38 {
    39 strPath += '\\';
    40 }
    41 }
    42
    43 HRESULT hr;
    44 IMalloc * pMalloc = NULL;
    45 hr = SHGetMalloc(&pMalloc);
    46 if(FAILED(hr))
    47 {
    48 AfxMessageBox(_T("系统函数调用出错"));
    49 }
    50 //释放pidl
    51 pMalloc->Free((void*)pidl);
    52 pidl = NULL;
    53
    54 //清除分配符
    55 pMalloc->Release();
    56 pMalloc = NULL;
    57
    58 return strPath;
    59 }


    MFC Demo: https://files.cnblogs.com/kekec/FolderSelect.rar

  • 相关阅读:
    每天一道LeetCode--141.Linked List Cycle(链表环问题)
    每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)
    每天一道LeetCode--118. Pascal's Triangle(杨辉三角)
    CF1277D Let's Play the Words?
    CF1281B Azamon Web Services
    CF1197D Yet Another Subarray Problem
    CF1237D Balanced Playlist
    CF1239A Ivan the Fool and the Probability Theory
    CF1223D Sequence Sorting
    CF1228D Complete Tripartite
  • 原文地址:https://www.cnblogs.com/kekec/p/2369058.html
Copyright © 2011-2022 走看看