Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

40

Part I

INTRODUCING VC++.NET

 

 

 

CPaintDC class encapsulates these functions. The constructor of the CPaintDC class invokes the BeginPaint function (and returns a device context that can be used for painting), and the destructor of CPaintDC invokes the EndPaint function. In a nutshell, the CPaintDC class is used to paint the invalid region of a client area.

 

 

 

 

 

 

 

 

CAUTION

 

 

Y

 

 

 

 

 

 

 

 

 

 

 

 

 

 

L

 

Don’t use the CClientDC class to handle the WM PAINT message because it doesn’t

 

 

 

 

F

 

provide the functionality of the BeginPaint and EndPaint functions.

 

 

 

 

M

 

 

 

 

A

 

 

CMetaFileDC. The C etaFileDC class, derived from the CDC class, han-

 

 

dles the processing of metafiles. It provides the member functions to cre-

 

 

 

E

 

 

 

ate, copy, play, and close a metafile.

 

 

 

T

 

 

 

CWindowDC. he CWindowDC class, another CDC-derived class, provides

the member functions to work with the window of your application, including both client and nonclient areas.

Besides the device context, Windows also provides a set of GDI objects, discussed next.

GDI Objects

The various drawing tools, such as pens and brushes, and drawing attributes, such as color and font, are collectively referred to as GDI objects. Table 2-1 lists various GDI objects and corresponding classes to which they belong.

Table 2-1 GDI Objects and MFC Classes

GDI Object

MFC Class

Pen

CPen

Brush

CBrush

Font

CFont

Bitmap

CBitmap

Palette

CPalette

Region

CRgn

 

 

Team-Fly®

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

41

 

 

 

 

 

The following are the steps that you need to follow to use a GDI object for a drawing:

1.Instantiate the GDI object.

2.Invoke the respective Create function; for example, the Create function of the CPen class if you have created an object of the CPen class.

3.Select the object into the device context that you are working with using

the SelectObject method.

CAUTION

At any point of time, only five GDI objects can be associated with a device context.

4. After your work is done, ensure that the GDI object is deleted.

NOTE

If you want to use a GDI object repeatedly, you can allocate the memory once and use it throughout, and then finally delete it.

The following is a code snippet of an application that illustrates the use of a device context and the GDI objects. The functionality of this application is that when the user clicks the left button, a rectangle with a blue outline and red filling is created.

# include <afxwin.h>

class MyWindow:public CFrameWnd

{

public:

CPen *newPen;

CREATING A GDI OBJECT

You can create a GDI object in two ways:

In one-stage construction, you construct and initialize the object in a single step within the constructor.

In two-stage construction, you construct the object using the constructor and initialize it using other member functions, such as the Create function.

42

Part I

INTRODUCING VC++.NET

 

 

 

//declared a pointer of the CPen class CBrush *newBrush;

//declared a pointer of the CBrush class

MyWindow()

{

Create(NULL,”Example Window”);

}

void OnLButtonDown(UINT flag, CPoint point)

{

CClientDC dc(this); //creating a device context, dc, for the client area

newPen=new CPen(PS_SOLID,5,RGB(0,0,255));

//creating a solid “blue” pen

dc.SelectObject(newPen);

//attaching the pen to the dc

newBrush=new CBrush(RGB(255,0,0));

//creating a “red” brush

dc.SelectObject(newBrush); //attaching the brush to the dc

dc.Rectangle(100,100,500,500);

//drawing a rec-

tangle with blue pen and red brush

 

//the Rectangle function accepts four parameters that are the four co-ordinates x1,y1, x2, y2

}

DECLARE_MESSAGE_MAP() };

BEGIN_MESSAGE_MAP(MyWindow, CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

class MyApp:public CWinApp

{

public:

BOOL InitInstance()

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

43

 

 

 

 

 

{

MyWindow *MyWindowObject = new MyWindow;

m_pMainWnd=MyWindowObject;

MyWindowObject->ShowWindow(SW_SHOWNORMAL);

return TRUE;

}

};

MyApp app;

You can edit the previous sample applications that you created to add this functionality. The output of the application is displayed in Figure 2-11.

FIGURE 2-11 Sample drawing application using client dc, pen, and brush

In the preceding sample application, you learned to draw a rectangle on the client area using the CClientDC class. In addition, you also learned to use a simple pen and a brush. You now are going to receive more details about the member functions available for drawing a line and an ellipse, and also some more details on the types of pens and brushes that can be created.

44

Part I

INTRODUCING VC++.NET

 

 

 

As stated earlier, the CDC class provides member functions for drawing. You have learned to use one of them, the Rectangle function. Similarly, the CDC class provides member functions to draw a line and an ellipse, described here:

MoveTo and LineTo. The MoveTo function accepts two parameters that are the starting coordinates of the line, x1 and y1, and moves the cursor to the specified starting point. The LineTo function also accepts two parameters that are the ending points of the line, x2 and y2, and draws a line from coordinates x1, y1 to x2, y2. The following is the syntax of these two functions:

CPoint MoveTo(int x1, int y1);

BOOL LineTo(int x2, int y2);

Ellipse. The Ellipse function, similar to the Rectangle function, accepts four parameters, x1, y1, x2, and y2. Here, x1 and y1 represent the upper-left corner of the ellipse, and x2 and y2 represent the lower-right corner of the ellipse.

Next, you will learn more about the GDI objects. You will learn about the Create method and about the different styles of pens and brushes that you can create. Following is a description of the alternate methods to create a pen and a brush.

CreatePen. You earlier learned to create a pen using the constructor. Instead, if required, you can also create pens using the CreatePen method of the CPen class. The fogr\ging is the syntax for the CreatePen method:

BOOL CreatePen(int nPenStyle, int nWidth, COLORREF crColor

);

In the preceding syntax, nPenStyle represents the style of the pen, such as a solid pen or a dotted pen. Table 2-2 describes the different styles of pens that can be created. nWidth represents the thickness of the pen, and crColor represents the color of the pen. Table 2-3 lists some of the common colors used. Functions accept colors as 32-bit COLORREF parameters, such as the crColor parameter of the CreatePen function. Therefore, you need to use the RGB macro that accepts the BYTE values for the colors as parameters and returns a COLORREF value representing the final color. The syntax of the COLORREF method is as follows:

COLORREF RGB(BYTE byRed, BYTE byGreen, BYTE byBlue );

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

45

 

 

 

 

 

CreateSolidBrush and CreateHatchBrush. You learned to create a brush using the constructor. Instead, if required, you can also create

brushes using the CreateSolidBrush and CreateHatchBrush methods of

the CBrush class. The following is the syntax for these methods:

BOOL CreateSolidBrush(COLORREF crColor );

BOOL CreateHatchBrush(int nIndex, COLORREF crColor );

Table 2-2 Styles of Pen

Style

Used to Create

PS_SOLID

PS_DASH

PS_DOT PS_DASHDOT PS_DASHDOTDOT PS_NULL

A solid pen

A dashed pen

A dotted pen

A pen with alternate dashes and dots

A pen with alternate dashes and double dots An invisible pen

NOTE

Except for the PS_SOLID style, all other styles can be used only if the width of the pen is set to 1 or less.

Table 2-3

List of Common Colors

 

Red

Green

Blue

Resultant Color

 

 

 

 

255

255

255

White

0

0

0

Black

255

0

0

Red

0

255

0

Green

0

0

255

Blue

255

255

0

Yellow

 

 

 

 

46

Part I

INTRODUCING VC++.NET

 

 

 

The following is a code snippet of an application that illustrates the use of the Ellipse, CreatePen, and CreateHatchBrush methods. The functionality of this application is that when the user clicks the left button, an ellipse with a red outline and black filling is created.

# include <afxwin.h>

class MyWindow:public CFrameWnd

{

public:

CPen *newPen; //declared a pointer of the CPen class

CBrush *newBrush; //declared a pointer of the CBrush class

MyWindow()

{

Create(NULL,”Example Window”);

}

void OnLButtonDown(UINT flag, CPoint point)

{

CClientDC dc(this); //creating a device context, dc, for the client area

newPen=new CPen; newPen->CreatePen(PS_DASHDOT,1,RGB(255,0,0));

//creating a pen with alternate dashes and dots

dc.SelectObject(newPen);

//attaching the pen to the dc

newBrush=new CBrush; newBrush->CreateHatchBrush(HS_CROSS,RGB(0,0,0));

//creating a hatch brush

dc.SelectObject(newBrush); //attaching the brush to the dc

dc.Ellipse(100,100,500,500);

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

47

 

 

 

 

 

//drawing an ellipse with a red dashdot pen and a black hatched brush

}

DECLARE_MESSAGE_MAP() };

BEGIN_MESSAGE_MAP(MyWindow, CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

class MyApp:public CWinApp

{

public:

BOOL InitInstance()

{

MyWindow *MyWindowObject = new MyWindow; m_pMainWnd=MyWindowObject; MyWindowObject->ShowWindow(SW_SHOWNORMAL); return TRUE;

}

};

MyApp app;

You can edit the previous sample applications that you created to add this functionality. The output of the application is displayed in Figure 2-12.

48

Part I

INTRODUCING VC++.NET

 

 

 

FIGURE 2-12 Sample drawing application using CreatePen and CreateHatchBrush functions

Summary

This chapter introduced you to the basics of MFC programming. It started with an overview of programming in Visual C++ .NET and then proceeded to discuss the basics of programming in MFC. You learned about the differences in DOS and Windows programming. The chapter then dealt with the two main highlighting features of Windows programming — event handling and device-inde- pendent programming. You learned how to handle events using various MFC classes, the main two being CWinApp and CFrameWnd. Besides this, you also learned to work with device contexts and the GDI objects. To summarize, you have learned the basics of MFC programming in this chapter. The chapters following will give you an in-depth knowledge of MFC programming.

Chapter 3

Creating a User

Interface