我正在尝试使用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;
}
有人可以帮忙吗?抱歉我的英文
答案 0 :(得分:1)
如果要使用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;
答案 1 :(得分: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);