我使用UpdateLayeredWindow的代码不起作用

时间:2012-03-17 07:23:16

标签: winapi mfc gdi+

我试图在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,任何人都可以编辑,多赞赏! (我现在已成为一个可怜的弱者......)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

如果您希望GDI +绘制到具有Alpha通道的图像,则必须绘制到Bitmap而不是HDC,并且您必须指定Bitmap的格式具有alpha。要使用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, ...