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


displayed on the screen. The sixth and seventh parameters specify it’s ' width ' and ' height '. The next two parameters i.e. the eighth and ninth, are ' 0 ' and so it should not be any concern to you. When we close the window, we are asking Microsoft Windows to do something for us. So it is now necessary to tell Microsoft Windows who we are. The tenth parameter is the handle of our window and this gives Microsoft Windows the information it requires. The last parameter is again ' 0 ', so we shall not explain it.

Now when you compile and run the program you'll see that there is no change in the output even though we have added the ‘ CreateWindow() ’ function. Let us try to understand what the problem is?

When we are saying ' CreateWindow ', the poor function, as it is, is already loaded down with so many parameters. And until now you haven't even said what the background colour should be or how you mouse should look like, the moment it enters the window. Now if you include these description as parameters to the 'CreateWindow()', then just imagine how large your function would become. Now you may keep creating windows. Microsoft wanted to make this creation process more effecient. They saw to it that this function did not land up with fifty parameters.

Here, we want to create a window that looks like the class "Hi", which is the first parameter of our function ' CreateWindow()'. But before this can be done, you have to first register the class i.e. "Hi". We register a class by giving it the address of a structure which looks like ' WNDCLASS '. When you register a class, it means that the ' CreateWindow() ' function can access the members which we had initialised earlier.



So far you have seen us speaking of " Hi " as a class, but may be wondering as to why it is called so? Well we have initialised a member ' lpszClassName ' to "Hi" and hence it is called as a class. A class is actually a hypothetical entity and it can't be physically shown. We have also initialised ' lpfnWndProc ' to ‘ zzz ’, so now whenever Windows wants to talk to us, he does not need to speak to ' WinMain() ' he will only talk to our ' zzz() ' function. That is why the function gets called four times. Now whenever Microfoft Windows wants to do something for us it would first need to know who we are and this is shown by the variable ' i '.

Registering a class...

Let us now learn how to register a class.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
FILE *fp;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   fp = fopen("c:\\z.txt","w");
   fprintf(fp,"start\n");
   RegisterClass(&a);
CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
}
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);
}


The ' RegisterClass ' should have R and C in capitals. Why this is so, is not known to us. We have so far never argued with syntax. They want the syntax of a function to be in a particular manner, so be it. We have always obliged them. If you want it to be worded differently, you can create your own version of ' C ' Windows programming language. Once you have created your programming language, please let us know, so that we can use your syntax. Until then, we shall use the function as it is. The ' RegisterClass() ' should be passed the address of our structure i.e.' a ' so that the compiler now knows whose variables are to be registered in memory.

After you compile and execute this program what do you see? Although we have executed the program only once, the ' zzz ' function is called four times. Now if you do not have the same output as us ( with regards to the numbers in our output) , please do not worry. These numbers tend to be different at different times.

The output that we get in ' z.txt ' is as follows:

start

1056..36..0..6618072

1056..129..0..6618108

1056..131..0..6618140

1056..1..0..6618108



Now let us put an ' fprintf ' statement after the RegisterClass and see what happens.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
FILE *fp;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   fp = fopen("c:\\z.txt","w");
   fprintf(fp,"start\n");
   RegisterClass(&a);
   fprintf(fp,"start1\n");
CreateWindow("Hi","Bye",WS_OVERLAPPEDWINDOW,1,100,200,300,0,0,i,0);
}
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);
}


What happens now? Looking at the output, there are two things you notice at once. The first being, that both the ' fprintf() ' functions of WinMain are executed first. This shows that the function ' zzz ' gets called only after the ' CreateWindow () ' function. The second thing you may have noticed is that the values of the parameters have also changed. Now did we not ask you to ignore the value of these parameters, but you would not listen, you will still notice that the values of the parameters are different, ignore them in future. Our output however now looks as under:

start

start1

1592..36..0..6618072

1592..129..0..6618108

1592..131..0..6618140

1592..1..0..6618108

You have still not been able to see a window as your output. For this however, you will have to first create a variable and store the return value of the function ‘CreateWindow()’ in it.

Your program now looks as below

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
FILE *fp;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   fp = fopen("c:\\z.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);
}
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);
}

In this program,the variable ‘ b ’ is of the type ' HWND '. HWND is a macro which is an ' unsigned int '. We have initialised ‘ b ‘ to the return value of the ‘ CreateWindow() ’ function. Now ' b ' stands for the number of the window which we have just created. It is possible to create multiple windows in one application - each having it's own unique identification number, and now whenever Windows sees ' b ', it knows that our window is to be called.

When you compile the program and run it, you will still not be able to see your window. Don't feel frustrated, just hold on to your patience a little while more, we are nearly home. We agree that you do not see the window. This is because when you say ' CreateWindow ' a window is created, but in memory. To see it on the screen you will have to use the function ' ShowWindow() ' .

A glimpse of the window...

Let us put in the line of code in our program to display the window.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
FILE *fp;
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
   a.hInstance = i;
   a.lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz;
   fp = fopen("c:\\z.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);
}
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);
}


Go ahead, you can build and run this program. Bingo! You can now see a window even if it is only for a nano second. Well you should be happy that you have now created your window, even if it is alive only for a nano second. But now and when you look at the output at DOS prompt in ' z.txt ' you will see that the function ' zzz ' has now been called more often. Your output will be something like below.

start

start1

1992..36..0..6618072

1992..129..0..6618108

1992..131..0..6618140

:: :: :: ::

:: :: :: ::

:: :: :: ::

We are forced into not displaying the entire output due to lack of space. Now if you are satisfied with this achievement, we can assume that you are easily satisfied with very little. Now, now, don't get frustrated. Take a dose of the " anti - frustration " tablet we had recommended and get back to work. Now that we have got the hang of creating a window, let’s proceed further and try to keep our window alive until it closes, only when we want it to.

Capturing the window...

Let us put some more code in our program.and see what it does.

#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;
   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);
}


The new lines of code which we have passed i.e. ‘ GetMessage() ’ and ‘ DispatchMessage() ’ shall be explained later. For the time being, just compile and run the program, keeping your faith in us. Executing the program, what do you see? Your faith in us is justified. We did not lead you astray. You can now view your window properly. But what is this? You have a window, but it is without clothes, you can see right through it. Are you not ashamed that

Page : << Previous 4  Next >>