Testing the Container Application

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

Using SWP.C as a Template
The previous section described the development of a simple application. That application can now serve as a template that will allow you to experiment with additional Windows functions. This template serves as the basis of many simple applications requiring only minor changes in coding. The next example illustrates how you can use the template to design a simple application that will draw a sine wave in the client area of a window.
Here is the SWP.C code modified to become the SINE.C application.
/*
* Sine.c
* An application that draws a sine wave.
* Developed using swp.c as a template.
* Copyright (c) William H. Murray and Chris H. Pappas, 1998
*/

#include <windows.h>
#include <math.h>

#define pi 3.14159265359

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        =NULL;
 wcApp.lpszMenuName =NULL;
 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,"A Sine Wave",
                   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;
 double y;
 int i;

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

     /* draw the x & y coordinate axes */
     MoveToEx(hdc,100,50,NULL);
     LineTo(hdc,100,350);
     MoveToEx(hdc,100,200,NULL);
     LineTo(hdc,500,200);
     MoveToEx(hdc,100,200,NULL);

/* draw the sine wave */

     for (i=0;i<400;i++) {
           y=120.0*sin(pi*i*(360.0/400.0)/180.0);
         LineTo(hdc,i+100,(int) (200.0-y));
     }

     ValidateRect(hWnd,NULL);
     EndPaint(hWnd,&ps);
     break;
   case WM_DESTROY:
     PostQuitMessage(0);
     break;
   default:
     return(DefWindowProc(hWnd,messg,wParam,lParam));
     break;
 }
 return(0);
}
In addition to the SINE.C file just shown, you will need a project file in order to compile and link the application within the integrated environment.
Examine the C source code and compare it with the previous SWP.C application. As you can see, this application makes only minor changes to the SWP.C template of the previous section.
Notice that new variables are declared in WndProc( ):
double y;
int i;
The actual sine wave plotting takes place under WM_PAINT. The coordinate axes are drawn with several calls to the MoveToEx( ) and LineTo( ) functions:
/* draw the x & y coordinate axes */
MoveToEx(hdc,100,50,NULL);
LineTo(hdc,100,350);
MoveToEx(hdc,100,200,NULL);
LineTo(hdc,500,200);
MoveToEx(hdc,100,200,NULL);
The sine wave is drawn and scaled in one operation. In this application, the waveform will extend 120 pixels above and below the horizontal axis. The sin( ) function from MATH.H is used to generate the sine values. The use of the constant PI is needed to convert angles from degrees to radians.
/* draw the sine wave */
for (i=0;i<400;i++) {
 y=120.0
*sin(pi*i*(360.0/400.0)/180.0);
 LineTo(hdc,i+100,(int)(200.0-y));
}
Since this application was designed to work in the default drawing mode, the program draws directly in screen pixels. On a VGA monitor, the figure will fill the entire screen. Figure 21-16 shows the output of the program on a VGA screen. If a high-resolution monitor operating in 1024 768 graphics mode is used, the figure will be drawn in the upper-left corner of the monitor. Changes in figure size such as this are usually considered undesirable, and you’ll see a technique for avoiding these variations in the PIE.C example that follows.
Figure 21-16: The SINE.C application draws a sine wave 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