MFC small note: simple drawing

First, demand

This article describes some simple drawing performance function.

Second, the interface

The main interface is a dialog with minimize, maximize, close the other functions. MFC rationale no longer introduced.

Third, the function

Drawing a line

definition:

enum MYCOLOR
{
    WHITE = 0,
    GRAY = 1,
    LBLUE = 2,
    BLUE = 3,
};

CPen m_pen[5];

Initialization brush:

    DWORD dwBackColor = GetSysColor(CTLCOLOR_DLG);
    byte r = (dwBackColor >> 16) & 0xff;
    byte g = (dwBackColor >> 8) & 0xff;
    byte b = (dwBackColor >> 0) & 0xff;
    // 参数:样式、宽度、颜色
    m_pen[WHITE].CreatePen(PS_SOLID, 2, RGB(r, g, b));     // 背景色
    m_pen[GRAY].CreatePen(PS_SOLID, 2, RGB(99, 69, 96));     // 灰色
    m_pen[LBLUE].CreatePen(PS_SOLID, 2, RGB(51, 102, 205));     // 浅蓝
    m_pen[BLUE].CreatePen(PS_SOLID, 2, RGB(0, 0, 205));      // 蓝色 注:使用205,深一些

Drawing a line function:

void CFoobar::Line(CDC *pDC, MYCOLOR color, int x1, int y1, int x2, int y2)
{
    pDC->MoveTo(x1, y1);
    pDC->LineTo(x2, y2);
//    Sleep(500);
}

Draw a cross and four-sided:


void CFoobar::DrawCross(CDC *pDC, int x, int y, MYCOLOR color, bool bDraw)
{
    CPen *oldPen = pDC->SelectObject(&m_pen[GRAY]);//保存DC原始画笔
    int radius = 22;
    int basegap = 4;
    int rectradius = radius- basegap;
    int rectgap = 10;
    
    if (bDraw)
    {
        pDC->SelectObject(&m_pen[color]);
    }
    else
    {
        pDC->SelectObject(&m_pen[WHITE]);
    }
    // 十字形,上、下、左、右
    Line(pDC, color, x - radius, y, x - basegap, y);
    Line(pDC, color, x + basegap, y, x + radius, y);
    Line(pDC, color, x, y - radius, x, y - basegap);
    Line(pDC, color, x, y + basegap, x, y + radius);

    // 四边角,逆时针画
    Line(pDC, color, x - rectgap, y - rectradius, x - rectradius, y - rectradius);
    Line(pDC, color, x - rectradius, y - rectradius, x - rectradius, y - rectgap);
    Line(pDC, color, x - rectradius, y + rectgap, x - rectradius, y + rectradius);
    Line(pDC, color, x - rectradius, y + rectradius, x - rectgap, y + rectradius);
    Line(pDC, color, x + rectgap, y + rectradius, x + rectradius, y + rectradius);
    Line(pDC, color, x + rectradius, y + rectradius, x + rectradius, y + rectgap);
    Line(pDC, color, x + rectradius, y - rectgap, x + rectradius, y - rectradius);
    Line(pDC, color, x + rectradius, y - rectradius, x + rectgap, y - rectradius);

    pDC->SelectObject(oldPen);       //回复DC原画笔
}

Note that, let's draw the line "disappear" There are many ways, this article uses the background color painting once again, in order to achieve the "disappearance" purposes. Gets the background color to use GetSysColor(CTLCOLOR_DLG)to get. CTLCOLOR_DLG As dialog box controls, other controls have CTLCOLOR_BTN, CTLCOLOR_EDIT and so on.
DrawCross function effects See address: https://github.com/libts/tslib. I used this library a decade ago, and now has been updated.

Published 481 original articles · won praise 244 · Views 1.1 million +

Guess you like

Origin blog.csdn.net/subfate/article/details/103651184