Windows NT Systems Programming: Spring 2000

[ Home | Syllabus | Course Notes | Assignments | Search]


Comparing Three Simple Programs

So far we have seen three simple programs which basically write some text to the screen:

  1. WIN32 "C" program
  2. Hand built MFC "C++" program
  3. Wizard built document/view MFC "C++" program

  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


WIN32 "C" Program

	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

Hand built MFC program

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"

Document/View

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

Routing the Message

SDIRoute.gif (8687 bytes)

 


Demo: Randomly Color Ellipse

DESCRIPTION

OBJECTIVES

CREATING THE DEMO

Full source code here.

Dragging an object/GDI: Ex5c

Example ex5c demostrates several interesting features of windows program.

First look at three message handlers for the mouse - which work together.
(NOTE: the handhptrd_left.gif (955 bytes) points to places where footnotes provide explanations)

rainbow.gif (2243 bytes)

void CEx05cView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CRect hptrd_left.gif (955 bytes)rectEllipse(m_pointTopLeft, m_sizeEllipse); // still logical
    CRgn  circle;hptrd_left.gif (955 bytes)

    CClientDC dc(this);
    OnPrepareDC(&dc);hptrd_left.gif (955 bytes)
    dc.LPtoDP(rectEllipse); hptrd_left.gif (955 bytes)// Now it's in device coordinates
    circle.CreateEllipticRgnIndirect(rectEllipse);
    if (circle.PtInRegion(point)) {
        // Capturing the mouse ensures subsequent LButtonUp message
        SetCapture();hptrd_left.gif (955 bytes)
        m_bCaptured = TRUE;hptrd_left.gif (955 bytes)
        CPoint pointTopLeft(m_pointTopLeft);
        dc.LPtoDP(&pointTopLeft);hptrd_left.gif (955 bytes)
        m_sizeOffset = point - pointTopLeft; hptrd_left.gif (955 bytes)// device coordinates
        // New mouse cursor is active while mouse is captured
        ::SetCursor(::LoadCursor(NULL, IDC_CROSS));hptrd_left.gif (955 bytes)
    }
}
rainbow.gif (2243 bytes)
rainbow.gif (2243 bytes)


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);hptrd_left.gif (955 bytes)
        m_pointTopLeft = point - m_sizeOffset;
        dc.DPtoLP(&m_pointTopLeft);
        CRect rectNew(m_pointTopLeft, m_sizeEllipse);hptrd_left.gif (955 bytes)
        dc.LPtoDP(rectNew);
        InvalidateRect(rectNew, TRUE);hptrd_left.gif (955 bytes)
    }
}
rainbow.gif (2243 bytes)

Also need to see drawing function

rainbow.gif (2243 bytes)

void CEx05cView::OnDraw(CDC* pDC)
{
    pDC->SelectStockObject(BLACK_BRUSH); // Deselect brushHatch
    pDC->Rectangle(CRect(100, -100, 200, -200));hptrd_left.gif (955 bytes) // Test invalid rect
    CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0));hptrd_left.gif (955 bytes)
    CPoint point(0, 0);                  // logical (0, 0)

    pDC->LPtoDP(&point);                 // In device coordinates,
    pDC->SetBrushOrg(point); hptrd_left.gif (955 bytes)         //  align the brush with
                                         //  the window origin
    pDC->SelectObject(&brushHatch);
    pDC->Ellipse(CRect(m_pointTopLeft, m_sizeEllipse));
    
}
rainbow.gif (2243 bytes)

Use these programs to experiment and test your ideas about windows programming
Comment out function calls
Change the parameters

Full source code here.


Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: January 18, 2000.