Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Why one shouldn't use BeginPaint unless in response to WM_PAINT?

Is it because BeginPaint clears the background (and validates the region so no painting will be done before a new invalid region occurs)? What if you InvalidateRect after it?

Thanks for any explanation.

Comments

  • pingpongpingpong Member Posts: 937
    This should answer your other question as well:

    BeginPaint: returns an HDC with the invalid region setup for you by Windows.
    GetDC: returns an HDC.

    That's the only different between the 2. Basically, this is what happens:

    - WM_PAINT handler:
    - You call BeginPaint
    - Windows returns an HDC for you, setup with te invalid clip region
    - you draw your stuff
    - you call EndPaint
    - Windows marks this window as valid

    So basically, what you are saying is true. I "think" you can do this:
    - WM_PAINT handler:
    - You call GetDC
    - Windows returns an HDC for you
    - you draw your stuff
    - you call ReleaseDC
    - You call ValidateRect (not InvalidateRect) to mark this Window as valid.

    But remember, you lose the optimization of the only drawing on the invalid region.

    Infact, theres a flag you can use (CS_OWNDC in WNDCLASSEX.style) which basically tells the OS to use only one HDC for this window. Meaning, there's no difference between GetDC and BeginPaint, always return the same HDC. This is helpful if you have a full-screen application for instance.

    : Is it because BeginPaint clears the background (and validates the region so no painting will be done before a new invalid region occurs)? What if you InvalidateRect after it?
    :
    : Thanks for any explanation.
    :

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    I NEVER use Begin/EndPaint. You need to control your HRC and HDC constantly in game programing, and the way OpenGL works is that it grabs the HRC and HDC when the window is created and it won't release it until you're done. This is how high-frames are made possible. Imagine trying to get a DC/RC, check for invalidated bs, draw, then release them both. It'd KILL frames! In fact, the ONLY time I remember using Begin/EndPaint was when I was first learning Windoze C/C++. Even on my normal apps that run windowed, I just use GetDC if I even need to.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • Justin BibJustin Bib USAMember Posts: 0

    ______ { http://forcoder.org } free video tutorials and ebooks about | Visual Basic, PL/SQL, C, Perl, PHP, MATLAB, Swift, C#, Python, Objective-C, C++, Ruby, Delphi, Assembly, R, Java, Go, JavaScript, Scratch, Visual Basic .NET Lua, Kotlin, F#, Rust, Logo, Erlang, SAS, Hack, Alice, Dart, ML, Transact-SQL, Bash, Scheme, Awk, D, Prolog, Julia, ABAP, VBScript, Ada, Scala, Lisp, Clojure, Apex, Crystal, COBOL, LabVIEW, Fortran, FoxPro | __________

Sign In or Register to comment.