2

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

我想在我的 hwnd 中放一个简单的 png 文件,例如这张图片: 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。那么还有其他解决方案吗?

4

1 回答 1

3

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

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

注意 - 我没有费心将 WM_ERASEBKGND 添加到 WndProc 函数。因此,每次重新绘制图像时,它都会直接绘制在已经存在的顶部(尝试调整窗口大小)。只需在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;
}
于 2013-04-04T15:54:27.217 回答