WINDOWS PROGRAMMING - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

WINDOWS PROGRAMMING

Description:

Write your own C Windows class library that uses Win32. Use the MFC ... members of a structure or CLASS that hold data. ... function, passing your class ... – PowerPoint PPT presentation

Number of Views:256
Avg rating:3.0/5.0
Slides: 57
Provided by: noww
Category:

less

Transcript and Presenter's Notes

Title: WINDOWS PROGRAMMING


1
WINDOWS PROGRAMMING
  • Where
  • the fun
  • starts

2
WINDOWS 98 (Win32) PROGRAMMING
  • FROM THE GROUND UP
  • With Application Programming Interface (API) and
    C

3
Key Features of Windows 98
  • 1. The 32-bit programming environment
  • 2. Thread-based multitasking
  • 3. The call-based interface
  • 4. Dynamic Link Libraries

4
32-bit Operating System
  • Windows 3.1 - 16 bit
  • Windows NT, 95, 98 - 32 bit

5
Thread-Based Multitasking
  • Multitasking operating system
  • Share CPU
  • process-based multitasking
  • thread-based multitasking
  • A thread is an individual unit of executable code
    within a process.

6
Call-Based Interface (API)
  • The call-based interface is an extensive set of
    system-defined functions that provide access to
    operating system features.
  • Collectively these functions are called
    Application Programming Interface (API).

7
Dynamic Link Libraries (DLLs)
  • The Win32 API functions are contained in Dynamic
    Link Libraries (DLLs).
  • Dynamic Linking provides
  • 1. Disk space from being wasted
  • 2. Update link library routines
  • 3. No recompilation required

8
Two Ways to Program for Window 98
  • 1. Use the API functions defined by Win32. In
    this approach, your programs directly utilize the
    API and explicitly handle all of the details
    associated with a Windows 98 program.

9
WINDOWS PROGRAMMING LANGUAGES
  • C C
  • BASIC VISUAL BASIC

10
C Windows Programming
  • 2. The second way to program for Windows 98 uses
    a special C class library, which encapsulate
    the API. By far the most popular Windows
    programming class library is MFC (Microsoft
    Foundation Classes).
  • It is best employed after you have gained a firm
    foundation in Windows programming using API!

11
WinMain( )
  • Function must perform the following
  • 1. Define a window class
  • 2. Register that class with Win98
  • 3. Create a window of that class.
  • 4. Display the window
  • 5. Begin running the message loop.

12
Windows Data Types
  • HANDLE is a value that identifies some resources
  • HWND is a 32-bit integer that is used as a window
    handle
  • UINT is an unsigned 32-bit integer
  • BYTE is an 8-bit unsigned character
  • WORD is a 16-bit unsigned short integer
  • DWORD is an unsigned long integer

13
Windows Data Types
  • LPSTR is a pointer to a string
  • LPCSTR is a const pointer to a string
  • LONG is another name for long
  • BOOL is an integer

14
Code Online
  • Please refer to the following web site for code
    examples
  • http//www.osborne.com

15
WINDOWS 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.

16
FLAGS
  • 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

17
New Data Types
  • MSG
  • BOOL
  • HWND
  • HANDLE
  • WNDCLASS
  • LRESULT
  • WPARAM
  • LPARAM
  • CALLBACK
  • UINT

18
New Files
  • include ltwindows.hgt
  • Resource Files .rc
  • Custom Header Files .h

19
New Classes
  • WNDCLASS
  • structure declaration (data type) in windows.h
  • contains members and pointers to functions for a
    basic window.

20
Visual C
  • Windows Programming

21
Visual C
  • Can call any Win 32 function
  • Code generated by wizards

22
Components of Visual C
  • ATL Active Template Library
  • MFC Microsoft Foundation Class Library
  • WFC Windows Foundation Class Library

23
Windows Programming
  • It is more important to know C than it is to
    know the Win 32 Application Programming Interface
    API

24
Win32 vs. Win16
  • 32-bit programming for
  • Microsoft Windows 95, 98, NT
  • using Win32 API
  • Compiled MFC programming interface will work on
    any of the above platforms

25
Win 32 Programming Features
  • COM Component Object Model
  • DAO Data Access Objects
  • ActiveX Controls
  • OLE/DB database programming
  • Dynamic HTML

26
GDI - Graphic Device Interface
  • Instead of addressing the hardware, the C
    program calls GDI functions that reference a data
    structure called a device context.

27
Resource-Based Programming
  • When you program for Windows, you store data in a
    resource file using a number of established
    formats.
  • The linker combines this binary resource file
    with the C compilers output to generate an
    executable program.

28
Resource Files
  • Can include
  • bitmaps, icons,
  • menu definitions,
  • dialog box layouts, strings
  • and even user defined custom resource formats

29
Components of Visual C
  • ATL Active Template Library
  • MFC Microsoft Foundation Class Library
  • WFC Windows Foundation Class Library

30
Memory Management
  • Past memory problems with older versions of
    Windows
  • locking memory handles
  • thunks
  • burgermasters

31
Memory Management Win 32
  • Virtual Memory
  • Memory-Mapped Files

32
DLL - Dynamic Link Libraries
  • Multiple applications share DLLs
  • Which saves memory and disk space
  • You can create your own Extension DLLs which
    built on the
  • MFC Microsoft Foundation Class Library.

33
Visual C Files
  • APS Supports resource view
  • BSC Browser information file
  • CLW Support class wizard
  • DEP Dependency file
  • DSP Project file

34
Visual C Files
  • DSW Workspace file
  • MAK External make file
  • NCB Support Class View
  • OPT Holds workspace configuration
  • PLG Builds log file

35
Windows Programming Options in C
  • Program in C with the Win32 API
  • Write your own C Windows class library that
    uses Win32
  • Use the MFC application framework
  • Use another Windows-based application framework
    such as Borlands Object Windows Library OWL

36
WinMain Function
  • Windows requires your application to have a
    WinMain function.

37
Single Document Interface (SDI)
  • SDI applications only have 1 window
  • Windows Notepad is an example

38
Multiple Document Interface (MDI)
  • An MDI application has multiple child windows,
    each of which corresponds to an individual
    document.
  • Microsoft Word is a good example.

39
View - Users Standpoint
  • A view is an ordinary window that the user can
    size, move and close in the same way as any other
    windows-based application window.

40
View - Programmers Standpoint
  • A view is a C object of a class derived from
    the MFC library Cview class.
  • Like any C object, the view objects behavior
    is determined by the member functions (and data
    members) of the class.

41
Building an Application
  • 1. Run AppWizard to generate SDI application
    source code.
  • 2. Compile and link the generated code.
  • 3. Test the resulting application.
  • 4. Browse the application.

42
Resource File
  • Although the applications resource script is an
    ASCII file, modifying it with a text editor is
    not a good idea.
  • Thats the resource editors job.

43
Resource File
  • Accelerator
  • Definitions for keys that simulate menu and
    toolbar selections
  • Dialog
  • Layout and content of dialog boxes
  • Icon
  • 16-by-16 and 32-by-32 pixels

44
Resource File
  • Menu
  • The applications top-level menu and associated
    pop-up menus.
  • String Table
  • Strings that are not part of the C source code.

45
Resource File
  • Toolbar
  • The row of buttons immediately below the menu.
  • Version
  • Program description, version number, language,
    and so on.

46
Encapsulation
  • 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.

47
Writing a Windows Program
48
STEP BY STEP APPROACH
49
STEP 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

50
BOOL 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
51
BOOL InitApplication ( HANDLE hInstance )
  • wc.lpszMenuName NULL
  • wc.lpszClassName szAppName
  • if ( !RegisterClass ( wc ) )
  • return ( FALSE )
  • return ( TRUE )

52
STEP 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.

53
BOOL 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 /

54
BACK 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).

55
STEP 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.

56
LRESULT 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 ) )
Write a Comment
User Comments (0)
About PowerShow.com