2. A Quick Start Using the IDE

Chapter 23 - Windows Applications Using the MFC

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

Drawing in the Client Area
The second application in this chapter is named GDI. The GDI application will draw several graphics shapes in the window’s client area. These are the same GDI drawing primitives discussed (and used separately) in Chapter 21.
This application also requires two files: the header file, GDI.H, and the source code file, GDI.CPP.
Enter each of the files carefully. When both files have been entered, the application can be compiled. Remember to state that this is an MFC application when building your application with the Project utility. The GDI.H header file, which follows, is similar in structure to the previous example:
class CMainWnd : public CFrameWnd
{
public:
 CMainWnd( );
 afx_msg void OnPaint( );
 DECLARE_MESSAGE_MAP( );
};

class CgdiAApp : public CWinApp
{
public:
 BOOL InitInstance( );
};
The source code file for the GDI.CPP application follows:
//
//  gdi.cpp
//  An extension of the mfcswp.cpp application
//  that allows experimentation with graphics
//  drawing primitives.
//  Copyright (c) William H. Murray and Chris H. Pappas, 1998
//

#include <afxwin.h>
#include “gdi.h”

CgdiAApp theApp;

CMainWnd::CMainWnd( )
{
 Create(NULL,"Experimenting With GDI Primitives",
        WS_OVERLAPPEDWINDOW,rectDefault,NULL,NULL);
}
void CMainWnd::OnPaint( )
{
 static DWORD dwColor[9]={RGB(0,0,0),        //black
                          RGB(255,0,0),      //red
                          RGB(0,255,0),      //green
                          RGB(0,0,255),      //blue
                          RGB(255,255,0),    //yellow
                          RGB(255,0,255),    //magenta
                          RGB(0,255,255),    //cyan
                          RGB(127,127,127),  //gray
                          RGB(255,255,255)}; //white
 short xcoord;
 POINT polylpts[4],polygpts[5];

 CBrush newbrush;
 CBrush* oldbrush;
 CPen  newpen;
 CPen* oldpen;

 CPaintDC dc(this);
 // draws a wide black diagonal line
 newpen.CreatePen(PS_SOLID,6,dwColor[0]);
 oldpen=dc.SelectObject(&newpen);
 dc.MoveTo(0,0);
 dc.LineTo(640,430);
 dc.TextOut(70,20,"<-diagonal line",15);
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );
 
 // draws a blue arc
 newpen.CreatePen(PS_DASH,1,dwColor[3]);
 oldpen=dc.SelectObject(&newpen);
 dc.Arc(100,100,200,200,150,175,175,150);
 dc.TextOut(80,180,"small arc->",11);
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );

 // draws a wide green chord
 newpen.CreatePen(PS_SOLID,8,dwColor[2]);
 oldpen=dc.SelectObject(&newpen);
 dc.Chord(550,20,630,80,555,25,625,70);
 dc.TextOut(485,30,"chord->",7);
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );

 // draws and fills a red ellipse
 newpen.CreatePen(PS_SOLID,1,dwColor[1]);
 oldpen=dc.SelectObject(&newpen);
 newbrush.CreateSolidBrush(dwColor[1]);
 oldbrush=dc.SelectObject(&newbrush);
 dc.Ellipse(180,180,285,260);
 dc.TextOut(210,215,"ellipse",7);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );
 // draws and fills a blue circle with ellipse function
 newpen.CreatePen(PS_SOLID,1,dwColor[3]);
 oldpen=dc.SelectObject(&newpen);
 newbrush.CreateSolidBrush(dwColor[3]);
 oldbrush=dc.SelectObject(&newbrush);
 dc.Ellipse(380,180,570,370);
 dc.TextOut(450,265,"circle",6);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );

 // draws a black pie wedge and fills with green
 newpen.CreatePen(PS_SOLID,1,dwColor[0]);
 oldpen=dc.SelectObject(&newpen);
 newbrush.CreateSolidBrush(dwColor[2]);
 oldbrush=dc.SelectObject(&newbrush);
 dc.Pie(300,50,400,150,300,50,300,100);
 dc.TextOut(350,80,"<-pie wedge",11);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );

 // draws a black rectangle and fills with gray
 newbrush.CreateSolidBrush(dwColor[7]);
 oldbrush=dc.SelectObject(&newbrush);
 dc.Rectangle(50,300,150,400);
 dc.TextOut(160,350,"<-rectangle",11);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
   
 // draws a black rounded rectangle and fills with blue
 newbrush.CreateHatchBrush(HS_CROSS,dwColor[3]);
 oldbrush=dc.SelectObject(&newbrush);
 dc.RoundRect(60,310,110,350,20,20);
 dc.TextOut (120,310,"<———rounded rectangle",24);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
 
 // draws several green pixels
 for(xcoord=400;xcoord<450;xcoord+=3)
   dc.SetPixel(xcoord,150,0L);
 dc.TextOut(455,145,"<-pixels",8);

 // draws several wide magenta lines with polyline
 newpen.CreatePen(PS_SOLID,3,dwColor[5]);
 oldpen=dc.SelectObject(&newpen);
 polylpts[0].x=10;
 polylpts[0].y=30;
 polylpts[1].x=10;
 polylpts[1].y=100;
 polylpts[2].x=50;
 polylpts[2].y=100;
 polylpts[3].x=10;
 polylpts[3].y=30;
 dc.Polyline(polylpts,4);
 dc.TextOut(10,110,"polyline",8);
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );

 // draws a wide cyan polygon and
 // fills with diagonal yellow
 newpen.CreatePen(PS_SOLID,4,dwColor[6]);
 oldpen=dc.SelectObject(&newpen);
 newbrush.CreateHatchBrush(HS_FDIAGONAL,dwColor[4]);
 oldbrush=dc.SelectObject(&newbrush);
 polygpts[0].x=40;
 polygpts[0].y=200;
 polygpts[1].x=100;
 polygpts[1].y=270;
 polygpts[2].x=80;
 polygpts[2].y=290;
 polygpts[3].x=20;
 polygpts[3].y=220;
 polygpts[4].x=40;
 polygpts[4].y=200;
 dc.Polygon(polygpts,5);
 dc.TextOut(70,210,"<-polygon",9);
 // delete brush objects
 dc.SelectObject(oldbrush);
 newbrush.DeleteObject( );
 // delete pen objects
 dc.SelectObject(oldpen);
 newpen.DeleteObject( );
}
BEGIN_MESSAGE_MAP(CMainWnd,CFrameWnd)
 ON_WM_PAINT( )
END_MESSAGE_MAP( )

BOOL CgdiAApp::InitInstance( )
{
 m_pMainWnd=new CMainWnd( );
 m_pMainWnd->ShowWindow(m_nCmdShow);
 m_pMainWnd->UpdateWindow( );

 return TRUE;
}
This example uses a variety of pens and brushes, in addition to investigating various GDI graphics primitives. Let’s look at the code in more detail.
The GDI.H Header File
Examine the GDI.H header file. Did you notice that only the name of the application has changed from the previous example? Basically, this feat is possible because of the simplicity of the example—no menus, dialog boxes, or other external resources.
The GDI.CPP Source Code File
Here is a portion of code the source code listing contained in the OnPaint( ) message handler function. An array is created to hold the RGB values for nine unique brush and pen colors. You’ll see shortly how colors are picked from this array:
static DWORD dwColor[9]={RGB(0,0,0),        //black
                        RGB(255,0,0),      //red
                        RGB(0,255,0),      //green
                        RGB(0,0,255),      //blue
                        RGB(255,255,0),    //yellow
                        RGB(255,0,255),    //magenta
                        RGB(0,255,255),    //cyan
                        RGB(127,127,127),  //gray
                        RGB(255,255,255)}; //white
The CBrush and CPen classes permit brush or pen objects to be passed to any CDC (base class for display context) member function. Brushes can be solid, hatched, or patterned, and pens can draw solid, dashed, or dotted lines. Here is the syntax that is required to create a new brush and pen object for this example:
CBrush newbrush;
CBrush
* oldbrush;
CPen  newpen;
CPen
* oldpen;
Since each GDI primitive’s code is somewhat similar to the others in the group, we will only examine two typical sections. The first piece of code is used to draw a wide black diagonal line in the window:
// draws a wide black diagonal line
newpen.CreatePen(PS_SOLID,6,dwColor[0]);
oldpen=dc.SelectObject(&newpen);
dc.MoveTo(0,0);
dc.LineTo(640,430);
dc.TextOut(70,20,"<-diagonal line",15);
// delete pen objects
dc.SelectObject(oldpen);
newpen.DeleteObject( );
The pen object is initialized by the CreatePen( ) function to draw black solid lines, six logical units wide. Once the pen is initialized, the SelectObject( ) member function is overloaded for the pen object class and attaches the pen object to the device context. The previously attached object is returned. The MoveTo( ) and LineTo( ) functions set the range for the diagonal line that is drawn by the selected pen. Finally, a label is attached to the figure with the use of the TextOut( ) function.
Brushes can be handled in a similar way. In the following code, the brush is initialized to be a hatch-brush filled with blue crosses (HS_CROSS). The brush object is selected in the same way the pen object was selected.
// draws a black rounded rectangle and fills with blue
newbrush.CreateHatchBrush(HS_CROSS,dwColor[3]);
oldbrush=dc.SelectObject(&newbrush);
dc.RoundRect(60,310,110,350,20,20);
dc.TextOut (120,310,"<———rounded rectangle",24);
// delete brush objects
dc.SelectObject(oldbrush);
newbrush.DeleteObject( );
The RoundRect( ) function draws a rounded rectangle in black at the given screen coordinates. A label is also printed for this figure.
The remaining shapes are drawn to the screen using a similar technique.
Running the GDI Application
Build the application by creating a project file specifically requesting the inclusion of the MFC library. Run the application and note the results that appear on your screen.
This application has a minor drawback, as you may have observed! All coordinate points for the GDI functions are set to pixel values valid for VGA monitors. What happens if you are using a super VGA display, for example? If you are using a higher-resolution display, such as a super VGA, the image will fill in only the upper-left part of your screen.
To eliminate this problem, your application can determine your display’s characteristics and adjust accordingly. This determination adds an extra layer of complexity to the application code, which has been kept as simple as possible to this point. However, the final two examples in this chapter will teach you how to scale your figures to fit the current display type. All of this can be done automatically if your program is properly written.
When you run this application, your screen should look something like that shown in Figure 23-2 if you are using a VGA monitor. The various GDI objects are displayed in very vivid colors.
Figure 23-2: The GDI.CPP application produces vivid GDI objects

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