VC6.0中用MFC实现用鼠标画椭圆,可以改变大小,绕某一点旋转
时间: 2024-03-25 22:40:39 浏览: 15
在VC6.0中,你可以使用MFC提供的CDC类来绘制椭圆,并通过鼠标交互来实现改变大小和绕某一点旋转的效果。具体实现过程如下:
1. 在你的对话框类中添加以下变量:
```
CPoint m_center; // 椭圆中心点坐标
CPoint m_down; // 鼠标按下时的坐标
CRect m_rect; // 椭圆矩形区域
BOOL m_bDragging; // 是否正在拖拽
```
2. 在OnInitDialog()函数中初始化变量:
```
m_center = CPoint(200, 200); // 椭圆中心点坐标
m_rect = CRect(m_center.x - 50, m_center.y - 25, m_center.x + 50, m_center.y + 25); // 椭圆矩形区域
m_bDragging = FALSE; // 初始状态下没有拖拽
```
3. 在OnPaint()函数中绘制椭圆:
```
CPaintDC dc(this);
dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(m_rect);
```
4. 在OnLButtonDown()函数中记录鼠标按下时的坐标,并判断是否在椭圆内部:
```
m_down = point;
if (m_rect.PtInRect(m_down)) {
m_bDragging = TRUE;
}
```
5. 在OnLButtonUp()函数中清空拖拽状态:
```
m_bDragging = FALSE;
```
6. 在OnMouseMove()函数中处理拖拽事件:
```
if (m_bDragging) {
CPoint delta = point - m_down;
m_rect.OffsetRect(delta);
m_center.Offset(delta);
m_down = point;
Invalidate();
}
```
7. 在OnKeyDown()函数中处理旋转事件(这里以按下R键为例):
```
if (nChar == 'R') {
CPoint p1 = m_center;
CPoint p2 = m_down;
double angle = atan2(p2.y - p1.y, p2.x - p1.x);
m_rect.OffsetRect(-m_center.x, -m_center.y);
m_rect = RotateRect(m_rect, angle);
m_rect.OffsetRect(m_center.x, m_center.y);
Invalidate();
}
```
其中,RotateRect()函数用于旋转矩形:
```
CRect RotateRect(CRect rect, double angle) {
CRect result;
CPoint center = rect.CenterPoint();
double sina = sin(angle);
double cosa = cos(angle);
result.left = center.x + (rect.left - center.x) * cosa - (rect.top - center.y) * sina;
result.top = center.y + (rect.left - center.x) * sina + (rect.top - center.y) * cosa;
result.right = center.x + (rect.right - center.x) * cosa - (rect.bottom - center.y) * sina;
result.bottom = center.y + (rect.right - center.x) * sina + (rect.bottom - center.y) * cosa;
return result;
}
```
这样,你就可以用鼠标画椭圆,并且可以改变大小、绕某一点旋转了。