Author : Vijay Mukhi
Page : << Previous 8 Next >>
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)
{
ii = 1;
h = GetDC(w);
MoveToEx(h,LOWORD(z),HIWORD(z),0);
}
if (x = = WM_LBUTTONUP)
{
ii = 0;
ReleaseDC(w,h);
}
if (x = = WM_MOUSEMOVE)
{
if( ii = = 1)
LineTo(h,LOWORD(z),HIWORD(z));
}
if (x = = WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}
In this program we are using three of the mouse functions viz. the WM_LBUTTONDOWN, WM_LBUTTONUP and the WM_MOUSEMOVE. By now you should be familiar with these three mouse button functions. Now when you click with the left mouse button and keep it pressed the first ' if ' statement will get called. In this ' if ' statement we are merely initialising the active mouse pixel to that position where we have clicked with the mouse button. When you move the mouse - keeping the left mouse button held down - you will now see that it keeps drawing a line to the previous pixel i.e. the active pixel keeps changing. Now with moving the mouse on your window you’ll see that even if you move it fast there will be no blank trails in the mouse move. The code for this happening is passed in the WM_MOUSEMOVE's ' if ' statement. The mouse will stop drawing on the screen the moment you release the mouse button. This is because of the code you have passed in the WM_LBUTTONUP's ' if ' statement that the moment you release the mouse button the value of ' ii ' changes back to ' 0 ' and hence ' WM_MOUSEMOVE ' does not get called.
Isn't this program superior to the previous program - in the sense that you can now draw faster with your mouse?
Callback function...
Now let us learn something which is different to these mouse moves. Let us learn what a callback function is ? In a program, there is a function that is used ( by the Microsoft Windows in our case ) to let us know of any events that are taking place in our window. This function keeps getting called repeatedly and hence it is called as the callback function. In our program the function ' zzz ' gets called every time the windows wants to tell us whatever is happening in it. Hence, this is our callback function.
Let us put some different code in our program 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,2);
while(GetMessage(&c,0,0,0))
DispatchMessage(&c);
}
long _stdcall zzz(UINT w, UINT x, UINT y, long z)
{
if (x = = WM_QUERYOPEN)
{
MessageBox(0,"Hi","Hi",0);
return 0;
}
if (x = = WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}
In our previous programs, after execution, whenever we had minimized and maximized the window it used to get maximized without giving us any problems. We had never understood then - nor cared - which function was getting called, when our window was being maximized.
On executing our program, the window is displayed in the minimized state. Now click on it to maximize it. You’ll now see that it does not get maximized, in fact try clicking on it as many number of times as you want, it will stubbornly refuse to get maximized. Now Windows whenever it is asked to maximizes a window it asks a question. He asks whether we want him to maximize the window for us, yes or no? At this point in time, it asks us the question by sending us a WM_QUERYOPEN Message. If you return a ' 0 ' in response to this question, it means that you are telling Windows not to maximize our window. Previously in all our other programs, we had never tried to trap this message anywhere. So it turned to ‘ DefWindowProc() ‘. Now ‘ DefWindowProc() ‘ by default always returns 1 and hence the window used to get maximized. But in this program we have trapped this message and returned ' 0 ' to it , which means that we are now telling Windows not to maximize our application. Hence now find yourself unable to maximize your application. To now remove this program from memory you have to either press ' Alt-F4 ', or go to the status bar - to the position where our window has been minimized - and click with the right mouse button and close the window. There is also another way to remove this window from memory which will be explained later.
Creating a MenuBar...
Enough of these commands, now let us try to create a MenuBar. By now you should be able to write the shortest program in ' C ' under Windows without any problem. So let’s create a new project workspace and open a text file. We have created a project ' aa ' in our root directory along with a File of the type ' TextFile ' called as ' aa.c ' . We advise you to do the same as it will be easier to understand. Let’s write that program we have learnt and insert one more line to the code 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);
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_DESTROY)
PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}
Compile and execute this program to see what happens. On execution there is nothing additional to our window. Now the question is, what new thing have we learnt in this program? Well, at a glance, we have at least shown you one more member of the structure ' a ' i.e. our structure which looks like ' WNDCLASS '. It is this member of the structure, which stores the menu name.
Now we have to understand that a menu has nothing to do with your program. All that we can now do is, in a separate file i.e. a rc file, we can writing code which will display a menu for us. This file should be saved as an ' .rc ' file. For accomplishing this task we can go to the DOS prompt and create a new file in our directory ‘ aa ’ having the name ' aa.rc '. In this file we type in the following details
AAA MENU
BEGIN
MENUITEM "A1",100
MENUITEM "A2",101
END
What do these lines of code mean? Here we are passing the codes which specify as to what we want to do with the MenuBar. These codes will be explained, but before that why don't we go back to our program and insert this file into our project? To insert this file into our project follow the following steps:
Select Project from your menu bar
Choose Add to projects
Choose files
Here you will see a dialog box, where you should click on aa.rc and select " insert ".
Now that your insertion is complete you can build the program once again. On execution of the program you can see a MenuBar with " A1 " and " A2 " on your window.
Let us explain what has happened. Whenever you have to pass some codes of our MenuBar - which are not part of the actual creation of the window - you have to pass them in a separate file. This file is called a resource file and it is to be saved as a ' .rc ' file. By the insertion of this resource file in our project, the compiler on compilation reads it’s contents. Now seeing that the first line of the resource file is the name of our MenuBar it promptly displays the MenuBar on our window screen.
While we are in the topic of resource files let us also understand as to what code we have passed to our MenuBar. The first line shows the name as "AAA" and that it is our Menu name. Now any code we have to pass in the resource file has to start with a " begin " statement. The compiler now knows that we have started with our coding part and reads it accordingly. In the third line we have a "menuitem" which is "A1", this has been given a number viz. ' 100 ' - in future whenever we call our command button and specify ' 100 ' it knows which menu item is to be called - we can understand the use of ' 100 ' better in another program. In the meantime let’s continue with our understanding of the other lines of the file. The fourth line is similar to the third line with the exception that the menuitem's name and number is different. Don't you think that it is always good to end whatever you have begun? Well the last line ends the code you had started when you said begin. The statements in the resource files are not case sensitive, except for the numbers which should be in the lower case.
Creating a popup...
Let us now learn how to create a popup. To create a popup you just have to change the resousce file
Page : << Previous 8 Next >>