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



   if (x = = WM_LBUTTONDOWN)
   {
      char aa[100];
      int x1,y1;
      x1 = LOWORD(z);
      y1 = HIWORD(z);
      sprintf(aa,"%d..%d..",x1,y1);
      MessageBox(0,aa,aa,0);
   }

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



Here, we have created a character array ' aa ' of size 100. We have also defined two ints x1 and y1. Now let us remember that a long is four bytes in length. Can't we treat a long as if it were made up of two ints, of two bytes each. Well that is exactly what we have done (remember that the last parameter of our function ' zzz ' is a long with a variable 'z'). The first two bytes of the variable ' z ' are read into x1, while the second two bytes are read into y1. Let us now understand what is stored in ' z '. The moment we click with our mousebutton in our window, the function ' zzz ' is called, and as usual, it puts the four parameters on the stack. The last parameter ' z ' stores the value of the ' x ' and ' y ' co-ordinates of our window in it. Thus when we store the first two bytes of ' z ' in ' x1 ' we get the ' x ' co-ordinates of our window. The next two bytes store the y co-ordinate of our window. That is the job of LOWORD(z) and HIWORD(z).

Now whenever you click with the mousebutton on the window the ' x ' and ' y ' co-ordinates of that position is displayed in the MessageBox.

Printing text on screen...

Don't you feel bored looking at MessageBoxes all the time? You do! Well now that we have got the hang of how the ' C ' programming under Windows works, we shall try to do something in our window. In our next program we shall try to learn how to display some text on our window. So let’s roll ahead and learn how it is done.

#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)
   {
      TextOut( LOWORD(z),HIWORD(z), "Hello",4);
   }

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


Now in this program, we have used another function of ' C ' Windows programming, viz. ' TextOut() '. We ask this function to print ' hello ' at the position the mouse is clicked on the window. For this we have passed the parameters LOWORD(z) and HIWORD(z) to show the ' x ' and ' y ' co-ordinates of the window, as the first two parameters to our ‘ TextOut() ’ function.We have also passed to this function, the string which we want to display on the screen, as the third parameter. The last parameter however is explained in the next program.

But when you compile this program the compiler is not satisfied with the number of parameters you have passed. It gives you an error ,which states, " 'TextOutA' : too few actual parameters ". This shows that we have passed the function a parameter too less.

Now let us understand that, whenever we want to display text on the screen of our window, we have to specify the font size, the colour, etc. Now how do we do this? Well whenever you want to specify these things, you have to use a Device Context called ' GetDC '. You have to pass this Device Context a parameter, which should be the number of our window. Where should we search for this window number? Don't worry, this window number is stored in the first parameter of our ' zzz ' function. This variable has to be passed as the parameter to the ' GetDC() ' function. You can check and see that both ' w ' and ' b ' - the variable which we used to create our window - will have the same value. Simply writing 'GetDC(w)' will not solve your problem. This function returns an int which is stored in a variable and passed as the first parameter to the function ' TextOut '. For this purpose in our next program we have created a variable ' h ' which looks like a Macro ' HDC '. This Macro has been predefined as an unsigned int in our header file <windows.h>.

We don't know how it is with you, but with us, we have a rule that, whenever we borrow something from others, we should always return it back. This rule of ours applies in this program also. It is always good programming sense to return the variable, which we use, back to our function ‘ zzz() ’. We have used the variables ' w ' and ' h ' in our ' GetDC ' Device Context, so it is only proper that we return this Device Context back. To return the variables, on exiting from ‘ GetDC() ’ back to the function ’ zzz() ’, we have to use the function ' ReleaseDC() '. This function takes the window handle and Device Context - which we return - as it’s parameters.

So now we have changed our program which looks as under.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
HDC h;
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)
   {
      h = GetDC(w);
      TextOut(h,LOWORD(z),HIWORD(z), "Hello",4);
      ReleaseDC(w,h);
   }

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


Now on building this program, you get no errors. Now, when you click on the window you now see a "Hello" at the position where you have clicked on the window. What ? You are not getting a "Hello"? What do you get? You get " Hell ". Well you may have put " Hell " as your string. No? You have put " Hello ". Well it may be because of one of the two reasons. One being that you have really been going through "Hell" in trying to understand ' C ' Windows programming. Or, it may be because you have passed ' 4 ' as the fourth parameter of our TextOut function which specifies the number of characters of the string which has to be displayed on the screen. Try putting ' 5 ' as the parameter and run it. You will now see " Hello " on your window.

Changing the text colour...

Let us make one small change to our program

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
HDC h;
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)
   {
      h = GetDC(w);
      SetTextColor(h,RGB(255,0,0));
      TextOut(h,LOWORD(z),HIWORD(z), "Hello",4);
      ReleaseDC(w,h);
   }

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


We were not satisfied with getting the text on our screen in black. So with the ' SetTextColor() ' function, we are passing the colour we want the text to be printed in. Please do not try to correct our incorrect spelling of the word colour in the program (We know the spelling of colour but the ' C ' program was designed by an American and all Americans spell colour as ' c.o.l.o.r ', so that is how you have to spell it if you want your program to work). This function asks for two parameters. The first parameter is a Device Context, so we pass it ' h ' which is assigned the value of our 'GetDC' Device Context. The second parameter is a function which specifies the colour we want.

Now we shall explain what 'RGB()' function means. Windows knows only the three basic colours red, green and blue. So when you use 'RGB()' you have to specify as to how much of each colour you want. You can choose from a minimum of ' 0 ' to a maximum of ' 255 '. These requirements of yours are specified as the three parameters to this ' RGB() ' function. They are then mixed together, to form the colour which you want, and displays the text in that colour as your output. In our case we have chosen the maximum value we can assign to ' red ', so do not be surprised and scream for help if you see "Hell" in blood red. You can choose some other value to specify the colour you want and check out the result - This can be a part of your R&D.

Learning to write with the mouse...

Have you ever had the urge to write on your window with a mouse. If you have the urge, then the next program will help you to write on screen with your mouse.

#include <windows.h>
#include<stdio.h>
WNDCLASS a;
long _stdcall zzz();
HWND b;
HDC h;
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


Page : << Previous 6  Next >>