Win32-从C/C ++选择目录对话框 [英] Win32 - Select Directory Dialog from C/C++

查看:52
本文介绍了Win32-从C/C ++选择目录对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从本地Win32应用程序中选择现有文件夹(或创建新文件夹)?

请您的用户帮忙,并至少设置 BIF_NEWDIALOGSTYLE 标志.

要设置初始文件夹,请添加以下代码:

  static int CALLBACK BrowseFolderCallback(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData){如果(uMsg == BFFM_INITIALIZED){LPCTSTR路径= reinterpret_cast< LPCTSTR>(lpData);:: SendMessage(hwnd,BFFM_SETSELECTION,true,(LPARAM)路径);}返回0;}//...BROWSEINFO binf = {0};...binf.lParam = reinterpret_cast< LPARAM>(initial_path_as_lpctstr);binf.lpfn = BrowseFolderCallback;... 

并提供合适的路径(例如记住上一次选择,您的应用程序数据文件夹或类似的文件)

How to select an existing folder (or create new) from a native Win32 application?

Here is a similar question. It has a good answer for C#/.NET. But I want the same thing for native Win32.

Anybody knows a solution, free code, etc?

Update:

I tried the function from the answer. Everything worked as expected, except it is necessary to call the SHGetPathFromIDList function to retrieve the name of selected directory. Here is a sample screen shot:

解决方案

SHBrowseForFolder

Do your users a favor, and set at least the BIF_NEWDIALOGSTYLE flag.

To set the initial folder, add the following code:

static int CALLBACK BrowseFolderCallback(
                  HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    if (uMsg == BFFM_INITIALIZED) {
        LPCTSTR path = reinterpret_cast<LPCTSTR>(lpData);
        ::SendMessage(hwnd, BFFM_SETSELECTION, true, (LPARAM) path);
    }
    return 0;
}

// ...
BROWSEINFO binf = { 0 };
...
binf.lParam = reinterpret_cast<LPARAM>(initial_path_as_lpctstr); 
binf.lpfn = BrowseFolderCallback;
...

and provide a suitable path (such as remembering the last selection, your applications data folder, or similar)

这篇关于Win32-从C/C ++选择目录对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆