How to use "SetWorldTransform"

I'm trying to rotate a metafile with the SetWorldTransform.
But it rotates around the upper left corner of my picture.
I want it to rotate around the center.
How do i do that ???
Who is Participating?
NickRepinConnect With a Mentor Commented:
I've tried to correct type error and made another one... :)

If you calculate the center point of the metafile correctly, the code in my answer will rotate metafile around it's center without any problem.

Suppose, you playing metafile in rect 20,30,100,150. And, as I understood, you want to rotate it around the center of this rectangle.
If so, the center you must use in code is 60,90 ( [100-20]/2+20, [150-30]/2+30 ). And not the center of the metafile itself
(the center of the metafile is width/2,height/2 - 40,60 in our case).

Next, if you want to resize metafile, say, twice, use SetViewportExtEx(2,2). Then metafile appears rotated around its center in rect 40,60,200,300. In this case the center of the resized metafile on screen will be in 120,180 and the metafile will seem shifted to right and down (and twice size, of course).

If you want to keep the center of the metafile on screen in first point, 60,90, then use SetViewportOrgEx(60-120,90-180).

I assume the size of window is 1,1 and its origin is 0,0.

If you have more question, feel free to write me directly to

By the way, the second CombineTransform is not necessary. It's enough to set x2.eDX=Pivot.x, x2.eDY=Pivot.y. Moreover, the rotation may be done in one step. But this is no so simple - it's need to apply some mathematics to fill XFORM.
how about setting the XFORM eDx and eDy to the point about which rotation occurs?
AndersCarlssonAuthor Commented:
Maybe some code could help ...

   hDC = BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );

      /* Rita kursskivan som bakgrund till kompassen */
      SetMapMode( hDC, MM_ANISOTROPIC );

      SetViewportOrgEx( hDC, ClientRect.left,, NULL );

      SetViewportExtEx( hDC, XSize, YSize, NULL );
      PlayMetaFile( hDC, hMFBackGround );

      /* Rita skillnadsfalt */
      if( Compass[ CpIx ]->Active ){
         SelectObject( hDC, YellowBrush );
      else SelectObject( hDC, BlackBrush );

      SelectObject( hDC, GetStockObject( NULL_PEN ) );
      SetMapMode( hDC, MM_TEXT );

      /* Rita pie:n */
      Pie( hDC, Origo.x - DiffRadius, Origo.y - DiffRadius,
                Origo.x + DiffRadius, Origo.y + DiffRadius,
                Start.x, Start.y, Stop.x, Stop.y );

      /* Rita kompass nal */
      NeedleWidth = NeedleRect.right - NeedleRect.left;
      NeedleHight = NeedleRect.bottom -;

      SetMapMode( hDC, MM_ANISOTROPIC );

      SetViewportOrgEx( hDC, (int)(Origo.x - NeedleWidth/2.0),
         (int)(Origo.y - NeedleHight/2.0), NULL );

      SetViewportExtEx( hDC, NeedleWidth, NeedleHight, NULL );

      Rotation = (float)(Compass[ CpIx ]->ComCourse * DEGTORAD);

      NeedleXForm.eM11 = (float)cos( Rotation );
      NeedleXForm.eM12 = (float)sin( Rotation );
      NeedleXForm.eM21 = (float)-sin( Rotation );
      NeedleXForm.eM22 = (float)cos( Rotation );

      OldGraphMode = SetGraphicsMode( hDC, GM_ADVANCED );
      SetWorldTransform( hDC, &NeedleXForm );

      PlayMetaFile( hDC, hMFNeedle );

      SetGraphicsMode( hDC, OldGraphMode );
   EndPaint( hWnd, (LPPAINTSTRUCT)&ps );

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

AndersCarlssonAuthor Commented:
Thanks danny_pav

. but i've tried that and it doesnt work

any other suggestions ?
do you keep in mind that SetWorldTransform does not supported on Win 95 ?
Use the eDx and eDy members of NeedleXForm to perform the horizontal and vertical translation (how much you want to move by).

danny_pav had this ...what's the effect of this
AndersCarlssonAuthor Commented:
NickRepin: I work on a NT machine.

Answers2000: I don't want to move the picture just rotate it.

If i set some values in eDx and eDy the picture moves to that position it seems (upper left corner). I want it to stay in center and rotate around the center of the metafile.
Try to call SetViewportExt and SetViewportOrg again after calling SetGraphicsMode.
Also, I think it would be better to call SetWindowExt and SetWindowOrg along with viewport functions after changing map/graphics mode. At least, that's how I did it, and all worked fine.
AndersCarlssonAuthor Commented:
I´ve tried to call SetViewportExt and Org again after SetGraphicsMode and to set XFORM eDx and eDy to the center of my metafile. But that doesn´t help at all, it still rotates around the upper left corner. Probably i´ve made some misstake somewhere, but i can´t find it. I´ve increased the points to 150 cause this is important for me.
World transformation doesn't depend on Window and Viewport settings. You should rotate like this:

   XFORM x1,x2,x3,result1,result2;
   // At first, move the center of the metafile in the center
   // of the world space
   x1.eDx=-center.x;  // Center of your metafile

   // Next, rotate around center of the world space

   // The last, return metafile back in its place

   // Now do eral job

Good luck!

Type errors...

// Now do REAL job....

Minus is so little on screen:
  x1.eDX= - center.x
  x2.eDY= - center.y
AndersCarlssonAuthor Commented:
It didn´t work NickRepin.
My MetaFile still rotates around the upper left corner
My code is as follows:

      OldGraphMode = SetGraphicsMode( hDC, GM_ADVANCED );

      SetViewportOrgEx( hDC, (int)(Origo.x - NeedleWidth/2.0),
         (int)(Origo.y - NeedleHight/2.0), NULL );
      SetViewportExtEx( hDC, NeedleWidth, NeedleHight, NULL );

        Pivot.x = (int)(NeedleWidth/2.0);
        Pivot.y = (int)(NeedleHight/2.0);

        // At first move the center of the metafile
        // in the center of the world space
        x1.eDx = -Pivot.x;
        x1.eDy = -Pivot.y;
        x1.eM11 = 1;
        x1.eM12 = 0;
        x1.eM21 = 0;
        x1.eM22 = 1;

        // Next rotate around center of world space
        x2.eDx = 0;
        x2.eDy = -Pivot.y;
        x2.eM11 = (float)cos( Rotation );
        x2.eM12 = (float)sin( Rotation );
        x2.eM21 = (float)-sin( Rotation );
        x2.eM22 = (float)cos( Rotation );

        // The last, return metafile back in its place
        x3.eDx = Pivot.x;
        x3.eDy = Pivot.y;
        x3.eM11 = 1;
        x3.eM12 = 0;
        x3.eM21 = 0;
        x3.eM22 = 1;

        // Now do real job
      CombineTransform(&result1, &x1, &x2 );
      CombineTransform(&result2, &result1, &x3 );

      SetWorldTransform( hDC, &result2 );

      PlayMetaFile( hDC, hMFNeedle );

      SetGraphicsMode( hDC, OldGraphMode );

   /* Avsluta uppdatering av klientarean */
   EndPaint( hWnd, (LPPAINTSTRUCT)&ps );

Whats wrong i think you can help me if you can ther´s another 100 points there for you.

1) x2.eDY must be 0
2) Check result of SetGraphicsMode(GM_ADVANCED)
AndersCarlssonAuthor Commented:
1) I just followed your "Type error" comment.
2) SetGraphicsMode result is nonzero.

I tried to move the centerpoint step by step, and finally i succeeded with center.x = 252 and center.y = 640.
I don´t understand why, cause my windowsize is 125*125.
Must i set windowextension or what ?
Another words, if I understood, instead of NeedleSize/2 use
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.