From 665ca5cb5e03a2dbf9a1e0fb21b874a4c944f4de Mon Sep 17 00:00:00 2001 From: Ha-DongYeon <113656330+Ha-DongYeon@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:26:12 +0900 Subject: [PATCH 1/3] Update ChildView.cpp --- ChildView.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 3 deletions(-) diff --git a/ChildView.cpp b/ChildView.cpp index 2e3f841..855dc6b 100644 --- a/ChildView.cpp +++ b/ChildView.cpp @@ -17,6 +17,7 @@ #define new DEBUG_NEW #endif + namespace { CString GetSystemTimeAndDate() { @@ -207,6 +208,7 @@ BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) return TRUE; } +/* void CChildView::OnPaint() { CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다. @@ -227,7 +229,30 @@ void CChildView::OnPaint() dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); } +*/ + +void CChildView::OnPaint() +{ + CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다. + + // Rought double buffering + CRect rect; + GetClientRect(&rect); + + CDC memDC; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap; + bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); + memDC.SelectObject(&bitmap); + memDC.Rectangle(0, 0, rect.Width(), rect.Height()); + + OnMyPaint(&memDC); + + dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, + 0, 0, SRCCOPY); +} +/* afx_msg void CChildView::OnMyPaint(CDC* dc) { // 현재 시간 표시 dc->TextOutW(10, 10, m_current_time); @@ -256,6 +281,45 @@ afx_msg void CChildView::OnMyPaint(CDC* dc) { // 폴리곤 그리기 Polygon(dc, {{300, 100}, {300, 50}, {250, 75}, {250, 100}}, RGB(255, 0, 255)); } +*/ + + + +afx_msg void CChildView::OnMyPaint(CDC* dc) { + // 현재 시간 표시 + dc->TextOutW(10, 10, m_current_time); + + // 마우스 위치 표시 + std::string pos = + "(" + std::to_string(m_mouse_pos.x) + ", " + + std::to_string(m_mouse_pos.y) + ")"; + dc->TextOut(10, 30, CString(pos.c_str())); + + // 마우스 이벤트 표시 + dc->TextOut(10, 50, _T("Event: ") + m_mouse_event); + + + dc->TextOut(10, 70, _T("Keyboard: ") + CString(std::to_string(m_keyboard).c_str())); + + + if (m_toolbar_mode == kToolbarDrawCircle) { + // 튀기는 공 그리기 + Circle(dc, m_ball_pos, m_ball_radius, RGB(0, 255, 255)); + } + + + Rectangle(dc, m_wall_rect, RGB(255, 255, 0)); + + + + + // 직선 그리기 + Line(dc, { 100, 100 }, { 200, 300 }); + + // 폴리곤 그리기 + Polygon(dc, { {300, 100}, {300, 50}, {250, 75}, {250, 100} }, RGB(255, 0, 255)); + +} void CChildView::OnContextMenu(CWnd* pWnd, CPoint point) { @@ -316,6 +380,16 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; + CRect rect; + GetClientRect(&rect); + m_ball_pos.x = rect.Width() / 2; + m_ball_pos.y = rect.Height() / 2; + + m_wall_rect.left = 0; + m_wall_rect.right = 0; + m_wall_rect.top = 0; + m_wall_rect.bottom = 0; + // 시계 타이머 설정(함수 호출 주기 설정) SetTimer(kTimerClock, /* ms */ 1000, nullptr); m_timer_event_listeners.Add(kTimerClock, [this](){ m_current_time = GetSystemTimeAndDate(); }); @@ -327,6 +401,7 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) // 마우스 이동 이벤트 리스너 추가 m_mouse_event_listeners.Add(kMouseMove, [this](auto, auto p) { m_mouse_pos = p; }); + /* m_mouse_event_listeners.Add(kMouseMove, [this](auto, auto p) { auto w = m_wall_rect.Width(); auto h = m_wall_rect.Height(); @@ -335,6 +410,31 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) m_wall_rect.top = p.y - h / 2; m_wall_rect.bottom = p.y + h / 2; }); + */ + + m_mouse_event_listeners.Add(kMouseLButtonDown, [this](auto, auto p) { + if (m_toolbar_mode == kToolbarDrawRectangle) { + auto w = m_wall_rect.Width(); + auto h = m_wall_rect.Height(); + m_wall_rect.left = p.x; + m_wall_rect.right = p.x; + m_wall_rect.top = p.y; + m_wall_rect.bottom = p.y; + } + + }); + + m_mouse_event_listeners.Add(kMouseLButtonUp, [this](auto, auto p) { + if (m_toolbar_mode == kToolbarDrawRectangle) { + auto w = m_wall_rect.Width(); + auto h = m_wall_rect.Height(); + // m_wall_rect.left = p.x - w / 2; + m_wall_rect.right = p.x; + // m_wall_rect.top = p.y - h / 2; + m_wall_rect.bottom = p.y; + } + }); + // 마우스 클릭 이벤트 리스너 m_mouse_event_listeners.Add(kMouseLButtonDown, [this](auto, auto p) { m_mouse_event = "LButtonDown"; }); @@ -351,8 +451,6 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) // 우클릭 시 그리기 모드 취소 m_mouse_event_listeners.Add(kMouseRButtonDown, [this](auto, auto p) { m_toolbar_mode = kToolbarNone; }); - // ESC 시 그리기 모드 취소 - m_keyboard_listeners.Add(VK_ESCAPE, [this](...) { m_toolbar_mode = kToolbarNone; }); return 0; } @@ -426,8 +524,13 @@ void CChildView::OnMButtonDblClk(UINT nFlags, CPoint point) { void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { - m_keyboard_listeners(nChar, nRepCnt, nFlags); m_keyboard = nChar; + m_toolbar_mode = kToolbarNone; + switch (nChar) { + case VK_ESCAPE: + m_toolbar_mode = kToolbarNone; + break; + } CWnd::OnKeyDown(nChar, nRepCnt, nFlags); } From 6f953168726015829acb95666f978f98f6276fc3 Mon Sep 17 00:00:00 2001 From: Ha-DongYeon <113656330+Ha-DongYeon@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:40:25 +0900 Subject: [PATCH 2/3] Update ChildView.cpp --- ChildView.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/ChildView.cpp b/ChildView.cpp index 855dc6b..a2e34cf 100644 --- a/ChildView.cpp +++ b/ChildView.cpp @@ -18,6 +18,11 @@ #endif +std::vector rect_arr; +int rect_cnt = 0; + + + namespace { CString GetSystemTimeAndDate() { @@ -307,8 +312,10 @@ afx_msg void CChildView::OnMyPaint(CDC* dc) { Circle(dc, m_ball_pos, m_ball_radius, RGB(0, 255, 255)); } - - Rectangle(dc, m_wall_rect, RGB(255, 255, 0)); + for (auto& a : rect_arr) { + Rectangle(dc, a, RGB(255, 255, 0)); + } + //Rectangle(dc, m_wall_rect, RGB(255, 255, 0)); @@ -414,24 +421,23 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) m_mouse_event_listeners.Add(kMouseLButtonDown, [this](auto, auto p) { if (m_toolbar_mode == kToolbarDrawRectangle) { - auto w = m_wall_rect.Width(); - auto h = m_wall_rect.Height(); - m_wall_rect.left = p.x; - m_wall_rect.right = p.x; - m_wall_rect.top = p.y; - m_wall_rect.bottom = p.y; + rect_arr.push_back(CRect()); + rect_arr[rect_cnt].left = p.x; + rect_arr[rect_cnt].right = p.x; + rect_arr[rect_cnt].top = p.y; + rect_arr[rect_cnt].bottom = p.y; } }); m_mouse_event_listeners.Add(kMouseLButtonUp, [this](auto, auto p) { if (m_toolbar_mode == kToolbarDrawRectangle) { - auto w = m_wall_rect.Width(); - auto h = m_wall_rect.Height(); + // m_wall_rect.left = p.x - w / 2; - m_wall_rect.right = p.x; + rect_arr[rect_cnt].right = p.x; // m_wall_rect.top = p.y - h / 2; - m_wall_rect.bottom = p.y; + rect_arr[rect_cnt].bottom = p.y; + rect_cnt += 1; } }); From d71a93dd85ab570215b8577f7acb22f7c432fc0c Mon Sep 17 00:00:00 2001 From: obsidian0402 Date: Tue, 19 Dec 2023 17:33:26 +0900 Subject: [PATCH 3/3] ver1 --- ChildView.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++------- ChildView.h | 14 ++++++++ 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/ChildView.cpp b/ChildView.cpp index a2e34cf..b194721 100644 --- a/ChildView.cpp +++ b/ChildView.cpp @@ -6,23 +6,28 @@ #include "MenuAndControls.h" #include "ChildView.h" + #include #include #include #include #include #include +#include #ifdef _DEBUG #define new DEBUG_NEW #endif +<<<<<<< Updated upstream std::vector rect_arr; int rect_cnt = 0; +======= +>>>>>>> Stashed changes namespace { CString GetSystemTimeAndDate() { @@ -32,8 +37,6 @@ CString GetSystemTimeAndDate() { /* \brief: 원을 그립니다 - - \dc: 디바이스 컨텍스트 \center: 원의 중심 좌표 \radius: 원의 반지름 @@ -42,8 +45,8 @@ CString GetSystemTimeAndDate() { \color_line: 테두리의 색 */ void Circle(CDC* dc, - CPoint center, int radius, COLORREF color, - int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) + CPoint center, int radius, COLORREF color, + int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) { // 새로운 펜 객체 사용 CPen pen(thickness <= 0 ? PS_NULL : PS_SOLID, thickness, color_line); @@ -64,8 +67,6 @@ void Circle(CDC* dc, /* \brief: 직사각형을 그립니다 - - \dc: 디바이스 컨텍스트 \rect: 직사각형의 좌표 \color: 도형의 내부 색 @@ -73,8 +74,8 @@ void Circle(CDC* dc, \color_line: 테두리의 색 */ void Rectangle(CDC* dc, - CRect rect, COLORREF color, - int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) { + CRect rect, COLORREF color, + int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) { CPen pen(thickness <= 0 ? PS_NULL : PS_SOLID, thickness, color_line); auto pen_prev = dc->SelectObject(&pen); @@ -159,6 +160,7 @@ void Polygon(CDC* dc, CChildView::CChildView() { + } CChildView::~CChildView() @@ -198,7 +200,7 @@ END_MESSAGE_MAP() -// CChildView 메시지 처리기 +// CChildView 메시지 ma BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) { @@ -233,6 +235,20 @@ void CChildView::OnPaint() dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); + + + + if (m_toolbar_mode == kToolbarDrawRectangle) { + // 직사각형 그리기 + CRect rect(m_ptRectStart, m_ptRectEnd); + dc.Rectangle(rect); + } + if (m_toolbar_mode == kToolbarDrawCircle) { + // 원 그리기 + int radius = static_cast(sqrt(pow(m_ptCirEnd.x - m_ptCirStart.x, 2) + pow(m_ptCirEnd.y - m_ptCirStart.y, 2))); + dc.Ellipse(m_ptCirStart.x - radius, m_ptCirStart.y - radius, + m_ptCirStart.x + radius, m_ptCirStart.y + radius); + } } */ @@ -271,7 +287,6 @@ afx_msg void CChildView::OnMyPaint(CDC* dc) { // 마우스 이벤트 표시 dc->TextOut(10, 50, _T("Event: ") + m_mouse_event); - dc->TextOut(10, 70, _T("Keyboard: ") + CString(std::to_string(m_keyboard).c_str())); // 튀기는 공 그리기 @@ -285,6 +300,16 @@ afx_msg void CChildView::OnMyPaint(CDC* dc) { // 폴리곤 그리기 Polygon(dc, {{300, 100}, {300, 50}, {250, 75}, {250, 100}}, RGB(255, 0, 255)); + + //마우스로 드래그해 사각형 그리기(문제1) + if (m_toolbar_mode == kToolbarDrawRectangle) { + //눌려져있을때 + + //뗏을때 + } + + + } */ @@ -442,6 +467,10 @@ int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) }); + /*m_mouse_event_listeners.Add(kMouseMove, [this](auto, auto p) { + m_ ? ? ? = p; + });*/ + // 마우스 클릭 이벤트 리스너 m_mouse_event_listeners.Add(kMouseLButtonDown, [this](auto, auto p) { m_mouse_event = "LButtonDown"; }); m_mouse_event_listeners.Add(kMouseLButtonUp, [this](auto, auto p) { m_mouse_event = "LButtonUp"; }); @@ -465,6 +494,19 @@ void CChildView::OnMouseMove(UINT nFlags, CPoint point) { m_mouse_event_listeners(kMouseMove, nFlags, point); CWnd::Invalidate(); CWnd::OnMouseMove(nFlags, point); + + if (m_toolbar_mode == kToolbarDrawRectangle && m_bIsDrawing) { + m_ptRectEnd = point; // 끝점 업데이트 + Invalidate(); // 화면을 다시 그립니다. + } + + if (m_toolbar_mode == kToolbarDrawRectangle && m_bIsDrawing) { + m_ptCirEnd = point; // 끝점 업데이트 + Invalidate(); // 화면을 다시 그립니다. + } + + CWnd::OnMouseMove(nFlags, point); + } void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { @@ -472,6 +514,19 @@ void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { CWnd::SetCapture(); CWnd::Invalidate(); CWnd::OnLButtonDown(nFlags, point); + + if (m_toolbar_mode == kToolbarDrawRectangle) { + m_bIsDrawing = true; // 그리기 시작 + m_ptRectStart = point; // 사각형 시작점 설정 + m_ptRectEnd = point; // 사각형 초기 끝점 설정 + } + CWnd::OnLButtonDown(nFlags, point); + + if (m_toolbar_mode == kToolbarDrawCircle) { + m_bIsDrawing = true; + m_ptCirStart = point; //원 시작점 설정 + m_ptCirEnd = point; //원 초기 끝점 설정 + } } void CChildView::OnLButtonUp(UINT nFlags, CPoint point) { @@ -479,6 +534,19 @@ void CChildView::OnLButtonUp(UINT nFlags, CPoint point) { ReleaseCapture(); CWnd::Invalidate(); CWnd::OnLButtonUp(nFlags, point); + + if (m_toolbar_mode == kToolbarDrawRectangle && m_bIsDrawing) { + m_bIsDrawing = false; // 그리기 완료 + m_ptRectEnd = point; // 최종 끝점 설정 + Invalidate(); // 최종 화면을 다시 그립니다. + } + + if (m_toolbar_mode == kToolbarDrawCircle && m_bIsDrawing) { + m_bIsDrawing = false; + m_ptCirEnd = point; + Invalidate(); + } + CWnd::OnLButtonUp(nFlags, point); } void CChildView::OnLButtonDblClk(UINT nFlags, CPoint point) { @@ -590,7 +658,6 @@ void CChildView::OnUpdateDrawCircle(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_toolbar_mode == kToolbarDrawCircle); } - BOOL CChildView::OnEraseBkgnd(CDC* pDC) { return TRUE; // return CWnd::OnEraseBkgnd(pDC); diff --git a/ChildView.h b/ChildView.h index 9dd46a7..0f31790 100644 --- a/ChildView.h +++ b/ChildView.h @@ -17,6 +17,13 @@ class CChildView : public CWnd { + bool m_bIsDrawing; // 드래그 중인지 여부 + CPoint m_ptRectStart; // 직사각형의 시작점 + CPoint m_ptRectEnd; // 직사각형의 끝점 + + CPoint m_ptCirStart; + CPoint m_ptCirEnd; + public: CChildView(); @@ -32,6 +39,8 @@ class CChildView : public CWnd ToolbarMode m_toolbar_mode = kToolbarNone; + + public: CPoint m_mouse_pos; CString m_mouse_event{"None"}; @@ -119,5 +128,10 @@ class CChildView : public CWnd EventListener m_keyboard_listeners; afx_msg BOOL OnEraseBkgnd(CDC* pDC); + + + + + };