Winforms, GDI+ and basic conceptual fightings

This forum is meant for anything you would like to share with other visitors
FFF
Posts: 1622
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Winforms, GDI+ and basic conceptual fightings

Post by FFF »

After years of avoiding this, i finally want to tackle some graphic output. Searching, i found a lamentable loss of docs for anything a bit more complicated than "DrawLine"...
Attached a sample of a booking plan, what should in the end be viewable and printable.
* Textblock, horizontally centered, pegged to the upper border
* Blocks of rows of rectangles, number of rectangles might differ for every row
* Each rectangle has a number inside
* Each row has "outside" a row number, ideally "not rotated"
* Each block has a naming letter, not rotated
* in the end all of them should be placed together and scaled to fit onto a given page format.

Basically my question is, how to dynamically handle multiple placings of somewhat rotated "Building blocks". Naturally, i might accept restrictions, calculating everything "by hand", but there has to be a better way.

I think i meanwhile grasped the concept of transformations more or less, but i struggle how to proceed. Would it get easier using panels?

(Chris, i remember you presented some GDI samples back in VN days, but can't find them anymore, is there a place to get them?)
VVK_VanBeethoven_Plan.pdf
(48.12 KiB) Downloaded 67 times
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3827
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Winforms, GDI+ and basic conceptual fightings

Post by wriedmann »

Hi Karl,
if you like to do that in Windows forms, I would build my own helper class that is built over DrawLine.
After all, what you need is a collection of lines.
Basically your objects are all rectangles with some caracteristics:
- width and length
- rotation
- origin
- available or not (X)
- text
So I would start with this...

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1622
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Re: Winforms, GDI+ and basic conceptual fightings

Post by FFF »

Wolfgang,
that's the easy part...
What puzzles me, is the "assembly" of the parts. For placement, i need bounding boxes, for rotation a useful origin for each part, and i still seem to misunderstand the effects of ResetTransform.
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 5102
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Winforms, GDI+ and basic conceptual fightings

Post by Chris »

Hi Karl,

My old session (for vulcan) was included in 2023's session material, in the folder "Old sessions", but I'm attaching it also here:

2005. Programming GDI+ in Vulcan.NET.zip
(385.93 KiB) Downloaded 60 times

Regarding rotation, have a look in the GDI+ demo code, in the method DrawWave(). Basically you need to

- Save current state with Graphics:BeginContainer()
- Move the painting coordinates to the center of the object (collection of lines etc) that you want to draw rotated with TranslateTransform()
- Rotate the painting axis with RotateTransform()
- Do the drawing
- Restore the default drawing state with EndContainer()

This will draw everything rotated, but if you need to do some parts not rotated, then I'm afraid you will need to do some math calculation on where you should draw normally those parts.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1622
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Re: Winforms, GDI+ and basic conceptual fightings

Post by FFF »

Thx Chris for the files, will study.

While on it, i get an

Code: Select all

error XS0507: 'DemoPictureBox.OnPaint(System.Windows.Forms.PaintEventArgs)': cannot change access modifiers when overriding 'protected' inherited member 'System.Windows.Forms.PictureBox.OnPaint(System.Windows.Forms.PaintEventArgs)'	135,1	GDI_plus_demo.prg	DemoPictureBox:OnPaint
on this line
METHOD OnPaint(e AS PaintEventArgs) AS VOID CLASS DemoPictureBox

This i also saw recently in another application - i have no clue, what is wrong here.
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 5102
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Winforms, GDI+ and basic conceptual fightings

Post by Chris »

Hi Karl,

The parent OnPaint() method of the PictureBox class is PROTECTED, message says that when you override this method in a subclass, it can't have different visibility, so you need to make it PROTECTED as well. Vulcan was doing this automatically.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1622
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Re: Winforms, GDI+ and basic conceptual fightings

Post by FFF »

Thx Chris, proper reading could have told me that...

Started meanwhile drawing boxes, but it all feels very non oop.

Would you say, it's complete nonsense, to draw buttons (or even checkboxes) instead? The result will be almost totally static, so graphic performance is a non issue here...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 5102
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Winforms, GDI+ and basic conceptual fightings

Post by Chris »

Hi Karl,

Problem is, how will you rotate the buttons?

I really like stuff like this, see this code below that uses OOP to draw such buttons, you can use it as a starting point:

Code: Select all

USING System.Windows.Forms
USING System.Drawing
USING System.Collections.Generic

[STAThreadAttribute];
FUNCTION Start( ) AS VOID
	DrawForm{}:ShowDialog()


CLASS DrawForm INHERIT System.Windows.Forms.Form
	PROTECT oPictureBox1 AS System.Windows.Forms.PictureBox
	PROTECT aBoxes AS List<BigBox>

	CONSTRUCTOR()
	
		SUPER()

		SELF:oPictureBox1 := System.Windows.Forms.PictureBox{}

		SELF:ClientSize := System.Drawing.Size{704 , 448}

		SELF:oPictureBox1:Location := System.Drawing.Point{8 , 11}
		SELF:oPictureBox1:Size := System.Drawing.Size{679 , 422}
		SELF:oPictureBox1:Paint += SELF:PictureBox1_Paint
		SELF:Controls:Add(SELF:oPictureBox1)
		
		SELF:aBoxes := List<BigBox>{}
		SELF:aBoxes:Add(BigBox{3,30,10,0})
		SELF:aBoxes:Add(BigBox{5,100,100,0})
		SELF:aBoxes:Add(BigBox{5,200,100,45})
		SELF:aBoxes:Add(BigBox{10,400,200,-30})
	RETURN

	METHOD PictureBox1_Paint(sender AS System.Object , e AS System.Windows.Forms.PaintEventArgs) AS VOID
		FOREACH oBox AS BigBox IN SELF:aBoxes
			oBox:DoPaint(e:Graphics)
		NEXT
END CLASS

CLASS BigBox
	PROTECT x,y,boxes AS INT
	PROTECT rotation AS REAL4

	STATIC PROTECT size := 20 AS INT
	STATIC PROTECT oPen := Pen{Color.Black} AS Pen
	STATIC PROTECT oFont := Font{"Arial", size/2} AS Font
	STATIC PROTECT oBrush := SolidBrush{Color.Black} AS SolidBrush
	CONSTRUCTOR(nBoxes AS INT, x AS INT, y AS INT, nRotation AS REAL4)
		SELF:boxes := nBoxes
		SELF:x := x
		SELF:y := y
		SELF:rotation := nRotation
	RETURN
	METHOD DoPaint(oGraphics AS Graphics) AS VOID
		LOCAL oContainer AS System.Drawing.Drawing2D.GraphicsContainer
		LOCAL oRect AS Rectangle

		oRect := Rectangle{SELF:x - (SELF:boxes * size / 2) , SELF:y - size / 2, SELF:boxes * size, size}

		oContainer := oGraphics:BeginContainer()
		oGraphics:TranslateTransform(SELF:x,SELF:y)
		oGraphics:RotateTransform(SELF:rotation)
		oGraphics:TranslateTransform(-SELF:x,-SELF:y)
		oGraphics:DrawRectangle(oPen, oRect)
		FOR LOCAL n := 1 AS INT UPTO SELF:boxes
			oGraphics:DrawLine(oPen, oRect:Left + n * size, oRect:Top, oRect:Left + n * size, oRect:Bottom)
			oGraphics:DrawString(n:ToString(), oFont , oBrush, oRect:Left + n * size - size / 2, oRect:Top + size / 4)
		NEXT
		oGraphics:EndContainer(oContainer)
	RETURN
	
END CLASS
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
TerryB1
Posts: 2
Joined: Thu Jan 30, 2025 12:08 pm
Location: United Kingdom

Re: Winforms, GDI+ and basic conceptual fightings

Post by TerryB1 »

Hi Karl

Having been away from this forum for some time, I thought Id take a look to see how things were going.

I note your recent post - wanting to tackle graphic output - and would advise that you take a look at WPF.

The reason is not that WinForms GDI+ will not do everything you want. In fact, one book I have (less than 5 years old) claims WinForms GDI to be the best thing since sliced bread!

Although it is a somewhat subjective and personal view, I find WPF an easier proposition.

Let me explain:
Whatever system you use, the rendering, on the full screen or even part of it depends on the precision and nature of the geometry of the object in question.

What is happening is just the same as we would do by hand with pencil and paper - ie draw, rub out and draw again. For motion it is just the same over and over again in slightly different position. Any of this is either over the full screen or just a small part.(Rectangular areas specified by View-Ports either 2D or 3D). Any imprecision would cause breaks or offsets in the rendering.

The precision depends on many calculations involving integers, doubles, floats and so on. The last time I tried WinForms I had to take account of all this (potentially confusing) maths and do it myself.

With WPF it is done for me.

Lines can be specified to follow any path geometry you like, however complex. (Bezier). x y z co-ordinates make angles a breeze. (There is a mini Geometry drawing package within WPF)

Furthermore, going forwards, the whole structure of WPF lends itself to programmers being able to construct their own frameworks specific to their own specific programming objectives.

Of course anything you glean from the WinForms route will have read across to WPF and vice versa.

Just a thought.

Terry
FFF
Posts: 1622
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Re: Winforms, GDI+ and basic conceptual fightings

Post by FFF »

Hi Terry,
thx for sharing. Interestingly, my first thought WAS WPF, as i thought the idea of 6 Panels on a canvas, each self adapting and resizing to the available space, it's interior independently rotateable,rather attractive.
Rant on: Got somewhat discouraged reading about things not working, not being implemented, no designer etc.
Frankly, the whole thing is a PITA. Diving a bit into Winforms, i read, that MS decided recently to drop doing enhancement on anything other than Windows, instead telling us, to use external libs, like Google's. WPF was announced as "the" solution to all your needs, then one hears about being a dead end, to be supplaced by Maui or whatever acronym you may like.
I have neither intention nor surplus time of life to learn every other day a new toy, because someone decides we need to be occupied. /rant off.

For my immediate need, i think i found a way to avoid the whole rotating business, using circles instead of boxes and letting all text be "regular" placed. Got, as usual, distracted, but hope to advance the next couple of days... Will keep you informed ;-)
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Post Reply