This post is gives you some knowledge about graphic mode programming, mainly about window programming, and its aspects. The programs used for graphic mode is not like normal programs written for character mode. It has lot of function calls and callbacks. Today computer programming(GUI) is very easy. Programer don't need to worry about the internal working of the interfaces(such as window, control windows etc). But in reality these interface(A dialog box, a window) on display performs in a very interesting way. The programming paradigm for GUI based application are called event-driven programming
A computer programs can be in two mode :
- Character-mode
- Graphic-mode
In character mode computing, provides only minimal interface for the user. To do any thing on the computer the user should type commands on to the interface which only accepts characters typed from keyboard(an input device). The interface usually, which is displayed on monitor(an output device)has black background with white colored text on it. For instance, command prompt (DOS). provided with MS-WINDOWS OS.
After the introduction of high speed processor and memory , opens ways to graphic mode computing in which a user can use both keyboard and some pointing devices(mouse, joystick,...) for giving command or inputs to computer interactively. In graphical mode the video display itself act as input device. This makes the user and program more attached. The interface in graphical mode is known as GUI(Graphical User Interface).
In graphic-mode user interact with various object displayed on the video display unit. The object can be a window, various push-button, scrollbars, check-box etc. A window is a rectangular area with border ,a caption bar, scroll-bar,menu, buttons.
Below show a basic window, in MS-Windows, pointed out the some characteristics.
Windowing program can be done by using both procedural and object oriented paradigm. Using the concept of C , which follows procedural programming paradigm, and of C++, which follow the OOP (Class and objects), we can create windows and window based applications.
How it works...?
How it works...?
Events and Messages
In computing an event is an user action. An event can be triggered by pressing key on keyboard, mouse clicks , mouse movements. So an event can be defined as "In computing, an event is an action or occurrence detected by the program that may be handled by the program". Program paradigm used for handling events or creating events can be called as Event-driven programming. So, in this paradigm the flow of program execution is determined by events. Events are generated not only by user, such as pressing key on keyboard, mouse clicks, but a program and OS can also generate events.
When an event triggered, OS send it to be processed by an application program. Program receive it as a message to do some job.So an event can be termed as some kind of input to the running application program. When an application program is under execution the OS keeps a message table(Which stores all messages to all windows an application program might create ) from which the application retrieves the message using a loop structure(which is called as message loop).The message which is retrieved by the loop is send to a function in the program to be processed. This, user defined, function is known as window procedure. This function has an important role in the window programs. This function acts as an interface between OS and the program. In window programming the program is called by OS. For that the OS usually calls this, window procedure, function. It usually have four parameters , a program handle or the handle to the window, a message identifier, and the last two parameter which provides additional information about the message. This function is also known as callback function. It is an user-defined function. This function determines how the window responds to the user and what to display window in it's client area.
Definition a window procedure is:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
Every window that a program creates has an associated window procedure. This window procedure is a function that could be either in the program itself or in DLL. The window procedure determines what the window display in its client area and how the window responds to user inputs. The OS sends a message to a window by calling the window procedure. The window procedure does some processing based on the message and then returns control to Windows.
Every message that a window procedure receives is identified by a number, which is the message parameter to the window
procedure. The Windows header-file WINUSER.H defines identifier beginning with the prefix WM(window message) for each type of message.
A window procedure can have any name. A window procedure is always associated with a particular window class that programmer register by calling RegisterClass().
The main function [ WinMain() ]
Every Windows program includes an entry-point function that is named WinMain() Syntax is:
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int cmdShow)
WINAPI is a calling convention. A calling convention defines how a function receives parameters from the caller(from left to right or right to left). For example, it defines the order that parameters appear on the stack.
WINAPI is a calling convention for Win32 API.
(For more about calling convention click here )
hInstance - is something called a "handle to an instance/instance handle" or "handle to a module". The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions — for example, to load icons or bitmaps.
hPrevInstance - This parameter, handle to a previous instance, stores the previous copy of the current instance if it still there. because same Windows application can have more than one copy at a time. It was used in 16-bit Windows, but is now always NULL by default.
cmdLine - Pointer to a null-terminated string that specifies the command line for the application. Contains the
command-line arguments as a Unicode string.
cmdShow - Specifies how the window is to be shown, that is, whether the main application window will be show minimized, maximized, or normal.
The WinMain() function returns an integer value.
The WinMain(), in window program, is similar to main() in C Programming language.
Necessary header files
Headerfiles includes all the files need for program to run. Win32 programming uses a a master header file called <windows.h>. There are main 5 header you needed for a normal windowing . Those areWINUSER.H, WINBASE.H, WINGDI.H, WINNT.H, WINDEF.H
WINDEF.H defines basic type definitions, WINNT.H defines type definitions for Unicode support, WINUSER.H defines user interface functions, WINBASE.H defines kernel functions and WINGDI.H defines functions for graphic device interfaces.
So, these header files defines all the Windows function calls , data types, data structures an constants identifiers. The WINDOWS.H header files includes all the above specified header files.
Creating the Window
To create a window one must use the function CreateWindow(). Using Window class which defines general characteristics of a window, such as style ,icon cursor, background colour etc. This function includes 11 parameters .The first parameter is the window class name provided on defining the window using WINDCLASS. second parameter is the caption for the window which is a text string. third parameter defines the style of the window. That is this window is a normal overlapped window. it will have all the characteristics of the normal window which defined by the programmer. the fourth and the fifth parameter specifies the default position of the newly created window on screen(x-coordinates and y- coordinates). sixth and seventh parameter specifies the initial size of the window. eighth parameter specifies the kind of window. is it parent window or child window(window which is from another window).This parameter should be null parent window always will be desktop. nest parameter is the the window menu handle. tenth parameter is set to the instance handle which s passed to the program , which is a parameter of WinMain() too. the last parameter is called creation parameter which can be useful later .
Syntax:
CreateWindow(szAppname,windowcaption,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,menu_handler,hInstance,NULL);
This function returns a value which is stored in a variable of type HWND, handle to a window, which used by the program to refer to the window.
This function returns a value which is stored in a variable of type HWND, handle to a window, which used by the program to refer to the window.
No comments:
Post a Comment