標籤:介面 mfc 圖形
編寫一個單一文件介面程式,該程式在使用者區能以在兩個矩形的相交地區為外接矩形畫一個橢圓。效果如下:
1)用MFC AppWizard[exe],建立一個名稱為RecRec的單文檔應用程式。
2)在視圖類CRecRecView的聲明中,添加兩個成員變數:
public:CRect m_rRect2;CRect m_rRect1;
3)在視圖類CRecRecView的建構函式CRecRecView()中,初始化資料成員:
CRecRecView::CRecRecView():m_rRect1(50,50,250,200),m_rRect2(100,120,300,350){// TODO: add construction code here}
4)在視圖類的OnDraw函數中,寫入如下代碼:
void CRecRecView::OnDraw(CDC* pDC){CRecRecDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCClientDC dc(this);//建立一個空畫刷CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//將畫刷選入裝置描述表CBrush *pOldBrush = dc.SelectObject(pBrush);int x1,y1;int x2,y2;dc.Rectangle(m_rRect1);dc.Rectangle(m_rRect2);//選出交叉地區的左上方if(m_rRect1.left<m_rRect2.left)x1=m_rRect2.left;elsex1=m_rRect1.left;if(m_rRect1.top<m_rRect2.top)y1=m_rRect2.top;elsey1=m_rRect1.top;//選出交叉地區的右下角if(m_rRect1.right<m_rRect2.right)x2=m_rRect1.right;elsex2=m_rRect2.right;if(m_rRect1.bottom<m_rRect2.bottom)y2=m_rRect1.bottom;elsey2=m_rRect2.bottom;//以交叉地區為橢圓的外接矩形pDC->Ellipse(x1,y1,x2,y2);}