2010-11-03 176 views
6

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

DC我如何绘制一个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. 
} 

回答

9

您需要查看GDI +。它有点faff的,但你可以按如下创建一个 “图形” 对象:

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> 

,并呼吁

GdiplusStartup(...); 

地方: )

你会注意到它非常非常类似于你的C#代码;)

其值得注意的是,你把你的FromArgb码75不设置75%的α-它实际上是设置二百五十五分之七十五字母或〜29%的α。

+0

如果我调用GdiplusStartup();我应该调用GdiplusShutdown吗? 我想我必须。如果我必须,它是否应该在Paint Event内? – 2010-11-03 14:16:20

+1

GdiplusStartup应该从你的MFC应用程序的InitInstance函数一次usally调用。然后,GdiplusShutdown应该在应用程序退出时调用一次。 – Goz 2010-11-03 14:18:34

3

GDI(因而MFC)具有用于与α-绘图没有像样的支持。但是GDI +也可以在C++代码中使用。使用#include <gdiplus.h>并使用GdiplusStartup()进行初始化。您可以使用Graphics类,使用CPaintDC中的Graphics(HDC)构造函数创建一个Graphics类。并使用它的FillRectangle()方法。 SDK文档are here

-1
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