首页 > 代码库 > MFC: 孙鑫教程4笔记

MFC: 孙鑫教程4笔记

这节课主要讲了消息映射和绘画的一些函数,结构体,消息映射使得我们不用去管WindowProc函数的调用,只要在类里面添加消息就可以对操作进行一定的反应了。让MFC变得简单。

绘画的这些函数比较多,到时候可以refer这堂视频

下面是DrawView.cpp里的一些消息映射函数,加成员变量的时候需要在CDrawView的声明中(.h文件)里手动添加变量(视频里可以直接在类视图里添加),然后再构造函数里进行赋值。

 1 void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 2 { 3     // TODO: 在此添加消息处理程序代码和/或调用默认值 4     //MessageBox("view clicked"); 5     m_ptOrigin = m_ptOld = point; 6     //m_ptOrigin = point; 7     m_bDraw = TRUE; 8     CView::OnLButtonDown(nFlags, point); 9 }10 11 void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)12 {13     // TODO: 在此添加消息处理程序代码和/或调用默认值14     /*HDC hdc;15     hdc = ::GetDC(m_hWnd);16     MoveToEx(hdc, m_ptOrigin.x, m_ptOrigin.y, NULL);17     LineTo(hdc, point.x, point.y);18     ::ReleaseDC(m_hWnd, hdc);*/19     /*CDC *pDC = GetDC();20     pDC->MoveTo(m_ptOrigin);21     pDC->LineTo(point);22     ReleaseDC(pDC);*/23     //CClientDC dc(this);24     /*CClientDC dc(GetParent());25     dc.MoveTo(m_ptOrigin);26     dc.LineTo(point);*/27 28     /*CWindowDC dc(this);29     CWindowDC dc(GetParent());30     CWindowDC dc(GetDesktopWindow());31     dc.MoveTo(m_ptOrigin);32     dc.LineTo(point);*/33 34     /*CPen pen(PS_DASH, 1, RGB(0, 255, 0));35     CClientDC dc(this);36     CPen *pOldPen = dc.SelectObject(&pen);37     dc.MoveTo(m_ptOrigin);38     dc.LineTo(point);39     dc.SelectObject(pOldPen);*/40 41     /*CBrush brush(RGB(255, 0, 0));42     CClientDC dc(this);43     dc.FillRect(CRect(m_ptOrigin, point), &brush);*/44 45     /*CBitmap bitmap;46     bitmap.LoadBitmapA(IDB_BITMAP1);47     CBrush brush(&bitmap);*/48 49     /*CClientDC dc(this);50     //dc.FillRect(CRect(m_ptOrigin, point), &brush);51     CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));52     CBrush *pOldBrush = dc.SelectObject(pBrush);53     dc.Rectangle(CRect(m_ptOrigin, point));54     dc.SelectObject(pOldBrush);*/55 56     m_bDraw = FALSE;57 58     CView::OnLButtonUp(nFlags, point);59 }60 61 void CDrawView::OnMouseMove(UINT nFlags, CPoint point)62 {63     // TODO: 在此添加消息处理程序代码和/或调用默认值64 65     CClientDC dc(this);66     CPen pen(PS_SOLID, 1, RGB(255, 0, 0));67     CPen *pOldPen = dc.SelectObject(&pen);68     if (m_bDraw == TRUE)69     {70         //dc.SetROP2(R2_MERGENOTPEN);71         dc.SetROP2(R2_BLACK);72         dc.MoveTo(m_ptOrigin);73         //dc.LineTo(point);74         dc.LineTo(m_ptOld);75         //dc.MoveTo(m_ptOrigin);76         dc.MoveTo(m_ptOld);77         dc.LineTo(point);78         //m_ptOrigin = point;79         m_ptOld = point;80     }81     dc.SelectObject(pOldPen);82     CView::OnMouseMove(nFlags, point);83 }
View Code

 

MFC: 孙鑫教程4笔记