MFC实例:图形移动

来源:互联网 发布:gunicorn windows 编辑:程序博客网 时间:2024/05/16 14:30

案例7: 实际应用
实现点击产生随机大小、随机颜色的球,若是左键按下产生的小球,按下过程中小球上移,若是右键按下产生的小球,按下过程中小球下移,能够保存、读取。
运行截图:
这里写图片描述
插入代码如下:
定义要存储的变量:(记得初始化)

class CBubbleUpDoc : public CDocument{// Attributespublic:CRect BubbleRect[MAX];int BubbleColor[MAX];int m_Bubble;...

修改Serialize函数实现数据存储:

void CBubbleUpDoc::Serialize(CArchive& ar){if (ar.IsStoring()){    // TODO: add storing code here    ar<<m_Bubble;    for (int i=0;i<m_Bubble;i++)    ar << BubbleRect[i];    for (i=0;i<m_Bubble;i++)    ar << BubbleColor[i];}else{    // TODO: add loading code here    ar>>m_Bubble;    for (int i=0;i<m_Bubble;i++)    ar >> BubbleRect[i];    for (i=0;i<m_Bubble;i++)    ar >> BubbleColor[i];    }}

修改OnDraw函数:

void CBubbleUpView::OnDraw(CDC* pDC){    CBubbleUpDoc* pDoc = GetDocument();    ASSERT_VALID(pDoc);    CBrush BrushNew[MAX],*BrushOld;    CPen PenNew,*PenOld;    PenNew.CreatePen(0,1,RGB(0,0,0));    PenOld = pDC->SelectObject(&PenNew);    for (int i=0;i<pDoc->m_Bubble;i++)    {        switch (pDoc->BubbleColor[i])        {            case (1):                BrushNew[i].CreateSolidBrush(RGB(255,0,0));                BrushOld = pDC->SelectObject(&BrushNew[i]);                break;            case (2):                BrushNew[i].CreateSolidBrush(RGB(0,255,0));                BrushOld = pDC->SelectObject(&BrushNew[i]);                break;            case (3):                BrushNew[i].CreateSolidBrush(RGB(0,0,255));                BrushOld = pDC->SelectObject(&BrushNew[i]);        }    pDC->Ellipse(pDoc->BubbleRect[i]);    }    pDC->SelectObject(PenOld);    pDC->SelectObject(BrushOld);    // TODO: add draw code for native data here}

添加的函数:

void CBubbleUpView::OnLButtonDown(UINT nFlags, CPoint point) {CBubbleUpDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: Add your message handler code here and/or call defaultSetTimer(1,100,NULL);if (pDoc->m_Bubble < MAX){int color = rand()%3 +1;pDoc->BubbleColor[pDoc->m_Bubble] = color;int r = rand()%100 + 15;CRect rect(point.x-r,point.y-r,point.x+r,point.y+r);pDoc->BubbleRect[pDoc->m_Bubble] = rect;pDoc->m_Bubble++;pDoc->SetModifiedFlag();InvalidateRect(rect,FALSE);}CView::OnLButtonDown(nFlags, point);}void CBubbleUpView::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultKillTimer(1);CView::OnLButtonUp(nFlags, point);}void CBubbleUpView::OnRButtonDown(UINT nFlags, CPoint point) {CBubbleUpDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: Add your message handler code here and/or call defaultSetTimer(2,100,NULL);if (pDoc->m_Bubble < MAX){int color = rand()%3 +1;pDoc->BubbleColor[pDoc->m_Bubble] = color;int r = rand()%100 + 15;CRect rect(point.x-r,point.y-r,point.x+r,point.y+r);pDoc->BubbleRect[pDoc->m_Bubble] = rect;pDoc->m_Bubble++;pDoc->SetModifiedFlag();InvalidateRect(rect,FALSE);}CView::OnRButtonDown(nFlags, point);}void CBubbleUpView::OnRButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultKillTimer(2);CView::OnRButtonUp(nFlags, point);}void CBubbleUpView::OnTimer(UINT nIDEvent) {CBubbleUpDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: Add your message handler code here and/or call defaultif (nIDEvent==1){CRect rect = pDoc->BubbleRect[pDoc->m_Bubble-1];rect.top-=5;pDoc->BubbleRect[pDoc->m_Bubble-1].top-=5;pDoc->BubbleRect[pDoc->m_Bubble-1].bottom-=5;pDoc->SetModifiedFlag();InvalidateRect(rect,TRUE);}else {CRect rect = pDoc->BubbleRect[pDoc->m_Bubble-1];rect.bottom+=5;pDoc->BubbleRect[pDoc->m_Bubble-1].top+=5;pDoc->BubbleRect[pDoc->m_Bubble-1].bottom+=5;pDoc->SetModifiedFlag();InvalidateRect(rect,TRUE);}CView::OnTimer(nIDEvent);}

知识点:
1.画刷不能用CreateSolidBrush(实心填涂)或者CreateHatchBrush(特殊填涂)来修改其性质(颜色、样式);
2.更新移动的矩形区域时记得刷新掉未移动的图形残留,即刷新区域比作图区域要多出移动距离那么长的一个矩形

0 0
原创粉丝点击