2010-11-03 178 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