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


your window is standing without any clothes in front of so many people ? Close it quickly. You now have a problem of providing your window with clothes. We shall learn, how to provide it with clothes, a little later. In the mean time, check your ' z.txt '. You'll see that the function ' zzz ' is now called more often that ever before. The ' z.txt ' file now looks as under

start

start1

3816..36..0..6618072

3816..129..0..6618108

3816..131..0..6618140

3816..1..0..6618108

:: :: :: ::

:: :: :: ::

:: :: :: ::

Clothing the window...

Let us proceed with the clothing of our window. Let’s see what we can achieve by adding some more lines of code in our program.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
MSG c;
FILE *fp;

_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   a.hbrBackground = GetStockObject(BLACK_BRUSH);
   a.hCursor = LoadCursor(0,IDC_CROSS);
   fp = fopen("c:\\a.txt","w");
   fprintf(fp,"start\n");
   RegisterClass(&a);
   fprintf(fp,"start1\n");
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)
{
   fprintf(fp,"%u..%u..%u..%ld\n",w,x,y,z);

   if (x = = WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}

After you build and run the program, what do you see? You will see two things. One, that you now have a window which is completely clothed - though it appears to be in mourning as it is wearing dark black clothes - and the other, that the moment you bring the cursor inside the window, it changes to a cross ' + '. These changes are not appearing due to some black magic on our part. They are appearing because of the additional lines of code.

Let us understand what these lines of codes mean. When you compiled this program you did not get any errors. This is because ' hbrBackground ' and ' hCursor ' are also members of our structure ' a '. The first member ' hbrBackground ' is used to store the background colour of the window. To get the background colour, you have to use a function ' GetStockObject() '. This function is passed a parameter, which is a Macro. This Macro - has already been assigned the number of a colour in the header file - knows which colour is to be displayed, which in our case is black. Now you can make some changes to this parameter using ' WHITE_BRUSH ' or ' GRAY_BRUSH '. You will see a nice white coloured window if you use ' WHITE_BRUSH ' and an old grayish looking window if you use the ' GRAY_BRUSH '. Similarly you are free to find out any other different colour in the header file as part of your R & D. Likewise in ' hCursor ' you are storing the value of the cursor which you get using the function ' LoadCursor() ' . This Macro is also pre-defined with the number in the header file <Windows.h>.

You have now seen the window on your screen, but have you bothered to look at your output in the ' z.txt ' file? If you have taken the pain to do so, you will see that the output there looks more or less as below.

start

start1

604..36..0..6618072

604..129..0..6618108

604..131..0..6618140

604..1..0..6618108

:: :: :: ::

:: :: :: ::

:: :: :: ::

We know that you are too bored to keep going to the DOS prompt and seeing the output in the ' z.txt ' file. So let us now remove the ' FILE ' macro, and all the ' fprintf() ' functions from our program and execute it, retaining only the following codes.



#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);
   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_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



Bet you did not notice it while writing the program. The colour of the background has been changed by us in this program to a peace loving white. This has been done by changing the value of ' a.hbrBackground ' from 'BLACK_BRUSH ' to 'WHITE_BRUSH'. Now when you execute this program, you’ll see that we still get our window - albeit one with a white background. If you get any errors, it means that either the Gods are angry with you or that you have not followed our coding correctly. Please recheck the code carefully and execute the program. It should not give you any errors and on execution should display the window to you.

Changing the size of our window during runtime...

Let us now change our program a little.

#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);
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,SW_SHOWMAXIMIZED);
   while (GetMessage(&c,0,0,0))
      DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{

   if (x = = WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



The only change we made to our program is that we have changed the second parameter of ‘ ShowWindow() ’ from ' 1 ' to ' SW_SHOWMAXIMIZED '. What does this do ? If you execute this program you will see that our window now occupies the full screen. When you pass ' SW_SHOWMAXIMIZED ' as the second parameter to the ‘ ShowWindow() ’ function, the fourth, fifth, sixth and seventh parameter of the ‘ CreateWindow() ’ will lose their meaning. Even if we change them all to ' 0 ' the window will still be displayed over the whole screen. Similarly you can try with ' SW_SHOWMINIMIZED ' wherein your window will be in the minimised form when you execute the program no matter what you pass as the fourth, fifth, sixth and seventh parameters to ShowWindow. Then change this parameter to ' SW_SHOWNORMAL' . You will see that it works the same way as it did when you used ' 1 '. You can even try to change the second parameter of ‘ ShowWindow() ’ to 2 and 3. You will see that when you use ' 2 ', it works in a manner similar to ' SW_SHOWMINIMIZED ', and when you use ' 3 ' it works similar to ' SW_SHOWMAXIMIZED '. You can change to only these three numbers to have any meaning to your program, you try to change the parameter to any other positive number you will see that it works as if it takes ' 1 ' as the parameter.

Trapping the mouse click in our window...

Now let us tinker around with one of the parameter of the function ' zzz ' and see what happens.

#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);
   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_LBUTTONDOWN)
   {
      MessageBox(0,"Hi","Hi",0);
   }

   if (x = = WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



Whenever we say ' CreateWindow() ' or ' ShowWindow() ', the function ' zzz() ' gets called. Likewise, whenever Microsoft Windows wants to tell us something important, it calls the ' zzz ' function and conveys any information through it. It is high time that you learnt that ' x ', which is the second parameter of the function ' zzz ', is used to tell us as to why Windows is calling the ' zzz() ' function. You don't have to wonder what WM_LBUTTONDOWN is. It is nothing but a Macro. So now whenever you click with the mouse on the window, Microsoft puts four parameters on the stack. The second parameter is the number WM_LBUTTONDOWN. It is always easier to remember WM_LBUTTONDOWN than to remember it’s number. So in our case, what the window has been told, is that the left mouse button has been clicked and held down. The moment you click on the window, the function ' zzz ' is called and it puts the four parameters of this function on the stack. The second parameter i.e. ' x ' is now passed the number of ' WM_LBUTTONDOWN '. This is easily understood because we have trapped the mousebutton’s message, and the moment we click on our window, the MessageBox gets called. So now whenever you click with the left mouse button, you will see a MessageBox displayed on the window with the message "Hi" in it.

Now for your R & D you can change WM_LBUTTONDOWN to WM_LBUTTONUP or use WM_RBUTTONDOWN or change it to WM_RBUTTONUP. With the first change you will see that the MessageBox is seen only after you release the mousebutton. With the second change you would see that now there is no MessageBox if you click with your left mouse button but you can see the MessageBox if you click with the right button. With the last change you will see the MessageBox being displayed only after you release the right mousebutton.

Now let’s mess around with the parameters some more. We add the following lines of code to our program which now looks as follows.

#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);
   RegisterClass(&a);
b=CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
   ShowWindow(b,SW_SHOWMAXIMIZED);
   while (GetMessage(&c,0,0,0))
      DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{


Page : << Previous 5  Next >>