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


get no errors because if you look at the structure ' WNDCLASS ', in the header file, you will see that ' hInstance ' is a member of this structure. Now since our structure ' a ' looks just like ‘ WNDCLASS’, ‘ a ’ also inherit’s these variables and can now call them as if they were it's own. Now what is the data type of this member of ' a '. It is obvious that it has to be an ' int ', otherwise we would have had a type mismatch error.

We add one more line of code to our program.

#include<windows.h>
WNDCLASS a;
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
   a.hInstance = i ;
   a. lpszClassName = "Hi" ;
}


When you build the program, you still do not get any errors. Do not get exited. There are no errors because 'a.lpszClassName' is also a member of ' WNDCLASS '. So now you know a second members of our structure ' a '. This member of our structure is a string and we have initialised it to "Hi". Now why did we pass this string as " Hi "? Well it's like this, whenever you meet a good friend on the road, how do you greet him? Obviously you say "Hi". Similarly, we thought that when we walk down the streets of Windows programming, and happen to see our window, it should greet us in a polite manner . You may not want your window to greet you politely, but we always like our windows to be polite. Even if this is the only virtue of our window that we may be able to boast about. Hence we have named this member as " Hi ".

Putting an additional line of code, we get the following program.

#include<windows.h>

WNDCLASS a;
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)

{
   a.hInstance = i ;
   a. lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz ;
}


Now you are in for a disappointment .On building the program you'll encounter an error. It is a compilation error which says " 'zzz' : undeclared identifier ". This is because ' zzz ' has not been defined by us. The error is not due to ' a.lpfnWndProc ' - otherwise it would have told you "' a.lpfnWndProc ' : undeclared identifier ". In our program we have initialised the third member of our structure ' a ' viz. ' a.lpfnWndProc '. It has to point to the starting position of the function we shall define in the next program.

Now let us define the function zzz in our program


#include<windows.h>
WNDCLASS a;
long _stdcall zzz();
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
   a.hInstance = i ;
   a. lpszClassName = "Hi" ;
   a.lpfnWndProc = zzz ;
}
long _stdcall zzz(UINT w,UINT x, UINT y, long z)
{
}



We now have a function prototype ' zzz ' in our program. This function follows the ' Standard Calling Convention ' and returns a ' long '. Now, whenever ' C ' encounters ' zzz ' it knows that it is the name of a function . But what does the name of a function tell us? It only tells us where the function starts in memory . If you had not defined the function, where would ' C ' search for it ? So we have to write the code of the function in our program. Hence we have added another function ' zzz ' to our program. This function is passed four parameters. The first three parameters are Macros which are of the type ' unsigned ints ', while the fourth parameter is a ' long '.

Now when you build this program you should not get any error. Even after writing so many lines of code in your program, on execution you see that it does nothing. This, to you, may seem very strange. But in actuality, what have we done? We have merely initialised some variables and declared a function. We have not asked our program to do anything. So why should it do anything for us?

We shall add some lines of code - which we had earlier learnt in ' C ' under DOS - to our program.

#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");
}
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 we have used ' FILE '. This you have already learnt in ' C ' under DOS. Unfortunately, when we use ' FILE ', we also have to include it's header file <stdio.h>. It is like the case when you bring home a pet dog. Well, bringing the pet is O.K. but then you also bring home additional problems like feeding the pet, etc. You can't have a pet without also having the problems that it brings. Similarly, when you use ' FILE ', you have to include it's header file in our program, without which, it will give us an error. We have also added ' fprintf() ' statements in both our functions viz. ' WinMain() ' and ' zzz() '. We have put an ' fprintf() ' statement in ' zzz() ' to store the values of the four parameters we have passed to the function in the ‘ z.txt ’ file.

What the last three lines of code of our function ' zzz() ' does, is not to be understood by you at this stage. It will be explained later. In the mean time, please take our word for it and write it as a part of syntax.

When you build the program you do not get any errors. Before execution however you have to take care that there is no file by the name ' z.txt ' in your root directory, because, on execution of this program your existing file will be completely overwritten. Now on execution of the program you still do not see any output on screen. This is because the output is stored in ' z.txt ' in the root directory. You can see for yourself, the output present in ' z.txt '.

The output in z.txt is as follows:

start

But what is this ? You have only ' start 'as your output. Where are the values of the parameters of our function ' zzz '. Go back to your program and see whether you have forgotten to put the ' fprintf ' statement in your function. You have it there. So what went wrong?

Let us try to understand why this has happened. In the function ‘ zzz() ’ we have used the ' fprintf() ' statement to print it’s parameters. But we have not called the function. This function gets called only after we use a function called CreateWindow. We shall incorporate this function in our next program.

Creation process...

So let us try to create a window in our program.



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

}


Phew! Just look at the number of parameters we have had to pass to our ' CreateWindow() ' function. Now why do we need to have so many parameters in our function, and what does each parameter signify? As to the question of why we need so many parameters, we do not know. You will have to ask the person who designed Windows programming for the reason. But as to what each of the parameters signify, we shall try to explain them. The first parameter you pass is a string. This is the same string which we had given earlier to ' a.lpszClassName ' in our program. The second parameter is also a string, which we have initialised as "Bye". You can give any value you choose, to this string . This string will appear as the title of your window. We would prefer to get rid of you as soon as possible and hence we have used "Bye" as our string. The third parameter is in capitals, hence it can be safely said to be a Macro. This Macro WS_OVERLAPPEDWINDOW, is defined as an ' unsigned int ' in the header file <windows.h>. It is passed a number, which makes no sense to us. Whenever the user wants to minimize or maximize his window, in fact, do anything with it, he should be allowed to do so. This parameter provides Windows System with the required information, to allow the user to do whatever he/she wants with the window.

Now, where do we want this window to appear ? i.e. At which position on the screen? This is specified by the next four parameters of our function ‘ CreateWindow() ‘. The fourth and fifth parameters constitute the' x ' & ' y ' co-ordinates of where our window is to be

Page : << Previous 3  Next >>