Showing posts with label Chapter2. Show all posts
Showing posts with label Chapter2. Show all posts

Friday, February 22, 2008

Structure of window program

Structure of window program:

A Window program performs two basic tasks:

• Creation of the program’s window and doing the tasks like setting aside memory space when the program is first loaded.

• Processing messages, which are received, from windows?



Application Creation Stage at API Programming:

We first design the user interface objects like menus, tool bars, status bars etc. This in turn creates resource files.

Then we have to concentrate on the writing the functional code to handle messages, this code is nothing but the source code.

After creating, both of them i.e., resource code and source code, they are builds and then executable file is created.

The general outline of the Window program is as follows:

Create the program’s window
Do any initialisation of variables
LOOP
Fetch messages sent by windows to this program

Is the message QUIT?
If the message = QUIT
{
Terminate the program;
Result control to windows;
}
If the message is another message
{
Do the appropriate actions based on the messages;
Return control to windows;
}
ENDLOOP

Thursday, February 21, 2008

HANDLES In WIN32

Every running program is an instance or copy of the program.

This is also called object of the program. Windows should be capable enough to keep track of number of objects of a particular application in the memory.

So windows keep track of the addresses of different objects in the memory by storing their addresses, which is in the form of an unsigned integer.

This unsigned number, which is used to represent address of an object in the memory is called a HANDLE.

Handle is unique from one object to another. In windows handles refer to all objects.

Handle is pointer to a pointer to a memory location.

There are a number of data types in windows for describing the handles. These data types begin with a H.

Example:
HWND –handle to a window

Internally windows maintain tables that allow window to convert handles to physical addresses.



Example of Win32 API Program to display an message:

#include

int PASCAL WinMain(HANDLE hInstance , HANDLE hPrevInstance,LPSTR lpszCmdLine,int nShowCmd)
{
MessageBox (0,”Application is executing”,”Application”, MB_OK);
return 0;
}

Explanation of the Program:

Windows.h:-

Windows.h is the one of the important header file for window programs.

It contains definitions of hundred’s of window functions, definition of structures, constants, data types and derived data types.

This can be included using # include statement.

WinMain Function:

The function declaration is as follows

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow);

WinMain is similar to main of C and C++.

Every window program must have a WinMain function. When a window program starts, it is the WinMain, which is executed first.

Whenever an application or a window program is executed the windows operating system executes Win Main method first and it (i.e.. operating system) will pass the necessary parameters to it, which can be used in the program later for processing the messages.


Parameters of WinMain: -


hInstance and hPrevInstance are two variables which are of HANDLE data type,that is there are capable enough to store addresses in the memory.

WinMain function will take these variables to store addresses of the different objects of that application.

hInstance is used to store the address of the current object or instance of the application.

In other words we can say that this is a handle to the current instance.

hPrevInstance is used to store address of the previous instance of the application.

In other words we can say that this is a handle to previous instance.

The third parameter lpszCmdLine is a long pointer to the string which in turn consists of command line parameters which are passed to the program if any,

lpsz stands for long pointer to string.


The fourth parameter nCmdShow is an integer value that is passed to another function called ShowWindow function.

Windows to convey the message that whether the program should appear minimised, maximised or normal uses this parameter when it is displayed.



Return value of WinMain:
The return value of the WinMain in int, it returns 0 if the function is successfully executed else 1.

PASCAL Keyword and MessageBox Function

PASCAL Keyword:

PASCAL is a compiler keyword.

It should be in uppercase.

It informs C compiler to use function-calling conventions of Pascal language but not C language.

The reason behind this is Pascal language’s function calling convention produces less code when compiled into machine instructions.

This is because the Pascal function calling conventions puts parameters passed by a function n the stack, in the order there are declared in function, in C calling convention this is the reverse.

As a stack uses LIFO (Last In First Out) principle.

In the case of Pascal, compiler always knows the first argument is at the end of the stack, while in C, the compiler has to search for the first parameter by checking each and every parameter in the stack ,so the extra number of CPU instructions are generated when the function is called.




MessageBox Function:

This function is used to display message on the message box.

The declaration of this function is the following :

int MessageBox(HWND hWnd , LPSTR lptext , LPSTR lpcaption ,WORD wType)

Parameters passed to MessageBox function:

The first parameter hWnd is the handle of the parent window.

0 can be used as first parameter if the message box belongs to the current window.

The second parameter lpText is the text, which should be displayed as a message.

The third parameter lpCaption is the caption of the message box window.

The fourth parameter wType is the type of the message box required.

The valid values, which can be used as fourth parameter, are

MB_OK --- to display a simple MessageBox
MB_ICONQUESTION -----to display Message box with a question mark as an icon.
MB_ICONEXCLAMATION ---to display MessageBox with a exclamation mark as an icon.
MB_ICONHAND----it display message box with cross mark as an icon.