从png,winapi创建透明图像

时间:2013-04-04 15:25:31

标签: c++ winapi png transparency

我创建了一个在winapi中测试的简单表单:我上传到这里:http://pastebin.com/7dNjE1Tb

我想将一个简单的png文件放到我的hwnd中,例如这张图片:http://www.ledavi-network.com/includes/images/bg_shadow_png.png

我知道我应该使用这个:http://msdn.microsoft.com/en-us/library/windows/desktop/dd145141(v=vs.85).aspx但我是winapi的新手,我没有找到如何使用这个TransparentBlt函数的任何示例。

有人可以帮我创建一个非常简单的例子吗?

编辑: 但我想做什么? 我想创建一个具有阴影特殊背景的按钮。因此,如果有人知道更简单的方法,我会忘记使用TransparentBlt。那么还有其他解决方案吗?

1 个答案:

答案 0 :(得分:3)

在这里,这将完成这项工作。您需要确保为AlphaBlend链接了gdiplus和msimg32库(TransparentBlt假设图像中的单个 rgb颜色将被视为透明。即使用TransparentBlt时它只是1位透明度 - 完全不透明或完全透明。)

你(可能)希望使用AlphaBlend来完成这项工作。

注意 - 我没有打算在WndProc函数中添加WM_ERASEBKGND。因此,每次重新绘制图像时,它都会直接绘制在已存在的图像的顶部(尝试调整窗口大小)。只需在erasebkg中做一个FillRect,你就可以了。

编辑:更新代码以使用带有NULL hbitmap的displayImage来执行背景。

这里'去:

#define WIN32_LEAN_AND_MEAN
#define WINVER 0x0600       // needed for alphablend function..

#include <windows.h>
#include <gdiplus.h>

const char g_szClassName[] = "myWindowClass";


// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
// requires GDIPlus
HBITMAP mLoadImg(WCHAR *szFilename)
{
   HBITMAP result=NULL;

   Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
   bitmap->GetHBITMAP(NULL, &result);
   delete bitmap;
   return result;
}

//void CStaticImg::displayImage(HBITMAP mBmp, HWND mHwnd)
void displayImage(HBITMAP mBmp, HWND mHwnd)
{
    RECT myRect;
    BITMAP bm;
    HDC screenDC, memDC;
    HBITMAP oldBmp;
    BLENDFUNCTION bf;

    GetObject(mBmp, sizeof(bm), &bm);

    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = 0xff;

    bf.AlphaFormat = AC_SRC_ALPHA;

    screenDC = GetDC(mHwnd);
    GetClientRect(mHwnd, &myRect);

    if (mBmp == NULL)
        FillRect(screenDC, &myRect, WHITE_BRUSH);

    else
    {
        memDC = CreateCompatibleDC(screenDC);
        oldBmp = (HBITMAP)SelectObject(memDC, mBmp);
        AlphaBlend (screenDC, 0, 0, myRect.right,myRect.bottom, memDC, 0, 0, bm.bmWidth,bm.bmHeight, bf);
        SelectObject(memDC, oldBmp);
        DeleteDC(memDC);
        ReleaseDC(mHwnd, screenDC);
    }
}



// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HBITMAP myImage;
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;

        case WM_ERASEBKGND:
            displayImage(NULL, hwnd);
        break;

        case WM_CREATE:
            myImage = mLoadImg(L"bg_shadow_png.png");
        break;

        case WM_PAINT:
            //displayImage(HBITMAP mBmp, HWND mHwnd);
            displayImage(myImage, hwnd);
            ValidateRect(hwnd, NULL);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    static Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    static ULONG_PTR gdiplusToken;
    // so we can load all the image formats that windows supports natively - (I'm using a transparent PNG on the main dialog)
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    Gdiplus::GdiplusShutdown(gdiplusToken);
    return Msg.wParam;
}