繁体   English   中英

在Win32中使用Alpha通道显示32位图像

[英]Displaying 32 bit image with alpha channel in Win32

截图: http//i.imgur.com/hbExq9N.jpg

您好,我正在尝试在静态控件上设置带有alpha通道的png,但透明像素总是填充白色或黑色。

如果图像位于静态控件之上或仅在主窗口上绘制,则并不重要。

如果您有一些想法,请提供反馈。
谢谢

对不起我以前应该有邮政编码

//First try loading icon with transparency to static control or button:
//static control;

HWND hL;
HICON iStick;
case WM_CREATE:
iStick = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0, LR_LOADTRANSPARENT);
hL = CreateWindow(L"STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP/*SS_ICON*/, 193, 290, 32, 32, hwnd, NULL, hInst, NULL);
SendMessage(hL, STM_SETIMAGE, IMAGE_ICON, (LPARAM)iStick);
//SendMessage(hL, STM_SETICON, IMAGE_ICON, (LPARAM)iStick);

//Second try to draw on background 32bit bmp A8R8G8B8 : 

PAINTSTRUCT ps;
HDC hdc;
RECT r;
static HDC membit;
static BITMAP bm;

case WM_CREATE:
    SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST | WS_EX_LAYERED);
    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE |SWP_NOSIZE);
    SetLayeredWindowAttributes(hWnd, 0, (255 * 100) / 100, LWA_ALPHA);
    hStick = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP15));
    GetObject(hStick, sizeof(bm), &bm);
    membit = CreateCompatibleDC(hdc);
    SelectObject(membit, hStick);
    ReleaseDC(hWnd, hdc);

case WM_CTLCOLOREDIT:
    SetTextColor((HDC)wParam, RGB(65, 65, 65));      //
    return 0;

//MakeStaticbackground transparent
case WM_CTLCOLORSTATIC:       
    SetBkMode((HDC)wParam, TRANSPARENT);             //  
    SetTextColor((HDC)wParam, RGB(38, 205, 247));    //
    return (BOOL)GetStockObject(NULL_BRUSH);    

case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    GetClientRect(hWnd, &r);

    FillRect(hdc, &r, hBrush);
    SetBkMode(hdc, TRANSPARENT);

    BLENDFUNCTION BlendFunction;
    BlendFunction.AlphaFormat = AC_SRC_ALPHA;
    BlendFunction.BlendFlags = 0;
    BlendFunction.BlendOp = AC_SRC_OVER;
    BlendFunction.SourceConstantAlpha = 255;

    //BitBlt(hdc, 500, 280, bm.bmWidth, bm.bmHeight, membit, 0, 0, SRCCOPY);

    AlphaBlend(hdc, 500, 280, bm.bmWidth, bm.bmHeight, membit, 0, 0, 32, 32, BlendFunction);

    //TransparentBlt(hdc, 500, 300, 32, 32, membit, 32, 32, 32, 32, RGB(255, 255, 255));

    EndPaint(hWnd, &ps);

    break;

case WM_ERASEBKGND:
    return (LRESULT)1;

还有更多我不记得但背景像素总是填充白色或黑色。

我想你只想要透明度,而不是alpha通道。 您需要LWA_COLORKEY而不是LWA_ALPHA

SetLayeredWindowAttributes(hWnd, window_background_brush, 255, LWA_COLORKEY);

这将用透明颜色替换window_background_brush 如果未设置背景画笔,则需要设置,最好是白色。 此外,最好使用图标,否则最终会出现粗糙的边缘。 以下是绘制图标的示例代码:

HDC hDC = ::GetDC(m_hWnd);
HICON hicon = (HICON)LoadImage(NULL, "fullpath.ico", IMAGE_ICON, w, h, LR_LOADFROMFILE);
DrawIconEx(hDC, x, y, hicon, w, h, 0, NULL, DI_NORMAL);
DestroyIcon(hicon);
::ReleaseDC(m_hWnd, hDC);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM