0

我正在尝试使用 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;
}

有人可以帮忙吗?对不起我的英语

4

2 回答 2

2

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

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;
于 2013-08-12T00:57:45.220 回答
0

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);
于 2014-05-02T10:17:19.843 回答