A Bar Chart with Resources

Chapter 21 - Procedure-oriented Windows Applications

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

A Complete Windows Program
We have been discussing portions of a complete program throughout the previous sections of this chapter. For your convenience, the following is a complete listing of all the code necessary to create the Simple Windows Program
The source code file, SWP.C, is straightforward. However, long listings warrant extra care when entering each line of code.
/*
*  swp.c
*  Simple Windows Program
*  Copyright (c) William H. Murray and Chris H. Pappas, 1998
*/

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

char szProgName[]="ProgName";
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPreInst,
                  LPSTR lpszCmdLine,int nCmdShow)
{
 HWND hWnd;
 MSG  lpMsg;
 WNDCLASS wcApp;
 
 wcApp.lpszClassName=szProgName;
 wcApp.hInstance    =hInst;
 wcApp.lpfnWndProc  =WndProc;
 wcApp.hCursor      =LoadCursor(NULL,IDC_ARROW);
 wcApp.hIcon        =0;
 wcApp.lpszMenuName =0;
 wcApp.hbrBackground=GetStockObject(WHITE_BRUSH);
 wcApp.style        =CS_HREDRAW|CS_VREDRAW;
 wcApp.cbClsExtra   =0;
 wcApp.cbWndExtra   =0;
 if (!RegisterClass (&wcApp))
   return 0;
 
 hWnd=CreateWindow(szProgName,"Simple Windows Program",
                   WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
                   CW_USEDEFAULT,CW_USEDEFAULT,
                   CW_USEDEFAULT,(HWND)NULL,(HMENU)NULL,
                   (HANDLE)hInst,(LPSTR)NULL);
 ShowWindow(hWnd,nCmdShow);
 UpdateWindow(hWnd);
 while (GetMessage(&lpMsg,0,0,0)) {
   TranslateMessage(&lpMsg);
   DispatchMessage(&lpMsg);
 }
 return(lpMsg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT messg,
                        WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;

 switch (messg)
 {
   case WM_PAINT:
     hdc=BeginPaint(hWnd,&ps);

     MoveToEx(hdc,0,0,NULL);
     LineTo(hdc,639,429);
     MoveToEx(hdc,300,0,NULL);
     LineTo(hdc,50,300);
     TextOut(hdc,120,30,"<- a few lines ->",17);

     ValidateRect(hWnd,NULL);
     EndPaint(hWnd,&ps);
     break;
   case WM_DESTROY:
     PostQuitMessage(0);
     break;
   default:
     return(DefWindowProc(hWnd,messg,wParam,lParam));
     break;
 }
 return(0);
}
Recall, from earlier discussions, that the bulk of this code is required to define and register a window. The code that draws two lines and text in the client area is small in comparison.
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,639,429);
MoveToEx(hdc,300,0,NULL);
LineTo(hdc,50,300);

TextOut(hdc,120,30,"<- a few lines ->",17);
It is at this location in the application’s code that you can experiment with a wide variety of Windows GDI graphics drawing functions, which are called drawing primitives.
Compile and link your application by using the compiler’s Build menu. You can choose the Build, Rebuild All, or Execute menu items. The Build menu item builds just the current file. The Rebuild All menu item rebuilds all project files (there is only one in this project). The Execute menu item attempts to run the application. When it finds no executable file, it will prompt you for permission to build and execute the application.
If everything goes okay during the build process, you’ll end up with several additional files in your subdirectory. The executable file will be in the Debug subdirectory, specified as a project default.
Figure 21-8 shows the application’s window. This application draws two diagonal lines on the screen and prints a short text message.
Figure 21-8: The SWP.C Application window
You can experiment with other GDI graphics primitives such as ellipses, chords, pie wedges, and rectangles. Let’s see how these GDI primitives work.
Drawing an Ellipse
The Ellipse( ) function is used for drawing an ellipse or a circle. The center of the ellipse is also the center of an imaginary rectangle described by the points x1,y1 and x2,y2, as shown in Figure 21-9.
Figure 21-9: An ellipse with its bounding rectangle
An ellipse is a closed figure. An ellipse is filled with the current brush. The handle for the device context is given by hdc. All other parameters are of type int. This function returns a type BOOL.
The syntax for the command is
Ellipse(hdc,x1,y1,x2,y2)
For example, the following code draws a small ellipse in the user’s window:
Ellipse(hdc,200,200,275,250);
TextOut(hdc,210,215,"<- an ellipse",13);
Figure 21-10 shows how the ellipse will appear on the screen.
Figure 21-10: Drawing an ellipse in a window
Drawing a Chord
The Chord( ) function is a closed figure with a line between two arc points, x3,y3 and x4,y4. Figure 21-11 shows these points. A chord is filled with the current brush.
Figure 21-11: A chord with arc points shown
The handle for the device context is given by hdc. All other parameters are of type int. This function returns a type BOOL.
The syntax for the command is
Chord(hdc,x1,y1,x2,y2,x3,y3,x4,y4)
For example, the following code draws a small chord in the user’s window:
Chord(hdc,550,20,630,80,555,25,625,70);
TextOut(hdc,470,30," A Chord ->",11);
Figure 21-12 shows the chord section and its location on the user’s screen.
Figure 21-12: A chord is drawn to the current window
Drawing a Pie Wedge
Use the Pie( ) function for drawing pie-shaped wedges. The center of the elliptical arc is also the center of an imaginary rectangle described by the points x1,y1 and x2,y2, as shown in Figure 21-13.
Figure 21-13: A pie wedge and its bounding rectangle
The starting and ending points of the arc are points x3,y3 and x4,y4. Two lines are drawn from each endpoint to the center of the rectangle. Drawing is done in a counterclockwise direction. The pie wedge is filled because it is a closed figure. The handle for the device context is given by hdc. All other parameters are of type int. This function returns a type BOOL.
The syntax for the command is
Pie(hdc,x1,y1,x2,y2,x3,y3,x4,y4)
For example, the following code draws a small pie-shaped wedge in the window:
Pie(hdc,300,50,400,150,300,50,300,100);
TextOut(hdc,350,80,"<- A Pie Wedge",14);
Figure 21-14 shows the pie wedge on the screen.
Figure 21-14: A pie wedge is drawn to the window
Drawing a Rectangle
The Rectangle( ) function draws a rectangle or box described by x1,y1 and x2,y2. Again, the rectangle is filled because it is a closed figure. The values for the parameters cannot exceed 32,767 (7FFFH). The handle for the device context is given by hdc. All other parameters are of type int. This function returns a type BOOL.
The syntax for the command is
Rectangle(hdc,x1,y1,x2,y2)
As an example, the following code draws a rectangular figure in the user’s window:
Rectangle(hdc,50,300,150,400);
TextOut(hdc,160,350,"<- A Rectangle",14);
Figure 21-15 shows the rectangle produced on the screen.
Figure 21-15: Drawing a rectangle to the window

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net