0

I can fill color by this code

      void  FillColorPolygon(POINT  pts[],int   ilnum,long  fillColor)  
      { 
        COLORREF    fillcol;
        fillcol = Gc_disp::ColorSet(fillColor);
        HBRUSH hBrushNew = CreateSolidBrush(fillcol);
        HBRUSH hBrushOld = (HBRUSH)SelectObject(m_hDC, hBrushNew);
        SetPolyFillMode(m_hDC, WINDING);    

        Polygon(m_hDC, pts, (short)ilnum);              
        SelectObject(m_hDC, hBrushOld);
        DeleteObject(hBrushNew);    
     }

ColorSet function is fill by 100% opaque

     COLORREF Gc_disp::ColorSet(long    col)
     {
         COLORREF   rcol = RGB(0, 0, 0);
         if( col >= 0 && col <= GRIP_MAXCOLORS + 1 )
             rcol = g_tblColor[ col ];

         return( rcol );
     }

But I don't know how to fill color with 50% opeque?

Edit:

After following @Jonathan's advise. I try to use AlphaBlend function. First I try to make some bitmap turn to be transparent, It seems work.

       void  FillColorPolygonAlpha(POINT  pts[],int   ilnum,long  fillColor)  
       {
         BLENDFUNCTION m_bf;    
         m_bf.BlendOp = AC_SRC_OVER;
         m_bf.BlendFlags = 0;
         m_bf.SourceConstantAlpha = 0xC8;
         m_bf.AlphaFormat = 0;

         CBitmap  m_bitmap;
         CImage image;
         image.Load(_T("C:\\Blas_grande.png"));
         CBitmap bitmap;
         m_bitmap.Attach(image.Detach());

         int m_nWidth, m_nHeight;
         BITMAP aBmp;
         m_bitmap.GetBitmap(&aBmp);
         m_nWidth = aBmp.bmWidth ;
         m_nHeight = aBmp.bmHeight;

         CDC* pDC = CDC::FromHandle( GetDC() );
         CDC  dcMem;    
         dcMem.CreateCompatibleDC(pDC); 

         CBitmap *pOldBitmap  =  dcMem.SelectObject(&m_bitmap);

          AlphaBlend(m_hDC, 0,0, m_nWidth, m_nHeight, dcMem, 0,0,m_nWidth, m_nHeight,m_bf);

         dcMem.SelectObject(pOldBitmap);
       }

The image was drawn with transparent like below.

the image is transparent.

But it isn't work when I try to fill polygon. I modify FillColorPolygonAlpha() function by changed to fill a polygon.

     void  FillColorPolygonAlpha(POINT  pts[],int   ilnum,long  fillColor)  
       {
         BLENDFUNCTION m_bf;    
         m_bf.BlendOp = AC_SRC_OVER;
         m_bf.BlendFlags = 0;
         m_bf.SourceConstantAlpha = 0xC8;
         m_bf.AlphaFormat = 0;

         CDC* pDC = CDC::FromHandle( GetDC() );
         CDC  dcMem;    
         dcMem.CreateCompatibleDC(pDC); 

         COLORREF   fillcol;
         fillcol = Gc_disp::ColorSet(fillColor);
         HBRUSH hBrushNew = CreateSolidBrush(fillcol);  
         Polygon(dcMem, pts, (short)ilnum); 

          AlphaBlend(m_hDC, 0,0, m_nWidth, m_nHeight, dcMem, 0,0,m_nWidth, m_nHeight,m_bf);

       }

It don't draw any polygon.

Fame th
  • 1,018
  • 3
  • 17
  • 37
  • 2
    Using GDI, the best you can do is fill a memory DC with the color you want, and then use the `AlphaBlend` function to blend it over the target. – Jonathan Potter Jun 01 '20 at 11:22
  • 1
    @Fame th Jonathan has given a useful solution. You can use `SourceConstantAlpha` which located in `BLENDFUNCTION` structure to achieve custom transparency. – Strive Sun Jun 02 '20 at 02:34
  • Now I try to use SourceConstantAlpha and AlphaBlend. It work when I use bitmap then blend it to transparent. Is it important to use Bitmap only? What possible? if I fill a polygon without creating bitmap. – Fame th Jun 03 '20 at 14:27

1 Answers1

1

I solved this problem by using GDI+.

Because of SolidBrush can set a transparent by alpha value.

void FillColorPolygonAlpha(POINT  pts[],int   ilnum,long  fillColor,int alpha)  
 {      
    COLORREF    rcol;
    rcol = Gc_disp::ColorSet(fillColor);

    Gdiplus::Graphics gr(m_hDC);    
    Gdiplus::SolidBrush semiTransBrush(Gdiplus::Color(alpha, GetRValue(rcol), GetGValue(rcol), GetBValue(rcol) ));      
    CArray<Gdiplus::Point,Gdiplus::Point> arrPoints;
    for(int i=0; i<sizeof(pts); i++)
    {
        Gdiplus::Point pt(pts[i].x , pts[i].y);
        arrPoints.Add(pt); 
    }   

    Gdiplus::Point* pPoints = arrPoints.GetData(); 
    gr.FillPolygon(&semiTransBrush,pPoints,ilnum);      
}
Fame th
  • 1,018
  • 3
  • 17
  • 37