要在一个界面上,左右边距为 20,上下边距为 10,使用 CBitmap 加载 bmp 图片为 3 行 4 列且图片之间的间隔为 10,根据屏幕分辨率调整图片大小,要求正常显示内容,可以按照以下步骤进行操作:

  1. 首先,创建一个 MFC 对话框应用程序。
  2. 在对话框的资源编辑器中添加一个静态控件,作为显示图片的容器。
  3. 在对话框类的头文件中声明一个 CBitmap 对象和一个 CRect 对象来保存图片和容器的位置信息:
CBitmap m_bitmap;
CRect m_rect;
  1. 在 OnInitDialog() 函数中,获取屏幕的分辨率并计算出图片的大小:
// 获取屏幕分辨率
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);

// 计算图片的大小
int imageWidth = (screenWidth - 2 * 20 - 3 * 10) / 4;
int imageHeight = (screenHeight - 2 * 10 - 2 * 20) / 3;
  1. 加载图片并调整大小:
// 加载图片
m_bitmap.LoadBitmap(IDB_BITMAP);

// 获取图片的原始大小
BITMAP bmpInfo;
m_bitmap.GetBitmap(&bmpInfo);
int originalWidth = bmpInfo.bmWidth;
int originalHeight = bmpInfo.bmHeight;

// 计算调整后的图片大小
int adjustedWidth = imageWidth;
int adjustedHeight = originalHeight * imageWidth / originalWidth;
if (adjustedHeight > imageHeight) {
    adjustedHeight = imageHeight;
    adjustedWidth = originalWidth * imageHeight / originalHeight;
}

// 设置图片的位置和大小
m_rect.left = 20;
m_rect.right = m_rect.left + adjustedWidth;
m_rect.top = 10;
m_rect.bottom = m_rect.top + adjustedHeight;
  1. 在 OnPaint() 函数中绘制图片:
CPaintDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap* pOldBitmap = memDC.SelectObject(&m_bitmap);
dc.StretchBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), &memDC, 0, 0, originalWidth, originalHeight, SRCCOPY);
memDC.SelectObject(pOldBitmap);
  1. 最后,在 OnInitDialog() 函数中调用 Invalidate() 函数来触发 OnPaint() 函数的调用,以显示图片:
Invalidate();

通过以上步骤,就能在一个界面上显示加载的 bmp 图片,并根据屏幕分辨率调整图片大小,以保证正常显示。

MFC C++: 调整大小并显示多行多列的 BMP 图片

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

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