user3226311
user3226311

Reputation: 1

Loading an image into a dialog picture control during runtime

I recently acquired a laptop with a high dpi screen and have been adapting one of my programs to take advantage of the higher resolution. In one of my Win32 dialog boxes I display a bitmap picture. Normally I have that assigned to a picture control via the resource editor and don't have to deal with it. Now I need to display one of two images depending on the screen resolution. I know how to load a bitmap and get its handle, but how do I assign it to the picture control during the dialog's initiation so that the built in dialog routine will display it? I'm thinking I should put a static picture control in the dialog, but I can't find a win32 example of how I assign the image to that picture control at run time. Seems like it should be simple, but I can't seem to find a windows function that's appropriate. I'm not using MFC.

--Terry

Upvotes: 0

Views: 12782

Answers (2)

vnduan
vnduan

Reputation: 41

  1. Load a bitmap from resource
  2. Send a message to Picture Control

case IDC_BUTTON_RELOAD:
        hbitmap = (HBITMAP)LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP_FULL));
        hbitmap2 = (HBITMAP)LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP_EMPTY));
        /*hdc = GetDC(hDlg);
        image_dc = CreateCompatibleDC(hdc);
        old_hbitmap = (HBITMAP)SelectObject(image_dc,hbitmap);
        BitBlt(hdc,1,0,530,450,image_dc,0,0,SRCCOPY);*/
        SendMessage(GetDlgItem(hDlg,IDC_STATIC_PICTURE2), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbitmap);
        SendMessage(GetDlgItem(hDlg,IDC_STATIC_PICTURE), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbitmap2);
        return TRUE;
case IDC_BUTTON_EXIT:
        MessageBox(hDlg,_T("Thank you"),_T("Close"),MB_ICONINFORMATION|MB_OK);
        DeleteObject(hbitmap);
        DeleteObject(hbitmap2);
        DestroyWindow(hDlg);
        return TRUE;

Upvotes: 1

enhzflep
enhzflep

Reputation: 13089

Here's an example. Basically, you need to send the STM_SETIMAGE message to the picture control, along with the handle of the image you'd like to display.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HBITMAP bmp1, bmp2;
    static bool isImg1 = true;

    switch(uMsg)
    {
        case WM_INITDIALOG:
            bmp1 = (HBITMAP)SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_GETIMAGE, IMAGE_BITMAP, 0);
            bmp2 = (HBITMAP)LoadImage(NULL, "bitmap2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            return TRUE;

        case WM_DESTROY:
            DeleteObject(bmp1);
            DeleteObject(bmp2);
            return true;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    if (isImg1)
                        SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp2);
                    else
                        SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp1);
                    isImg1 = !isImg1;
                    return TRUE;
            }
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

Upvotes: 3

Related Questions