Paint form background

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm experimenting with GDI and I have the form looking almost how I want it. I'd like to make a small change, but I can't see where to change this. Here's a screenshot of the form, and then I'll explain further.
gditest.jpg


I would like the bottom right corner to have the same look as the top right corner. Also, if I resize the form, this graphic isn't resizing to fit the whole form, which doesn't make sense because I'm using Me.ClientSize.Width, etc. for all of the rectangles and ellipses in the Paint event. Here's my code.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
perfCounterCategories()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rec [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle
rec.X = 0
rec.Y = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mypoint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point
mypoint.X = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientSize.Width
mypoint.Y = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientSize.Height
rec = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientRectangle
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] trans [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Drawing2D.Matrix()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Rotate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
trans.RotateAt(Rotate, mypoint)
Path.AddRectangle(rec)
Path.AddEllipse(rec)
Path.Transform(trans)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mycolor(3) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Color
mycolor.SetValue(Color.Cyan, 0)
mycolor.SetValue(Color.LightGray, 1)
mycolor.SetValue(Color.Black, 2)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Paint
g = Graphics.FromHwnd([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Handle)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rec [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle
rec.X = 0
rec.Y = 0
rec.Width = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientSize.Width
rec.Height = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientSize.Height
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mypoint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point
mypoint.X = 0
mypoint.Y = 0
rec = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ClientRectangle
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] gb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Drawing.Drawing2D.PathGradientBrush(Path)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] trans [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Drawing2D.Matrix()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Rotate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
trans.RotateAt(Rotate, mypoint)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mycolor(4) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Color
mycolor.SetValue(Color.LightGray, 0)
mycolor.SetValue(Color.LightGray, 1)
mycolor.SetValue(Color.Navy, 2)
mycolor.SetValue(Color.LightGray, 3)
mycolor.SetValue(Color.LightGray, 4)
gb.CenterColor = Color.Navy
gb.SurroundColors = mycolor
g.DrawRectangle(Pens.Red, rec)
g.FillRectangle(gb, rec)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Move all code to Paint event handler, you are now doing different things in form load that affect the computation involving clientrectangle. That includes the declaration of the GraphicsPath object.

Remove the line "g = Graphics.FromHwnd...". In Paint event you use the graphics instance provided from e parameter. e.Graphics

Add Form Resize event handler where you call for a repaint. Me.Refresh.
 
Thank you. That works well. Now, I'm just trying to get it to work faster so that you don't see it drawing the form. I tried to override the Paint event with a sub that threads the process, but that didn't work. The GDI part of it is coming along though.
form.png
 
Setting controlsstyles for buffering at form load will help some.
VB.NET:
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.UserPaint, True)
Better will be to combine this with your own buffering. Instead of using the using the Paint event you could make the drawing to a bitmap sized to the forms client rectangle and display this image through the forms BackgroundImage property. This is good because Paint event happens a lot for different reasons, but your painting only need to be redrawn from scratch if form is resized, where you just call the same method to have another background image drawn.
 
You can save even more by drawing the image at reasonable size and just set the BackgroundImageLayout to Stretch. If you have details that should not stretch, for example the text at lower-right these can be drawn on top at Paint event.
 
Thanks. Before you mentioned it, I was thinking about creating a bitmap from the graphics and just setting that as the background. I will definitely be giving this a try. I will also try those few lines of code to try to increase performance. This application is for use at work, so I was willing to give up on the background to keep speed reasonable, but it seems like setting the background image should be a good solution.
 
Back
Top