C++ MFC How to Draw Alpha transparent Rectangle

cmfcpaint

in a C++ MFC application. using the dc of ( CPaintDC dc(this); )

How do i draw a rectangle ( LPRECT ) with an alpha transparency that i can adjust.?

Following is an example c# code which i need to convert into 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.
} 

Best Answer

You need to look into GDI+. Its a bit of a faff but you can create a "Graphics" object as follows:

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 );

Don't forget to do

#include <gdiplus.h>

and to call

 GdiplusStartup(...);

somewhere :)

You'll notice it's pretty damned similar to your C# code ;)

Its worth noting that the 75 you put in your FromArgb code doesn't set 75% alpha it actually sets 75/255 alpha or ~29% alpha.