November 29, 2006

Basic OnPaint and OnSizeChanged Methods for a Resizable Control

// after InitializeComponent()
this.ResizeRedraw = true; // If you want resizing to cause a redraw

protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    this.Invalidate();
}

protected override void OnPaint(PaintEventArgs pea)
{
    try
    {
        pea.Graphics.FillRectangle(Brushes.DarkOliveGreen, 
new Rectangle(0, 0, Width, Height)); // Create a new pen. using (Pen pen = new Pen(Brushes.Black)) { // Draw a line Point pt1 = new Point(ClientRectangle.Left,
ClientRectangle.Bottom - 1); Point pt2 = new Point(ClientRectangle.Right,
ClientRectangle.Bottom - 1); pea.Graphics.DrawLine(pen, pt1, pt2); } } catch (Exception ex) { //TODO: Log these errors. } }

No comments: