Knowledge Cube
Knowledge Cube

Reputation: 1010

How do I get my Windows Forms drawn user control to resize larger without clipping?

I am in the process of creating my own user control in a Windows Forms application using VB.NET, and having it change in size along with the form containing it. It looks okay so long as the window size stays constant and the control stays within its original bounds. However, if I resize the window so as to make it larger, the control's contents will resize with it but end up getting clipped at the original size. I am unsure where this is coming from and have not found any way so far to fix it.

Below is some quick sample code for a user control which can reproduce the problem I'm having:

TheCircle.vb:

Public Class TheCircle
    Private _graphics As Graphics

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        _graphics = CreateGraphics()
        SetStyle(ControlStyles.ResizeRedraw, True)
        BackColor = Color.Red
    End Sub

    Private Sub TheCircle_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        _graphics.FillRectangle(Brushes.Blue, ClientRectangle)
        _graphics.FillEllipse(Brushes.LimeGreen, ClientRectangle)
    End Sub
End Class

I then place this user control after rebuilding the project in my main form and either dock it or anchor it (same result, but the latter helps better show where the clipping issues are). And below is a screenshot of my result when I attempt to resize the control beyond its "default" size:

Imgur

The green ellipse and the blue "background" rectangle should occupy the entire control area, but they aren't (it's clipped and you see the red BackColor instead). It looks like it behaves as intended though when the control is at the original size or smaller. How can I fix this? I'm pretty new to GDI+, so I'm sure it must be right under my nose...

Upvotes: 0

Views: 454

Answers (1)

DonBoitnott
DonBoitnott

Reputation: 11025

This is unnecessary, and a generally bad idea: _graphics = CreateGraphics()

It's bad because it's volatile. You get a one-time graphics object, draw something with it, and then the next refresh cycle, it's lost unless you keep doing it.

The proper approach is to use the Paint event, as it supplies you with the Graphics object in its PaintEventArgs and it is called every time a re-paint is required. You can ask for a re-paint at any point in your code by calling theCircle.Invalidate() (or Refresh() for a more immediate redraw).

Public Class TheCircle

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        SetStyle(ControlStyles.ResizeRedraw, True)
        BackColor = Color.Red
    End Sub

    Private Sub TheCircle_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        e.Graphics.FillRectangle(Brushes.Blue, ClientRectangle)
        e.Graphics.FillEllipse(Brushes.LimeGreen, ClientRectangle)
    End Sub
End Class

Upvotes: 2

Related Questions