选择文件或文件夹对话框---带有回调函数

QString FileDialogUtils::getSaveFileDir(const QString& tips/* = QString()*/, QWidget* parent/* = 0*/) 
{
    QString result;

    BROWSEINFO bi = {0};

    if(!tips.isEmpty()) {
        bi.lpszTitle = tips.utf16();
    }

    if(parent) {
        bi.hwndOwner = (HWND)parent->winId();
    }

  //BIF_BROWSEINCLUDEFILES | // 用来显示文件
    bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; 

    bi.lpfn = BrowseCallbackProc; // 回调函数

    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

    if (pidl != NULL) {  // else, user canceled
        TCHAR szPath[MAX_PATH];
        if(SHGetPathFromIDList(pidl, szPath)) {
            result = QString::fromUtf16(szPath);
        }
    }
    
    return result;
}

INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) 
{
    LPITEMIDLIST tmp = (LPITEMIDLIST)pData;
    QString result;

    switch(uMsg) 
    {
    case BFFM_INITIALIZED: 
       //SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szdir);
        break;

    case BFFM_SELCHANGED: 
        // Set the status window to the currently selected path.
        if (tmp != NULL) {  // else, user canceled
            TCHAR szPath[MAX_PATH];
            if(SHGetPathFromIDList(tmp, szPath)) {
                result = QString::fromUtf16(szPath);
            }
        }
        break;
    }

    return 0;
}

 

posted @ 2014-10-18 15:08  纠纠结结  阅读(536)  评论(0编辑  收藏  举报