Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

用GDI函数写一个显示bmp图像的程序,尽量做到所有WINDOWS都能运行,直接用g++就能编译运行的,程序内不要出现非utf-8的字!

  1. User

    用GDI函数写一个显示bmp图像的程序,尽量做到所有WINDOWS都能运行,直接用g++就能编译运行的,程序内不要出现非utf-8的字!

  2. Assistant

    下面是一个用GDI函数显示bmp图像的程序,使用了g++编译器,并且程序内的字符均为UTF-8编码。

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow) {
        HWND hwnd;
        MSG  msg;
        WNDCLASSW wc;
        // 创建窗口类
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.lpszClassName = L"Window";
        wc.hInstance = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpszMenuName = NULL;
        wc.lpfnWndProc = WndProc;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        RegisterClassW(&wc);
        // 创建窗口
        hwnd = CreateWindowW(wc.lpszClassName, L"Display BMP", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                             100, 100, 400, 300, NULL, NULL, hInstance, NULL);
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
        // 消息循环
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        HDC hdc;
        PAINTSTRUCT ps;
        BITMAP bmp;
        HBITMAP hBmp;
        HDC hdcMem;
        HANDLE hOld;
        switch (msg) {
            case WM_CREATE:
                // 从bmp文件创建一个位图对象
                hBmp = (HBITMAP)LoadImageW(NULL, L"path_to_bmp_file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
                if (hBmp == NULL) {
                    MessageBoxW(hwnd, L"Failed to load bmp image.", L"Error", MB_OK | MB_ICONERROR);
                    return -1;
                }
                // 获取位图对象的信息
                GetObject(hBmp, sizeof(BITMAP), &bmp);
                // 创建一个内存设备上下文
                hdc = GetDC(hwnd);
                hdcMem = CreateCompatibleDC(hdc);
                // 选择位图对象到内存设备上下文中
                hOld = SelectObject(hdcMem, hBmp);
                // 绘制位图到窗口客户区
                BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY);
                // 清理内存
                SelectObject(hdcMem, hOld);
                DeleteDC(hdcMem);
                ReleaseDC(hwnd, hdc);
                return 0;
            case WM_CLOSE:
                // 释放位图对象
                DeleteObject(hBmp);
                PostQuitMessage(0);
                return 0;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
    

    在上述代码中,LoadImageW函数用于从文件中加载bmp图像,你需要将L"path_to_bmp_file.bmp"替换为你要显示的bmp文件的路径。

  3. User

    e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x24c): undefined reference to __imp_GetObjectA' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x270): undefined reference to __imp_CreateCompatibleDC' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x288): undefined reference to __imp_SelectObject' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x2dc): undefined reference to __imp_BitBlt' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x2f0): undefined reference to __imp_SelectObject' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x300): undefined reference to __imp_DeleteDC' e:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:\tmp\ccM0aV73.o:ml.cpp:(.text+0x32b): undefined reference to `__imp_DeleteObject' collect2.exe: error: ld returned 1 exit status

  4. Assistant

    这个链接错误通常发生在在编译和链接程序时没有正确指定GDI库。你可以在编译时使用-lgdi32选项来链接GDI库,指令如下:

    g++ -o display_bmp ml.cpp -lgdi32
    

    在上述指令中,display_bmp是生成的可执行文件的名称,ml.cpp是包含源代码的文件名。

    这样,编译器将会自动链接GDI库,并解决未定义的引用问题。

  5. User

    窗口D显示的是白的,没有图像

  6. Assistant

    如果窗口显示的是白色而不是图像,可能有几个原因。

    首先,请确保 LoadImageW 函数成功加载了 BMP 图像文件。你可以添加以下代码来检查加载是否成功:

    hBmp = (HBITMAP)LoadImageW(NULL, L"path_to_bmp_file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    if (hBmp == NULL) {
        MessageBoxW(hwnd, L"Failed to load bmp image.", L"Error", MB_OK | MB_ICONERROR);
        return -1;
    }
    

    请将 L"path_to_bmp_file.bmp" 替换为实际的 BMP 文件路径。

    其次,请确保 BMP 图像的尺寸和窗口的尺寸匹配。如果 BMP 图像的宽度和高度大于窗口的客户区域的宽度和高度,图像可能无法完全显示。你可以尝试调整窗口的大小,以确保足够容纳整个图像。

    最后,请确保在 WM_PAINT 消息处理中更新窗口的绘图内容。你可以使用 InvalidateRect(hwnd, NULL, TRUE); 函数强制窗口重绘。将其放在 WM_CREATE 消息处理中的 BitBlt 调用之后。

    如果上述方法仍然无法解决问题,请提供更多的代码,以便我们检查问题的更多细节。

  7. User

    在哪里添加

  8. Assistant

    你需要在WM_CREATE消息的处理代码中添加检查加载图像是否成功的代码,并在WM_PAINT消息的处理代码中添加重绘窗口的代码。

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        static HBITMAP hBmp;
    
        switch (msg) {
            case WM_CREATE:
                // 从bmp文件创建一个位图对象
                hBmp = (HBITMAP)LoadImageW(NULL, L"path_to_bmp_file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
                if (hBmp == NULL) {
                    MessageBoxW(hwnd, L"Failed to load bmp image.", L"Error", MB_OK | MB_ICONERROR);
                    return -1;
                }
                return 0;
            case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                
                // 创建一个与窗口客户区域相同尺寸的内存设备上下文
                HDC hdcMem = CreateCompatibleDC(hdc);
                RECT clientRect;
                GetClientRect(hwnd, &clientRect);
    
                // 选择位图对象到内存设备上下文中
                HBITMAP hOldBmp = (HBITMAP)SelectObject(hdcMem, hBmp);
    
                // 绘制位图到窗口客户区
                BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, hdcMem, 0, 0, SRCCOPY);
    
                // 清理内存
                SelectObject(hdcMem, hOldBmp);
                DeleteDC(hdcMem);
    
                EndPaint(hwnd, &ps);
                return 0;
            }
            case WM_CLOSE:
                // 释放位图对象
                DeleteObject(hBmp);
                PostQuitMessage(0);
                return 0;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
    

    在上述代码中,我们在WM_CREATE消息处理中加载图像并将其保存到hBmp变量中。然后,在WM_PAINT消息处理中,我们使用BitBlt函数绘制图像到窗口的客户区域。请将L"path_to_bmp_file.bmp"替换为你要显示的BMP文件的路径。

  9. User

    把改完的代码给我

  10. Assistant

    下面是经过修改的代码供参考:

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow) {
        HWND hwnd;
        MSG  msg;
        WNDCLASSW wc;
        // 创建窗口类
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.lpszClassName = L"Window";
        wc.hInstance = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpszMenuName = NULL;
        wc.lpfnWndProc = WndProc;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        RegisterClassW(&wc);
        // 创建窗口
        hwnd = CreateWindowW(wc.lpszClassName, L"Display BMP", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                             100, 100, 400, 300, NULL, NULL, hInstance, NULL);
        // 消息循环
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        static HBITMAP hBmp;
    
        switch (msg) {
            case WM_CREATE:
                // 从bmp文件创建一个位图对象
                hBmp = (HBITMAP)LoadImageW(NULL, L"path_to_bmp_file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
                if (hBmp == NULL) {
                    MessageBoxW(hwnd, L"Failed to load bmp image.", L"Error", MB_OK | MB_ICONERROR);
                    return -1;
                }
                return 0;
            case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                
                // 创建一个与窗口客户区域相同尺寸的内存设备上下文
                HDC hdcMem = CreateCompatibleDC(hdc);
                RECT clientRect;
                GetClientRect(hwnd, &clientRect);
    
                // 选择位图对象到内存设备上下文中
                HBITMAP hOldBmp = (HBITMAP)SelectObject(hdcMem, hBmp);
    
                // 绘制位图到窗口客户区
                BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, hdcMem, 0, 0, SRCCOPY);
    
                // 清理内存
                SelectObject(hdcMem, hOldBmp);
                DeleteDC(hdcMem);
    
                EndPaint(hwnd, &ps);
                return 0;
            }
            case WM_CLOSE:
                // 释放位图对象
                DeleteObject(hBmp);
                PostQuitMessage(0);
                return 0;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
    

    请将代码中的 L"path_to_bmp_file.bmp" 替换为你要显示的BMP文件的路径。然后使用g++编译器编译该程序即可。