简体   繁体   English

C ++ MFC如何绘制Alpha透明矩形

[英]C++ MFC How to Draw Alpha transparent Rectangle

in a C++ MFC application. 在C ++ MFC应用程序中。 using the dc of ( CPaintDC dc(this); ) 使用dcCPaintDC dc(this);

How do i draw a rectangle ( LPRECT ) with an alpha transparency that i can adjust.? 如何绘制一个可以调整的Alpha透明度矩形( LPRECT )。

Following is an example c# code which i need to convert into C++ 以下是我需要转换为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.
} 

You need to look into GDI+. 你需要研究GDI +。 Its a bit of a faff but you can create a "Graphics" object as follows: 它有点像faff但你可以创建一个“Graphics”对象,如下所示:

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 ;) 你会发现它非常类似于你的C#代码;)

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. 值得注意的是,你在FromArgb代码中放入的75并没有设置75%的alpha值,它实际上设置了75/255 alpha或~29%alpha。

GDI (and thus MFC) has no decent support for drawing with an alpha. GDI(以及MFC)对使用alpha绘图没有得体。 But GDI+ is available in C++ code as well. 但是GDI +也可以在C ++代码中使用。 Use #include <gdiplus.h> and initialize it with GdiplusStartup(). 使用#include <gdiplus.h>并使用GdiplusStartup()初始化它。 You can use the Graphics class, create one with its Graphics(HDC) constructor from your CPaintDC. 您可以使用Graphics类,使用CPaintDC中的Graphics(HDC)构造函数创建一个。 And use its FillRectangle() method. 并使用其FillRectangle()方法。 The SDK docs are here . SDK文档在这里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM