Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INTRODUCTION AND RELEVANT INFORMATION:

I have a complex painting to implement in my main window’s WM_PAINT handler.

I have submitted a picture bellow to illustrate it:

http://pbrd.co/18fNYYp[^]

Main window has static controls, instead of buttons, which have style SS_NOTIFY.

When user clicks on them, certain actions occur in program.

The following picture shows where static controls in the main window are:

http://pbrd.co/18fOkya[^]

Map on the orange panel is an EMF file ,top left and right logos are PNG files, and other pictures are bitmaps.

In order to implement this task, I have decided to draw the entire picture in WM_PAINT, and to put invisible static controls over the images on the picture that correspond them.

Therefore, I only return NULL_BRUSH in WM_CTLCOLORSTATIC handler like this:

C++
case WM_CTLCOLORSTATIC:
    return (LRESULT)( (HBRUSH)GetStockObject(NULL_BRUSH) );


I work on Windows XP, using MS Visual Studio C++ 2008 Express Edition and pure Win32 API.

One note: since Express edition of VS doesn't have resource editor, resource file and resource header were created using ResEdit from here:

http://www.resedit.net/[^]

UPDATE ( UPDATED on December, 5th 2013 ) :

I have added SWP_NOCOPYBITS flag to SetWindowPos call, in my WM_SIZE handler, and performance has greatly improved.

The remaining problems I am facing now are described bellow in the section PROBLEM, subsection UPDATE ( UPDATED on December, 5th 2013 ) :

PROBLEM:

When I resize my window, static controls slightly flicker.

UPDATE ( UPDATED on December, 5th 2013 ) :

Although performance is greatly improved with the changes I have made ( described above ), flickering occurs when I resize window from left to right, and it also happens when I resize it from top to bottom.

If I resize from right to left/ bottom to top the following effects occur, respectively:

right/bottom edge seems to "lag behind" in redrawing, comparing to the other parts of the window.

This effect happens even if I remove static controls.

This is the only thing left for me to solve.

MY EFFORTS TO SOLVE THE PROBLEM:

I have handled WM_ERASEBKGND (returned (LRESULT)1), and I have excluded styles CS_VREDRAW and CS_HREDRAW from my window class-therefore flickering should not be caused because of this.

My window doesn’t have WS_CLIPCHILDREN style, since parts of desktop background are shown where static controls are, after I turn on this style.

I have implemented double buffering for both handlers, in order to avoid flickering.

I have used the tool GDIView, downloaded from here:

http://www.nirsoft.net/utils/gdi_handles.html[^]

to track down GDI leaks.

Each time I resize my window, GDIView shows +4 in column for regions, which means that I leak regions.

I can’t figure out how is this possible, since I do not use API’s that manipulate with regions.

To illustrate exactly what I am facing with, I have made a demo application , with thorough comments:

http://www.filedropper.com/geotermistgrafika[^]

I believe that this is more efficient way, then posting code since it will consume too much space.

QUESTION:

How can I modify code in demo project to get rid of flickering?

Is my approach wrong, and if it is, what is the right one?

Thank you. Regards.
Posted
Updated 5-Dec-13 0:34am
v2

Flicker can be in part caused by the Windows message WM_ERASEBKGND which you can handle to do nothing, as explained here: http://stackoverflow.com/questions/2473799/gdi-double-buffering-in-c[^] (see answer 3).

A widely used "brute-force" way of removing flicker is the use of optimized double buffering. You accumulate all intermediate changes in buffer and than show all buffer at once. A simple "manual" implementation is shown in answer 2 on the page referenced above.

See also: http://www.robertelder.ca/doublebuffering/[^].

—SA
 
Share this answer
 
Comments
AlwaysLearningNewStuff 4-Dec-13 13:13pm    
Thank you Mr.Kryukov, for submitting your solution.
After carefully looking into content of the links you have provided, I must inform you that I have done all of those already at the first place.

Everything you have suggested so far is listed in the section MY EFFORTS TO SOLVE THE PROBLEM

That is why I have submitted a demo application.

I believe that it is the only way for solving this problem, to have someone experienced to look what is done there and try to offer some advice.

I would highly appreciate if you could take a short look, if that is not a problem for you, since I highly respect your professional opinion.

I have tried to document/comment everything, but if there are additional questions, as always, I will promptly answer on any of those.

Best regards.
Sergey Alexandrovich Kryukov 4-Dec-13 13:42pm    
Yes, I see, sorry for the redundancy in my answer, probably you are right...
—SA
AlwaysLearningNewStuff 4-Dec-13 13:50pm    
No need to apologize, I have assumed that you have given the question a "quick look".

Again, if you could help me with this, I would highly appreciate it, since I am out of ideas at the moment.

I believe that my code is good, but believing and knowing are two different things, that is why I have uploaded demo project and asked for the community to take a look and confirm the validity of my code, or to point out the mistakes.

Also, "flicker-like effect" does not describe my problem in the most accurate way, but seeing the issue "in action" can be more efficient, in my humble opinion.

Thank you again for swift reply.

Regards.
Sergey Alexandrovich Kryukov 4-Dec-13 13:58pm    
I cannot promise right now; it would need much more time... I can bookmark it in hope to find some time later.
—SA
AlwaysLearningNewStuff 4-Dec-13 14:03pm    
That is exactly what I had in mind.

Holidays are coming, people have responsibilities, and jobs, and I understand it.

I am one of those people too :)

If you could find some time to take a quick look I would be very grateful.

Hopefully we shall "speak" soon :)

Best regards :)
The problem was in SetWindowPos API.

I am quoting the developer who helped me to solve this problem:

" When the main window is resized the WM_SIZE handler moves the static child windows.

When a child window is moved, Windows automatically copies the client area of the child control from the old location to the new location.

The main window will then repaint itself in the WM_PAINT handler.

In other words, when the window is resized all the static controls move to their new position and then the rest of the window is repainted. This lack of synchronization appears as flicker.

You can avoid this by passing the SWP_NOCOPYBITS flag to SetWindowPos "

As for the remaining problems about flickering, I think that they deserve a new question, since this one was about static controls flickering.

Thanks everyone who tried to help, and I hereby thank Mr. Kryukov for providing the links that made me realize the problem didn't lie in my implementation of double buffering nor in my handling of WM_ERASEBKGND.
 
Share this answer
 
Comments
enhzflep 5-Dec-13 22:30pm    
Ahhhhhhhhh! Now there's some information worth it's weight in bitcoins. Thanks, bookmarked.
AlwaysLearningNewStuff 6-Dec-13 6:25am    
I agree!

I thought that this might be the problem at the first place, but discarded it after "careful" thinking ( next time I will first try, and then think! ) .

Luckily, there was someone "on the other side of the wire" to point me in the right direction :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900