1 / 59

Introduction to Windows Programming

Introduction to Windows Programming. Objectives. In this chapter you will learn: About Windows programming with Visual C++ About Windows architecture About the Windows API How to create a WinMain() function About events and messages. Windows Programming with Visual C++.

ewan
Download Presentation

Introduction to Windows Programming

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Windows Programming Microsoft Visual C++ .NET Chapter 9

  2. Objectives In this chapter you will learn: • About Windows programming with Visual C++ • About Windows architecture • About the Windows API • How to create a WinMain() function • About events and messages Microsoft Visual C++ .NET Chapter 9

  3. Windows Programming with Visual C++ • The ability to create Windows programs is one of the most important aspects of the Visual C++ environment • There are two ways to create Windows programs in Visual C++: with C or C++ and the Windows application programming interface, or with C++ and Microsoft Foundation Classes • An application programming interface, or API, is a library of methods and code that allows programmers to access the features and functionality of an application or operating system Microsoft Visual C++ .NET Chapter 9

  4. Windows Programming with Visual C++ • The Windows API allows you to write programs for Windows operating systems • You create Windows programs by combining code written in a source programming language with calls to the Windows API • Although you can create Windows programs using C++ and the Windows API, with Visual C++ the preferred method of creating Windows programs is to use C++ with the Microsoft Foundation Classes Microsoft Visual C++ .NET Chapter 9

  5. Building a Windows Program Microsoft Visual C++ .NET Chapter 9

  6. Windows Programming with Visual C++ • If the Microsoft Foundation Classes make the task of writing Windows programs with Visual C++ much easier, why bother to spend time learning how to create Windows API applications? • The answer is because Microsoft Foundation Classes programming is built on Windows API programming • You should already be more than familiar with the graphical user interface of Windows environments • A graphical user interface, or GUI, is a graphically based environment that you use to interact with applications Microsoft Visual C++ .NET Chapter 9

  7. Windows Programming with Visual C++ • To create a simple Windows API program, perform the steps listed on pages 469 and 470 Microsoft Visual C++ .NET Chapter 9

  8. SimpleApp Application Window Microsoft Visual C++ .NET Chapter 9

  9. Windows Architecture • The term bit refers to a binary number of 0 or 1 • The bus, or data bus, refers to the electronic path that the bits travel into the microprocessor • Figure 9-6 illustrates bits being transmitted along the data bus into a microprocessor Microsoft Visual C++ .NET Chapter 9

  10. Windows Architecture • There are Windows operating systems specifically designed to work with each width of a data bus: 16-bit, 32-bit, and 64-bit • The older (and essentially obsolete) Windows 3.1 operates at 16 bits • Windows NT, 95, 98, and Windows CE are 32-bit operating systems • Windows 2000 and XP are the only 64-bit Windows operating systems Microsoft Visual C++ .NET Chapter 9

  11. Windows Architecture • A separate Windows API exists for each generation of Windows operating systems • All 32-bit Windows operating systems share the same Windows API, known as Win32 API • One of the problems with the different generations of Windows operating systems is that applications written specifically for one generation will not usually work with earlier Windows operating systems Microsoft Visual C++ .NET Chapter 9

  12. The Windows API • A C++ program can access the Windows API by importing the window.h header file, which is included as part of the Visual Studio development environment • The windows.h header file includes all of the functions, variables, and other programming elements that make up the Windows API • You import the windows.h header file into your program using the #include statement, just as you would any other header file Microsoft Visual C++ .NET Chapter 9

  13. The Windows API • The windows.h header file is also included as part of the Microsoft Platform Software Development Kit (SDK), or Platform SDK • The Platform SDK contains header files, sample code, tools, and information for building applications using the Windows API • The Windows API includes hundreds of data types so that it can control the information passed to it from source programming languages (such as C, C++, Visual Basic, and so on), and correctly execute a program on different types of computers Microsoft Visual C++ .NET Chapter 9

  14. The Windows API • A pointer data type declares the type and name of a Windows API pointer • In comparison, you declare C and C++ pointers using standard variable data types, such as int and char • Most Windows API pointer data types begin with a prefix of P or LP (P stands for Pointer and LP stands for Long Pointer) • Another category of Windows API data types you will use in this chapter are handle data types • A handle is used to access an object that has been loaded into memory Microsoft Visual C++ .NET Chapter 9

  15. The Windows API • A handle is essentially the same thing as a C++ pointer • Handle data types begin with a prefix of H • You already understand that all Windows applications are contained within their own window • An important concept to grasp, however, as you start creating Windows API programs is that all individual controls (such as buttons) and user interface components are also windows, each requiring its own handle Microsoft Visual C++ .NET Chapter 9

  16. The Windows API • A child window always appears within the area defined by a parent window • A parent window is a primary application window containing one or more child windows • Figure 9-7 uses the calculator program to illustrate the concept of parent and child windows Microsoft Visual C++ .NET Chapter 9

  17. The WinMain() Function • Just as the main() function is the starting point for any C++ program, the WinMain() function is the starting point for any Windows API program • The WinMain() function declaration must be declared with type int • The WINAPI portion of the function declaration is a Windows API data type that is required in all Windows API functions • An instance is a particular copy of a window, program, or other type of object that happens to be running Microsoft Visual C++ .NET Chapter 9

  18. The WinMain() Function • Figure 9-8 demonstrates the concept of instances by showing multiple running copies of the game Minesweeper • Each copy of Minesweeper is an instance of the application Microsoft Visual C++ .NET Chapter 9

  19. The WinMain() Function • The first HINSTANCE parameter, named hInstance, is the handle that represents the current instance of the program’s parent window • The second HINSTANCE parameter, named hPrevInstance, represents a previously created instance of the program • The third parameter, named lpCmdLine, is of the LPSTR data type, which is a pointer to a string • The last parameter, named nCmdShow, is the int data type Microsoft Visual C++ .NET Chapter 9

  20. The WinMain() Function • The nCmdShow parameter returns a constant representing the window’s startup mode • Windows constants begin with the prefix SW_, followed by a description of the window mode Microsoft Visual C++ .NET Chapter 9

  21. The WinMain() Function • To add a WinMain() function definition to the calculator program, use the instructions on page 476 of the textbook • One of the most important tasks performed by the WinMain() function (other than being an entry point for all Window applications) is the creation and instantiation of windows • The steps involved in creating and instantiating windows are shown on pages 476 and 477 Microsoft Visual C++ .NET Chapter 9

  22. Defining the Windows Class • To create and display a window, you must first define and register its window class • The window class defines the characteristics of a program’s main window • The predefined WNDCLASS structure is used for defining the characteristics of a window class • Each of the fields in the WNDCLASS structure determines various characteristics of a window • Figure 9-10 lists the WNDCLASS structure fields along with a description of what the field controls Microsoft Visual C++ .NET Chapter 9

  23. WNDCLASS Structure Fields Microsoft Visual C++ .NET Chapter 9

  24. Defining the Windows Class • The hInstance field contains the handle to the current program, so you assign to the hInstance field the hInstance argument of the WinMain() class • The lpfn WndProc field contains a pointer to the name of a special function known as the window procedure that will handle messages • Figure 9-11 shows how to declare a new variable named wc, based on the WNDCLASS structure Microsoft Visual C++ .NET Chapter 9

  25. Declaring a New Variable Based on the WNDCLASS Structure Microsoft Visual C++ .NET Chapter 9

  26. Defining the Windows Class • Once you have defined the WNDCLASS structure, you need to use the RegisterClass() function, which informs the operating system about the newly defined window class • The RegisterClass() function accepts a single variable consisting of the address of the WNDCLASS structure you defined Microsoft Visual C++ .NET Chapter 9

  27. Creating New Windows • The WNDCLASS structure and RegisterClass() function only define the characteristics of a window; they do not actually create and display the window • After declaring a WNDCLASS structure and calling the RegisterClass function, you must call the CreateWindow() function to create a new window based on the window class • The CreateWindow() function creates a new window based on several parameters, including window class, size, position, and style Microsoft Visual C++ .NET Chapter 9

  28. lpClassName • The lpClassName parameter is a pointer to a text string representing the name of the class upon which you want to base the new window • You can assign to the lpClassName parameter either the name of the window class you created with the WNDCLASS structure and RegisterClass() function, or a predefined control class • Predefined control classes represent standard types of window controls such as buttons, edit boxes, and scroll bars Microsoft Visual C++ .NET Chapter 9

  29. Predefined Control Classes Microsoft Visual C++ .NET Chapter 9

  30. lpWindowName • The lpWindowName parameter is a pointer to a text string containing a name for the window • If the window being created contains a title bar, then the value pointed to by the lpWindowName parameter is used as the title bar text Microsoft Visual C++ .NET Chapter 9

  31. dwStyle • The dwStyle parameter determines the specific window styles that will be applied to the new window • Figure 9-15 in the text lists the values that can be passed to the dwStyle parameter • You can pass multiple window styles to the dwStyle property by separating them with a pipe | Microsoft Visual C++ .NET Chapter 9

  32. x, y, nWidth, and nHeight • The x and y parameters represent the starting position of a window’s upper-left corner • The nWidth and nHeight parameters represent the window’s lower-right corner • A window’s position and size are measured in pixels • A pixel (short for picture element) represents a single point on a computer screen • The number of pixels available depends on a computer monitor’s resolution—640 columns by 480 rows of pixels on a VGA monitor, and 1,024 columns by 768 rows on a Super VGA monitor Microsoft Visual C++ .NET Chapter 9

  33. Pixel Positions Microsoft Visual C++ .NET Chapter 9

  34. hWndParent • The hWndParent parameter specifies a handle to a parent window and tells the system the correct window within which a child window should be created • If the window is a top-level parent window (such as the calculator program’s main window), set the hWndParent parameter to NULL Microsoft Visual C++ .NET Chapter 9

  35. hMenu • The hMenu parameter specifies a handle to a menu • You will set this value to NULL because you do not need to work with menus in the calculator program Microsoft Visual C++ .NET Chapter 9

  36. hInstance • The hInstance parameter informs the system in which application instance the window should be created • In most cases, you use the HINSTANCE argument declared in the WinMain() function declaration Microsoft Visual C++ .NET Chapter 9

  37. lpParam • The lpParam parameter points to either a value passed through the CREATESTRUCT structure or to a CLIENTCREATESTRUCT structure • The code shown on page 485 illustrates a complete example of a CreateWindow() function whose return value is assigned to a handle named hSampleWnd • To create the windows that make up the calculator program, perform the processes shown on pages 486 through 488 Microsoft Visual C++ .NET Chapter 9

  38. Displaying New Windows • The final step in creating a new window is to display it • The ShowWindow() function displays a window onscreen that was created with the CreateWindow() function • The ShowWindow() function accepts two arguments: the handle to a window and the nCmdShow argument that was passed to the WinMain() function • To add a ShowWindow() function to the calculator program, use the directions listed on page 488 Microsoft Visual C++ .NET Chapter 9

  39. Events and Messages • An event is a specific circumstance that is monitored by Windows • Some of the most common events are actions that users take, such as clicking a button in a Windows application • A message is a set of information about a particular event, such as where and when the event occurred • You refer to messages in code by using predefined Windows constants beginning with a prefix of WM_ (WM stands for Windows Messaging) Microsoft Visual C++ .NET Chapter 9

  40. Processing of an Event Microsoft Visual C++ .NET Chapter 9

  41. Events and Messages • One of the more commonly used messages is the WM_COMMAND message, which is generated for events involving menus and buttons, such as when a user clicks a button • Every 32-bit Windows application has its own message queue where messages are placed until they are processed by the application • To process a message in an application’s message queue, you must perform the two steps on page 490 of the textbook Microsoft Visual C++ .NET Chapter 9

  42. Processing of the Message Queue Microsoft Visual C++ .NET Chapter 9

  43. Message Loops • Windows applications need a way of checking the message queue for new messages • A message loop continually checks the queue for new messages, and then sends any new messages found to the window procedure • The MSG structure contains information about the current message in the application’s message queue • The GetMessage() function retrieves messages from an application’s message queue and is called as the while loop’s conditional expression Microsoft Visual C++ .NET Chapter 9

  44. Message Loops • The WM_QUIT message is generated when an application closes, such as when a user clicks the main application window’s close icon • The body of the while loop contains the TranslateMessage() and the DispatchMessage() functions • The TranslateMessage() function converts keyboard messages into a format Windows can understand • The DispatchMessage() function sends messages to the window procedure for processing Microsoft Visual C++ .NET Chapter 9

  45. Message Loop Added to Calculator.cpp Microsoft Visual C++ .NET Chapter 9

  46. Window Procedures • A window procedure, or windproc, is a special function that processes any messages received by an application • You may also see window procedures referred to as callback functions because they give Windows a way of “calling back” your application once it generates an event and message • Figure 9-22 illustrates the concept of a callback function, using an expanded version of the message queue figure you have already seen Microsoft Visual C++ .NET Chapter 9

  47. Callback Function Microsoft Visual C++ .NET Chapter 9

  48. Window Procedures • The primary purpose of a window procedure is to execute the appropriate code, or handler, for a specific message • A handler is a segment of code within a window procedure that executes for a specific message • You use a switch structure to determine the type of message contained in the msg parameter of the window procedure’s function definition • Case labels within the switch statement contain the handlers or call the appropriate handler functions for each message Microsoft Visual C++ .NET Chapter 9

  49. Window Procedures • The WM_DESTROY message is generated when a window is being destroyed • The handler for the WM_DESTROY message needs to call the PostQuitMessage() function, which generates the WM_QUIT message that tells the message loop to end the program • The DefWindowProc() function calls the default window procedure for any message for which you do not provide a handler Microsoft Visual C++ .NET Chapter 9

  50. Completing the Calculator Program • To complete the calculator program, you need to understand how to calculate numbers in C++ • The functionality of the program is not very complex and does calculations with only two numbers (operands) using a single operator (+,-,*, or /) • The calculator program requires several string manipulation functions in order to set and retrieve the values that will appear in the edit box Microsoft Visual C++ .NET Chapter 9

More Related