Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Validate PowerShell samples

on:
push:
pull_request:

permissions:
contents: read

jobs:
validate:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Parse and validate samples
shell: pwsh
run: ./Tests/Test-Repository.ps1
117 changes: 70 additions & 47 deletions HiDPICustomGraphicalInputBoxSample.ps1
Original file line number Diff line number Diff line change
@@ -1,61 +1,84 @@
#this sample is Based on is microsoft sample thit is Creating a Custom Input Box
#URL:https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2
# Based on Microsoft's "Creating a custom input box" PowerShell sample.
# This version adds DPI scaling and layout containers so controls remain visible.

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
[System.Windows.Forms.Application]::EnableVisualStyles()

$form = [System.Windows.Forms.Form]::new()
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,50)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.ClientSize = [System.Drawing.Size]::new(480, 180)
$form.MinimumSize = [System.Drawing.Size]::new(400, 190)
$form.AutoScaleDimensions = [System.Drawing.SizeF]::new(96.0, 96.0)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
$form.Topmost = $true

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,220)
$cancelButton.Size = New-Object System.Drawing.Size(200,50)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
# Use the current Windows dialog font. Point-based fonts scale more naturally
# than a fixed, large pixel font when the display DPI changes.
$uiFont = [System.Drawing.SystemFonts]::MessageBoxFont.Clone()
$form.Font = $uiFont

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(800,40)
$layout = [System.Windows.Forms.TableLayoutPanel]::new()
$layout.Dock = [System.Windows.Forms.DockStyle]::Fill
$layout.Padding = [System.Windows.Forms.Padding]::new(12)
$layout.ColumnCount = 1
$layout.RowCount = 3
[void]$layout.RowStyles.Add([System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::AutoSize))
[void]$layout.RowStyles.Add([System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100))
[void]$layout.RowStyles.Add([System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::AutoSize))
$form.Controls.Add($layout)

$label = [System.Windows.Forms.Label]::new()
$label.AutoSize = $true
$label.Margin = [System.Windows.Forms.Padding]::new(3, 3, 3, 10)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)
$layout.Controls.Add($label, 0, 0)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,100)
$textBox.Size = New-Object System.Drawing.Size(800,20)
#For High DPI, We Set Font Size
#In Japanese, "Yu Gothic UI" is the best font type. However, this font is for Japan only. Therefore, the default font definition is undefined.
#$Font = New-Object System.Drawing.Font("Yu Gothic UI",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$textBox.Font = $Font
$textBox = [System.Windows.Forms.TextBox]::new()
$textBox.Dock = [System.Windows.Forms.DockStyle]::Top
$layout.Controls.Add($textBox, 0, 1)

$form.Controls.Add($textBox)
$buttonPanel = [System.Windows.Forms.FlowLayoutPanel]::new()
$buttonPanel.AutoSize = $true
$buttonPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
$buttonPanel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft
$buttonPanel.WrapContents = $false
$layout.Controls.Add($buttonPanel, 0, 2)

$form.Topmost = $true
$cancelButton = [System.Windows.Forms.Button]::new()
$cancelButton.AutoSize = $true
$cancelButton.MinimumSize = [System.Drawing.Size]::new(80, 30)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$buttonPanel.Controls.Add($cancelButton)

$okButton = [System.Windows.Forms.Button]::new()
$okButton.AutoSize = $true
$okButton.MinimumSize = [System.Drawing.Size]::new(80, 30)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$buttonPanel.Controls.Add($okButton)

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.Add_Shown({ $textBox.Select() })

$selectedText = $null
try {
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$selectedText = $textBox.Text
}
}
finally {
$form.Dispose()
$uiFont.Dispose()
}

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
if ($null -ne $selectedText) {
$selectedText
}
107 changes: 69 additions & 38 deletions HiDPIGraphicalDatePickerSample.ps1
Original file line number Diff line number Diff line change
@@ -1,50 +1,81 @@
#This sample is Based on is microsoft sample thit is Graphical Date Picker
#URL:https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-graphical-date-picker?view=powershell-7.2
# Based on Microsoft's "Creating a graphical date picker" PowerShell sample.
# This version adds DPI scaling and layout containers so controls remain visible.

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form -Property @{
StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
Size = New-Object Drawing.Size 243, 300
Text = 'Select a Date'
Topmost = $true
}
[System.Windows.Forms.Application]::EnableVisualStyles()

#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
$form = [System.Windows.Forms.Form]::new()
$form.Text = 'Select a Date'
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.ClientSize = [System.Drawing.Size]::new(320, 280)
$form.MinimumSize = [System.Drawing.Size]::new(300, 270)
$form.AutoScaleDimensions = [System.Drawing.SizeF]::new(96.0, 96.0)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
$form.Topmost = $true

$uiFont = [System.Drawing.SystemFonts]::MessageBoxFont.Clone()
$form.Font = $uiFont

$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
ShowTodayCircle = $false
MaxSelectionCount = 1
}
$form.Controls.Add($calendar)
$layout = [System.Windows.Forms.TableLayoutPanel]::new()
$layout.Dock = [System.Windows.Forms.DockStyle]::Fill
$layout.Padding = [System.Windows.Forms.Padding]::new(12)
$layout.ColumnCount = 1
$layout.RowCount = 2
[void]$layout.RowStyles.Add([System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100))
[void]$layout.RowStyles.Add([System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::AutoSize))
$form.Controls.Add($layout)

$okButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 38, 500
Size = New-Object Drawing.Size 75, 50
Text = 'OK'
DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$calendarPanel = [System.Windows.Forms.FlowLayoutPanel]::new()
$calendarPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
$calendarPanel.FlowDirection = [System.Windows.Forms.FlowDirection]::LeftToRight
$calendarPanel.WrapContents = $false
$layout.Controls.Add($calendarPanel, 0, 0)

$cancelButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 113, 500
Size = New-Object Drawing.Size 150, 50
Text = 'Cancel'
DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$calendar = [System.Windows.Forms.MonthCalendar]::new()
$calendar.MaxSelectionCount = 1
$calendar.ShowTodayCircle = $false
$calendarPanel.Controls.Add($calendar)

$buttonPanel = [System.Windows.Forms.FlowLayoutPanel]::new()
$buttonPanel.AutoSize = $true
$buttonPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
$buttonPanel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft
$buttonPanel.WrapContents = $false
$layout.Controls.Add($buttonPanel, 0, 1)

$cancelButton = [System.Windows.Forms.Button]::new()
$cancelButton.AutoSize = $true
$cancelButton.MinimumSize = [System.Drawing.Size]::new(80, 30)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$buttonPanel.Controls.Add($cancelButton)

$okButton = [System.Windows.Forms.Button]::new()
$okButton.AutoSize = $true
$okButton.MinimumSize = [System.Drawing.Size]::new(80, 30)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$buttonPanel.Controls.Add($okButton)

$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$result = $form.ShowDialog()
$selectedDate = $null
try {
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$selectedDate = $calendar.SelectionStart
}
}
finally {
$form.Dispose()
$uiFont.Dispose()
}

if ($result -eq [Windows.Forms.DialogResult]::OK) {
$date = $calendar.SelectionStart
Write-Host "Date selected: $($date.ToShortDateString())"
if ($null -ne $selectedDate) {
$selectedDate
}
Loading
Loading