Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Win32 Tutorial Part 2d- Creating Window


  • Please log in to reply
1 reply to this topic

#1 freiza


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 23 February 2012 - 08:02 AM

Step 4) -->

	case WM_DESTROY: PostQuitMessage(0);return 0;
	default: return DefWindowProc(hwnd,uMsg,wParam,lParam);
	return 0;

Now its time to handle messages and add functionality to our window.
But little theory first.
After retrieving message through GetMessage function. the message is send via DispatchMessage function to WindowProc.
In WindowProc function we receive top 4 arguments of message structure.

Here uMsg will contain the message number.

for example: if left mouse button is pressed in our window then WM_LBUTTONDOWN message is generated.

WM_LBUTTONDOWN is just a plain number. And it is defined as

#define WM_LBUTTONDOWN 0x0201

there are many other messages some important message are:

WM_DESTROY - Sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed after the window is removed from the screen.
WM_CLOSE - Sent as a signal that a window or an application should terminate. WM_CLOSE indicates a request to close a window, such as by clicking on the close button.
WM_LBUTTONDBLCLK - when mouse left mouse button is double clicked.
WM_RBUTTONDOWN -When right mouse button is clicked.
WM_RBUTTONUP - When right mouse button is released

You can check more message constants in msdn.

Since there are hundreds of message that can happen to our little program. But we are interested in dealing only one of them "WM_DESTROY"

Therefore, We have used switch statement. As we know the message number is stored in uMsg. Now we will compare it with WM_DESTROY since we only want
to handle this message

case WM_DESTROY: PostQuitMessage(0);

Q1) What is PostQuitMessage ?
Q2) Since we are dealing with only one message what will happen to other messages?

Ans1) If you remember we had put GetMessage function inside a while loop. And while loop will iterate till GetMessage function returns false.
GetMessage returns false when we call PostQuitMessage() function.
PostQuitMessage() sends WM_QUIT message to GetMessage function forcing it to return false. hence while loop terminates. and our program ends
Hence PostQuitMessage() is just a function whenever called it sends WM_QUIT message to GetMessage function forcing it to return false to while loop.
WM_QUIT message is a constant when received by GetMessage function it will return false.

Ans2) Since we cannot tackle each and every messages created by our window. Our operating system comes into the scene.
The messages which are not interesting to us. We just pass them back to operating system. And operating system take care of that messages for us.
for ex:- We have not tackled minimize messages. So if somehow we pass minimize message to operating system. It will perform default operations. Here It will minimize the window.

Q)so how do we do this?
Ans) we simply return DefWindowProc(hwnd,uMsg,wParam,lParam) with the same parameters that we got from DispatchMessage. It is a default window procedure to handle all the unhandled messages.

		case WM_DESTROY: PostQuitMessage(0);
                             return DefWindowProc(hwnd,uMsg,wParam,lParam);  // this function handles all the ignored messages.


Translate function just translates virtual-key messages into character messages

I think Part 2 is finished.
  • 0

#2 ZZTopZZ


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 06 April 2012 - 04:36 AM

gr8 job. Is there part 3?
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download