Выбрать главу

Step 4: the Window Procedure

If the message loop is the heart of the program, the window procedure is the brain. This is where all the messages that are sent to our window get processed.

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

 switch (msg) {

 case WM_CLOSE:

  DestroyWindow(hwnd);

  break;

 case WM_DESTROY:

  PostQuitMessage(0);

  break;

 default:

  return DefWindowProc(hwnd, msg, wParam, lParam);

 }

 return 0;

}

The window procedure is called for each message, the HWND parameter is the handle of your window, the one that the message applies to. This is important since you might have two or more windows of the same class and they will use the same window procedure (WndProc()). The difference is that the parameter hwnd will be different depending on which window it is. For example when we get the WM_CLOSE message we destroy the window. Since we use the window handle that we received as the first paramter, any other windows will not be affected, only the one that the message was intended for.

WM_CLOSE is sent when the user presses the Close Button

or types Alt-F4. This will cause the window to be destroyed by default, but I like to handle it explicitly, since this is the perfect spot to do cleanup checks, or ask the user to save files etc. before exiting the program.

When we call DestroyWindow() the system sends the WM_DESTROY message to the window getting destroyed, in this case it's our window, and then destroys any remaining child windows before finally removing our window from the system. Since this is the only window in our program, we are all done and we want the program to exit, so we call PostQuitMessage(). This posts the WM_QUIT message to the message loop. We never receive this message, because it causes GetMessage() to return FALSE , and as you'll see in our message loop code, when that happens we stop processing messages and return the final result code, the wParam of WM_QUIT which happens to be the value we passed into PostQuitMessage(). The return value is only really useful if your program is designed to be called by another program and you want to return a specific value.

Step 5: There is no Step 5

Phew. Well that's it! If I haven't explained stuff clearly enough yet, just hang in there and hopefully things will become more clear as we get into more usefull programs.

Handling Messages

Example: window_click

Alright, we've got a window, but it doesn't do anything except what DefWindowProc() allows it to, like be sized, maximised, etc… Not really all that exciting.

In the next section I am going to show you how to modify what you already have to do somehting new. This way I can just tell you "Handle this message, and do this in it…" and you will know what I mean and be able to do so without seeing an entire example. That's the hope anyway, so pay attention :P

Okay for starters take the example code for the last window we worked on and make sure it compiles and runs as expected. Then you can either keep working on it for the next little bit or copy it to a new project to modify.

We're going to add the capability to show the user what the name of our program is when they click on our window. Not very exciting, it's basically to get the hang of handling messages. Lets look at what we have in our WndProc():

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

 switch(msg) {

 case WM_CLOSE:

  DestroyWindow(hwnd);

  break;

 case WM_DESTROY:

  PostQuitMessage(0);

  break;

 default:

  return DefWindowProc(hwnd, msg, wParam, lParam);

 }

 return 0;

}

If we want to handle mouse clicks, we need to add a WM_LBUTTONDOWN handler (or WM_RBUTTONDOWN, WM_MBUTTONDOWN , for right and middle clicks respectively).

If I or someone else refers to handling a message they mean to add it into the WndProc() of your window class as follows:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

 switch(msg) {

 case WM_LBUTTONDOWN:    // <-

                         // <-     we just added this stuff

  break;                 // <-

 case WM_CLOSE:

  DestroyWindow(hwnd);

  break;

 case WM_DESTROY:

  PostQuitMessage(0);

  break;

 default:

  return DefWindowProc(hwnd, msg, wParam, lParam);

 }

 return 0;

}

The order in which you handle your messages rarely matters. Just make sure you've got your break; after each one. As you can see we added another case into our switch() . Now we want something to happen when we get to this part of our program.

First I will present the code we want to add (that will show the user the filename of our program) and then I will integrate it into our program. Later on I will probably just show you the code and let you integrate it into your program. This is of course better for me as I don't have to type as much and it's better for you because you will be able to add the code into ANY program and not just the ones I present. If you aren't sure how to do it, look at the example zip file included with the section.

GetModuleFileName(hInstance, szFileName, MAX_PATH);

MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);

Now this code does not stand on it's own, it can't just be slapped into our code any old place. We specifically want it to run when the user clicks the mouse button so this is how I would merge this small bit of code into our skeleton program:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

 switch(msg) {

 case WM_LBUTTONDOWN:

// BEGIN NEW CODE

  {

   char szFileName[MAX_PATH];

   HINSTANCE hInstance = GetModuleHandle(NULL);

   GetModuleFileName(hInstance, szFileName, MAX_PATH);

   MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);

  }

// END NEW CODE

  break;

 case WM_CLOSE: