Link to home
Start Free TrialLog in
Avatar of PMH4514
PMH4514

asked on

Which is more appropriate and why?

In my never ending quest to better understand this language, just a simple question on which of the two snippets of code is "more appropriate" and why..
The class is derived from CSliderCtrl, just a customized UI widget and we're talking about OnPaint()

void CProgressSlider::OnPaint()
{
    PAINTSTRUCT lpPaint;

    CDC* dc = BeginPaint(&lpPaint);

    DrawProgress(dc);      // custom draws UI element.
    EndPaint(&lpPaint);
}

or


void CProgressSlider::OnPaint()
{
    PAINTSTRUCT lpPaint;

    BeginPaint(&lpPaint);      

    CWindowDC dc(this);

    DrawProgress(&dc);

    EndPaint(&lpPaint);
}

both work just fine, is there any reason one is "better" than the other?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1001466
Member_2_1001466

Otherwise I see no obvious difference. In the second example you you have one additional function call. Depending on the number of paint messages this could effect execution speed (but I doubt that you will recognize).
The only reason to use method 2 I could see is when you need to draw outside the client area. But since it will be clipped to the client area in OnPaint you won't see any effect. You need to override OnNcPaint to draw in the non client area.
Avatar of PMH4514

ASKER

I like your first comment, even less code.. I didn't realize BeginPaint and EndPaint were called by the constructor/destructor of dc.

thanks!
Not of every DC. This is only true for a CPaintDC but for example not for a CWindowDC. Latter only calls GetDC on construction and ReleaseDC on destruction but will not reset the invalid area which BeginPaint does. So keep in mind that only a CPaintDC is calling BeginPaint and EndPaint !!!!!!