Fonction SetWoldTransform () : transformations graphiques

Résolu
_michel Messages postés 77 Date d'inscription mardi 27 juin 2006 Statut Membre Dernière intervention 12 août 2010 - 8 août 2006 à 12:39
_michel Messages postés 77 Date d'inscription mardi 27 juin 2006 Statut Membre Dernière intervention 12 août 2010 - 8 août 2006 à 17:40
Je suis amené à me servir de la fonction SetWorldTransform (), qui permet les transformations graphiques, mais même en recopiant l'exemple de l'aide Windows, je n'arrive pas à la faire marcher.
Voici le code (condensé) :

#include <windows.h>



/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);




/* Fonction de transformation */
void TransformAndDraw(HWND hWnd);




int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
 LPSTR lpszArgument, int nFunsterStil)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */




    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = "Window App";
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;




    if (!RegisterClassEx (&wincl)){return 0;}




    hwnd = CreateWindowEx (0, "Window App", "Figure", WS_OVERLAPPEDWINDOW |
        WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 344, 375, HWND_DESKTOP, NULL,
  hThisInstance, NULL);




    while (GetMessage (&messages, NULL, 0, 0)){DispatchMessage(&messages);}
    return messages.wParam;
}






LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 HDC hdc;
 
    switch (message)                  /* handle the messages */
    {
  case WM_CREATE:
   hdc = GetDC (hwnd);
   SetGraphicsMode (hdc, GM_ADVANCED);
   ReleaseDC (hwnd, hdc);
            return DefWindowProc (hwnd, message, wParam, lParam);




  case WM_PAINT:
   TransformAndDraw (hwnd);
   ValidateRect (hwnd, NULL);
   return 0;
   
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;




        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }




    return 0;
}




void TransformAndDraw(HWND hWnd)
{
 HDC hDC;
 XFORM xForm;
 RECT rect;




 hDC = GetDC(hWnd);




 /* Transformation */
 xForm.eM11 = (FLOAT) 0.8660;
 xForm.eM12 = (FLOAT) 0.5000;
 xForm.eM21 = (FLOAT) -0.5000;
 xForm.eM22 = (FLOAT) 0.8660;
 xForm.eDx  = (FLOAT) 0.0;
 xForm.eDy  = (FLOAT) 0.0;
 if (SetWorldTransform(hDC, &xForm)){
  MessageBox (hWnd, "Transformation effectuée avec succès", NULL, MB_OK);}




 /* Find the midpoint of the client area. */
 GetClientRect(hWnd, (LPRECT) &rect);
 DPtoLP(hDC, (LPPOINT) &rect, 2);




 /* Select a hollow brush. */
 SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));




 /* Affichage de la figure */
 Ellipse(hDC, (rect.right / 2 - 100), (rect.bottom / 2 + 100),
 (rect.right / 2 + 100), (rect.bottom / 2 - 100));
 Ellipse(hDC, (rect.right / 2 -94), (rect.bottom / 2 + 94),
 (rect.right / 2 + 94), (rect.bottom / 2 - 94));
 Rectangle(hDC, (rect.right / 2 - 13), (rect.bottom / 2 + 113),
    (rect.right / 2 + 13), (rect.bottom / 2 + 50));
 Rectangle(hDC, (rect.right / 2 - 13), (rect.bottom / 2 + 96),
    (rect.right / 2 + 13), (rect.bottom / 2 + 50));
 MoveToEx(hDC, (rect.right / 2 - 150), (rect.bottom / 2 + 0), NULL);
 LineTo(hDC, (rect.right / 2 + 150), (rect.bottom / 2 + 0));
 MoveToEx(hDC, (rect.right / 2 + 0), (rect.bottom / 2 - 150), NULL);
 LineTo(hDC, (rect.right / 2 + 0), (rect.bottom / 2 + 150));




 ReleaseDC(hWnd, hDC);
}

2 réponses

racpp Messages postés 1909 Date d'inscription vendredi 18 juin 2004 Statut Modérateur Dernière intervention 14 novembre 2014 17
8 août 2006 à 16:29
Salut,
La fonction SetGraphicsMode() ne doit pas être utilisée pendant le traitement de WM_CREATE car le HDC de la fenêtre n'est pas encore disponible. Tu peux placer cette fonction dans ta fonction de transformation juste après la récupéraion du HDC de la fenêtre:
 hDC = GetDC(hWnd);
 SetGraphicsMode (hDC, GM_ADVANCED);

Pour le traitement de WM_PAINT, tu dois faire comme ceci:
 case WM_PAINT:
      PAINTSTRUCT ps;
      BeginPaint(hwnd,&ps);
      TransformAndDraw (hwnd);
      EndPaint(hwnd,&ps);
      return 0;

Voilà, ça devrait marcher maintenant.
3
_michel Messages postés 77 Date d'inscription mardi 27 juin 2006 Statut Membre Dernière intervention 12 août 2010
8 août 2006 à 17:40
Merci, ça marche au poil !

 
0
Rejoignez-nous