2012-03-17 23 views
1

我attampted繪製不規則窗口與UpdateLayeredWindow(),在msvc2008,XP SP3。
這裏是我的代碼部分:我的代碼,它使用UpdateLayeredWindow不起作用

//Add something(CreateWindowEx()): 
hwndCyauWnd = CreateWindowEx(
    /*WS_EX_TOOLWINDOW |*/ WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_LAYERED, 
    lpwsCyauClassName, 
    lpwsCyauWndName, 
    WS_CLIPSIBLINGS | WS_POPUP, 
    GetSystemMetrics(SM_CXSCREEN)-320, 
    GetSystemMetrics(SM_CYSCREEN)-232, 
    320, 200, 
    NULL, 
    NULL, 
    hInstance, 
    NULL); 

//Skip Lines 
HDC hdcCyauWnd = GetDC(hwndCyauWnd); 
HDC hdcBuffer = CreateCompatibleDC(hdcCyauWnd); 
//HBITMAP hbmCyau = CreateCompatibleBitmap(hdcBuffer,120, 93); 
//SelectObject(hdcBuffer, hbmCyau); 

POINT ptZero = {0, 0}; 
POINT ptDrawPos = {0, 0}; 
RECT rctCyauWnd; 
GetWindowRect(hwndCyauWnd, &rctCyauWnd); 
SIZE szCyauWnd={rctCyauWnd.right - rctCyauWnd.left, rctCyauWnd.bottom - rctCyauWnd.top}; 
BLENDFUNCTION blendPixelFunction = { AC_SRC_OVER, 0, 100, AC_SRC_ALPHA}; 

Graphics gphCyauWnd(hdcBuffer); 
Image imgCyau(L"surface0000.png"); 
gphCyauWnd.DrawImage(&imgCyau, 0, 0, 125, 93); 

UpdateLayeredWindow(hwndCyauWnd, 
    hdcCyauWnd, &ptZero, 
    &szCyauWnd, 
    hdcBuffer, &ptZero, 
    0, //RGB(255, 255, 255), 
    &blendPixelFunction, 
    ULW_ALPHA); 

while (GetMessage(&msg, NULL, 0, 0))  
{ 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
} 
return msg.wParam; 

我嘗試了好幾種方法來使用此功能,但都失敗了,沒有出現在屏幕上。
有誰告訴我發生了什麼,以及如何解決售後服務呢?

地址:
整個源文件已被上傳到my skydrive,任何人都可以編輯,非常感謝! (我已成爲一個可憐的失敗者現在...)

+0

每[文件](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633556.aspx),窗口傳遞作爲「UpdateLayeredWindow」函數的第一個參數必須具有使用「CreateWindowEx」函數創建時指定的「WS_EX_LAYERED」樣式。而在Windows 8之前的版本中,該窗口必須是頂層窗口(Windows 8支持頂級Windows *和*子窗口的'WS_EX_LAYERED'風格。) – 2012-03-17 08:52:13

+0

@CodyGray哦,對不起。我留下了一些線。 hwndCyauWnd在塊之前已經有WS_ES_LAYERED樣式。所以它應該可以工作,但事實並非如此。這就是我困惑的原因。 – cuter44 2012-03-17 09:19:53

+0

嗯,請務必編輯問題並添加您遺漏的那些行。特別是你調用'CreateWindowEx'來創建窗口。像這樣的問題(您要求某人幫助您調試代碼)應該包含一個我們可以自行運行的小型自包含演示項目。 – 2012-03-17 09:24:01

回答

1

你混了GDI和GDI +,這是不是一個好主意。這是一個工作示例:

hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0, 
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 

// Load PNG 
CImage img; 
img.Load("BACKGR.png"); 
// Get image sizes 
int nWidth = img.GetWidth(); 
int nHeight = img.GetHeight(); 
// Create memory DC 
HDC hdcScreen = GetDC(NULL); 
HDC hDC = CreateCompatibleDC(hdcScreen); 
// Create memory bitmap 
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, nWidth, nHeight); 
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp); 
// Draw image to memory bitmap (currently selected in memory DC) 
img.Draw(hDC, 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight); 

// Call UpdateLayeredWindow 
BLENDFUNCTION blend = {0}; 
blend.BlendOp = AC_SRC_OVER; 
blend.SourceConstantAlpha = 128;// half transparent 
blend.AlphaFormat = AC_SRC_ALPHA; 
POINT ptLocation = {0, 0}; 
SIZE szWnd = {nWidth, nHeight}; 
POINT ptSrc = {0, 0}; 
UpdateLayeredWindow(hWnd, hdcScreen, &ptLocation, &szWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA); 
ShowWindow(hWnd, SW_SHOW); 

SelectObject(hDC, hBmpOld); 
DeleteObject(hBmp); 
DeleteDC(hDC); 
ReleaseDC(NULL, hdcScreen); 
+0

Errr ......,什麼毛病'img.Draw(HDC,0,0,nWidth,nHeight參數,0,0,nWidth,nHeight參數);'跟蹤它,發現它停在'內嵌無效的CImage :: ReleaseDC()常量擲(){ATLASSERT(HBITMAP == m_hBitmap);}'的窗口仍然看不見,甚至不能由間諜捕獲++ – cuter44 2012-03-17 16:56:58

+0

對不起,不知何故的ShowWindow(HWND,SW_SHOW)從代碼滑落,見編輯答案。添加後,所有工作都很好。你確定你的PNG文件可以嗎? – Flot2011 2012-03-17 19:57:49

+0

仍然無法正常工作,甚至無法分辨發生了什麼......我已將代碼上傳到skydrive,請您多幫助我一下?非常感謝! – cuter44 2012-03-18 03:43:00

1

如果你想GDI +繪製與alpha通道的圖像,你必須繪製成位圖,而不是一個HDC,你必須指定位圖的格式有阿爾法。要用HBITMAP做到這一點,您還必須將GDI +指向位圖位。

事情是這樣的:

BITMAPINFOHEADER bih; 
HBITMAP hbmp; 
HDC hdc; 
void *bits; 

bih.biSize = sizeof(bih); 
bih.biWidth = width; 
bih.biHeight = -height; 
bih.biPlanes = 1; 
bih.biBitCount = 32; 
bih.biCompression = BI_RGB; 
bih.biSizeImage = 0; 
bih.biXPelsPerMeter = 0; 
bih.biYPelsPerMeter = 0; 
bih.biClrUsed = 0; 
bih.biClrImportant = 0; 

hdc = CreateCompatibleDC(NULL); 

hbmp = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits, NULL, 0); 

Bitmap bitmap(width, height, 0, PixelFormat32bppPARGB, bits); 

Graphics graphics(bitmap); 

graphics->DrawWhatever(...); 

graphics->Flush(); 

SelectObject(hdc, hbitmap); 

UpdateLayeredWindow(hwnd, hdc, ... 
+0

「你必須繪製到位圖,而不是HDC」我被這個問題困住了好幾個月! – WawaBrother 2014-02-16 22:56:40