Solved

problem abstracting border drawing

Posted on 2003-10-29
12
322 Views
Last Modified: 2010-04-16
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
Comment
Question by:cuynen
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
try adding your border drawing  to the paint event
0
 
LVL 2

Author Comment

by:cuynen
Comment Utility
:: 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
 
LVL 11

Accepted Solution

by:
Agarici earned 250 total points
Comment Utility
then in the derived class call 'base.OnPaint' before doing any other drawing
0
 
LVL 2

Author Comment

by:cuynen
Comment Utility
so there is no way the base class can auto-draw the border after the derived is finished painting?
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
the 'OnPaint' method in the base class wont get called unless you call 'base.OnPaint'
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:cuynen
Comment Utility
yes, that's what I had after Agarici's second comment
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
You could use cuynen's variant.
0
 
LVL 3

Expert Comment

by:WiB
Comment Utility
Any special reason to draw the border in base class?
0
 
LVL 2

Author Comment

by:cuynen
Comment Utility
so all subclasses will have a border drawn around them
0
 
LVL 2

Author Comment

by:cuynen
Comment Utility
*** around their visual representation, that is
0
 
LVL 3

Expert Comment

by:WiB
Comment Utility
The code in comment from  ptmcomp
Date: 10/31/2003 01:03AM PST  
should work perfectly.

 
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now