[ Home | Syllabus | Course Notes | Assignments | Search]
So far we have seen three simple programs which basically write some text to the screen:
They differ in the way the write to the screen and the way they route messages to the code that writes to the screen.
Let's look at "painting the screen" first
hdc = BeginPaint (hwnd, &ps) ; // get device context associated with window receiving message GetClientRect (hwnd, &rect) ; DrawText (hdc, "Hello, Windows 95!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ; // return device context
void CMainWindow::OnPaint ( ) { CPaintDC dc (this); // get a device context for "this" window CRect rect; GetClientRect (&rect); // find out how big the window is // compare to above for difference between functional and object mechanisms. dc.DrawText ("Hello, MFC", // Draw this Text -1, // which is terminated by the null character &rect, // in this rectangle DT_SINGLELINE | DT_CENTER | DT_VCENTER); // on a single line in the center } // device context returned in destructor for "dc"
void CEx03aView::OnDraw(CDC* pDC) // device context passed in to View object { CRect rect; GetClientRect(&rect); pDC->DrawText("Just Do It", &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); }
Win 32 "C" program: WinProc "switch" statement routes the message to the drawing code
MFC - see previous notes on message routing using Message Maps
Document/View: see below
DESCRIPTION
OBJECTIVES
CREATING THE DEMO
void CRandColorView::OnMouseMove(UINT nFlags, CPoint point) { if (m_circle.PtInRect(point)) {// if inside circle if(!m_center.PtInRect(point)) { // but not center m_color = RGB(rand()%256,rand()%256,rand()%256); // randomly choose a 24 bit color value InvalidateRect(m_circle); // force a redraw } } }
void CRandColorView::OnDraw(CDC* pDC) { CBrush myBrush(m_color); // pick a colored brush pDC->SelectObject(myBrush); pDC->Ellipse(m_circle); // draw colored circle pDC->SelectStockObject(BLACK_BRUSH); pDC->Ellipse(m_center); // draw black center }
CRandColorView::CRandColorView(): m_circle(0,0,200,200), m_center(75,75,125,125) // use constructor initializer to call non-default constructors of CRect { m_color = RGB(0,255,0); // start with green - see table page 83 srand(NULL); // initialize random number sequence }
Full source code here.
Example ex5c demostrates several interesting features of windows program.
First look at three message handlers for the mouse - which work together.
(NOTE: the hand points to places where footnotes provide
explanations)
void CEx05cView::OnLButtonDown(UINT nFlags, CPoint point) { CRect rectEllipse(m_pointTopLeft, m_sizeEllipse); // still logical CRgn circle; CClientDC dc(this); OnPrepareDC(&dc); dc.LPtoDP(rectEllipse); // Now it's in device coordinates circle.CreateEllipticRgnIndirect(rectEllipse); if (circle.PtInRegion(point)) { // Capturing the mouse ensures subsequent LButtonUp message SetCapture(); m_bCaptured = TRUE; CPoint pointTopLeft(m_pointTopLeft); dc.LPtoDP(&pointTopLeft); m_sizeOffset = point - pointTopLeft; // device coordinates // New mouse cursor is active while mouse is captured ::SetCursor(::LoadCursor(NULL, IDC_CROSS)); } }
void CEx05cView::OnLButtonUp(UINT nFlags, CPoint point) { if (m_bCaptured) { ::ReleaseCapture(); // release capture so mouse can go to another application m_bCaptured = FALSE; } } void CEx05cView::OnMouseMove(UINT nFlags, CPoint point) { if (m_bCaptured) { CClientDC dc(this); OnPrepareDC(&dc); CRect rectOld(m_pointTopLeft, m_sizeEllipse); dc.LPtoDP(rectOld); InvalidateRect(rectOld, TRUE); m_pointTopLeft = point - m_sizeOffset; dc.DPtoLP(&m_pointTopLeft); CRect rectNew(m_pointTopLeft, m_sizeEllipse); dc.LPtoDP(rectNew); InvalidateRect(rectNew, TRUE); } }
Also need to see drawing function
void CEx05cView::OnDraw(CDC* pDC) { pDC->SelectStockObject(BLACK_BRUSH); // Deselect brushHatch pDC->Rectangle(CRect(100, -100, 200, -200)); // Test invalid rect
CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0)); CPoint point(0, 0); // logical (0, 0) pDC->LPtoDP(&point); // In device coordinates, pDC->SetBrushOrg(point); // align the brush with // the window origin pDC->SelectObject(&brushHatch); pDC->Ellipse(CRect(m_pointTopLeft, m_sizeEllipse)); }
Use these programs to experiment and test your ideas about windows programming
Comment out function calls
Change the parameters
Full source code here.