2013-03-06 173 views
2

我想創建一個具有透明背景的Direct2D應用程序,其中放置了一些不透明的複雜控件。 該問題可以分解爲幾個子問題:Direct2D:在透明父窗口上的不透明子窗口

架構:控件應該作爲子窗口實現嗎?我認爲這是正確的方法,而不是創建實現子窗口功能的Direct2D多邊形。

我試圖通過初始化父窗口來實現這一點:

SetWindowLong(m_hwnd, GWL_EXSTYLE, GetWindowLong(m_hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); 
SetLayeredWindowAttributes(m_hwnd, 0, (255 * 50)/100, LWA_ALPHA); 

,並創建子窗口WS_CHILD。這導致了一個小孩,其中包括背景在內的所有D2D遊標都是透明的。我找不到讓孩子不透明的方法。 當我創建子窗口爲WS_POPUPWS_OVERLAPPED不透明度問題已解決,但子窗口位於與父項無關的桌面上。

分層窗口? 我選擇與Layered Windows一起工作,但由於我瞄準VistaSP2及更高版本,可能會有更好的解決方案。 我試過解決方案here,但我也沒有實現它。

回答

1

你的意思是創建一個每像素32位的窗口? (對不起,暫時無法評論,在這裏沒有足夠的代表)

在這種情況下,你不得不使用UpdateLayeredWindow(和CreateDIBSection通話,而你初始化),無論什麼時候,每當你完成一次繪製場景之後你完成繪製的場景,如:

// Draw to your D2D1 RenderTarget here 
RECT rcWin = {0}; 
GetWindowRect(hWnd,&rcWin); 
POINT ptw = {rcWin.left,rcWin.top}; 
SIZE pts = {rcWin.right-rcWin.left,rcWin.bottom-rcWin.top}; 
POINT ptsrc = {0}; 
HDC ScreenDC = GetDC(0); 
UpdateLayeredWindow(hWnd, ScreenDC, &ptw, &pts, MemDC, &ptsrc, 0, &bf, ULW_ALPHA); 
ReleaseDC(0,ScreenDC); 

關於初始化:

RECT r = {0}; 
GetWindowRect(hWnd,&r); 
HDC scrDC = GetDC(0); 
MemDC = CreateCompatibleDC(scrDC); 
ReleaseDC(0,scrDC); 
if(!MemDC) 
    { FailInit(); } 
BITMAPINFO bmi = {0}; 
bmi.bmiHeader.biBitCount = 32; 
bmi.bmiHeader.biCompression = BI_RGB; 
bmi.bmiHeader.biPlanes = 1; 
bmi.bmiHeader.biWidth = r.right-r.left; 
bmi.bmiHeader.biHeight = r.bottom-r.top; 
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); 
DIBSectionBitmap = CreateDIBSection(MemDC,&bmi,DIB_RGB_COLORS,0,0,0); 
if(!DIBSectionBitmap) 
    return 0; 
OldBmp = (HBITMAP)SelectObject(MemDC,DIBSectionBitmap); 
// Now create the HWND D2D1 RenderTarget. 

關於資源的釋放:

// Free the D2D1 RenderTarget here 
if(MemDC && OldBmp) 
    SelectObject(MemDC,OldBmp); 
if(DIBSectionBitmap) 
    DeleteObject(DIBSectionBitmap); 
if(MemDC) 
    DeleteDC(MemDC); 
MemDC = 0; 
OldBmp = 0; 
DIBSectionBitmap = 0; 

編輯:MemDC,OldBmp和DIBSectionBitmap是每個窗口。MemDC是一個HDC。 OldBmp是一個HBITMAP。 DIBSectionBitmap是一個HBITMAP。 此時,您可以繪製您的子窗口,就好像它們是您自己的主窗口的一部分,具有每像素alpha精度,但您需要自行處理對焦和消息傳遞。