简体   繁体   中英

How do I set the image of a (Win32 API) Picture Control at runtime?

The test case is very simple. I want to set the contents of a Picture Control (IDC_STATIC1) upon the pressing of a button (IDC_BUTTON2) to a bitmap resource (IDB_BITMAP1). The problem I'm encountering is that when I press the button, the Picture Control doesn't load the image. I have verified that the button press IS being registered properly and the return value from LoadImage is NOT null.

The following code is the message handler for the dialog:

BOOL WINAPI DialogProc2(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            EndDialog(hWindow, 0);
            DestroyWindow(hWindow);
            return TRUE;

        case WM_INITDIALOG:
            return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDC_BUTTON2)
            {
                HBITMAP bmp;
                bmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
                if (bmp == NULL)
                {
                    MessageBox(NULL, "Error", "ERROR", MB_OK);
                    return TRUE;
                }
                SendDlgItemMessage(hWindow, IDC_STATIC1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp);
                //placing a MessageBox here proves that the button press is being registered properly and that the message is being sent, but nothing is happening...
            }
            return TRUE;
    }

    return FALSE;
}

It would seem the "Name" property of the Picture Control was set to "IDC_STATIC1" but the "ID" property was set to "IDC_STATIC". Changing it to "IDC_STATIC1" solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM