C++;MFC如何绘制Alpha透明矩形 在C++ MFC应用程序中的应用。使用(CPaintDC dc(this);)的dc)

C++;MFC如何绘制Alpha透明矩形 在C++ MFC应用程序中的应用。使用(CPaintDC dc(this);)的dc),c++,mfc,paint,C++,Mfc,Paint,如何绘制一个具有可调整alpha透明度的矩形(LPRECT) 下面是一个示例c代码,我需要将其转换为c++ private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Color color = Color.FromArgb(75,Color.Red); //sets color Red with 75% alpha transparency R

如何绘制一个具有可调整alpha透明度的矩形(
LPRECT

下面是一个示例c代码,我需要将其转换为c++

private void pictureBox1_Paint(object sender, PaintEventArgs e)  
{
    Graphics g = e.Graphics;
    Color color = Color.FromArgb(75,Color.Red); //sets color Red with 75% alpha transparency

    Rectangle rectangle = new Rectangle(100,100,400,400);
    g.FillRectangle(new SolidBrush(color), rectangle); //draws the rectangle with the color set.
} 

GDI(以及MFC)对使用alpha进行绘图没有适当的支持。但是GDI+也可以用C++代码提供。使用
#包含
并用GdiplusStartup()初始化它。您可以使用Graphics类,从CPaintDC创建一个带有图形(HDC)构造函数的类。并使用其FillRectangle()方法。SDK文档。

您需要查看GDI+。这有点假,但您可以创建一个“图形”对象,如下所示:

Gdiplus::Graphics g( dc.GetSafeHdc() );
Gdiplus::Color color( 192, 255, 0, 0 );

Gdiplus::Rect rectangle( 100, 100, 400, 400 );
Gdiplus::SolidBrush solidBrush( color );
g.FillRectangle( &solidBrush, rectangle );
别忘了做点什么

#include <gdiplus.h>
某处:)

你会注意到它与你的C代码非常相似。)


值得注意的是,您在argb的
中输入的75并没有设置75%的alpha值,而是设置了75/255 alpha或~29%alpha值。

如果我调用GdiplusStartup();我必须调用GdiplusShutdown吗?我想我必须。如果必须,它是否应该在Paint事件中?GdiplusStartup应该从MFC应用程序的InitInstance函数中调用一次。然后,当应用程序退出时,应该调用一次GdiplusShutdown。
int StartHoriz,StartVert,BarWidth,BarHeight; // rect start, width and height
StartHoriz=0;
StartVert=100;
width = 100;
height=120;
CDC* pCDC = GetDC();      // Get CDC pointer
CRect Rect(StartHoriz,StartVert,BarWidth,BarHeight);  //create rectangle dimensions
pCDC->Rectangle(Rect);   //draw rectangle
int StartHoriz,StartVert,BarWidth,BarHeight; // rect start, width and height
StartHoriz=0;
StartVert=100;
width = 100;
height=120;
CDC* pCDC = GetDC();      // Get CDC pointer
CRect Rect(StartHoriz,StartVert,BarWidth,BarHeight);  //create rectangle dimensions
pCDC->Rectangle(Rect);   //draw rectangle