如何从资源上显示PictureBox上的图像?

时间:2021-05-19 09:00:47

I'm trying to code the GUI of a simple program with pure Windows API in C. I'm having trouble to show an image on a picturebox. The image is on a resource. The code is this:

我正在尝试用C语言中的纯Windows API编写简单程序的GUI。我无法在图片框上显示图像。图像位于资源上。代码是这样的:

#include <Windows.h>
#include "resource.h"
#define IDC_MAIN_BUTTON 101 
const char g_szClassName[] = "myWindowClass";
HBITMAP hBitmap = NULL;


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hWndButton;
char *idb="IDB_BITMAP1";
switch(msg)
{
case WM_CREATE:
    hBitmap = (HBITMAP)LoadImage(hInstance,MAKEINTRESOURCE(idb), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        hWndButton=CreateWindowEx(NULL, 
    "BUTTON",
    "Encrypt",
    WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_FLAT,
    150,
    225,
    100,
    24,
    hwnd,
    (HMENU)IDC_MAIN_BUTTON,
    GetModuleHandle(NULL),
    NULL);

    break;
case WM_PAINT:
    PAINTSTRUCT     ps;
    HDC             hdc;
    BITMAP          bitmap;
    HDC             hdcMem;
    HGDIOBJ         oldBitmap;

    hdc = BeginPaint(hwnd, &ps);

    hdcMem = CreateCompatibleDC(hdc);
    oldBitmap = SelectObject(hdcMem, hBitmap);

    GetObject(hBitmap, sizeof(bitmap), &bitmap);
    BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, oldBitmap);
    DeleteDC(hdcMem);

    EndPaint(hwnd, &ps);
    break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        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;

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;
}

hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The title of my window",
    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
    CW_USEDEFAULT, CW_USEDEFAULT, 450, 300,
    NULL, NULL, hInstance, NULL);

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

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

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return Msg.wParam;
}

Somebody can help? Sorry about my english

有人可以帮忙吗?抱歉我的英语

2 个解决方案

#1


1  

If you want to use LR_LOADFROMFILE you must give a BMP filename not a resource. With ressource try with something like that.

如果要使用LR_LOADFROMFILE,则必须提供BMP文件名而不是资源。用ressource尝试类似的东西。

case WM_CREATE:
{
    LPCREATESTRUCT pCreat = (LPCREATESTRUCT) lParam;
    hBitmap = (HBITMAP) LoadBitmap(pCreat->hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
    or
    hBitmap = (HBITMAP) LoadImage(pCreat->hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 0);
    [...]
}
break;

#2


0  

There's no "PictureBox" in Win32 API, but you can use "Static Control" and pass STM_SETIMAGE message to the control :

Win32 API中没有“PictureBox”,但您可以使用“静态控制”并将STM_SETIMAGE消息传递给控件:

HWND staticimage1 = CreateWindowW(L"STATIC", L"This is Label", WS_CHILD | WS_VISIBLE | SS_BITMAP, 50, 50, 100, 100, hwnd, (HMENU) 12345, NULL, NULL);
HBITMAP hbitmap = LoadImageW(NULL, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 0);

SendMessageW(staticimage1, STM_SETIMAGE, IMAGE_BITMAP, hbitmap);

#1


1  

If you want to use LR_LOADFROMFILE you must give a BMP filename not a resource. With ressource try with something like that.

如果要使用LR_LOADFROMFILE,则必须提供BMP文件名而不是资源。用ressource尝试类似的东西。

case WM_CREATE:
{
    LPCREATESTRUCT pCreat = (LPCREATESTRUCT) lParam;
    hBitmap = (HBITMAP) LoadBitmap(pCreat->hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
    or
    hBitmap = (HBITMAP) LoadImage(pCreat->hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 0);
    [...]
}
break;

#2


0  

There's no "PictureBox" in Win32 API, but you can use "Static Control" and pass STM_SETIMAGE message to the control :

Win32 API中没有“PictureBox”,但您可以使用“静态控制”并将STM_SETIMAGE消息传递给控件:

HWND staticimage1 = CreateWindowW(L"STATIC", L"This is Label", WS_CHILD | WS_VISIBLE | SS_BITMAP, 50, 50, 100, 100, hwnd, (HMENU) 12345, NULL, NULL);
HBITMAP hbitmap = LoadImageW(NULL, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 0);

SendMessageW(staticimage1, STM_SETIMAGE, IMAGE_BITMAP, hbitmap);