சனி, 20 பிப்ரவரி, 2010

Windows Programming

Different ways to do windows programming:

• Event Driven Programming

• Windows Programming Using SDK (Difficult)
(SDK = System Development Kit)

• Windows Programming Using MFC (Easy)
(MFC = Microsoft Foundation Class)

• Windows Programming Using MFC and VC++ Wizards
(Easiest and most productive)

________________________________________________________________________________________________

Event Driven Programming

CONCEPT:

EVENTS are the user-initiated actions that are received by the OPERATING SYSTEM. For example when a key on the keyboard is pressed down an event called WM_KEYDOWN is generated similarly when the left mouse button is pressed a WM_LBUTTONDOWN is generated. Thus a link between the user and the operating system is developed. An operating system recognizes events as "MESSAGES". A message is a physical chunk of data that is not dependent on a particular language or processor type, so it can be easily queued, prioritized and ported to other CPU's. All together there are about 250 events that have been developed and recognized by the operating system. Messages are submitted to the "MESSAGE QUEUE", a part of the operating system, and handled for submission to the APPLICATION PROGRAM for further processing.

The application program needs two things to be written. One is the 'WinMain' that puts the received messages into the "MESSAGE LOOP". The second is the 'Windows Procedure' which holds the corresponding "MESSAGE HANDLER" i.e., an instruction code on how to handle the event. A MESSAGE MAP is used to connect messages sent to the program with the message handlers. If no message handler is found in the application program, messages are returned back to the windows operating system and handled by the special function called the default windows procedure (DefWindowProc). It is evident at this point that a one-way link between the user and the application program has developed.




OPERATING SYSTEM
Incoming messages










Message
queue


Retrieved messages

Application Program

WinMain
Message
loop


Window
Procedure Dispatched messages














Unprocessed messages



The responsibility of the WinMain is to:
1. Create the data structure (referred to 'CWnd' Class) for the Graphic User Interface (GUI) of the program.
2. 2. Register the Window class with the operating system.
3. Make the GUI visible.
4. Start the message loop.

A windows program can also send messages to other windows. Since every control used in a windows program is another window, messages are often used for communication with controls that may deal with user input. When a window procedure receives the message, the parameters passed along with the message are used to help decide how the message should be handled. Therefore a two-way communication between the user and the application program can be established.

The messages are usually of two types.
1. General-purpose windows messages starting with WM for general window.
2. Specialized control windows e.g., AC for Animated control, BM for Button control, CB for Combobox control, TB for Toolbar Control, LV List-bar control etc

COMMON WINDOWS MESSAGES
________________________________________________________________________
Message Sent When
________________________________________________________________________
WM_CHAR A character is input from the keyboard.
WM_COMMAND The user selects an item from a menu, or a child
Window has a notification to send to its parent.
WM_CREATE A window is created.
WM_DESTROY A window is destroyed.
WM_LBUTTONDOWN The left mouse button is pressed.
WM_LBUTTONUP The left mouse button is released.
WM_MOUSEMOVE The mouse pointer is moved within the window's
Client area.
WM_PAINT A window needs repainting (redrawing).
WM_QUIT The application is about to terminate (breaks the loop).
WM_SIZE A window is resized.
________________________________________________________________________


A WM_COMMAND is generated on all the menu item clicks. These are differentiated by the ID of the each for example a FILE -> OPEN event may have an ID of FILE_OPEN and a FILE-> EXIT may have an ID of FILE_EXIT etc.






MFC follows common Hungarian prefixes to declare variables.

COMMON HUNGARIAN NOTATION PREFIIXES
________________________________________________________________________
Prefix Data Type
________________________________________________________________________
b BOOL
c char
cx, cy distance in horizontal or vertical direction.
dw DWORD
h handle
l long
n int
p pointer
pt Cpoint or POINT
rc Crect or RECT
str CString
sz zero-delimited string
w WORD
wnd CWnd
________________________________________________________________________

If you have a data member of a class then always use m_ as a prefix.
Global MFC functions have "Afx" as a prefix.

AFX FUNCTION SAMPLER
________________________________________________________________________
Function Name Description
________________________________________________________________________
AfxAbort Unconditionally aborts an application. Usually
Called when an unrecoverable error occurs.
AfxBeginThread Creates a new thread and begins executing it.
AfxEndThread Terminates the thread that is currently executing.
AfxFormatString1 Substitutes a specified string for instances of "%1"
In a string resource and copies the resultant string
to a Cstring object.
AfxMessageBox Displays a windows message box.
AfxGetApp Returns a pointer to the application object.
AfxGetAppName Returns the name of the application.
AfxGetMainWnd Returns a pointer to the applications main window.
AfxGetInstanceHandle Returns a handle identifying the current application
Instance.
AfxRegisterWndClass Registers a custom WNDCLASS for an MFC application.

கருத்துகள் இல்லை:

கருத்துரையிடுக