Solved

Which is more appropriate and why?

Posted on 2004-09-02
5
207 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:PMH4514
  • 4
5 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 60 total points
Comment Utility
If you want to change version 1 you could use (since you don't use the information provided in the PAINTSTRUCT:

void CProgressSlider::OnPaint()
{
    CPaintDC dc(this); // BeginPaint is called in constructor of dc

    DrawProgress(&dc);
}                             // EndPaint is called in destructor of dc
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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).
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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.
0
 

Author Comment

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

thanks!
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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 !!!!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 98
bigDiff challenge 17 75
twoTwo  challenge 35 86
zeroMAx challenge 20 76
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

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

18 Experts available now in Live!

Get 1:1 Help Now