如何将 50% 不透明颜色填充到多边形?
How to fill 50% opaque color to polygon?
我可以用这个代码填充颜色
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 函数由 100% 不透明填充
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 );
}
但是我不知道如何用 50% opeque 填充颜色?
编辑:
听从@Jonathan 的建议后。我尝试使用 AlphaBlend 函数。
首先我试着让一些位图变成透明的,看起来可行。
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);
}
图像是用透明绘制的,如下所示。
但是当我尝试填充多边形时它不起作用。
我通过更改为填充多边形来修改 FillColorPolygonAlpha() 函数。
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);
}
它不绘制任何多边形。
我用GDI+解决了这个问题
因为SolidBrush可以通过alpha值设置透明。
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);
}
我可以用这个代码填充颜色
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 函数由 100% 不透明填充
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 );
}
但是我不知道如何用 50% opeque 填充颜色?
编辑:
听从@Jonathan 的建议后。我尝试使用 AlphaBlend 函数。 首先我试着让一些位图变成透明的,看起来可行。
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);
}
图像是用透明绘制的,如下所示。
但是当我尝试填充多边形时它不起作用。 我通过更改为填充多边形来修改 FillColorPolygonAlpha() 函数。
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);
}
它不绘制任何多边形。
我用GDI+解决了这个问题
因为SolidBrush可以通过alpha值设置透明。
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);
}