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);
}
}
}