Title: WINDOWS PROGRAMMING
1WINDOWS PROGRAMMING
2WINDOWS PROGRAMMING LANGUAGES
3Encapsulation
- Combining data and the functions that process the
data into a single entity. - Data refers to the members of a structure or
CLASS that hold data. - Functions refer to pointers to functions
included as members of a structure or CLASS - Window and Dialog Classes Provided by C are
Actually Structures Set up this Way.
4Writing a Windows Program
5STEP BY STEP APPROACH
6STEP 1
- Initialize the Application . . .
- Write a function that does the following
- declare a variable of the WNDCLASS type
- fill the members of that variable with values
that customize your window - call the RegisterClass function, passing your
class - return TRUE or FALSE (BOOL) to the main function,
based upon whether RegisterClass was successful
or not
7BOOL InitApplication ( HANDLE hInstance )
WNDCLASS wc wc.style
CS_HREDRAW CS_VREDRAW wc.lpfnWndProc
MainWndProc wc.cbClsExtra
0 wc.cbWndExtra 0 wc.hInstance
hInstance wc.hIcon LoadIcon (
hInstance, IDI_APPLICATION ) wc.hCursor
LoadCursor ( NULL, IDC_ARROW
) wc.hbrBackground GetStockObject (
WHITE_BRUSH
8BOOL InitApplication ( HANDLE hInstance )
- wc.lpszMenuName NULL
- wc.lpszClassName szAppName
- if ( !RegisterClass ( wc ) )
- return ( FALSE )
- return ( TRUE )
-
9STEP 2
- Initialize an Instance of the Application
- Write a function to do the following
- Assign the current instance to a global variable
for use by other windows and dialog boxes in the
program. - Call the CreateWindow function, passing the App
Name as specified in your class, the windows
position and size, handles to parent and child
windows and the handle to the current instance. - If the window could not be created, return FALSE,
otherwise call the ShowWindow function to display
the window.
10BOOL InitInstance ( HANDLE hInstance, int
nCmdShow )
- hInst hInstance / assign instance to
global variable / - hWnd CreateWindow ( szAppName, / ClassName
/ - My Window", / window title /
- WS_OVERLAPPEDWINDOW, / window style flag/
- 70, / horizontal position
/ - 70, / vertical position
/ - 500, / window width /
- 350, / window height /
- NULL, / handle of parent
window / - NULL, / handle of child or
menu / - hInstance, / handle of app
instance / - NULL ) / window creation
data / - if ( !hWnd ) / If window could not be created,
return "failure / - return ( FALSE )
- ShowWindow ( hWnd, nCmdShow ) / Show the
window / - return ( TRUE ) / Returns the
value from PostQuitMessage /
11BACK IN MAIN
- Make Calls to Your InitApplication and
InitInstance Functions. - Write a Windows Message Loop that Continuously
Checks for New Messages (such as the click of a
mouse button or press of a key on the keyboard) - When a Message is Received, the WNDCLASS Window
you Created will Call the Window Procedure it
points to (MainWndProc).
12STEP 3
- Handle the Windows Messages Received
- Write MainWndProc, which does the following
- Use nested switch constructs to handle valid
windows messages expected by the main window. - Makes calls to other procedures such as Dialog
Box procedures, when necessary for handling
messages to other resources.
13LRESULT CALLBACK MainWndProc ( HWND hWnd,
UINT message,WPARAM wParam, LPARAM
lParam )
-
- switch ( message )
-
- case WM_COMMAND / Menu Selection or Dialog
Box Control / - switch (wParam)
- case CM_EXIT / ID of the EXIT
menu item / - DestroyWindow ( hWnd )
- case CM_MESSAGE_TO_SCREEN / another menu
item / - MessageBox( hWnd, "Message Text",
Message Title", MB_OK) - default
- return ( DefWindowProc ( hWnd, message,
wParam, lParam ) ) -
- default
- return ( DefWindowProc ( hWnd, message, wParam,
lParam ) ) -
14WINDOWS MESSAGES
- WM_COMMAND - Sent as the result of a mouse click
or key press. - Carries as part of its structure, IDs of Menu
Items, Buttons or Dialog Box Controls selected by
the mouse or keyboard. - Other messages can be sent to a window or dialog
box by a call to a function inside the program
telling the object to behave a certain way.
15FLAGS
- Determine Window Style or Behavior
- Can Be Grouped in a Single Call to a Creation
Function in Order to Add Functionality to a
Window - hWnd CreateWindow ( szAppName, / ClassName
/ - My Window", / window title /
- WS_OVERLAPPEDWINDOW, / window style flag/
- 70, / horizontal position
/ - 70, / vertical position
/ - 500, / window width /
etc... - Flags are Names for an Underlying Number
Created with define in windows.h
16New Data Types
- MSG
- BOOL
- HWND
- HANDLE
- WNDCLASS
- LRESULT
- WPARAM
- LPARAM
- CALLBACK
- UINT
17New Files
- include ltwindows.hgt
- Resource Files .rc
- Custom Header Files .h
18New Classes
- WNDCLASS
- structure declaration (data type) in windows.h
- contains members and pointers to functions for a
basic window.