Solved

Which is more appropriate and why?

Posted on 2004-09-02
5
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 60 total points
ID: 11963344
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
ID: 11963372
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
ID: 11963444
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
ID: 11963737
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
ID: 11963779
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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