some problem, beginnger of windows programming

0. what does SelectObject mean? I saw in MSDN :
The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.
, I am wondering what's the new object and what's the previous, and what's the specified DC. Why save the old DC, and then delete something? (is it replace oldBM with bmHDC, and delete bmHDC ?)
[output]hdc = BeginPaint(hWnd, &ps); 

bmHDC = CreateCompatibleDC(hdc); //Create a system memory device context. 

oldBM = (HBITMAP)SelectObject(bmHDC, ghBitMap); //Hook up the bitmap to the bmHDC. 
BitBlt(//......omit); //copy the pixels from the bitmap bmHDC

SelectObject(bmHDC, oldBM); //Select the originally loaded bitmap. 

DeleteDC(bmHDC); //Delete the system memory device context. 

1. I saw in my book initializing a BITMAP object, what does the {} mean? all member zero ?
[output]BITMAP bitmap = {0};

2. in WinMain function, we usually put this after the CreateWindow function, what does the return false mean? shouldn't we return int?
if(ghMainWnd == 0) {
	MessageBox(0, "CreateWindow -- Failed", 0, 0);
	return false;

3. in WinProc function, we use switch(msg), and in each case, we return 0, why don't use break? any difference?
case WM_PAINT:
    return 0; //why not default?

4.what does InvalidateRect function mean? I mean, invalidate, isn't cancel something? how can it become update region?

5.about prefix:
what the EX in the WNDCLASSEX and MoveToEx() mean, is it mean the same thing?
I want to know where can I found a document contains the naming notation explanation, it can really help understand the code.
Last edited on
You can think of a device context as a basket with all the stuff you need to write. It has the font, the actual device to write to, ..., everything you need.

SelectObject() is how you say "I want that device". You get that new one and it gives you a handle to the one it had.

what does the return false mean? shouldn't we return int?

The numeric value of false is an int, or at least can be promoted to one.
That logic you show is based on the idea that if the CreateWindow() call for the main program window fails, there isn't much sense in doing anything beyond exiting WinMain().

In terms of your question about the way the switch is structured in the Window Procedure, never, ever try what you suggest. It is critical to the internal workings of Windows that the correct return value for each message processed be used. If you intend to handle some specific message such as WM_CREATE, WM_PAINT, etc., then you must look up that message in the MSDN documentation, where the return value to Windows will be specified. I guarantee you will come to grief if you do not do this correctly, as the code that Windows runs internally to support your windows is depandant on what you return from each message.

The way the Window Procedure has to be set up, is that if you handle a specific message, i.e., provide code for it, you must return the value specified for that message. In most cases, although not all, the return is zero. Another way of saying this is that after your code runs for the specific message you are handling, you must exit the Window Procedure. The reason why a 'return' is used rather than a 'break' is that a 'return' causes an immediate exit, while a 'break' does not.

If you have no case clause specified for a particular message, then that message must be passed on to the default Window Procedure within Windows.

There are actually at leat two ways of accomplishing this logic, and you should become familiar with both constructs. This is actually a critical area of Windows Programming to understand.

Realize with SelectObject() that there are memory allocations involved with just about any graphics object you or Windows creates, and these memory allocations will be in your process memory. If you do not use SelectObject() and DeleteObject() properly, you'll have memory leaks in your process. For long running programs such memory leaks can crash your program.
very thanks,
, you take me out of the puzzle.
also thanks
, I don't know what to say but thanks.
Topic archived. No new replies allowed.