Use of CDC in CView class (MFC)

Article directory

    • 1.Preliminary knowledge
    • 2. Experimental purpose
    • 3.Experimental content
    • 4. Code implementation
    • 5. Running results
    • 6. Summary
      • 1. Difficulties encountered in the experiment
        • How to set the color and width of the drawn curve
        • How to draw axes
        • How to graph a function
      • 2. Experience

1. Preliminary knowledge

MFC (Microsoft Foundation Classes) is a set of class libraries provided by Microsoft for creating Windows-based applications. In MFC, the CDC class is a very important drawing class, used to handle all drawing operations.

The CDC class represents a device context, which is a virtual device used for drawing. In Windows, all screen output occurs through the device context. The device context can be regarded as a canvas on which the application can draw various graphics, such as lines, rectangles, ellipses, text, etc.

The CDC object provides member functions that handle device contexts such as monitors or printers, and members that handle the display context corresponding to the client area of the window. The CDC class has many member functions that can be used to perform various drawing operations. For example:

  • CDC::TextOut(): Use the currently selected font to write a string at the specified position.
  • CDC::LineTo(): Draws a straight line from the current position to a point, but not including that point.
  • CDC::Arc(): Draw an elliptical arc.
  • CDC::PolylineTo(): Draw one or more straight lines and move the current position to the end of the last straight line.

In addition to drawing functions, the CDC class also has many operations related to the device context, such as setting the background color, setting the text color, setting the brush style, etc.

In MFC, when you create a window or control, MFC will create a CDC object for you and pass the pointer of the CDC object to you through the control’s message processing function. You can use this CDC object to draw various graphics.

2. Experiment purpose

Be familiar with the VS2010 development platform and master the use of CDC classes.

3.Experimental content

Automatically draw sinusoids using MFC (single document).
The sine curve is required to be black with a width of 2, and the coordinate system is red with a width of 1.

4. Code implementation

First of all, due to the need to use the sin function and the use of constants

π

\pi

π, add the following preparations in Ex2View.cpp:

#include <math.h>
// const double pi(52163.0 / 16604);
const double pi2(52163.0 / 33208);

Rewrite the OnDraw function of CEx2View as follows:

void CEx2View::OnDraw(CDC *pDC)
{<!-- -->
    CEx2Doc *pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // Generate image points of sin(x)
    short k(550);
    CPoint sin_x[550], *p(sin_x + k);
    do
    {<!-- -->
        (--p)->x = 50 + k;
        p->y = 200 - 70 * sin(k * pi2 / 50 - pi2);
    } while (--k);
    CPen axis(PS_SOLID, 1, RGB(255, 0, 0)), fun(PS_SOLID, 2, RGB(0, 0, 0));
    pDC->SelectObject(axis);
    pDC->SetTextColor(RGB(255, 0, 0));
    // draw x-axis
    const short s_1(1), end(11);
    CString str(TEXT("-pi/2"));
    pDC->TextOutW(40, 205, str);
    pDC->MoveTo(50, 195);
    pDC->LineTo(50, 200);
    pDC->LineTo(100, 200);
    str.Format(TEXT("O"));
    pDC->TextOutW(90, 205, str);
    pDC->MoveTo(100, 195);
    pDC->LineTo(100, 200);
    while ( + + k != end)
    {<!-- -->
        short pos(100 + 50 * k);
        pDC->LineTo(pos, 200);
        if (k & s_1)
        {<!-- -->
            str.Format(TEXT("%d/2*pi"), k);
            pDC->TextOutW(pos - 25, 205, str);
        }
        else if (k != 2)
        {<!-- -->
            str.Format(TEXT("%d*pi"), k >> 1);
            pDC->TextOutW(pos - 15, 205, str);
        }
        else
        {<!-- -->
            str.Format(TEXT("pi"));
            pDC->TextOutW(pos - 5, 205, str);
        }
        pDC->MoveTo(pos, 195);
        pDC->LineTo(pos, 200);
    }
    // draw y-axis
    pDC->MoveTo(105, 340);
    pDC->LineTo(100, 340);
    str.Format(TEXT("-2"));
    pDC->TextOutW(80, 330, str);
    pDC->LineTo(100, 60);
    pDC->MoveTo(100, 270);
    pDC->LineTo(105, 270);
    pDC->MoveTo(100, 140);
    pDC->LineTo(105, 140);
    pDC->MoveTo(100, 60);
    pDC->LineTo(105, 60);
    str.Format(TEXT("-1"));
    pDC->TextOutW(80, 260, str);
    str.Format(TEXT("1"));
    pDC->TextOutW(85, 130, str);
    str.Format(TEXT("2"));
    pDC->TextOutW(85, 50, str);
    // Draw function image
    pDC->SelectObject(fun);
    pDC->Polyline(p, 550);
}

5. Operation results

6. Summary

1. Difficulties encountered in the experiment

How to set the color and width of the drawn curve

At first, the corresponding API was not found in the documentation of the CDC class. After consulting the information, I learned that it needs to be set with the help of the CPen type. There are two setting methods. One is to initialize directly in the constructor. Here the first parameterized constructor is used:

CPen::CPen( int nPenStyle, int nWidth, COLORREF crColor );

Since the function image and coordinate axes are both solid lines, the PS_SOLID style is used. And nWidth and crColor can be filled in according to the experimental requirements. Another way is to use the CreatePen setting. The function is used as follows:

BOOL CPen::CreatePen( int nPenStyle, int nWidth, COLORREF cfColor );

The parameters are the same as those of the constructor, so no details are given.

How to draw coordinate axes

The CDC class does not provide an API for directly drawing the coordinate axes, so you can just draw them one by one.

How to draw a function graph

As long as the points are dense enough, you can approximately use a line chart instead of a function image, and just call Polyline to draw a line chart.

2. Experience

Through this experiment, I learned the basic method of using the CDC class for drawing in MFC. The CDC class is a very important drawing class that can implement various drawing operations, such as drawing lines, rectangles, ellipses, text, etc. In this experiment, I successfully drew the sine curve and coordinate system by calling the functions of the CDC class.

During the experiment, I encountered some difficulties, such as how to set the color and width of the plotted curve, how to plot the coordinate axis, and how to plot the function image. By consulting relevant information, I learned to use the CPen type to set the color and width of the curve, use the line drawing function of the CDC class to draw the coordinate axis, and use the Polyline function to draw function graphs.

Through this experiment, I also learned that in MFC, when drawing graphics, you can approximately use a line chart instead of a function image. As long as sufficiently dense points are obtained, an approximate drawing of the function image can be achieved by drawing a line graph.

Through this experiment, I not only mastered the method of using the CDC class for drawing, but also deepened my understanding of the MFC development platform. I will continue to explore and learn in practical applications.

Code address: https://github.com/zsc118/MFC-exercises