PoshCode Archive  Artifact [88158d5c0a]

Artifact 88158d5c0ad279063d5e686e38a9543e898cb0e786eade1c13453cb108ea5344:

  • File Modal-File-Dialogs.ps1 — part of check-in [a23bab7826] at 2018-06-10 13:03:55 on branch trunk — There are problems with displaying modal dialogs from PowerShell in XP SP3. When the ShowDialog() method is called, the dialog is not modal and is behind the PowerShell window. To solve this problem, you need to add a class that implements System.Windows.Forms.IWin32Window and instantiate that class with the handle to the main window of the running process, and then pass that handle as a parameter to the ShowDialog() method to make the dialog act modally. (user: George Howarth size: 1275)

# encoding: ascii
# api: csharp
# title: Modal File Dialogs
# description: There are problems with displaying modal dialogs from PowerShell in XP SP3. When the ShowDialog() method is called, the dialog is not modal and is behind the PowerShell window. To solve this problem, you need to add a class that implements System.Windows.Forms.IWin32Window and instantiate that class with the handle to the main window of the running process, and then pass that handle as a parameter to the ShowDialog() method to make the dialog act modally.
# version: 0.1
# type: class
# author: George Howarth
# license: CC0
# x-poshcode-id: 2002
# x-archived: 2017-03-25T01:51:40
# x-published: 2011-07-22T02:41:00
#
#
Add-Type -TypeDefinition @"
using System;
using System.Windows.Forms;

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

    public IntPtr Handle
    {
        get { return _hWnd; }
    }
}
"@ -ReferencedAssemblies "System.Windows.Forms.dll"

$owner = New-Object Win32Window -ArgumentList ([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.ShowDialog($owner)