Topic : C Windows Programming
Author : Vijay Mukhi
Page : << Previous 9  Next >>
Go to page :


in the following manner.

AAA MENU
BEGIN
POPUP "ABC"
BEGIN
MENUITEM "A1",100
MENUITEM "A2",101
END
END
When you say " popup " the compiler knows that you want to create a popup menubar. Now inside a " begin " statement we have another " begin " statement in which you display the menu items you want to see in your popup. Since we have two " begin " statements in our resource file we also should also have two " end " statement in it.

Individual selection of menuitems...

Let us learn how to select a menuitem from our MenuBar.


#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x = = WM_COMMAND)
   {
      MessageBox(0,"Hi","Hello",0);
   }
   
   if (x = = WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}

The ' WM_COMMAND ' which is used here, captures the selection of the menuitems. But here we have not specified, as to which menuitem when clicked, is to display the MessageBox. So, on clicking any of the two menuitems, you see the same MessageBox being displayed.

Importance of the numbers passed to our menuitems...

So far in our previous programs, you have learnt what three, of the four parameters, of our callback function mean viz. ' w ' which is our window number, ' x ' which tells us as to why our window is being called and ' z ' which tells us what is the position of the mouse on our screen. Let us now learn what the other parameter ' y ' does. The parameter ' y ' is number which tells us the selection made in our window. Let us now use this parameter to capture the menuitem we want to select from our MenuBar. Here we shall also learn the importance of the number which we had passed as a value in our resource file.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x = = WM_COMMAND)
   {
      if(y = =100)
      {
         MessageBox(0,"Hi","Hi",0);
      }
      else
      if(y = =101)
      {
         MessageBox(0,"Bye","Bye",0);
      }
   }
   
   if (x = = WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



In this program the additional code which you have passed in ' WM_COMMAND ' specifies the menuitem you have selected. Remember, we had given ' 100 ' as the number to ' A1 ' and ' 101 ' to ' A2 ', hence on selection of ' A1 ' we get a different MessageBox as compared to the MessageBox we get when we select 'A2 '.

Now that you have learnt about MenuBars, menuitems and popups why don't you try to create a MenuBar with two or more popups and different menuitems as part of your R & D.

Capturing the System's Menuitem...

Now that you have learnt of how to capture the menuitems selected from your popup, we shall learn how to capture the menuitems of the System popup.

To do this you have to first learn where the System menu exists. Now if you were an observant person, you would have seen that when your window is created, on the topmost left hand corner of your window there is a picture. This picture is provided by Microsoft Windows, to our window - the one which we create in our program - free of charge. When you click with the mouse on this picture, that you will see your System popup menu displayed.

So let’s learn how to trap these command.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x == WM_SYSCOMMAND)
   {
      MessageBox(0,"Hi","Hi",0);
   }
   
   if (x == WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



Now on execution of our program, whenever you click on any of the System’s menu item you will see the MessageBox being displayed. You will also see that when you click anywhere on the System MenuBar - it being in someway or the other effecting your System popup - your MessageBox being displayed.

Let’s make a small change in our program and see what it does.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x == WM_SYSCOMMAND && y == SC_CLOSE)
   {
      MessageBox(0,"Hi","Hi",0);
      return 1;
   }
   
   if (x == WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}

There are two changes in our program. We have extended the ' if ' statement by anding it with the ' y ' parameter to trap the close button of the System menu popup. We have also returned a value to our ' if ' condition. Execute the program and try to close it. What do you see? Don't you see your MessageBox ? Well, go ahead and close your window. Go ahead, we dare you to close the window using any of the limited commands at your disposal. Well you can't ? Now, now don't get upset. Just like in our normal menu we had given the numbers ' 100 ' , ' 101 ' to our menuitems, similarly the Windows menu has also given our application’s menuitems the numbers ' SC_CLOSE ', 'SC_MOVE ', ' SC_SIZE', etc. In our program we have trapped the ' SC_CLOSE ' number in our ' if ' statement, so now you will see the MessageBox being displayed only when you click on close or you click on the ‘ x ‘ on your menu bar or you use "Alt+F4". You click on any other menuitem, you will not see the MessageBox. Keep in mind that your parameter ' x ' i.e. which tells us when the callback function is called, is ' WM_SYSCOMMAND '. To get out of your window you have to press " Ctrl + Alt + Del " only once. You will get a dialog box. select " Bye " from it and say " End Task " to come out of the program. If you press " Ctrl + Alt + Del " twice you will be rebooting your machine.

Let us learn why you could not close the window. Normally when you close the window, your 'if ' condition does not return a value. So whenever you click on ' x ' on the Menubar or you press 'Alt + F4 ' or say close on the System Menu, you return the values used - in the callback function - in the ' DefWindowProc() ', and after it receives all the values, ‘ DefWindowProc() ‘ start it's shutdown process and closes your window. Now when you return a value in the ' if ' statement, you say that the ‘ DefWindowProc() ‘ is not to be called and since it does not get called, the values of ' x ' and ' y ' - which in our case is "WM_SYSCOMMAND' and 'SC_CLOSE' respectively - does not reach it. And since the values of ' x ' and ' y ' do not reach it, the ‘ DefWindowProc() ‘ does not start the shutdown procedure of our window and it can't close. You now have to call the anytime troubleshooter " Ctrl + Alt + Del " button to shutdown our window.

Now let’s remove the return statement, and put in some other code.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x == WM_SYSCOMMAND && y == SC_CLOSE)
   {
      MessageBox(0,"Hi","Hi",0);
   }
   if(x == WM_CLOSE)
   {
      MessageBox(0,"Close","Close",0);
   }
   if (x == WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}

After you compile and run this program you now see two MessageBoxes. The first with the message "Hi" in it and the second with the message "close" in it. This shows that when you close the window i.e. when you click on " close " in the System menu or you press " Alt + F4 " or when you click on th 'x' on the System's MenuBar, ‘ DefWindowProc() ‘ receives the values of ' x ' and ' y ' and sends your window a WM_CLOSE message.You will however encounter the same problem you had in the previous program if you return a value to it i.e. you will be unable to close your window.

Now let’s add just two more lines of code to our program.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(WHITE_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   a.lpszMenuName = "AAA";
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,1);
   while(GetMessage(&c,0,0,0))
   DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
   if(x == WM_SYSCOMMAND && y == SC_CLOSE)
   {
      MessageBox(0,"Hi","Hi",0);
   }
   if(x == WM_CLOSE)
   {
      MessageBox(0,"Close","Close",0);
   }
   if (x == WM_DESTROY)
   {
      MessageBox(0,"in Wm_Destroy", "in Wm_Destroy",0);
      PostQuitMessage(0);
   }
   return DefWindowProc(w,x,y,z);

Run the program, and close the window. What do you find ? You find that, you are now getting three MessageBoxes. This shows that when you close the window even the WM_DESTROY gets called as part of the shutdown

Page : << Previous 9  Next >>