//声明变量,在类的定义中 Unit srcUnit; Gdiplus::Image *img RectF rectf2; ColorMatrix *cm; ImageAttributes *att; //初始化变量,在初始化函数OnInitDialog中 WCHAR * w ; len = MultiByteToWideChar(CP_ACP , 0 , m_pImgPath.c_str() , -1 , NULL , 0 ); w = new WCHAR[ len ] ; MultiByteToWideChar( CP_ACP , 0 , m_pImgPath.c_str() , -1 , w , len ); image1 = new Gdiplus::Image( w ); //设置透明系数,在初始化函数OnInitDialog中 att = new ImageAttributes; cm= new ColorMatrix(); cm->m[0][0] = 1; cm->m[1][1] = 1 ; cm->m[2][2] = 1 ; cm->m[3][3] = 0.6f; cm->m[4][4] = 1; att->SetColorMatrix( cm ); //贴图。重载OnEraseBkgnd()函数,在函数中添加代码 CDC dc; dc.CreateDC("DISPLAY", NULL, NULL, NULL ); CDC dch ; dch.CreateCompatibleDC( &dc ); CBitmap bp; bp.CreateCompatibleBitmap( &dc , 650, 500 ); dch.SelectObject( bp ); CRect rect; this->GetClientRect( &rect ); dch.FillSolidRect( &rect, RGB(255,255,255) ); Graphics gps1( dch.m_hDC ); gps1.DrawImage( img, destpoint,3 ,rectf2.GetTop(), rectf2.GetLeft() , rectf2.GetRight(),rectf2.GetBottom(), srcUnit , att,NULL,NULL );//destpoint 为平移或者旋转后的三个角点坐标数组 pDC->BitBlt( 20, 20, 650, 500, &dch, 0, 0, SRCCOPY ); dch.DeleteDC(); dc.DeleteDC();
平移或者旋转后的三个角点位置可以用每次位置更新的中心点计算得到:
destpoint[0].x = -width/2*cosa - height/2 *sina +centerx;//width是要显示的图像宽度,height是要显示的图像高,a为图像的角度,centerx是中心点的x坐标 destpoint[0].y = width/2*sina - height/2 *cosa +centery; destpoint[1].x = width/2*cosa - height/2 *sina +centerx; destpoint[1].y = - width/2*sina - height/2 *cosa +centery; destpoint[2].x = -width/2*cosa + height/2 *sina +centerx; destpoint[2].y = width/2*sina + height/2 *cosa +centery;
平移时候 角度不变,中心点位置 在MouseMove和LbuttonDown及LButtonUP中确定。
旋转时候中心点位置不变,角度在PreTranslateMessage中确定,通过响应按键盘消息修改角度值