class TH_MemDC {
private:
CDC m_mem_dc;
CBitmap m_mem_bmp;
int m_width = 0, m_height = 0;
public:
~TH_MemDC() {
if (m_mem_dc.m_hDC != NULL) {
m_mem_bmp.DeleteObject();
m_mem_dc.DeleteDC();
}
}
void Create(CWnd * ap_wnd, int a_width, int a_height) {
CClientDC dc(ap_wnd);
m_width = a_width;
m_height = a_height;
m_mem_dc.CreateCompatibleDC(&dc);
m_mem_bmp.CreateCompatibleBitmap(&dc, a_width, a_height);
m_mem_dc.SelectObject(&m_mem_bmp);
}
void Resize(CWnd * ap_wnd, int a_width, int a_height) {
if (a_width != m_width || a_height != m_height) {
if (m_width && m_height) {
m_mem_bmp.DeleteObject();
m_width = a_width;
m_height = a_height;
CClientDC dc(ap_wnd);
m_mem_bmp.CreateCompatibleBitmap(&dc, a_width, a_height);
m_mem_dc.SelectObject(&m_mem_bmp);
}
}
}
inline CDC * GetDC() {
return &m_mem_dc;
}
inline int GetWidth() {
return m_width;
}
inline int GetHeight() {
return m_height;
}
void Draw(CDC * ap_dc, int a_x, int a_y) {
ap_dc->BitBlt(0, 0, m_width, m_height, &m_mem_dc, 0, 0, SRCCOPY);
}
}; m_mem_dc.CreateCompatibleDC(&dc);
m_mem_bmp.CreateCompatibleBitmap(&dc, a_width, a_height);
m_mem_dc.SelectObject(&m_mem_bmp);
}
void Resize(CWnd * ap_wnd, int a_width, int a_height) {
m_mem_bmp.DeleteObject();
m_width = a_width;
m_height = a_height;
CClientDC dc(ap_wnd);
m_mem_bmp.CreateCompatibleBitmap(&dc, a_width, a_height);
m_mem_dc.SelectObject(&m_mem_bmp);
}
inline CDC * GetDC() {
return &m_mem_dc;
}
inline int GetWidth() {
return m_width;
}
inline int GetHeight() {
return m_height;
}
void Draw(CDC * ap_dc, int a_x, int a_y) {
ap_dc->BitBlt(0, 0, m_width, m_height, &m_mem_dc, 0, 0, SRCCOPY);
}
};
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// ์ด ๋ํ ์์์ ์์ด์ฝ์ ์ค์ ํฉ๋๋ค. ์์ฉ ํ๋ก๊ทธ๋จ์ ์ฃผ ์ฐฝ์ด ๋ํ ์์๊ฐ ์๋ ๊ฒฝ์ฐ์๋
// ํ๋ ์์ํฌ๊ฐ ์ด ์์
์ ์๋์ผ๋ก ์ํํฉ๋๋ค.
SetIcon(m_hIcon, TRUE); // ํฐ ์์ด์ฝ์ ์ค์ ํฉ๋๋ค.
SetIcon(m_hIcon, FALSE); // ์์ ์์ด์ฝ์ ์ค์ ํฉ๋๋ค.
// TODO: ์ฌ๊ธฐ์ ์ถ๊ฐ ์ด๊ธฐํ ์์
์ ์ถ๊ฐํฉ๋๋ค.
CRect r;
GetClientRect(r);
// MemDC Class๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
m_mem_dc.Create(this, r.Width(), r.Height());
srand((unsigned int)time(NULL));
for (int i = 0; i < MAX_COUNT; i++) {
m_list[i].x = rand() % m_mem_dc.GetWidth();
m_list[i].y = rand() % m_mem_dc.GetHeight();
m_list[i].r = rand() % 40 + 10; // 10 ~ 49
m_list[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
/*
// MemDC Class๋ฅผ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
m_height = r.Height();
m_width = r.Width();
CClientDC dc(this);
m_mem_dc.CreateCompatibleDC(&dc);
m_mem_bmp.CreateCompatibleBitmap(&dc, m_width, m_height);
m_mem_dc.SelectObject(&m_mem_bmp);
srand((unsigned int)time(NULL));
for (int i = 0; i < MAX_COUNT; i++) {
m_list[i].x = rand() % w;
m_list[i].y = rand() % h;
m_list[i].r = rand() % 40 + 10; // 10 ~ 49
m_list[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
*/
SetTimer(1, 50, NULL);
return TRUE; // ํฌ์ปค์ค๋ฅผ ์ปจํธ๋กค์ ์ค์ ํ์ง ์์ผ๋ฉด TRUE๋ฅผ ๋ฐํํฉ๋๋ค.
}
void CMFCApplication1Dlg::OnPaint()
{
CPaintDC dc(this); // ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ํ ๋๋ฐ์ด์ค ์ปจํ
์คํธ์
๋๋ค.
if (IsIconic())
{
// ...
}
else
{
// MemDC Class๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
m_mem_dc.Draw(&dc, 0, 0);
// MemDC Class๋ฅผ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
// dc.BitBlt(0, 0, m_width, m_height, &m_mem_dc, 0, 0, SRCCOPY);
// CDialogEx::OnPaint();
}
}
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1) {
CircleData * p = m_list;
// MemDC Class๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
// dc -> m_mem_dc
// CBrush fill_brush, *p_old_brush = dc.GetCurrentBrush();
CBrush fill_brush, *p_old_brush = m_mem_dc.GetDC()->GetCurrentBrush();
m_mem_dc.GetDC()->FillSolidRect(0, 0, m_mem_dc.GetWidth(), m_mem_dc.GetHeight(), RGB(220, 220, 220));
for (int i = 0; i < MAX_COUNT; i++) {
p->r--;
if (p->r <= 0) {
p->x = rand() % m_mem_dc.GetWidth();
p->y = rand() % m_mem_dc.GetHeight();
p->r = rand() % 40 + 10; // 10 ~ 49
p->color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
fill_brush.CreateSolidBrush(p->color);
// dc.SelectObject(&fill_brush);
m_mem_dc.GetDC()->SelectObject(&fill_brush);
// dc.Ellipse(p->x - p->r, p->y - p->r, p->x + p->r, p->y + p->r);
m_mem_dc.GetDC()->Ellipse(p->x - p->r, p->y - p->r, p->x + p->r, p->y + p->r);
fill_brush.DeleteObject();
p++;
}
m_mem_dc.GetDC()->SelectObject(p_old_brush);
/*
// MemDC Class๋ฅผ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
// dc -> m_mem_dc
// CBrush fill_brush, *p_old_brush = dc.GetCurrentBrush();
CBrush fill_brush, *p_old_brush = m_mem_dc.GetCurrentBrush();
m_mem_dc.FillSolidRect(0, 0, m_width, m_height, RGB(220, 220, 220));
for (int i = 0; i < MAX_COUNT; i++) {
p->r--;
if (p->r <= 0) {
p->x = rand() % m_width;
p->y = rand() % m_height;
p->r = rand() % 40 + 10; // 10 ~ 49
p->color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
fill_brush.CreateSolidBrush(p->color);
// dc.SelectObject(&fill_brush);
m_mem_dc.SelectObject(&fill_brush);
// dc.Ellipse(p->x - p->r, p->y - p->r, p->x + p->r, p->y + p->r);
m_mem_dc.Ellipse(p->x - p->r, p->y - p->r, p->x + p->r, p->y + p->r);
fill_brush.DeleteObject();
p++;
}
m_mem_dc.SelectObject(p_old_brush);
*/
Invalidate(FALSE);
}
else {
CDialogEx::OnTimer(nIDEvent);
}
}
void CMFCApplication1Dlg::OnDestroy()
{
CDialogEx::OnDestroy();
KillTimer(1);
// MemDC๋ฅผ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
// m_mem_bmp.DeleteObject();
// m_mem_dc.DeleteDC();
}
void CMFCApplication1Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// MemDC๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
m_mem_dc.Resize(this, cx, cy);
/*
// MemDC๋ฅผ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
if (cx != m_width || cy != m_height) {
if (m_width && m_height) {
m_mem_bmp.DeleteObject();
m_width = cx;
m_height = cy;
CClientDC dc(this);
m_mem_bmp.CreateCompatibleBitmap(&dc, m_width, m_height);
m_mem_dc.SelectObject(&m_mem_bmp);
}
}
*/
}