Vc++6.0MFC中使用鼠标画椭圆的实现过程
时间: 2024-03-25 18:39:45 浏览: 109
以下是在VC++6.0 MFC中使用鼠标画椭圆的实现过程:
1. 创建一个新的MFC应用程序项目。
2. 在资源视图中添加一个菜单资源,例如IDR_MAINFRAME。
3. 打开应用程序类的头文件,添加以下代码:
```cpp
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMyWnd : public CFrameWnd
{
public:
CMyWnd();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
private:
BOOL m_bDrawing;
CRect m_rectEllipse;
};
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMyWnd;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMyWnd::CMyWnd()
{
Create(NULL, _T("My Oval Drawing Program"));
m_bDrawing = FALSE;
}
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
if (m_bDrawing)
{
dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(m_rectEllipse);
}
}
void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bDrawing = TRUE;
m_rectEllipse.left = point.x;
m_rectEllipse.top = point.y;
m_rectEllipse.right = point.x;
m_rectEllipse.bottom = point.y;
SetCapture();
}
void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bDrawing = FALSE;
ReleaseCapture();
Invalidate();
}
void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bDrawing)
{
CDC* pDC = GetDC();
pDC->SelectStockObject(NULL_BRUSH);
pDC->SetROP2(R2_NOTXORPEN);
pDC->Ellipse(m_rectEllipse);
m_rectEllipse.right = point.x;
m_rectEllipse.bottom = point.y;
pDC->Ellipse(m_rectEllipse);
ReleaseDC(pDC);
}
}
BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
CMyApp theApp;
```
4. 编译并运行应用程序。当您单击菜单中的“画椭圆”按钮时,应用程序将允许您使用鼠标在窗口中绘制椭圆形。
阅读全文