我可以更改FolderBrowserDialog 的标题吗?

发布于 2024-08-01 19:30:36 字数 33 浏览 6 评论 0原文

我很好奇,它可以给我的小应用程序画龙点睛。 谢谢!

I'm curious and it could give my little app a nice finishing touch. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小伙你站住 2024-08-08 19:30:36

如果直接使用 FolderBrowserDialog 类,则不能。 但我在某处读到可以使用 P/Invoke 并发送 WM_SETTEXT 消息来更改标题。

在我看来,这是不值得的痛苦。 只需使用属性 Description 添加信息:

FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "Select the document folder";

You can't if you use the class FolderBrowserDialog directly. But I read somewhere that it could be possible to change the title with P/Invoke and sending WM_SETTEXT message.

In my opinion, it is not worth the pain. Just use the property Description to add the information:

FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "Select the document folder";
因为看清所以看轻 2024-08-08 19:30:36

您可以使用以下方法更改它:

SetWindowText (hwnd, "Select a Folder");

其中 hwnd 是窗口句柄,它会触发“浏览文件夹”对话框。

You can change it by using:

SetWindowText (hwnd, "Select a Folder");

Where hwnd is the window handle, which triggers the Browse For Folder Dialog.

幸福丶如此 2024-08-08 19:30:36

我正在寻找如何做到这一点,但很大程度上必须自己解决。
我希望这可以节省任何人的时间:

在我的主要方法上方,我输入:

    [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
    public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);
    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
    public static extern IntPtr FindWindow(string className, string windowName);

由于我知道显示对话框时线程将暂停,因此我创建了一个新线程来检查该对话框窗口是否存在。 就像这样:

    bool notfound = true;
    new Thread(() => { 
    while (notfound)
    {
        //looks for a window with the title: "Browse For Folder"
        IntPtr ptr = FindWindow(null, "Browse For Folder");
        if (ptr != IntPtr.Zero)
        {
            //tells the while loop to stop checking
            notfound = false;
            //changes the title
            SetWindowText(ptr, "Be happy!");
        }
    }
    }).Start();

然后,我发起对话:

    using (var fbd = new FolderBrowserDialog())
    {
        DialogResult result = fbd.ShowDialog();
        if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
        {
            //do stuff
        }
    }

这对我有用,而且并不那么复杂。 希望这可以帮助任何可能偶然发现此页面的人。
顺便说一句,请记住该线程必须在启动对话框窗口之前启动,以便它可以在窗口存在时立即运行并检查该窗口。

I was looking up how to do this, but largely had to figure it out on my own.
I hope this saves anyone some time:

Above my main method, I put:

    [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
    public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);
    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
    public static extern IntPtr FindWindow(string className, string windowName);

Since I know that the thread will pause while the dialog is shown, I created a new thread to check for that dialog window while it exists. Like so:

    bool notfound = true;
    new Thread(() => { 
    while (notfound)
    {
        //looks for a window with the title: "Browse For Folder"
        IntPtr ptr = FindWindow(null, "Browse For Folder");
        if (ptr != IntPtr.Zero)
        {
            //tells the while loop to stop checking
            notfound = false;
            //changes the title
            SetWindowText(ptr, "Be happy!");
        }
    }
    }).Start();

Then, I initiate the dialog:

    using (var fbd = new FolderBrowserDialog())
    {
        DialogResult result = fbd.ShowDialog();
        if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
        {
            //do stuff
        }
    }

This has worked for me, and it's not so complicated. Hope this helps anyone that might stumble on this page.
By the way, remember that the thread must start before the dialog window is initiated, so that it can run and check for the window as soon as it exists.

极致的悲 2024-08-08 19:30:36

是的你可以。 FolderBrowserDialog 类有一个名为 UseDescriptionForTitle 的属性,该属性指示是否使用 Description 属性的值作为对话框标题。

这是一个代码片段:

var dialog = new System.Windows.Forms.FolderBrowserDialog() 
{
    Description = "Choose a folder",
    UseDescriptionForTitle = true
}

Yes you can. There is a property for the FolderBrowserDialog class called UseDescriptionForTitle that indicates whether to use the value of the Description property as the dialog title.

Here is a code snippet :

var dialog = new System.Windows.Forms.FolderBrowserDialog() 
{
    Description = "Choose a folder",
    UseDescriptionForTitle = true
}
金兰素衣 2024-08-08 19:30:36

简单的答案是你不能。 该对话框使用 Windows 上文件夹浏览器样式对话框的标准标题进行显示。 最好的选择是通过设置“描述”属性来确保您拥有有意义的描述性文本。

即使您要使用 P/Invoke 调用 SHBrowseForFolder 直接使用Win32 API函数,唯一的选择仍然无法更改对话框的实际标题。 您可以设置 BROWSEINFO 的 lpszTitle 字段结构,即

指向空终止字符串的指针
显示在树视图上方
对话框中的控制。 这个字符串
可用于指定指令
用户。

The simple answer is that you can't. The dialog displays using the standard title for a folder browser style dialog on Windows. The best option is to ensure that you have meaningful descriptive text by setting the Description property.

Even if you were to use P/Invoke to call the SHBrowseForFolder Win32 API function directly, the only option you still can't change the actual title of the dialog. You can set the lpszTitle field of the BROWSEINFO structure, which is

A pointer to a null-terminated string
that is displayed above the tree view
control in the dialog box. This string
can be used to specify instructions to
the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文