Get the App
SLTechnology News&Howtos  ›  Servers  › 

Several ways of Powershell pop-up window

Shulou Source: shulou.com Published: 2022-06-02 18:27:32 09月28日 Update

Originally, this article was supposed to be finished in the morning, but as a result, I had to admit that I was a little lazy and made some summary of the recent projects at hand. Powershell development, operation and maintenance has also been done for a period of time. Today, let's talk about those simple GUI programming operations and maintenance. Let's start with the pop-up window.

Pop-up window to write their own PS GUI code is often used in three ways, wscript mode, Forms way, VB way, first talk about Wscript way, this way is the simplest, but also the most crude, only two lines of code can simply make a pop-up window.

Wscript mode:

Function Read-MessageBoxDialog

{

$PopUpWin = new-object-comobject wscript.shell

$PopUpWin.popup ("Hello World")

}

Read-MessageBoxDialog

Forms way, compared with Wscript way, this way writes more code, but presents a more friendly style.

Function Read-MessageBoxDialog

{

Param ([string] $Message

[string] $WindowTitle

[System.Windows.Forms.MessageBoxButtons] $Buttons = [System.Windows.Forms.MessageBoxButtons]:: OK

[System.Windows.Forms.MessageBoxIcon] $Icon = [System.Windows.Forms.MessageBoxIcon]:: None)

Add-Type-AssemblyName System.Windows.Forms

Return [System.Windows.Forms.MessageBox]:: Show ($Message, $WindowTitle, $Buttons, $Icon)

}

Read-MessageBoxDialog-Message "Hello World"-WindowTitle "CustomTitleHere"-Buttons OK-Icon Information

The last way is VB, which calls VB in PS to make pop-up windows, which is basically similar to Forms.

Function Read-MessageBoxDialog

{

Param ([string] $Message, [string] $WindowTitle)

Add-Type-AssemblyName Microsoft.VisualBasic

Return [Microsoft.VisualBasic.Interaction]:: MsgBox ($Message,'Information',$WindowTitle)

}

Read-MessageBoxDialog-Message "Hello World"-WindowTitle "CustomTitleHere"

Finally, we can see that the style of VB is the same as that of Forms, but the code is much less than that of Forms. If it is just a prompt window, it is recommended to use VB form. If you want the prompt window to be displayed in Information form, and the OK and Cancel buttons exist at the same time, set the Buttons property in Forms mode to OKCancel.

Function Read-MessageBoxDialog

{

Param ([string] $Message

[string] $WindowTitle

[System.Windows.Forms.MessageBoxButtons] $Buttons = [System.Windows.Forms.MessageBoxButtons]:: OK

[System.Windows.Forms.MessageBoxIcon] $Icon = [System.Windows.Forms.MessageBoxIcon]:: None)

Add-Type-AssemblyName System.Windows.Forms

Return [System.Windows.Forms.MessageBox]:: Show ($Message, $WindowTitle, $Buttons, $Icon)

}

Read-MessageBoxDialog-Message "Hello World"-WindowTitle "CustomTitleHere"-Buttons OKCancel-Icon Information

Tags: Mode code style form hint crude just at the same time properties suggestions hands buttons time is in the article results project development programming Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei MariaDB Shulou Tech Info Microsoft