In visual c++, what is the difference between these three:

WindowClass.style = CS_HREDRAW | CS_VREDRAW; //Found in winmain()

UpdateWindow(hWnd); // Also found in winmain()

WM_PAINT //It is a switch case in funtion winproc()

I unerstand that all three of them does something like to draw/redraw the client window/window. But can someone elaborate abit about these three in simple and their difference in simple terms.

Thank you.

Recommended Answers

All 7 Replies

WindowClass.style = CS_HREDRAW | CS_VREDRAW

This causes the system to call request a repaint of the window when the width of the window is changed (CS_HREDRAW) or the height of the window is changed (CS_VREDRAW).

UpdateWindow()

This causes the WM_PAINT message to be sent to the specified window (requests a repaint).

WM_PAINT

This is a macro (kind of like a constant number) that signifies that the system is requesting a paint of the window.

These three items you mention refer to three distinct stages in the creation of a window using the low level Windows Api (the best way to program Windows in my opinion).

Before you can create a window you fill out all the fields of a WNDCLASSEX structure. This...

WindowClass.style = CS_HREDRAW | CS_VREDRAW;

pertains to the setting of one of the many fields (10 or so). It tells the operating system that when a window of the class szClassName is instantiated, and any change occurs to the window, the whole window should be redrawn (Windows will send a WM_PAINT message).

Note that the filling out of all the fields of a WNDCLASSEX structure is done before any windows of the class specified by the sz.ClassName field can be created/instantiated. In that sence, it represents the 1st step.

Once all the fields of the WNDCLASS striucture are filled out, one uses the RegisterClassEx() Api function to register the class with windows. the .szClassName field is of critical importance here. Let me digress for a moment.

Lets say I want to create an edit control (text box) with somewhat different characteristics than the standard/stock text box included with Windows. If I want to do that I create my own Window Class and fill out a WNDCLASSEX structure and specify the name of my new class/control through the .szClassName field. Lets say I call mine "MyEdit" . This would distinguish my edit control class from Windows own "edit" control class. Naturally, I'd have to fill in all the other fields too. And I'd have to RegisterClassEx() it too. At that point Windows would know about the general characteristics of a class known as "MyEdit".

At that point I could use the CreateWindowEx() function to create an actual instance of my "MyEdit" class with the specific characteristics I provide in the various parameters of the CreateWindowEx() call. For example, I might want my specific instance of the "MyEdit" class to be located at 75,90, in terms of x,y coordinates on the desktop, and I might want it to be a window 200 pixels wide and 225 pixels deep.

After the CreateWindowEx() call Windows will know what function to call when it wishes to provide information it has gleaned about your window because in filling out the fields of the WNDCLASSEX structure you would have provided Windows with the address of the Window Procedure to call in the lpWndProc field. If, immediately after your CreateWindowEx() call, you place a ShowWindow() call with the SW_SHOWNORMAL flag set, the window will probably become visible on your screen even without an UpdateWindow() call. However, the UpdateWindow() call forces a WM_PAINT message to be sent to the window's WndProc() function, and that message should cause a repainting of the window.

Hey! I'm not as good at explaining this as Charles Petzold! If you wish to persist at low level Api coding for Windows you absolutely need to get Petzold's Windows 95 or 98 book. They can be cheaply gotten used for a buck or two.

Having said that I do have tutorials on this subject at Jose Roca's forum for PowerBASIC programmers set up at...

http://www.jose.it-berater.org/smfforum/index.php?board=285.0

PowerBASIC might be something you would be interested in. I use different languages, but with Windows I invariably use the Windows Api. I commend you for attempting this. Stick with it.

Alright, so the first sends a request for the windows to be repainted whenever the width/height is edited by the user.

So, when this is there then what is the use of the second one(i.e update() ).

Aren't these both doing doing the samething? (i.e sending reapaint request whenever width/height is changing)

Alright, so the first sends a request for the windows to be repainted whenever the width/height is edited by the user.

No, you've got the timing entirely wrong. Filling out the .style field of a WNDCLASSEX structure only occurs one time and that happens long before (in computer miliseconds) a window is created or becomes visible.

In contrast, an UpdateWindow() call can be made countless times during the execution of a running program. You need to grasp the concept of update regions possibly to fully understand this. A window can accumulate invalid regions without Windows automatically repainting it. To force an immediate WM_PAINT UpdateWindow can be used.

As I mentioned, you need to study a real solid three dimensional book with weight & mass to grasp all this.

Sort of, the first one simply tells the system to request a repaint given those conditions. The UpdateWindow(hWnd); function on the other hand, allows you to request an update when you need one. If you make some changes to the window, where you don't change the size, then you will need to request a repaint yourself.

Hmmm...... Slightly confusing but I got the idea.

@Fredrick2 : I'm currently learning VC++ from Ivon Horton's Beginning Visual C++ 2008. I think this book is good enough but sometime the explanation for some stuffs are too brief. Thats why I got this doubt.

Anyways, thanks alot guys.

Sort of, the first one simply tells the system to request a repaint given those conditions. The UpdateWindow(hWnd); function on the other hand, allows you to request an update when you need one. If you make some changes to the window, where you don't change the size, then you will need to request a repaint yourself.

Exactly. In some types of scroll code, for example, when the user clicks the scroll bar up or down arrows, you may wish to entirely repaint the window to change what is displayed. A typical way of doing that would be to use the InvalidateRect() function followed by the UpdateWindow() function to force a WM_PAINT message. In the WM_PAINT processing different lines of text or whatever would be drawn.

I have Ivor Horton's VC++ 6 (around 2000 or so) "Beginning C++ 6" book. In that edition anyway there was one chapter devoted to Sdk style Windows programming. The rest of the book was all MFC.

If your only interest is to become acquainted with Sdk style coding for Windows then quickly move on to MFC or .NET (various class frameworks), then Ivor's book would be enough. However, if you wish to really delve deeper into programming Windows with the raw Api, then you need to acquire Charles Petzold's "Programming Windows" book.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.