[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Which is more appropriate and why?

Posted on 2004-09-02
5
Medium Priority
?
217 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 240 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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