I have a situation in which I will be moving painted graphics around on a form. Is there a way to change the z-order of painted graphics like you do controls?
Just a sample..
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
VB Code:
Dim g As Graphics = Me.CreateGraphics()
Dim str As String = "135"
g.FillRectangle(Brushes.Black, 15, 5, 25, 17)
g.DrawString(str, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point), Brushes.White, 15, 5)
g.Dispose()
How can I switch the DrawString and FillRectangle so that one happens before the other (other than just changing the code order of when they are drawn)?
Hi, Z order is based on the order that the primitives are draw, so in your case you will have to swap them around, perhaps with a couple of if statement.
Drawing packages would typically store the primitives in a Collection or ArrayList making it easy to move them around, to affect the Z order.
The best way would be to create a simple class for each type of thing you want to draw. Each class should inherit from a common class that has a Draw method. The derived classes override the Draw method and accept a Graphics parameter, to which the class draws itself.
Then store a load of these derived classes in an arraylist and draw them all in a loop. To change the drawing order, simply change the order of the objects in the array.
I can write you a sample project if you want. Let me know.
I made it as simple as possible just to demonstrate the ZOrder principle and a bit of inheritance that lets you treat diamonds and circles in the same way.
I will have to look at closely, but what I need (to clarify further) is to draw an image at startup. While my program is running...another image will be drawn to the screen. This image must be (behind) the image that was drawn first. Will you example do that? To make it worse...I will have another image that will be moving around the screen as the program runs. This image must pass over (and under) other drawn images. That is why having a z-order to change on the fly would be nice.
This image must pass over (and under) other drawn images
I wish you had said that earlier.
You'd be better off thinking about transparent colours and alphablending instead of zorder. You cannot draw something 'behind' something else but you can draw one thing and then something else on top. You have to draw from the back to the front in that order.
My demo will not change the z order for you but it is ready to have that feature added simply by shuffling the contents of an arraylist in whatever sequence you like. Just look at the Button's code and you see what I mean.
I didn't code the shuffling because i wasn't sure how you wanted to change the zOrder I've left it open ended for you.
Thanks! I thought when I said "z-order" that would be a clue to "over and under"...but anyway....
I really appreciate your efforts and will give a great rating!
Thanks again..
I knew you meant "over and under" that is the very definition of the Z axis Its just the way you mentioned animation that makes alphablending more suitable. You can easily use BOTH z ordering AND alphablending to great effect though, give it a shot
Hi, you should be able to animate with Z order, as long as you redraw all the primitives every frame. This is well within the capabilities of GDI+, well up to approx 2000 – 3000 thousand primitives anyway!
See the attached project for a demo. I have little experience with animation but you will need to address the animation speed issue, at the moment I just change the number of frames, I'm sure there is a more refined way of doing this.
I’ve not commented but should be clear enough, let me know if you have any questions!