以下是在 MFC 中使用 CImage 类加载和显示 BMP 图片,并根据屏幕分辨率调整图片大小的示例代码。

// 在头文件中定义 CImage 对象
CImage m_Image;

// 在 OnInitDialog() 函数中加载和显示图片
BOOL CMyDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 加载图片
    m_Image.Load(_T("image.bmp"));

    // 获取屏幕分辨率
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);

    // 计算图片显示区域的大小
    int imageWidth = (screenWidth - 2 * 20 - 3 * 10) / 4;
    int imageHeight = (screenHeight - 2 * 10 - 2 * 10) / 3;

    // 创建显示图片的静态控件
    CStatic* pImageControl = new CStatic;
    pImageControl->Create(_T(""), WS_VISIBLE | WS_CHILD | SS_BITMAP, CRect(20, 10, 20 + imageWidth, 10 + imageHeight), this);
    pImageControl->SetBitmap(m_Image.Detach());

    return TRUE;
}

上述代码中,首先在头文件中定义了一个 CImage 对象 m_Image。在 OnInitDialog() 函数中,通过调用 m_Image.Load() 函数加载 BMP 图片。然后,通过 GetSystemMetrics() 函数获取屏幕的宽度和高度,接着根据屏幕分辨率计算出图片显示区域的大小。最后,通过创建一个静态控件,并设置其大小和位置,将加载的图片显示在静态控件中。

需要注意的是,示例中的图片文件名为'image.bmp',请根据实际情况修改。另外,根据具体需求,可能需要对图片进行缩放或裁剪,以适应不同的屏幕分辨率。


原文地址: https://www.cveoy.top/t/topic/phgU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录