473,413 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Template encapsulating function call

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




Oct 22 '08 #1
2 1597
On 22 Okt., 19:07, Mosfet <mos...@anonymous.orgwrote:
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;)

Oct 22 '08 #2
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);
// ...
}
Oct 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
13
by: jsnX | last post by:
say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool> { ... } and then i decide to feed it a bunch of pointers to...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
1
by: amparikh | last post by:
I have something like this. typedef enum TYPES{ X =0, Y, Z, MAX}; template <typename T> class A { public:
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: rn5a | last post by:
Please have a look at the image at the URL given below: http://rn5a.brinkster.net/Template.gif I would like to use that image as a template for all the ASP pages in a website. Note that though...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.