簡體   English   中英

轉換向量 <unsigned char> 到C ++中的HBITMAP

[英]Convert vector <unsigned char> to HBITMAP in C++

我在這里使用代碼將PNG圖像加載到BMP原始向量std::vector <unsigned char> 現在,我需要將此圖像作為背景應用到WinAPI窗口,並且我不知道如何將其轉換為HBITMAP 也許有人以前做過,或者我可以使用其他格式或變量類型

您可以從一開始就使用Gdiplus,打開png文件並獲取HBITMAP句柄

//initialize Gdiplus:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

HBITMAP hbitmap;
HBRUSH hbrush;

Gdiplus::Bitmap *bmp = Gdiplus::Bitmap::FromFile(L"filename.png");
bmp->GetHBITMAP(0, &hbitmap);
hbrush = CreatePatternBrush(hbitmap);

//register classname and assign background brush
WNDCLASSEX wcex;
...
wcex.hbrBackground = hbrush;

CreateWindow...

清理:

DeleteObject(hbrush);
DeleteObject(hbitmap);

delete bmp;

Gdiplus::GdiplusShutdown(gdiplusToken);

您需要包括“ gdiplus.h”並鏈接到“ gdiplus.lib”庫。 頭文件默認情況下應可用。

在Visual Studio中,您可以按以下方式鏈接到Gdiplus:

#pragma comment( lib, "Gdiplus.lib")


編輯

或在WM_PAINT使用Gdiplus::Image

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);

    if (image)
    {
        RECT rc;
        GetClientRect(hwnd, &rc);
        Gdiplus::Graphics g(hdc);
        g.DrawImage(image, Gdiplus::Rect(0, 0, rc.right, rc.bottom));
    }

    EndPaint(hwnd, &ps);
    return 0;
}

窗口過程中的WM_PAINT

 case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); if (image) { RECT rc; GetClientRect(hwnd, &rc); Gdiplus::Graphics g(hdc); g.DrawImage(image, Gdiplus::Rect(0, 0, rc.right, rc.bottom)); } EndPaint(hwnd, &ps); return 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM