发布网友
共1个回答
热心网友
这段代码将图片搬移到窗口中心,在旋转60度。
代码是在MFC的OnDraw函数中的。
注意到GDI+中的矩阵是将自己右乘行向量的。
void CScaleObjectView::OnDraw(CDC* pDC)
{
CScaleObjectDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc) return;
static const float fAngle = 3.1415926536f / 3.0f; //旋转角度为60度
static const float fSin = sinf(fAngle);
static const float fCos = cosf(fAngle);
static Matrix matWorld; //矩阵,用于将对象变换到世界坐标
RECT rcClient; //客户区域
this->GetClientRect(&rcClient);
float dx = (float)rcClient.right / 2.0f; //dx dy用于将图片搬移到窗口中心
float dy = (float)rcClient.bottom / 2.0f;
matWorld.SetElements( //该矩阵相当于
fCos, fSin, // Graphics::RotateTransform 乘以
-fSin, fCos, // Graphics::TranslateTransform
dx, dy);
Graphics g(pDC->GetSafeHdc());
g.SetTransform(&matWorld);
g.DrawImage(
m_bitmap,
-((float)m_bitmap->GetWidth() / 2.0f),
-((float)m_bitmap->GetHeight() / 2.0f));
}
//效果如图: