Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WM_PAINT and WM_ERASEBKGND

WM_PAINT and WM_ERASEBKGND

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nishad S
    wrote on last edited by
    #1

    Why WM_ERASEBKGND, since all the paintings can be done with the WM_PAINT itself?

    - NS -

    S 1 Reply Last reply
    0
    • N Nishad S

      Why WM_ERASEBKGND, since all the paintings can be done with the WM_PAINT itself?

      - NS -

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      In general the WM_PAINT handler will not "touch" every pixel: it might just write some text in the corner for example. Still the entire client area need to be updated if its dirty. Thus the two stage painting.

      Steve

      N 1 Reply Last reply
      0
      • S Stephen Hewitt

        In general the WM_PAINT handler will not "touch" every pixel: it might just write some text in the corner for example. Still the entire client area need to be updated if its dirty. Thus the two stage painting.

        Steve

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        OK... that i know. But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary? And i could find out that if a WM_ERASEBKGND message is sent, WM_PAINT will be surely sent after that to update the whole area. So i think there is no need to process WM_ERASEBKGND to draw anything.

        - NS -

        S M 2 Replies Last reply
        0
        • N Nishad S

          OK... that i know. But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary? And i could find out that if a WM_ERASEBKGND message is sent, WM_PAINT will be surely sent after that to update the whole area. So i think there is no need to process WM_ERASEBKGND to draw anything.

          - NS -

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          NS17 wrote:

          But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary

          It depends. If your WM_PAINT handler paints every dirty pixel then you don't need WM_ERASEBKGND. If, as is generally the case, the WM_PAINT handler only touches a subset of the dirty pixels then without the WM_ERASEBKGND handler the dirty pixels not painted will still be dirty. It depends on your application. You can handle the WM_ERASEBKGND message and return 0 to stop the erasing if you want to.

          Steve

          N C 2 Replies Last reply
          0
          • S Stephen Hewitt

            NS17 wrote:

            But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary

            It depends. If your WM_PAINT handler paints every dirty pixel then you don't need WM_ERASEBKGND. If, as is generally the case, the WM_PAINT handler only touches a subset of the dirty pixels then without the WM_ERASEBKGND handler the dirty pixels not painted will still be dirty. It depends on your application. You can handle the WM_ERASEBKGND message and return 0 to stop the erasing if you want to.

            Steve

            N Offline
            N Offline
            Nishad S
            wrote on last edited by
            #5

            Stephen Hewitt wrote:

            as is generally the case, the WM_PAINT handler only touches a subset of the dirty pixels then without the WM_ERASEBKGND handler the dirty pixels not painted will still be dirty

            Could you please explain a little more?

            - NS -

            W 1 Reply Last reply
            0
            • N Nishad S

              Stephen Hewitt wrote:

              as is generally the case, the WM_PAINT handler only touches a subset of the dirty pixels then without the WM_ERASEBKGND handler the dirty pixels not painted will still be dirty

              Could you please explain a little more?

              - NS -

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              The WM_PAINT does not always paint the whole of the DC, it is passed an 'update region' which is a series of 'dirty' rectangles. Lets say you drag a button around on your window. The rectangle for the new position is passed to WM_PAINT, without the WM_ERASEBKGND The rectangle of the old position would not be filled in, causing a trail effect. The button will only paint within it's own bounds ( the first rectangle ) since it knows nothing of lies beneath it. In most cases WM_ERASEBKGND is not required, but if there is anythig on your main window that changes appearance ( uncovering pixels beneath it ) then this message will fill in those pixels giving WM_PAINT a fresh canvas. There are a few class styles that also effect the painting of a window, CS_VREDRAW, CS_HREDRAW, CS_CLIPCHILDREN, CS_CLIPSIBLINGS to name a few...

              N 1 Reply Last reply
              0
              • W Waldermort

                The WM_PAINT does not always paint the whole of the DC, it is passed an 'update region' which is a series of 'dirty' rectangles. Lets say you drag a button around on your window. The rectangle for the new position is passed to WM_PAINT, without the WM_ERASEBKGND The rectangle of the old position would not be filled in, causing a trail effect. The button will only paint within it's own bounds ( the first rectangle ) since it knows nothing of lies beneath it. In most cases WM_ERASEBKGND is not required, but if there is anythig on your main window that changes appearance ( uncovering pixels beneath it ) then this message will fill in those pixels giving WM_PAINT a fresh canvas. There are a few class styles that also effect the painting of a window, CS_VREDRAW, CS_HREDRAW, CS_CLIPCHILDREN, CS_CLIPSIBLINGS to name a few...

                N Offline
                N Offline
                Nishad S
                wrote on last edited by
                #7

                WalderMort wrote:

                The rectangle for the new position is passed to WM_PAINT, without the WM_ERASEBKGND

                ...but from Spy++ tool I found that WM_ERASEBKGND is also coming while dragging a message box over a window (I used notepad).

                - NS -

                W 1 Reply Last reply
                0
                • N Nishad S

                  WalderMort wrote:

                  The rectangle for the new position is passed to WM_PAINT, without the WM_ERASEBKGND

                  ...but from Spy++ tool I found that WM_ERASEBKGND is also coming while dragging a message box over a window (I used notepad).

                  - NS -

                  W Offline
                  W Offline
                  Waldermort
                  wrote on last edited by
                  #8

                  Just like with my example using your window and a button, the desktop is itself a window, when you move a window over yours, those pixels are erased, the desktop is telling your window that it is now uncovered and should fill in those uncovered areas.

                  1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    NS17 wrote:

                    But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary

                    It depends. If your WM_PAINT handler paints every dirty pixel then you don't need WM_ERASEBKGND. If, as is generally the case, the WM_PAINT handler only touches a subset of the dirty pixels then without the WM_ERASEBKGND handler the dirty pixels not painted will still be dirty. It depends on your application. You can handle the WM_ERASEBKGND message and return 0 to stop the erasing if you want to.

                    Steve

                    C Offline
                    C Offline
                    code_discuss
                    wrote on last edited by
                    #9

                    Can anyone show a condition when WM_PAINT is sent but WM_ERASEBKGND is not? or when WM_ERASEBKGND is sent but WM_PAINT is not?

                    M J 2 Replies Last reply
                    0
                    • C code_discuss

                      Can anyone show a condition when WM_PAINT is sent but WM_ERASEBKGND is not? or when WM_ERASEBKGND is sent but WM_PAINT is not?

                      M Offline
                      M Offline
                      Member 1619255
                      wrote on last edited by
                      #10

                      If you use InvalidateRect(...,..., false) then only WM_PAINT is sent. If you use InvalidateRect(...,..., true) then WM_PAINT is called and when you use 'BeginPaint' inside WM_PAINT it calls WM_ERASEBKGND (when WM_ERASEBKGND is called, the Invalid rectangle is nullified). In a normal circumstance the Invalidate rectangles received in WM_ERASEBKGND and WM_PAINT are same.

                      1 Reply Last reply
                      0
                      • N Nishad S

                        OK... that i know. But my doubt is, since WM_PAINT will update enough area in the window, is WM_ERASEBKGND necessary? And i could find out that if a WM_ERASEBKGND message is sent, WM_PAINT will be surely sent after that to update the whole area. So i think there is no need to process WM_ERASEBKGND to draw anything.

                        - NS -

                        M Offline
                        M Offline
                        Member 1619255
                        wrote on last edited by
                        #11

                        Whether it is background or foreground you draw on same DC. This is only an option given to you to separate background and foreground. The usefulness comes when 'InvalidateRect' etc. are called in the program. Normally, WM_ERASEBKGND and WM_PAINT both get called ('resize' say). If you use InvalidateRect(...,...,false) only WM_PAINT gets called. If you use InvalidateRect(...,...,true) then WM_ERASEBKGND gets called when you use 'BeginPaint' inside WM_PAINT.

                        N 1 Reply Last reply
                        0
                        • M Member 1619255

                          Whether it is background or foreground you draw on same DC. This is only an option given to you to separate background and foreground. The usefulness comes when 'InvalidateRect' etc. are called in the program. Normally, WM_ERASEBKGND and WM_PAINT both get called ('resize' say). If you use InvalidateRect(...,...,false) only WM_PAINT gets called. If you use InvalidateRect(...,...,true) then WM_ERASEBKGND gets called when you use 'BeginPaint' inside WM_PAINT.

                          N Offline
                          N Offline
                          Nishad S
                          wrote on last edited by
                          #12

                          Thanks! But why a reply now, after years? (just due to curiosity) :)

                          - ns ami -

                          1 Reply Last reply
                          0
                          • C code_discuss

                            Can anyone show a condition when WM_PAINT is sent but WM_ERASEBKGND is not? or when WM_ERASEBKGND is sent but WM_PAINT is not?

                            J Offline
                            J Offline
                            Javad Taheri drjackool
                            wrote on last edited by
                            #13

                            Hi I think this is depend on how the window be update. When you call Invalidate(...), you can specify the erase background occuer or no. I think when you want to update only an item in the window like a button in the toolbar NOT all window area, WM_ERASEBKGND is not needed because not change made in other places of the window. Kind Regards. mrjavadtaheri@gmail.com

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups