当我创建表单并添加控件时,我将大小和位置设置为以表单为中心。相反,我得到的东西向右和向下偏移。
造成此偏移量的原因是什么!!
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
# Main form
$form = New-Object System.Windows.Forms.Form
$form.Size = [System.Drawing.Size]::new(400,500)
$form.StartPosition = 'CenterScreen'
$form.SizeGripStyle = 'Hide'
$form.MaximizeBox = $false
$form.ShowInTaskbar = $true
$form.Text = 'I wont display right.'
#control
$mainPanel = New-Object System.Windows.Forms.Panel
$mainPanel.Anchor = [System.Windows.Forms.AnchorStyles]::Top
$mainPanel.Size = [System.Drawing.Size]::new(($form.Width - 50),($form.Height - 50))
$mainPanel.Location = [System.Drawing.Point]::new(25,25)
$mainPanel.AutoScroll = $true
$mainPanel.BackColor = [System.Drawing.Color]::Black
$form.Controls.Add($mainPanel)
$form.BringToFront()
$form.ShowDialog()
结果:
答案 0 :(得分:1)
表单大小和工作区(客户端)区域不同。
仅更改代码中的一行即可获得期望的输出:
$ mainPanel.Size = [System.Drawing.Size] :: new((($ form.ClientSize.Width- 50),($ form.ClientSize.Height-50))
答案 1 :(得分:0)
尝试以下调整:
$mainPanel.Size = [System.Drawing.Size]::new(($form.ClientSize.Width - 50),($form.ClientSize.Height - 50))