本文介绍了BrowseForFolder-Dialog:居中并使 TopMost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Winforms Gui 和一个 BrowseForFolder-Dialog.

I have a Winforms Gui with a BrowseForFolder-Dialog.

有什么办法可以让这个对话框成为TopMost窗口和/或居中它在屏幕上?

Is there any way to make this dialog the TopMost window and/or center it on the screen?

$getfolder = New-Object -com shell.application
$foldername = $getfolder.BrowseForFolder(0,"Text",16,"")

推荐答案

您必须指定所有者句柄.

You have to specify the owner handle.

$handle = [System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle
$getfolder = New-Object -ComObject Shell.Application
$foldername = $getfolder.BrowseForFolder([int]$handle, "Text", 16, "")

但它是 COM 和 .NET 的混合体.我推荐以下解决方案

But it is a mixture of COM and .NET. I recommend the following solution

$win32WindowDefinition = @"
using System;
using System.Windows.Forms;

public class Win32Window : IWin32Window
{
    public Win32Window(IntPtr handle)
    {
        Handle = handle;
    }

    public IntPtr Handle { get; private set; }
}
"@

Add-Type -TypeDefinition $win32WindowDefinition -ReferencedAssemblies System.Windows.Forms.dll 

$ownerHandle = New-Object Win32Window -ArgumentList ([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
$folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialogResult = $folderBrowserDialog.ShowDialog($ownerHandle)
if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
    $folderName = $folderBrowserDialog.SelectedPath
}

来自 http://poshcode.org/2002

这篇关于BrowseForFolder-Dialog:居中并使 TopMost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 15:39