[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Template encapsulating function call

John Doe

10/22/2008 5:08:00 PM

Hi,

I am currently writing a graphical control(a list control) that can
display a picture followed by some text and to do so I first calculate
an item height and then I really draw.
I was wondering if it would be a good idea to merge the two
methods(calculating and drawing) knowing that when calculating we
actually simulate a drawing.
Basically my control is called the first time to calculate the height of
an Item (MeasureItem) and then to draw (DrawItem).
I was thinking of something like that :

// Method to handle OwnerDraw
void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
DrawItem(lpDIS, TRUE);
}
void DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
DrawItem(lpDIS, FALSE);
}


// custom method that calulate or really draw
void DrawItem(LPDRAWITEMSTRUCT lpDIS, BOOL bOnlyCalculate)
{

}


Now you need to know that the difference between drawing text or
calculating its height relies on an flag (DT_CALCRECT) passed to a
function DrawText:

DrawText(hDC,lpString, nCount, &Rect, uFormat)

if uFormat & DT_CALCRECT we calculate otherwise it means we draw.

So I was wondering if it would be possible to write a template that
could take a pointer to DrawText function and that would add DT_CALCRECT
to the argument when calculating :

DrawTextFunc<false>(....) would mean we call function to calculate
DrawTextFunc<true>(....) would mean we really draw

About drawing bitmap we could do something similar

DrawBitmap<false>() would mean we don't display bitmap - so should call
an empty function
DrawBitmap<true>() really draw the bitmap


Could someone tell me how to write something like my DrawTextFunc and
DrawBitmap ?

Thanks












4 Answers

.rhavin grobert

10/22/2008 6:18:00 PM

0

On 22 Okt., 19:07, Mosfet <mos...@anonymous.org> wrote:
> Hi,
>
> I am currently writing a graphical control(a list control) that can
> display a picture followed by some text and to do so I first calculate
> an item height and then I really draw.
> I was wondering if it would be a good idea to merge the two
> methods(calculating and drawing) knowing that when calculating we
> actually simulate a drawing.
> Basically my control is called the first time to calculate the height of
> an Item (MeasureItem) and then to draw (DrawItem).
> I was thinking of something like that :
>
> // Method to handle OwnerDraw
> void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
> {
>    DrawItem(lpDIS, TRUE);}
>
> void DrawItem(LPDRAWITEMSTRUCT lpDIS)
> {
>    DrawItem(lpDIS, FALSE);
>
> }
>
> // custom method that calulate or really draw
> void DrawItem(LPDRAWITEMSTRUCT lpDIS, BOOL bOnlyCalculate)
> {
>
> }
>
> Now you need to know that the difference between drawing text or
> calculating its height relies on an flag (DT_CALCRECT) passed to a
> function DrawText:
>
> DrawText(hDC,lpString, nCount, &Rect, uFormat)
>
> if uFormat & DT_CALCRECT we calculate otherwise it means we draw.
>
> So I was wondering if it would be possible to write a template that
> could take a pointer to DrawText function and that would add DT_CALCRECT
> to the argument when calculating :
>
> DrawTextFunc<false>(....) would mean we call function to calculate
> DrawTextFunc<true>(....) would mean we really draw
>
> About drawing bitmap we could do something similar
>
> DrawBitmap<false>() would mean we don't display bitmap - so should call
> an empty function
> DrawBitmap<true>() really draw the bitmap
>
> Could someone tell me how to write something like my DrawTextFunc and
> DrawBitmap ?
>
> Thanks

Just some thoughts:

Isn't the main difference between drawing and calculating that you
dont need to play (in your case) with CDCs and CFonts? In complex
scenatios, try to measure the item the first time it is drawn, save
the result in some appropriate struct and use it in any following
MeasureItem-calls. Recalculate just only if your item really changed.
If you can just measure your item by actually "simulating a draw" try
the following:

MeasureItem:
Draw your item offscreen (MemDC) and return appropriate result.
DrawItem:
BitBlt your offscreen item into the DC.

~.rhavin;)

Jeff Schwab

10/22/2008 7:41:00 PM

0

Mosfet wrote:
> Hi,
>
> I am currently writing a graphical control(a list control) that can
> display a picture followed by some text and to do so I first calculate
> an item height and then I really draw.
> I was wondering if it would be a good idea to merge the two
> methods(calculating and drawing) knowing that when calculating we
> actually simulate a drawing.
> Basically my control is called the first time to calculate the height of
> an Item (MeasureItem) and then to draw (DrawItem).
> I was thinking of something like that :
>
> // Method to handle OwnerDraw
> void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
> {
> DrawItem(lpDIS, TRUE);
> }
> void DrawItem(LPDRAWITEMSTRUCT lpDIS)
> {
> DrawItem(lpDIS, FALSE);
> }
>
>
> // custom method that calulate or really draw
> void DrawItem(LPDRAWITEMSTRUCT lpDIS, BOOL bOnlyCalculate)
> {
>
> }
>
>
> Now you need to know that the difference between drawing text or
> calculating its height relies on an flag (DT_CALCRECT) passed to a
> function DrawText:
>
> DrawText(hDC,lpString, nCount, &Rect, uFormat)
>
> if uFormat & DT_CALCRECT we calculate otherwise it means we draw.
>
> So I was wondering if it would be possible to write a template that
> could take a pointer to DrawText function and that would add DT_CALCRECT
> to the argument when calculating :
>
> DrawTextFunc<false>(....) would mean we call function to calculate
> DrawTextFunc<true>(....) would mean we really draw
>
> About drawing bitmap we could do something similar
>
> DrawBitmap<false>() would mean we don't display bitmap - so should call
> an empty function
> DrawBitmap<true>() really draw the bitmap
>
>
> Could someone tell me how to write something like my DrawTextFunc and
> DrawBitmap ?
>

You have a nice idea, but rather than parameterizing the function with a
boolean value, consider parameterizing with a "policy" type that
translates DrawBitmap's actions into either drawing or calculation. For
example, something like:

/* Accumulates height of drawing operations. */
struct MeasuringContext {
// ...
void verticalLine(Point const& base, std::size_t length) {
updateMaxHeight(base, length);
}
};

/* Forwards drawing operations to graphical device. */
struct DrawingContext {
// ...
void verticalLine(Point const& base, std::size_t length) {
drawVerticalLine(base, length);
}
};

template<typename Context>
void DrawBitmap(Context context) {
// ...
context.verticalLine(base, length);
// ...
}

SkyEyes

10/5/2013 8:00:00 AM

0

Jeanne Douglas <hlwdjsd2@NOSPAMgmail.com> wrote in news:hlwdjsd2-
B2A158.16460404102013@news.giganews.com:

> In article <zoydnStDfbz7ndLPnZ2dnUVZ_gadnZ2d@giganews.com>,
> John Manning <jrobertm@terra.com.br> wrote:
>
>> WATCH: http://youtu.be/J...
>
> Gawd, how I adore that woman. She is my hero.

She is a force of nature, isn't she?

--
Brenda Nelson, A.A.#34 and A+ atheist
BAAWA Knight of the Golden Litterbox
EAC Professor of Feline Thermometrics and Cat-Herding
skyeyes nine at cox dot net OR
skyeyes nine at yahoo dot com



Jeanne Douglas

10/5/2013 9:16:00 AM

0

In article <XnsA250A3D49CFskyeyes9coxnet@78.46.70.116>,
SkyEyes <skyeyes9@cox.net> wrote:

> Jeanne Douglas <hlwdjsd2@NOSPAMgmail.com> wrote in news:hlwdjsd2-
> B2A158.16460404102013@news.giganews.com:
>
> > In article <zoydnStDfbz7ndLPnZ2dnUVZ_gadnZ2d@giganews.com>,
> > John Manning <jrobertm@terra.com.br> wrote:
> >
> >> WATCH: http://youtu.be/J...
> >
> > Gawd, how I adore that woman. She is my hero.
>
> She is a force of nature, isn't she?

Yep.

I'd pick her to head the Fed, except we still need her in the Senate,
speaking out for all us regular people.

--

JD

"Osama Bin Laden is dead and GM is alive."--VP Joseph Biden