Clearing the window api


I'm starting learning win32 api programming.

I working on developing pages, but the problem is that when I get back to the main page, the window isn't clear and there're stuff from the previous page.

How to clear the window if I want to wipe the current window and print new stuff ?
Other than you beginning to learn WinAPI programming what you are experiencing is not at all clear. Explaining with more detail, including what your compiler and Win OS version are, would be helpful in pointing you in the right direction.

With that said, there is a decent if very old tutorial that explains the basics of programming for the Desktop WinAPI, formerly known as Win32, theForger's Win32 API Tutorial.

Fair warning, the tutorial was written for Windows 9X using pure C, modern Windows versions might get a bit cranky when trying to compile those sources. Especially true if compiling the code as C++. The Desktop WinAPI has been reworked to deal with modern Windows on x86 and x64 processors.

I have updated those WinAPI lessons to work with modern Windows (Win10) and Visual Studio 2022 as my compiler and (shameless self-promotion plug) plopped the code into a github repo:

There are other, IMO less detailed, tutorials available if you search the internet:

If you are serious about wanting to learn Desktop WinAPI programming consider getting a copy of Charles Petzold's book, Programming Windows - Fifth Edition. It was published in 1998 so is targeted for Win9X/NT.
My system is windows 10 64-bit. Working on Code::Blocks.
My compiler is the default GCC C compiler.

In my project I'm working on the buttons, I've added tool bar menus, and window controls. Everything is working ok.

My only issue is that when I print, let say for example:
A main page contain 4 buttons, each button reprint a new page:
If I pressed button1, then it reprint page1 which contain 5 buttons. When I get back to main page, the 5th button of page1 is still not erased.
Last edited on
win32 MFC sometimes needs a call to invalidate or invalidaterect to ensure the current window is drawn correctly after a change. I am not sure if this is what you are asking, but look at those 2 commands to see if maybe it is.
You don't show the code, but are you using GetUpdateRect() with the bErase param set to true?
When it comes to what's been drawn in an app's client area and whether it needs to be painted again when part or all of the client area is obscured changed starting with Vista. Desktop composition, run by the Windows Desktop Manager (DWM)

Note carefully: Win apps no longer write/draw directly to the screen or primary display device as done with pre-Vista apps, with the DWM any output is written/drawn to offscreen surfaces that are used to create the visible desktop image.

IOWs, what is written to an app's DC is maintained by the DWM and if the device context (DC) needs to be redrawn it is composited from the stored offscreen data.

Most WinAPI examples/samples/ documentation were created before Windows Vista, so presume data is written directly to an app's DC.

The DWM from Vista to Win 7 could be programmatically turned on and off by an app. Since Win 8 DWM is always on. That means DWM is always enabled with Win 10.

Now when an app needs to change what is drawn to the screen needs to invalidate the DC to force a repaint.

That includes any windowed controls in the client area, a WinAPI button is a control.

Ever hover your pointer over the taskbar and any apps that are displayed there? You will see a live preview of the app even when it is minimized or obscured by other window(s). That is one feature of the DWM.

With all that said without seeing your code it is nigh um-possible to definitively diagnose what the problem might be. A Short, Self Contained, Correct (Compilable), Example would be most helpful.

Sharing your code online as a git repository would let us run the code through our compiler and see what the problem(s) might be, and then provide possible solutions. GitHub is one service, and it can be used for free.

I have several public GitHub repos you can look at, C/C++/WinAPI (shameless self-promotion):
Registered users can post here. Sign in or register to post.