以下是使用MFC读取res中的5张bmp图片,并在规定的坐标上绘制的示例代码:

#include "stdafx.h"
#include "resource.h" // Include the resource.h file that contains the resource IDs for the BMP images

#define NUM_IMAGES 5 // Number of images to load and draw

CBitmap bmpImages[NUM_IMAGES]; // Array to store the loaded bitmaps

void LoadImages()
{
    for (int i = 0; i < NUM_IMAGES; i++)
    {
        // Load the BMP image from the resource
        bmpImages[i].LoadBitmapW(IDB_BITMAP1 + i);
    }
}

void DrawImages(CPaintDC& dc)
{
    // Set the initial position for drawing the images
    int x = 10;
    int y = 10;

    for (int i = 0; i < NUM_IMAGES; i++)
    {
        // Create a memory device context for each image
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);

        // Select the bitmap into the memory device context
        CBitmap* pOldBmp = memDC.SelectObject(&bmpImages[i]);

        // Get the size of the bitmap
        BITMAP bmpInfo;
        bmpImages[i].GetBitmap(&bmpInfo);

        // Draw the bitmap on the main device context
        dc.BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCCOPY);

        // Move to the next position
        x += bmpInfo.bmWidth + 10;

        // Restore the original bitmap in the memory device context
        memDC.SelectObject(pOldBmp);
    }
}

class CMyDialog : public CDialogEx
{
public:
    CMyDialog() : CDialogEx(IDD_MYDIALOG) {}

protected:
    virtual void DoDataExchange(CDataExchange* pDX) override
    {
        CDialogEx::DoDataExchange(pDX);
    }

    virtual BOOL OnInitDialog() override
    {
        CDialogEx::OnInitDialog();

        // Load the images from the resource
        LoadImages();

        return TRUE;
    }

    virtual void OnPaint() override
    {
        CPaintDC dc(this); // device context for painting

        // Draw the images on the dialog
        DrawImages(dc);
    }

    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
END_MESSAGE_MAP()

int main()
{
    CWinApp app;
    CMyDialog dialog;
    dialog.DoModal();

    return 0;
}

在这个示例代码中,我们首先定义了一个全局的CBitmap数组bmpImages来保存加载的位图。然后我们定义了两个函数LoadImages()DrawImages(CPaintDC& dc)

LoadImages()函数用于从资源中加载位图。我们使用一个循环来加载每个位图,通过LoadBitmapW()函数和资源ID来加载每个位图。我们假设资源中的位图ID分别为IDB_BITMAP1IDB_BITMAP5

DrawImages(CPaintDC& dc)函数用于在指定坐标上绘制位图。我们使用一个循环来绘制每个位图。首先,我们创建一个内存设备上下文(CDC)来存储位图。然后,我们选择位图到内存设备上下文中,并获取位图的大小。接下来,我们使用BitBlt()函数将位图绘制到主设备上下文(dc)上的指定位置。最后,我们移动到下一个位置,并恢复内存设备上下文中的原始位图。

CMyDialog类中,我们重写了OnInitDialog()函数,在对话框初始化时调用LoadImages()函数来加载位图。我们还重写了OnPaint()函数,在对话框绘制时调用DrawImages()函数来绘制位图。

最后,在main()函数中,我们创建了一个CMyDialog对象并调用DoModal()函数来显示对话框。

请注意,上述代码仅为示例,实际使用时可能需要根据具体的应用场景进行适当的修改。

MFC读res中的5张bmp图片加载出来画在规定坐标上

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

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