mfc_Paint_4 - 8BitsCoding/RobotMentor GitHub Wiki

์ „์ฒด ์ง€์šฐ๊ธฐ, ์„  ํฌ๊ธฐ, ์„  ์ข…๋ฅ˜, ์„  ์ƒ‰์ƒ ์ฑ„์šฐ๊ธฐ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด ๋ณด์ž

์ „์ฒด ์ง€์šฐ๊ธฐ ๊ธฐ๋Šฅ ์ถ”๊ฐ€

// OnBnClickClear
ClearImage();
// ClearImage
CRect r;
GetClientRect(r);

m_image_dc.FillSolidRect(r, RGB(255, 255, 255));
Invalidate();

ํŽœ์˜ ์ƒ‰์ƒ, ๋‘๊ป˜, ์ข…๋ฅ˜๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž

// DrawWnd.h
COLORREF m_pen_color;
int m_pen_width, m_pen_style;

// ...

void SetPenWidth(int a_width);
void SetPenColor(COLORREF a_color);
// DrawWnd()
m_pen_color = RGB(0, 0, 0);
m_pen_width = 1;
m_pen_style = PS_SOLID;

m_my_pen.CreatePen(m_pen_style, m_pen_width, m_pen_color);
// ChangePen
m_my_pen.DeleteObject();
m_my_pen.CreatePen(m_pen_style, m_pen_width, m_pen_color);

m_image_dc.SelectObject(&m_my_pen);
m_temp_dc.SelectObject(&m_my_pen);
// DrawWnd.cpp
// SetPenColor
m_pen_color = a_color;
ChangePen();
// SetPenWidth
m_pen_width = a_width;
ChangePen();
// SetPenStyle
m_pen_style = a_style;
ChangePen();

์„  ๋‘๊ป˜๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ๋ฐ•์Šค ์ƒ์„ฑ

// Dlg.h
class MyWidthList : public TH_ListBox
{
public:
    void DrawUserItem(CDC * ap_dc, RECT * ap_rect, int a_index, void * ap_data, unsigned char a_select_flag, unsigned char a_focus_flag)
    {
        CString str;
        str.Format(L"%d", a_index + 1);
        ap_dc->SetTextColor(RGB(255, 200, 0));
        ap_dc->DrawText(str, ap_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
}
// Dlg.h
private:
    MyWidthList m_width_list;
// OnInitDlg
// ...
m_width_list.SubclassDlgItem(IDC_WIDTH_LIST, this);
m_width_list.SetColumnWidth(25);
m_width_list.SetItemHeight(0, 25);

for(int i = 0; i < 2=10; i++) {
    m_width_list.InsertSTring(i, L"");
}
m_width_list.SetCurSel(0);
// OnLbnSelchangeWidthList
int index = m_width_list.GetCurSel();
if(LB_ERR != index) {
    m_draw_wnd.SetPenWidth(index + 1);
}

์ด๋ฏธ์ง€


ํŽœ์˜ ์ข…๋ฅ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž

// Dlg.h
class MyStyleList : public TH_ListBox
{
private:
    CPen m_style_pen[5];

public:
    MyStyleList()
    {
        for(int i = 0; i < 5; i++) {
            m_style_pen[i].CreatePen(i, 1, RGB(255, 200, 0));
        }
    }

    virtual ~MyStyleList()
    {
        for(int i = 0; i < 5; i++) {
            m_style_pen[i].DeleteObject();
        }
    }

    void DrawUserItem(CDC * ap_dc, RECT * ap_rect, int a_index, void * ap_data, unsigned char a_select_flag, unsigned char a_focus_flag)
    {
        CPen * p_old_pen = ap_dc->SelectObject(&m_style_pen[a_index]);

        ap_dc->MoveTo(ap_rect->left + 2, ap_rect->top + (ap_rect->top + ap_rect->bottom) / 2);
        ap_dc->LineTo(ap_rect->right + 2, ap_rect->top + (ap_rect->top + ap_rect->bottom) / 2);

        ap_dc->SelectObject(p_old_pen);
    }
}
// Dlg.h
private:
    MyStyleList m_style_list;
// OnInitDlg
m_style_list.SubclassDlgItem(IDC_STYLE_LIST, this);
m_style_list.SetItemHeight(0, 25);

for(int i = 0; i < 5; i++) {
    m_style_list.InsertString(i, L"");
}
m_style_list.SetCurSel(0);
// OnLbnSelectchangeStyleList
int index = m_style_list.GetCurSel();
if(LB_ERR != index) {
    m_draw_wnd.SetPenStyle(index);
}

์ด๋ฏธ์ง€


๋ฌธ์ œ์  ๋ฐœ์ƒ!

์„ ์ด ์–‡์„๋•Œ๋Š” ์ƒ๊ด€์—†๋Š”๋ฐ ๊ตต์–ด์ง€๋ฉด ์„  ์ข…๋ฅ˜๊ฐ€ ์•ˆ๋จน๋Š”๋‹ค!

์ด๋ฏธ์ง€

// ChangePen
m_my_pen.DeleteObject();

LOGBRUSH lb;
lb.lbStyle = BS_SOLID;
lb.lbColor = m_pen_color;
lb.lbHatch = 0;

m_my_pen.CreatePen(PS_GEOMETRIC | m_pen_style, m_pen_width, &lb, 0, 0);

m_image_dc.SelectObject(&m_my_pen);
m_temp_dc.SelectObject(&m_my_pen);

์ด๋ฏธ์ง€


๋„ํ˜• ๋‚ด๋ถ€ ์ฑ„์šฐ๊ธฐ ๊ตฌํ˜„

์šฐ์„  ๋‚ด๋ถ€์— ์ฑ„์šฐ๊ธฐ ์—†์Œ ๋ถ€ํ„ฐ ๊ตฌํ˜„

// OnInitDialog()

// ...

COLORREF color_table[21] = { // ์ƒ‰์ƒ 
    , 0xFFFFFFFF}

// ...
// Dlg.h
class MyColorList : public TH_ListBox
{
public:
    void DrawUserItem(CDC * ap_dc, RECT * ap_rect, int a_index, void * ap_data, unsigned char a_select_flag, unsigned char a_focus_flag)
    {
        COLORREF color = GetItemData(a_index);
        if(color == 0xFFFFFFFF) {
            ap_dc->SetTextColor(RGB(255, 200, 0));
            ap_dc->DrawText(L"[X]", 3, ap_rect, DT_DENTER | DT_VCENTER | DT_SINGLELNE);
        } else {
            CRect r(ap_rect->left + 2, ap_rect->top + 2, ap_rect->right - 2, ap_rect->bottom - 2);
            ap_dc->FillSolidRect(r, color);
        }
    }
}

์ด๋ฏธ์ง€