IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API graphiques Discussion :

mixer GDI et GDI+


Sujet :

API graphiques

  1. #1
    Membre du Club
    Inscrit en
    Février 2004
    Messages
    79
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 79
    Points : 57
    Points
    57
    Par défaut mixer GDI et GDI+
    J'ai une application MFC qui utilise GDI pour faire le rendu graphique. Je tente d'insérer au code actuel quelques rendus graphiques avec GDI+ (pour utiliser de l'alpha blending et avoir des éléments translucides). Pour l'instant, les éléments translucide sont des plaques triangulaires. Jusqu'à présent, j'ai réussi à faire afficher mes plaques, mais les coordonnées avec GDI+ ne sont pas les mêmes qu'en GDI!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    void CPlaque3NAffiche::AfficheTranslucide(CAffView* pView, CDC* pDC) const
    {
    	GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
     
    	Gdiplus::Graphics*		myGraphics	= new Gdiplus::Graphics(pDC->m_hDC);
    	Gdiplus::SolidBrush*	myBrush		= new Gdiplus::SolidBrush(Color(128,255,0,0));
     
    	Gdiplus::Point listPts[3];
    	listPts[0] = Point( m_pPlaque->m_pNi->GetXAff(), m_pPlaque->m_pNi->GetYAff() );
    	listPts[1] = Point( m_pPlaque->m_pNj->GetXAff(), m_pPlaque->m_pNj->GetYAff() );
    	listPts[2] = Point( m_pPlaque->m_pNk->GetXAff(), m_pPlaque->m_pNk->GetYAff() );
     
    	myGraphics->FillPolygon(myBrush, listPts, 3);
    	delete myGraphics;
    	delete myBrush;
     
    	GdiplusShutdown(gdiplusToken);
    }
    Et je sais que les coordonnées que je vais chercher sont les bonnes si je suis en GDI.

  2. #2
    Membre du Club
    Inscrit en
    Février 2004
    Messages
    79
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 79
    Points : 57
    Points
    57
    Par défaut
    Sinon, quelle méthode puis-je utiliser pour tracer des surface de polygone avec de l'alpha blending en GDI ?

    avoir une espèce de CDC->alphaPolygon... ?

  3. #3
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 381
    Points
    20 381
    Par défaut
    Pour GDI+ ce qui m'intrigue c'est que c'est du "managed code" en théorie.
    Peux-t-on passer des pointeurs à des objets GDI + ?
    Pour faire des éléments translucides avec le GDI sur www.codeproject.com ou www.codeguru.com cela peut se trouver.
    Alpha Blending a Bitmap
    The following code sample divides a window into three horizontal areas. Then it draws an alpha-blended bitmap in each of the window areas as follows:

    In the top area, constant alpha = 50% but there is no source alpha.
    In the middle area, constant alpha = 100% (disabled) and source alpha is 0 (transparent) in the middle of the bitmap and 0xff (opaque) elsewhere.
    In the bottom area, constant alpha = 75% and source alpha changes.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
     
    void DrawAlphaBlend (HWND hWnd, HDC hdcwnd)
    {
        HDC hdc;               // handle of the DC we will create 
        BLENDFUNCTION bf;      // structure for alpha blending
        HBITMAP hbitmap;       // bitmap handle
        BITMAPINFO bmi;        // bitmap header
        VOID *pvBits;          // pointer to DIB section
        ULONG   ulWindowWidth, ulWindowHeight;      // window width/height
        ULONG   ulBitmapWidth, ulBitmapHeight;      // bitmap width/height
        RECT    rt;            // used for getting window dimensions
        UINT32   x,y;          // stepping variables
        UCHAR ubAlpha;         // used for doing transparent gradient
        UCHAR ubRed;        
        UCHAR ubGreen;
        UCHAR ubBlue;
        float fAlphaFactor;    // used to do premultiply
     
        // get window dimensions
        GetClientRect(hWnd, &rt);
     
        // calculate window width/height
        ulWindowWidth = rt.right - rt.left;  
        ulWindowHeight = rt.bottom - rt.top;  
     
        // make sure we have at least some window size
        if ((!ulWindowWidth) || (!ulWindowHeight))
            return;
     
        // divide the window into 3 horizontal areas
        ulWindowHeight = ulWindowHeight / 3;
     
        // create a DC for our bitmap -- the source DC for AlphaBlend 
        hdc = CreateCompatibleDC(hdcwnd);
     
        // zero the memory for the bitmap info
        ZeroMemory(&bmi, sizeof(BITMAPINFO));
     
        // setup bitmap info 
        // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas.
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth/5)*2;
        bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight/5)*2;
        bmi.bmiHeader.biPlanes = 1;
        bmi.bmiHeader.biBitCount = 32;         // four 8-bit components
        bmi.bmiHeader.biCompression = BI_RGB;
        bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;
     
        // create our DIB section and select the bitmap into the dc
        hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);
        SelectObject(hdc, hbitmap);
     
        // in top window area, constant alpha = 50%, but no source alpha
        // the color format for each pixel is 0xaarrggbb 
        // set all pixels to blue and set source alpha to zero
        for (y = 0; y < ulBitmapHeight; y++)
            for (x = 0; x < ulBitmapWidth; x++)
                ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x000000ff; 
     
        bf.BlendOp = AC_SRC_OVER;
        bf.BlendFlags = 0;
        bf.SourceConstantAlpha = 0x7f;  // half of 0xff = 50% transparency
        bf.AlphaFormat = 0;             // ignore source alpha channel
     
        if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5, 
                        ulBitmapWidth, ulBitmapHeight, 
                        hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
            return;                     // alpha blend failed
     
        // in middle window area, constant alpha = 100% (disabled), source 
        // alpha is 0 in middle of bitmap and opaque in rest of bitmap 
        for (y = 0; y < ulBitmapHeight; y++)
            for (x = 0; x < ulBitmapWidth; x++)
                if ((x > (int)(ulBitmapWidth/5)) && (x < (ulBitmapWidth-ulBitmapWidth/5)) &&
                    (y > (int)(ulBitmapHeight/5)) && (y < (ulBitmapHeight-ulBitmapHeight/5)))
                    //in middle of bitmap: source alpha = 0 (transparent).
                    // This means multiply each color component by 0x00.
                    // Thus, after AlphaBlend, we have a, 0x00 * r, 
                    // 0x00 * g,and 0x00 * b (which is 0x00000000)
                    // for now, set all pixels to red
                    ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x00ff0000; 
                else
                    // in the rest of bitmap, source alpha = 0xff (opaque) 
                    // and set all pixels to blue 
                    ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0xff0000ff; 
                endif;
     
        bf.BlendOp = AC_SRC_OVER;
        bf.BlendFlags = 0;
        bf.AlphaFormat = AC_SRC_ALPHA;  // use source alpha 
        bf.SourceConstantAlpha = 0xff;  // opaque (disable constant alpha)
     
        if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5+ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
            return;
     
        // bottom window area, use constant alpha = 75% and a changing
        // source alpha. Create a gradient effect using source alpha, and 
        // then fade it even more with constant alpha
        ubRed = 0x00;
        ubGreen = 0x00;
        ubBlue = 0xff;
     
        for (y = 0; y < ulBitmapHeight; y++)
            for (x = 0; x < ulBitmapWidth; x++)
            {
                // for a simple gradient, base the alpha value on the x 
                // value of the pixel 
                ubAlpha = (UCHAR)((float)x / (float)ulBitmapWidth * 255);
                //calculate the factor by which we multiply each component
                fAlphaFactor = (float)ubAlpha / (float)0xff; 
                // multiply each pixel by fAlphaFactor, so each component 
                // is less than or equal to the alpha value.
                ((UINT32 *)pvBits)[x + y * ulBitmapWidth] 
                    = (ubAlpha << 24) |                       //0xaa000000
                     ((UCHAR)(ubRed * fAlphaFactor) << 16) |  //0x00rr0000
                     ((UCHAR)(ubGreen * fAlphaFactor) << 8) | //0x0000gg00
                     ((UCHAR)(ubBlue   * fAlphaFactor));      //0x000000bb
            }
     
        bf.BlendOp = AC_SRC_OVER;
        bf.BlendFlags = 0;
        bf.AlphaFormat = AC_SRC_ALPHA;   // use source alpha 
        bf.SourceConstantAlpha = 0xbf;   // use constant alpha, with 
                                         // 75% opaqueness
     
        AlphaBlend(hdcwnd, ulWindowWidth/5, 
                   ulWindowHeight/5+2*ulWindowHeight, ulBitmapWidth, 
                   ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, 
                   ulBitmapHeight, bf);
     
        // do cleanup
        DeleteObject(hbitmap);
        DeleteDC(hdc);
     
    }

Discussions similaires

  1. Choisir GDI ou GDI+ ?
    Par sphynxounet dans le forum API graphiques
    Réponses: 3
    Dernier message: 14/12/2009, 19h51
  2. [WD14] API GDI et GDI+ : copier une image PNG dans un champ image
    Par Froglock dans le forum WinDev
    Réponses: 3
    Dernier message: 26/08/2009, 21h40
  3. OpenGL + GDI
    Par Twofy dans le forum OpenGL
    Réponses: 4
    Dernier message: 26/02/2004, 17h13
  4. [mfc][gdi] un tuto ?
    Par VitamineC dans le forum MFC
    Réponses: 8
    Dernier message: 06/02/2004, 22h51
  5. [MFC] libération des objets GDI's
    Par Kevgeii dans le forum MFC
    Réponses: 5
    Dernier message: 01/02/2004, 10h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo