#include <Windows.h>
// Declare global variables
HWND hWnd;
HBITMAP hBitmap;
// Function to load and set a new image in the PictureBox
void ChangeImage(const char* imagePath) {
HBITMAP hNewBitmap = (HBITMAP)LoadImageA(NULL, imagePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hNewBitmap != NULL) {
if (hBitmap != NULL) {
DeleteObject(hBitmap);
}
SendMessage(GetDlgItem(hWnd, IDC_PICTUREBOX), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hNewBitmap);
hBitmap = hNewBitmap;
}
}
// Windows message handler function
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
// Create PictureBox
CreateWindowEx(0, L"STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP,
10, 10, 300, 200, hwnd, (HMENU)IDC_PICTUREBOX, GetModuleHandle(NULL), NULL);
break;
case WM_DESTROY:
// Release resources and exit
if (hBitmap != NULL) {
DeleteObject(hBitmap);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
// Entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Register the window class
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"PictureBoxExample";
RegisterClass(&wc);
// Create the main window
hWnd = CreateWindow(L"PictureBoxExample", L"PictureBox Example", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL);
// Show the window
ShowWindow(hWnd, nCmdShow);
// Message loop
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}