MFC二维矩形图形进行缩放、平移、旋转的代码
时间: 2023-08-20 08:05:18 浏览: 248
以下是MFC中对二维矩形图形进行缩放、平移、旋转的代码:
1. 缩放
```C++
void CMyView::OnScale()
{
CDC* pDC = GetDC();
CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
pDC->SelectObject(&pen);
CRect rect(100, 100, 200, 200);
pDC->Rectangle(rect);
double scaleX = 1.5;
double scaleY = 0.8;
CSize size(rect.Size());
CPoint center(rect.CenterPoint());
rect.left = (LONG)(center.x - size.cx * scaleX / 2);
rect.right = (LONG)(center.x + size.cx * scaleX / 2);
rect.top = (LONG)(center.y - size.cy * scaleY / 2);
rect.bottom = (LONG)(center.y + size.cy * scaleY / 2);
pDC->Rectangle(rect);
}
```
2. 平移
```C++
void CMyView::OnTranslate()
{
CDC* pDC = GetDC();
CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
pDC->SelectObject(&pen);
CRect rect(100, 100, 200, 200);
pDC->Rectangle(rect);
int dx = 50;
int dy = 30;
rect.OffsetRect(dx, dy);
pDC->Rectangle(rect);
}
```
3. 旋转
```C++
void CMyView::OnRotate()
{
CDC* pDC = GetDC();
CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
pDC->SelectObject(&pen);
CRect rect(100, 100, 200, 200);
pDC->Rectangle(rect);
CPoint center(rect.CenterPoint());
double angle = 30.0;
double radians = angle * 3.1415926 / 180.0;
int x = center.x + (int)((rect.left - center.x) * cos(radians) - (rect.top - center.y) * sin(radians));
int y = center.y + (int)((rect.left - center.x) * sin(radians) + (rect.top - center.y) * cos(radians));
rect.left = x;
rect.top = y;
x = center.x + (int)((rect.right - center.x) * cos(radians) - (rect.top - center.y) * sin(radians));
y = center.y + (int)((rect.right - center.x) * sin(radians) + (rect.top - center.y) * cos(radians));
rect.right = x;
rect.top = y;
x = center.x + (int)((rect.right - center.x) * cos(radians) - (rect.bottom - center.y) * sin(radians));
y = center.y + (int)((rect.right - center.x) * sin(radians) + (rect.bottom - center.y) * cos(radians));
rect.right = x;
rect.bottom = y;
x = center.x + (int)((rect.left - center.x) * cos(radians) - (rect.bottom - center.y) * sin(radians));
y = center.y + (int)((rect.left - center.x) * sin(radians) + (rect.bottom - center.y) * cos(radians));
rect.left = x;
rect.bottom = y;
pDC->Rectangle(rect);
}
```