Template encapsulating function call

M

Mosfet

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
 
R

.rhavin grobert

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;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top