求助,MFC下实现图像控件的摆动(旋转)!!!

Hallon_Yuan 2017-03-19 01:32:36
加精

如图,图中的指针是一个图片控件,我想接到一个角度让其旋转。谁能帮帮我,我已经看了好多帖子但是实现不了!

下面是我写的一个函数,但是测试没有效果,也可以帮我看看是什么问题!很急!!!
inline int RotateDC(HDC hDc, int iAngle, POINT centerPt)
{
int nGraphicsMode = SetGraphicsMode(hDc, GM_ADVANCED);
XFORM xform;
if (iAngle != 0)
{
double fangle = (double)iAngle / 180. * 3.1415926;
xform.eM11 = (float)cos(fangle);
xform.eM12 = (float)sin(fangle);
xform.eM21 = (float)-sin(fangle);
xform.eM22 = (float)cos(fangle);
xform.eDx = (float)(centerPt.x - cos(fangle)*centerPt.x + sin(fangle)*centerPt.y);
xform.eDy = (float)(centerPt.y - cos(fangle)*centerPt.y - sin(fangle)*centerPt.x);
SetWorldTransform(hDc, &xform);
}
return nGraphicsMode;
}
...全文
3984 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
漠上行云 2017-04-21
  • 打赏
  • 举报
回复
复杂的ui设计 用QT比较方便 ,像这种QT做就很简单的, y用MFC的话 我能想到的就是用OpenCV把图片转过来再绘制了 Cimage应该也行
赵4老师 2017-04-21
  • 打赏
  • 举报
回复
仅供参考:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
wchar_t formats[5][11]={
    L"image/bmp",
    L"image/jpeg",
    L"image/gif",
    L"image/tiff",
    L"image/png",
};
wchar_t exts[5][5]={
    L".bmp",
    L".jpg",
    L".gif",
    L".tif",
    L".png",
};
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   ImageCodecInfo* pImageCodecInfo = NULL;
   GetImageEncodersSize(&num, &size);
   if(size == 0) return -1;  // Failure
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL) return -1;  // Failure
   GetImageEncoders(num, size, pImageCodecInfo);
   for (UINT j = 0; j < num; ++j) {
      if ( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
int wmain(int argc,wchar_t *argv[]) {
    int r=1;
    if (argc<4) {
    USAGE:
        wprintf(L"%s srcimg.{bmp|jpg|gif|tif|png|wmf|emf|ico}  desimg.{bmp|jpg|gif|tif|png}  angle\n",argv[0]);
        return r;
    }
    int i;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[1]+wcslen(argv[1])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[2]+wcslen(argv[2])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
    {
        Image img(argv[1]);
        if (Ok==img.GetLastStatus()) {
            UINT height = img.GetHeight();
            UINT width  = img.GetWidth();
            REAL angle;
            if (1==swscanf_s(argv[3],L"%f",&angle)) {
                REAL size;
                size=(REAL)sqrt(1.0*width*width+1.0*height*height);
                Matrix mat;
                mat.Translate(size / -2.0f, size / -2.0f);
                mat.Rotate(-angle, MatrixOrderAppend);
                mat.Translate(size / 2.0f, size / 2.0f, MatrixOrderAppend);
                PointF pfTL((size-width)/2.0f      ,(size-height)/2.0f       );
                PointF pfTR((size-width)/2.0f+width,(size-height)/2.0f       );
                PointF pfBL((size-width)/2.0f      ,(size-height)/2.0f+height);
                PointF pfBR((size-width)/2.0f+width,(size-height)/2.0f+height);
                Graphics tgp(&img);
                Bitmap bmp((UINT)size,(UINT)size,&tgp);//Let bmp Resolution equal to img Resolution
                Graphics gp(&bmp);
                gp.SetTransform(&mat);
                gp.DrawImage(&img,pfTL);
                REAL xmin,ymin,xmax,ymax,x,y,rw,rh;
                mat.TransformPoints(&pfTL);
                xmin=xmax=pfTL.X;
                ymin=ymax=pfTL.Y;
                mat.TransformPoints(&pfTR);
                if (xmin>pfTR.X) xmin=pfTR.X;
                if (xmax<pfTR.X) xmax=pfTR.X;
                if (ymin>pfTR.Y) ymin=pfTR.Y;
                if (ymax<pfTR.Y) ymax=pfTR.Y;
                mat.TransformPoints(&pfBL);
                if (xmin>pfBL.X) xmin=pfBL.X;
                if (xmax<pfBL.X) xmax=pfBL.X;
                if (ymin>pfBL.Y) ymin=pfBL.Y;
                if (ymax<pfBL.Y) ymax=pfBL.Y;
                mat.TransformPoints(&pfBR);
                if (xmin>pfBR.X) xmin=pfBR.X;
                if (xmax<pfBR.X) xmax=pfBR.X;
                if (ymin>pfBR.Y) ymin=pfBR.Y;
                if (ymax<pfBR.Y) ymax=pfBR.Y;
                x=xmin;
                y=ymin;
                rw=xmax-x;
                rh=ymax-y;
                Bitmap* clone;
                clone = bmp.Clone(x,y,rw,rh,PixelFormat24bppRGB);//bmp.GetPixelFormat()
                CLSID encoderClsid;
                if (0<=GetEncoderClsid(formats[i],&encoderClsid)) {
                    if (Ok==clone->Save(argv[2],&encoderClsid)) {
                        wprintf(L"OK to %s  %s  %s  %s\n",argv[0],argv[1],argv[2],argv[3]);
                        r=0;
                    } else {
                        wprintf(L"Error to save %s\n",argv[2]);
                        r=4;
                    }
                } else {
                    wprintf(L"Error to GetEncoderClsid(%s,...)\n",formats[i]);
                    r=3;
                }
                delete clone;
            } else {
                wprintf(L"Error to get angle %s\n",argv[3]);
                r=2;
            }
        } else {
            wprintf(L"Error to load %s\n",argv[1]);
            r=5;
        }
    }
    GdiplusShutdown(gdiplustoken);
    return r;
}
cdcjk 2017-04-20
  • 打赏
  • 举报
回复
GDI+
枫青痕 2017-04-20
  • 打赏
  • 举报
回复

楼主看下这个怎么样
yangyanzhao 2017-04-13
  • 打赏
  • 举报
回复
一起学习学习
gaoshanlan 2017-04-11
  • 打赏
  • 举报
回复
学习学习, 这很有用
cdcjk 2017-04-04
  • 打赏
  • 举报
回复
工程上可以用有限个图片来表示不同的角度,那么不同的角度用不同的贴图也是可以考虑的。
wowocpp 2017-04-01
  • 打赏
  • 举报
回复
不懂啊 学习一下 ,MFC GDI 绘图
  • 打赏
  • 举报
回复
不要用控件,自己绘图,控件你永远也旋转不了
  • 打赏
  • 举报
回复
最简单就是贴图了
worldy 2017-03-21
  • 打赏
  • 举报
回复
SetWorldTransform(hDc, &xform); 之后,你要BitBlt进去啊
向立天 2017-03-21
  • 打赏
  • 举报
回复
GDI+是可以实现图片旋转的
schlafenhamster 2017-03-21
  • 打赏
  • 举报
回复
void CMyImageDlg::OnButton2()
{
// TODO: Add your control notification handler code here
// RedrawWindow(); // ErsaeOld
CDC *pDC=GetDC();
GdipRotate(pDC->m_hDC);
// PasteWmf(pDC);
}
#define R 20 // radius
#define K 1.732f // sqr(3)
#define A 60 // degree
void GdipRotate(HDC hdc)
{
static float deg=0.0;
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255),2.0f);
//
Matrix matrix;// identity matrix
#if 0 // 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
REAL m[6];
matrix.GetElements(m);
afxDump << m[0] << " m11\n";// 1.0
afxDump << m[1] << " m12\n";// 0
afxDump << m[2] << " m21\n";// 0
afxDump << m[3] << " m22\n";// 1.0
afxDump << m[4] << " dx\n"; // 0
afxDump << m[5] << " dy\n"; // 0
#endif
PointF Center(200, 200);
matrix.RotateAt(deg, Center , MatrixOrderPrepend);// matrix is on the right
PointF pt0(Center.X,80);// arrow
PointF pt1(Center.X-R/2,Center.Y-K*R/2);
PointF pt2(Center.X+R/2,Center.Y-K*R/2);
graphics.SetTransform(&matrix);
// ^ =-90 ; -> = 0
RectF rc(Center.X-R,Center.Y-R, 2*R, 2*R);
graphics.DrawArc(&pen,rc,-A,360-A);
graphics.DrawLine(&pen,pt1,pt0);
graphics.DrawLine(&pen,pt2,pt0);
deg += 10.0f;
}
zgl7903 2017-03-21
  • 打赏
  • 举报
回复
赵4老师 2017-03-20
  • 打赏
  • 举报
回复
GDI+
sichuanwww 2017-03-20
  • 打赏
  • 举报
回复
用GDI是否可行? 在深入点,DirectX
ooolinux 2017-03-19
  • 打赏
  • 举报
回复
Hallon_Yuan 2017-03-19
  • 打赏
  • 举报
回复
引用 2 楼 u010165006 的回复:
如果指针转动的角度是固定的,工程上可以用有限个图片来表示不同的角度,那么不同的角度用不同的贴图也是可以考虑的。
转动的角度不是固定的,要怎么才能做到让图片旋转一定的角度呢?在线等
ooolinux 2017-03-19
  • 打赏
  • 举报
回复
如果指针转动的角度是固定的,工程上可以用有限个图片来表示不同的角度,那么不同的角度用不同的贴图也是可以考虑的。
Hallon_Yuan 2017-03-19
  • 打赏
  • 举报
回复
我想写一个函数,返回值为void,传入的参数为整型值,一个角度,void Rotate(int iAngle),来实现指针的旋转,求大神帮助!!!

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧