• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

problem abstracting border drawing

ref. the code below
have a class that draws a border around itself
want a base class that handles the border drawing

:: here the border gets drawn ::

public MyClass
{
     public MyClass()
     {
     }

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

     protected override void OnPaint(PaintEventArgs e)
     {
          Graphics g = e.Graphics;

          /* draw shape */

          /* draw border */
     }
}

:: here it never gets drawn ::

public BaseClass
{
     public BaseClass()
     {
          this.Invalidated += new InvalidateEventHandler(this.DrawBorder);
     }

     protected void DrawBorder(object sender, InvalidateEventArgs e)
     {
          Graphics g = Graphics.FromWhnd(this.Handle);

          /* draw border */
     }
}

:: solution ::

that's why I'm here :D
0
cuynen
Asked:
cuynen
  • 5
  • 3
  • 2
  • +1
1 Solution
 
AgariciCommented:
try adding your border drawing  to the paint event
0
 
cuynenAuthor Commented:
:: subscribing to the Paint event ::

public BaseClass
{
     public BaseClass()
     {
          this.Paint += new PaintEventHandler(this.DrawBorder);
     }

     protected void DrawBorder(object sender, PaintEventArgs e)
     {
          Graphics g = e.Graphics;

          /* draw border */
     }
}

:: thoughts ::

it doesn't work because I've overridden the protected OnPaint method in the derived class
if I comment out the overridden method, the border is drawn
but of course then my derived class serves no purpose anymore :)
0
 
AgariciCommented:
then in the derived class call 'base.OnPaint' before doing any other drawing
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cuynenAuthor Commented:
so there is no way the base class can auto-draw the border after the derived is finished painting?
0
 
AgariciCommented:
the 'OnPaint' method in the base class wont get called unless you call 'base.OnPaint'
0
 
ptmcompCommented:
How about this:

public MyClass
{
    public MyClass()
    {
    }

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

    protected override void OnPaint(PaintEventArgs e)
    {
         Graphics g = e.Graphics;

         /* draw shape */

        base.OnPaint(e);
    }
}


public BaseClass
{
    public BaseClass()
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
         Graphics g = e.Graphics;

         /* draw border */
    }
}
0
 
cuynenAuthor Commented:
yes, that's what I had after Agarici's second comment
0
 
ptmcompCommented:
You could use cuynen's variant.
0
 
WiBCommented:
Any special reason to draw the border in base class?
0
 
cuynenAuthor Commented:
so all subclasses will have a border drawn around them
0
 
cuynenAuthor Commented:
*** around their visual representation, that is
0
 
WiBCommented:
The code in comment from  ptmcomp
Date: 10/31/2003 01:03AM PST  
should work perfectly.

 
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now