590 likes | 1.13k Views
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
E N D
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 • 4. Dynamic Link Libraries
32-bit Operating System • Windows 3.1 - 16 bit • Windows NT, 95, 98 - 32 bit
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) • 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) • 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 • 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 • C C++ • BASIC VISUAL BASIC
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( ) • 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 • 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 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 • Please refer to the following web site for code examples: • http://www.osborne.com
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 • 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
MSG BOOL HWND HANDLE WNDCLASS LRESULT WPARAM LPARAM CALLBACK UINT New Data Types
New Files • #include <windows.h> • Resource Files *.rc • Custom Header Files *.h
New Classes • WNDCLASS • structure declaration (data type) in windows.h • contains members and pointers to functions for a basic window.
Visual C++ Windows Programming
Visual C++ • Can call any Win 32 function • Code generated by wizards
Components of Visual C++ • ATL Active Template Library • MFC Microsoft Foundation Class Library • WFC Windows Foundation Class Library
Windows Programming • It is more important to know C++ than it is to know the Win 32 Application Programming Interface API
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 • COM Component Object Model • DAO Data Access Objects • ActiveX Controls • OLE/DB database programming • Dynamic HTML
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 • 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 • Can include: • bitmaps, icons, • menu definitions, • dialog box layouts, strings • and even user defined custom resource formats
Components of Visual C++ • ATL Active Template Library • MFC Microsoft Foundation Class Library • WFC Windows Foundation Class Library
Memory Management • Past memory problems with older versions of Windows • locking memory handles • thunks • burgermasters
Memory Management Win 32 • Virtual Memory • Memory-Mapped Files
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 • APS Supports resource view • BSC Browser information file • CLW Support class wizard • DEP Dependency file • DSP Project file
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 • 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 • Windows requires your application to have a WinMain function.
Single Document Interface (SDI) • SDI applications only have 1 window • Windows Notepad is an example
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 • 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 • 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 • 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 • 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 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 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 File • Toolbar • The row of buttons immediately below the menu. • Version • Program description, version number, language, and so on.
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 • 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