Painting new borders on groupboxes produces extra lines on sub-controls

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
itbiodk
Posts: 8
Last visit: Thu Oct 26, 2023 7:59 am

Painting new borders on groupboxes produces extra lines on sub-controls

Post by itbiodk »

Hello,
I have spent hours trying to understand this problem, but I simply can't wrap my head around it.
The issue is when I use the Paint event for 3 groupboxes to draw new borders that stand out more. It works perfectly, but it also draws on one of the sub-controls inside that groupbox. Only the final 2 lines does this and it seems to be related to the controls position within the groupbox.
I can show with a couple of screenshots:
GroupboxesProblem1.png
GroupboxesProblem1.png (27.17 KiB) Viewed 2156 times
The border itself works perfectly, but it draws lines on the radiobutton controls. If I remove the last two drawn lines on the groupbox (left and bottom), the lines on the radiobuttons will go away..
GroupboxesProblem2.png
GroupboxesProblem2.png (2.58 KiB) Viewed 2156 times
The drawn line on the radiobutton control here is longer. Not sure why.
GroupboxesProblem3.png
GroupboxesProblem3.png (2.79 KiB) Viewed 2156 times
If I move the control to the right, the line gets shorter. It would disappear if I moved it further.

I couldn't add the final picture for some reason. so an explanation will have to suffice.
If I move the radiobutton closer to the edge, the line bends. If one squints, the original border can be made out.

Now for the code. I believe that the only thing needed here is the PaintEventHandler that these groupboxes call with their Paint event:

Code: Select all

$groupboxes_Paint=[System.Windows.Forms.PaintEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
	
	$gfx = $_.Graphics
	$string = $this.text
	$font = New-Object System.Drawing.Font('Segoe UI', 8.25, [System.Drawing.FontStyle]::Bold)
	[int]$stringsize = ($gfx.MeasureString($string, $font)).Width +7
	$gfx.SmoothingMode = 'AntiAlias'
	$Pen = New-Object System.Drawing.Pen([System.Drawing.Color]::'Black')
	$Pen.width = 2
	[int]$width = ($_.ClipRectangle.Width - 1)
	[int]$height = ($_.ClipRectangle.Height - 2)	
	$gfx.drawline($Pen, 0, 9, 0, $height)
	$gfx.drawline($Pen, 0, 9, 7, 9)
	$gfx.drawline($Pen, $stringsize, 9, $width, 9)
	$gfx.drawline($Pen, $width, 9, $width, $height)
	$gfx.drawline($Pen, $width, $height, 0, $height)

}
Hope someone can help, because insanity is a definite possibility for me at this point.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Painting new borders on groupboxes produces extra lines on sub-controls

Post by jvierra »

You have to detect which control is being painted.
User avatar
itbiodk
Posts: 8
Last visit: Thu Oct 26, 2023 7:59 am

Re: Painting new borders on groupboxes produces extra lines on sub-controls

Post by itbiodk »

I thought so too, but I did a debug, the only ones getting in are those I have assigned the event to. I have also tried to block any that wasn't a groupbox by an if statement on $this.name.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Painting new borders on groupboxes produces extra lines on sub-controls

Post by jvierra »

The paint event is submitted to the parent control for every child control. You have to detect when the paint is painting the frame only. A GroupBox is a collection of parts and is the parent to all contained controls.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Painting new borders on groupboxes produces extra lines on sub-controls

Post by jvierra »

Why must you draw a border when a GroupBox has a border property that can be set?

I tested you code and it does not draw around anything but the GroupBox.
User avatar
itbiodk
Posts: 8
Last visit: Thu Oct 26, 2023 7:59 am

Re: Painting new borders on groupboxes produces extra lines on sub-controls

Post by itbiodk »

Heh. I tried looking up more stuff to read about, but I don't have many blue links left and maybe 95%, or more, about painting is from vb and c# which I know nothing about and find it challenging to translate.
However I did stumble on someone recommending to use control.ClientRectangle to measure borders and combined with what you said, I put together a little dumb workaround. I don't know if it is optimal, but it works.
Instead of using every control that comes through, I only use the groupboxes now.

Code: Select all

$groupboxes_Paint=[System.Windows.Forms.PaintEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
	$RunAlong = $false
	switch ($this.name)
	{
		"groupbox1"{
			[int]$width = ($groupbox1.ClientRectangle.Width - 1)
			[int]$height = ($groupbox1.ClientRectangle.Height - 2)
			$RunAlong = $true
		}
		"groupbox2"{
			[int]$width = ($groupbox2.ClientRectangle.Width - 1)
			[int]$height = ($groupbox2.ClientRectangle.Height - 2)
			$RunAlong = $true
		}"groupbox3"{
			[int]$width = ($groupbox3.ClientRectangle.Width - 1)
			[int]$height = ($groupbox3.ClientRectangle.Height - 2)
			$RunAlong = $true
		}
	}
	
	if ($RunAlong -eq $true)
	{
		$gfx = $_.Graphics
		$string = $this.text
		$font = New-Object System.Drawing.Font('Segoe UI', 8.25, [System.Drawing.FontStyle]::Bold)
		[int]$stringsize = ($gfx.MeasureString($string, $font)).Width + 7
		$gfx.SmoothingMode = 'AntiAlias'
		$Pen = New-Object System.Drawing.Pen([System.Drawing.Color]::'Black')
		$Pen.width = 2
		
		$gfx.drawline($Pen, 0, 9, 0, $height)
		$gfx.drawline($Pen, 0, 9, 7, 9)
		$gfx.drawline($Pen, $stringsize, 9, $width, 9)
		$gfx.drawline($Pen, $width, 9, $width, $height)
		$gfx.drawline($Pen, $width, $height, 0, $height)
	}
}
Thank you once again jvierra
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked