Solved

Which is more appropriate and why?

Posted on 2004-09-02
5
210 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.

830 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