Erase Window
-
// MainFrame.cpp #include "MainFrame.h" #include // for __min() "2 underscores" #define ID_SB 210 // All child windows require an // identifier. Not used here. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) // message calls this function ON_WM_PAINT() // void OnPaint() ON_WM_LBUTTONDOWN() // void OnLButtonDown(...) ON_WM_RBUTTONDOWN() //Left Button Down Add************ ON_WM_SIZE() // void OnSize(...) ON_WM_CHAR() // void OnChar(...) END_MESSAGE_MAP() CMainFrame::CMainFrame() // Constructor { // Step 1: Register a window class if wish // Step 2: Create the window Create(NULL, "Ex06a_StatusBar & Shapes", WS_OVERLAPPEDWINDOW, CRect(0,0,360,250)); // Step 3: Create a status bar myStatusBar.Create (WS_CHILD | WS_VISIBLE, CRect(1,2, 1,2), this, ID_SB); int rtEdges[] = {45,140,250}; // Right edge of panes myStatusBar.SetParts (3,rtEdges); // Step 4: Create Pens, Brushes, Fonts, etc penReg.CreatePen (PS_SOLID, 10, RGB(0,0,255)); // Step 5: Initialize application variables nPts = 0; ShapeKind = 'L'; CenterWindow(); }; void CMainFrame::OnPaint() { CPaintDC dc (this); dc.TextOut (1,1,"Click left button repeatedly. Press L, B, P"); CString s; s.Format("# pts:%d", nPts); myStatusBar.SetText (s,0,0); s.Format ("Shape(L,B,P): %c", ShapeKind); myStatusBar.SetText(s,2,SBT_POPOUT); if (nPts <= 1) return; dc.SelectObject (&penReg); switch (ShapeKind) { case 'L': dc.Polyline (ShapePts, nPts); break; case 'P': dc.SelectStockObject(LTGRAY_BRUSH); dc.Polygon (ShapePts,nPts); break; case 'B': if ((nPts - 4) % 3 == 0) dc.PolyBezier(ShapePts,nPts); else { // use max legal # of pts int n = (nPts - 4)/3; int NPts = __min(n*3 + 4, nPts); dc.PolyBezier(ShapePts,NPts); // then draw polyline dc.SelectStockObject (BLACK_PEN); dc.Polyline (ShapePts,nPts); } break; default: break; } } void CMainFrame::OnLButtonDown (UINT nFlags, CPoint pt) { if (nPts >= 99) // array full return; else { ShapePts[nPts] = pt; nPts++; CString s; s.Format("Last pt:(%d, %d)",pt.x, pt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS);
-
// MainFrame.cpp #include "MainFrame.h" #include // for __min() "2 underscores" #define ID_SB 210 // All child windows require an // identifier. Not used here. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) // message calls this function ON_WM_PAINT() // void OnPaint() ON_WM_LBUTTONDOWN() // void OnLButtonDown(...) ON_WM_RBUTTONDOWN() //Left Button Down Add************ ON_WM_SIZE() // void OnSize(...) ON_WM_CHAR() // void OnChar(...) END_MESSAGE_MAP() CMainFrame::CMainFrame() // Constructor { // Step 1: Register a window class if wish // Step 2: Create the window Create(NULL, "Ex06a_StatusBar & Shapes", WS_OVERLAPPEDWINDOW, CRect(0,0,360,250)); // Step 3: Create a status bar myStatusBar.Create (WS_CHILD | WS_VISIBLE, CRect(1,2, 1,2), this, ID_SB); int rtEdges[] = {45,140,250}; // Right edge of panes myStatusBar.SetParts (3,rtEdges); // Step 4: Create Pens, Brushes, Fonts, etc penReg.CreatePen (PS_SOLID, 10, RGB(0,0,255)); // Step 5: Initialize application variables nPts = 0; ShapeKind = 'L'; CenterWindow(); }; void CMainFrame::OnPaint() { CPaintDC dc (this); dc.TextOut (1,1,"Click left button repeatedly. Press L, B, P"); CString s; s.Format("# pts:%d", nPts); myStatusBar.SetText (s,0,0); s.Format ("Shape(L,B,P): %c", ShapeKind); myStatusBar.SetText(s,2,SBT_POPOUT); if (nPts <= 1) return; dc.SelectObject (&penReg); switch (ShapeKind) { case 'L': dc.Polyline (ShapePts, nPts); break; case 'P': dc.SelectStockObject(LTGRAY_BRUSH); dc.Polygon (ShapePts,nPts); break; case 'B': if ((nPts - 4) % 3 == 0) dc.PolyBezier(ShapePts,nPts); else { // use max legal # of pts int n = (nPts - 4)/3; int NPts = __min(n*3 + 4, nPts); dc.PolyBezier(ShapePts,NPts); // then draw polyline dc.SelectStockObject (BLACK_PEN); dc.Polyline (ShapePts,nPts); } break; default: break; } } void CMainFrame::OnLButtonDown (UINT nFlags, CPoint pt) { if (nPts >= 99) // array full return; else { ShapePts[nPts] = pt; nPts++; CString s; s.Format("Last pt:(%d, %d)",pt.x, pt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS);
It will repaint the window. If you want to erase the current shape, you need to change ShapeKind first. Christian Graus - Microsoft MVP - C++