案例7: 實際應用
實現點擊產生隨機大小、隨機顏色的球,如果左鍵按下產生的小球,按下過程當中小球上移,如果右鍵按下產生的小球,按下過程當中小球下移,可以保存、讀取。
運行截圖:
插入代碼以下:
定義要存儲的變量:(記得初始化)web
class CBubbleUpDoc : public CDocument
{
// Attributes
public:
CRect BubbleRect[MAX];
int BubbleColor[MAX];
int m_Bubble;
...
修改Serialize函數實現數據存儲:svg
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
}
添加的函數:spa
void CBubbleUpView::OnLButtonDown(UINT nFlags, CPoint point)
{
CBubbleUpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: Add your message handler code here and/or call default
SetTimer(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 default
KillTimer(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 default
SetTimer(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 default
KillTimer(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 default
if (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.更新移動的矩形區域時記得刷新掉未移動的圖形殘留,即刷新區域比做圖區域要多出移動距離那麼長的一個矩形code