Windows programming
Download
1 / 56

Download presentation source - PowerPoint PPT Presentation


  • 294 Views
  • Updated On :

WINDOWS PROGRAMMING. Where the fun starts. WINDOWS 98 (Win32) PROGRAMMING. FROM THE GROUND UP With Application Programming Interface (API) and C++. Key Features of Windows 98. 1. The 32-bit programming environment 2. Thread-based multitasking 3. The call-based interface

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Download presentation source' - johana


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Windows programming

WINDOWS PROGRAMMING

Where

the fun

starts


Windows 98 win32 programming

WINDOWS 98 (Win32) PROGRAMMING

FROM THE GROUND UP

With Application Programming Interface (API) and C++


Key features of windows 98
Key Features of Windows 98

  • 1. The 32-bit programming environment

  • 2. Thread-based multitasking

  • 3. The call-based interface

  • 4. Dynamic Link Libraries


32 bit operating system
32-bit Operating System

  • Windows 3.1 - 16 bit

  • Windows NT, 95, 98 - 32 bit


Thread based multitasking
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.


Call based interface api
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).


Dynamic link libraries dlls
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


Two ways to program for window 98
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.


Windows programming languages
WINDOWS PROGRAMMING LANGUAGES

  • C C++

  • BASIC VISUAL BASIC


C windows programming
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!


Winmain
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.


Windows data types
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


Windows data types1
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


Code online
Code Online

  • Please refer to the following web site for code examples:

  • http://www.osborne.com


Windows messages
WINDOWS MESSAGES

  • WM_COMMAND - Sent as the result of a mouse click or key press.

    • Carries as part of its structure, ID’s 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.


Flags
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


New data types

MSG

BOOL

HWND

HANDLE

WNDCLASS

LRESULT

WPARAM

LPARAM

CALLBACK

UINT

New Data Types


New files
New Files

  • #include <windows.h>

  • Resource Files *.rc

  • Custom Header Files *.h


New classes
New Classes

  • WNDCLASS

  • structure declaration (data type) in windows.h

  • contains members and pointers to functions for a basic window.


Visual c

Visual C++

Windows Programming


Visual c1
Visual C++

  • Can call any Win 32 function

  • Code generated by wizards


Components of visual c
Components of Visual C++

  • ATL Active Template Library

  • MFC Microsoft Foundation Class Library

  • WFC Windows Foundation Class Library


Windows programming1
Windows Programming

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


Win32 vs win16
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


Win 32 programming features
Win 32 Programming Features

  • COM Component Object Model

  • DAO Data Access Objects

  • ActiveX Controls

  • OLE/DB database programming

  • Dynamic HTML


Gdi graphic device interface
GDI - Graphic Device Interface

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


Resource based programming
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++ compiler’s output to generate an executable program.


Resource files
Resource Files

  • Can include:

  • bitmaps, icons,

  • menu definitions,

  • dialog box layouts, strings

  • and even user defined custom resource formats


Components of visual c1
Components of Visual C++

  • ATL Active Template Library

  • MFC Microsoft Foundation Class Library

  • WFC Windows Foundation Class Library


Memory management
Memory Management

  • Past memory problems with older versions of Windows

  • locking memory handles

  • thunks

  • burgermasters


Memory management win 32
Memory Management Win 32

  • Virtual Memory

  • Memory-Mapped Files


Dll dynamic link libraries
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.


Visual c files
Visual C++ Files

  • APS Supports resource view

  • BSC Browser information file

  • CLW Support class wizard

  • DEP Dependency file

  • DSP Project file


Visual c files1
Visual C++ Files

  • DSW Workspace file

  • MAK External make file

  • NCB Support Class View

  • OPT Holds workspace configuration

  • PLG Builds log file


Windows programming options in c
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 Borland’s Object Windows Library OWL


Winmain function
WinMain Function

  • Windows requires your application to have a WinMain function.


Single document interface sdi
Single Document Interface (SDI)

  • SDI applications only have 1 window

  • Windows Notepad is an example


Multiple document interface mdi
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.


View user s standpoint
View - User’s 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.


View programmer s standpoint
View - Programmer’s Standpoint

  • A view is a C++ object of a class derived from the MFC library Cview class.

  • Like any C++ object, the view object’s behavior is determined by the member functions (and data members) of the class.


Building an application
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.


Resource file
Resource File

  • Although the application’s resource script is an ASCII file, modifying it with a text editor is not a good idea.

  • That’s the resource editors’ job.


Resource file1
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


Resource file2
Resource File

  • Menu

    • The application’s top-level menu and associated pop-up menus.

  • String Table

    • Strings that are not part of the C++ source code.


Resource file3
Resource File

  • Toolbar

    • The row of buttons immediately below the menu.

  • Version

    • Program description, version number, language, and so on.


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




Step 1
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


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


Bool initapplication handle hinstance
BOOL InitApplication ( HANDLE hInstance )

  • wc.lpszMenuName = NULL;

  • wc.lpszClassName = szAppName;

  • if ( !RegisterClass ( &wc ) )

  • return ( FALSE );

  • return ( TRUE );

  • }


Step 2
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 window’s 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.


Bool initinstance handle hinstance int ncmdshow
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 */

  • }


Back in main
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).


Step 3
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.


Lresult callback mainwndproc hwnd hwnd uint message wparam wparam lparam lparam
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 ) );

  • }

  • }


ad