vb.net - How do I get my Windows Forms drawn user control to resize larger without clipping? -
i in process of creating own user control in windows forms application using vb.net, , having change in size along form containing it. looks okay long window size stays constant , control stays within original bounds. however, if resize window make larger, control's contents resize end getting clipped @ original size. unsure coming , have not found way far fix it.
below quick sample code user control can reproduce problem i'm having:
thecircle.vb
:
public class thecircle private _graphics graphics public sub new() ' call required designer. initializecomponent() ' add initialization after initializecomponent() call. _graphics = creategraphics() setstyle(controlstyles.resizeredraw, true) backcolor = color.red end sub private sub thecircle_paint(sender object, e painteventargs) handles me.paint _graphics.fillrectangle(brushes.blue, clientrectangle) _graphics.fillellipse(brushes.limegreen, clientrectangle) end sub end class
i place user control after rebuilding project in main form , either dock or anchor (same result, latter helps better show clipping issues are). , below screenshot of result when attempt resize control beyond "default" size:
the green ellipse , blue "background" rectangle should occupy entire control area, aren't (it's clipped , see red backcolor
instead). looks behaves intended though when control @ original size or smaller. how can fix this? i'm pretty new gdi+, i'm sure must right under nose...
this unnecessary, , bad idea: _graphics = creategraphics()
it's bad because it's volatile. one-time graphics object, draw it, , next refresh cycle, it's lost unless keep doing it.
the proper approach use paint
event, supplies graphics
object in painteventargs
, called every time re-paint required. can ask re-paint @ point in code calling thecircle.invalidate()
(or refresh()
more immediate redraw).
public class thecircle public sub new() ' call required designer. initializecomponent() ' add initialization after initializecomponent() call. setstyle(controlstyles.resizeredraw, true) backcolor = color.red end sub private sub thecircle_paint(sender object, e painteventargs) handles me.paint e.graphics.fillrectangle(brushes.blue, clientrectangle) e.graphics.fillellipse(brushes.limegreen, clientrectangle) end sub end class
Comments
Post a Comment