在运行时将图像加载到对话框图片控件中

时间:2014-01-23 05:35:57

标签: c++ winapi

我最近购买了一台具有高dpi屏幕的笔记本电脑,并且一直在调整我的一个程序以利用更高的分辨率。在我的一个Win32对话框中,我显示了一个位图图片。通常我通过资源编辑器将其分配给图片控件,而不必处理它。现在我需要根据屏幕分辨率显示两个图像中的一个。我知道如何加载位图并获取其处理,但是如何在对话框启动期间将其分配给图片控件,以便内置对话框例程显示它?我想我应该在对话框中放置一个静态图片控件,但我找不到一个win32示例,说明如何在运行时将图像分配给该图片控件。看起来它应该很简单,但我似乎无法找到适合的Windows功能。我没有使用MFC。

- 特里

2 个答案:

答案 0 :(得分:2)

这是一个例子。基本上,您需要将STM_SETIMAGE消息发送到图片控件,以及您要显示的图像的句柄。

#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);
}

答案 1 :(得分:0)

  1. 从资源
  2. 加载位图
  3. 向优化校准发送消息
  4. 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;