Label and Picturebox position collision

2019-08-02 20:59发布

According to first question where I met another problem: Label hidden by PictureBox in Powershell using Windows Forms I created next question, to solve it.

Problem is that I use two different controls (PictureBox and Label) and I cannot fit PictureBox to Top Right corner of forms without problems with label which is centered in forms.

Image declaration

$Image = [system.drawing.image]::FromFile("C:\xxx.png")
$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.Dock = [System.Windows.Forms.DockStyle]::Right
$pictureBox.BackColor = "Transparent"
$pictureBox.AutoSize = $True
$pictureBox.Image=$Image
$Form.Controls.Add($pictureBox)
$pictureBox.SendToBack()

Label declaration

$redLabel1 = New-Object System.Windows.Forms.Label
$redLabel1.Location = New-Object System.Drawing.Size($Form.Width, $Form.Height)
$redLabel1.AutoSize = $False
$redLabel1.TextAlign = "MiddleCenter"
$redLabel1.Dock = "Fill"
$redLabel1.Text = "Something"
$redLabel1.ForeColor = "Red"
$redLabel1.BackColor = "Transparent"
$Font = New-Object System.Drawing.Font("Arial", 55, [System.Drawing.FontStyle]::Bold)
$redLabel1.Font = $Font
$Form.Controls.Add($redLabel1)
$redLabel1.BringToFront()

At the moment I got something like this:

enter image description here

I think problem is with Style.Dock. Because when I am using Style.Dock = "Top" label is OK and centered without problems but PictureBox is on the left side. When I am use Style.Dock = "Right", I get same what is in screenshot above.

When I am using Style.Anchor, picture is not showing, so it's not working or I am using it in wrong way.

0条回答
登录 后发表回答