This problem occurs. In onerasebkgnd, if you do not call the default
The onerasebkgnd of is only a re-painting background, and will not flash. In onpaint,
Because it implicitly calls onerasebkgnd, and you have not processed onerasebkgnd
Function, which is related to the default background of the window.
The onerasebkgnd operation uses the Default background of the window to fl the new background (generally
And then you re-draw the background, causing the screen to flash.
Another problem is that onerasebkgnd is not called every time. If you
If the parameter is true when invalidate is called, it is hidden in onpaint.
The wm_erasebkgnd message is generated when beginpaint is called. If the Parameter
If this parameter is set to false, the background will not be refreshed.
So there are three and a half solutions:
1. Use onerasebkgnd instead of calling the original onerasebkgnd function.
2. onpaint is used and onerasebkgnd is reloaded.
3. Use onpaint to set the background to blank when creating a window.
4. Use onpaint, but use invalidate (false) for Refresh.
(However, in this case, the refresh caused by window overwrite still needs to flash.
So it is not a thorough solution)
They are quite simple.
------------------------------------------------------
In MFC, the drawing of any window component is placed in the two member functions.
Onerasebkgnd () is used to draw the basemap, while onpaint () is used to draw the main object.
For example, there is text on the gray button.
What onerasebkgnd () does is to gray the button.
What onpaint () does is to draw text.
Since both member functions are used to draw Components
So why should we separate onpaint () and onerasebkgnd ()?
In fact, the onpaint () and onerasebkgnd () features are different.
1. onerasebkgnd () requires a quick drawing insideProgramIt is best not to spend too much time
Because onerasebkgnd () will be called immediately whenever there is any small change to the window component ()
2. onpaint () is called only when the program is idle.
3. onerasebkgnd () is called before onpaint ()
Therefore, onpaint () may call onerasebkgnd () several times before being called.
If we are a person who is working on graphical user interfaces
We usually need to set a beautiful image as the basemap of our dialog.
Program for drawingCodeSome problems may occur frequently in onpaint ().
For example, drag a window and move it on the dialog.
The dialog changes to gray until the action stops.
This is because the program calls onerasebkgnd () immediately every time it needs to be re-painted ()
Onerasebkgnd () draws the dialog into gray.
The program will call onpaint () only after the action is stopped. Then the basemap we want to draw will be pasted.
The solution to this problem is to rewrite onerasebkgnd () to a function that does not work.
As shown below
[CPP] View plain Copy Print ?
- BoolCmydlg: onerasebkgnd (CDC * PDC)
- {
- ReturnTrue;
- }
Bool cmydlg: onerasebkgnd (CDC * PDC) {return true ;}
The above will call cdialog: onerasebkgnd (), but if we do not call
The program won't have a gray background.
It is better to directly move the plotting program from onpaint () to onerasebkgnd ().
As shown below
[CPP] View plain Copy Print ?
- // M_bmp bkgnd is a cbitmap object and our basemap has been loaded before.
- // The size of the basemap is the same as that of the client.
- BoolCmydlg: onerasebkgnd (CDC * PDC)
- {
- Crect RC;
- Getupdaterect (& rc );
- CDC srcdc;
- Srcdc. createcompatibledc (PDC );
- Srcdc. SelectObject (m_bmp bkgnd );
- PDC-> bitblt (RC. Left, RC. Top, RC. getwidth (),
- RC. getheight (), & srcdc, RC. Left, RC. Top, srccopy );
- ReturnTrue;
- }
// M_bmp bkgnd is a cbitmap object and the size of the basemap has already been loaded. // the size of the basemap is the same as that of the window client. bool cmydlg: onerasebkgnd (CDC * PDC) {crect RC; getupdaterect (& rc); CDC srcdc; srcdc. createcompatibledc (PDC); srcdc. selectObject (m_bmp bkgnd); PDC-> bitblt (RC. left, RC. top, RC. getwidth (), RC. getheight (), & srcdc, RC. left, RC. top, srccopy); Return true ;}
Note that getupdaterect () instead of getclientrect () is used to obtain the redraw size ()
If getclientrect () is used, it will redraw the areas where the image should not be repainted.
Q: What is the difference between the background image in the draw dialog box in onerasebkgnd and the background image in the draw dialog box in onpaint? What is the difference between onerasebkgnd and ctlcolor?
A: onerasebkgnd occurs when the window size changes. It draws the window background, while onctlcolor is used when the window control needs to be drawn, it draws the control of the window. A: onerasebkgnd: called when the window background needs to be re-painted. onpaint: At this time, onerasebkgnd has been called. Therefore, the background operations in the response function will overwrite the operations in onerasebkgnd. onctlcolor: the response is made when the window is to be drawn (for the first time). In the subwindow, you can send an hbrush request to the parent window through wm_ctlcolor.