C++ 在 Code::Blocks 中使用 Gdiplus with MinGW 编译器

C++ Using Gdiplus in Code::Blocks with MinGW compiler

本文关键字:with Gdiplus MinGW 编译器 Code Blocks C++      更新时间:2023-10-16

我的开发环境:视窗 7 64 位代码::块 13.12 与 MinGW 编译器。在 Code::blocks 中更改了设置 - 仅在设置 -> 编译器 ->链接器设置 ->其他链接器选项:-static-libgcc -static-libstdc++

这是我失败的代码:主要:

#include "main.h"
#include <iostream>
#include <objidl.h>
#include <Gdiplus.h>

using namespace std;
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")

MainCode /// Look better even then main() :).
{
    Execute_InitializeWindow; /// Initialize root window (For WinGUI)

    HWND hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW | WS_VISIBLE, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    Graphics* hGraphics = Graphics::FromHWND(hwnd,false); /// <---------------- HERE IT FAILS!

    /// Massage handler
    while (1){ if (GetMessage (&msg, NULL, 0, 0) <= 0) {break;}
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&msg);
        //cout << msg << endl;
        /* Send message to WindowProcedure */
        DispatchMessage(&msg);
    }

    return msg.wParam; // The program return-value is 0 - The value that PostQuitMessage() gave
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc (hwnd, message, wParam, lParam);
    switch (message)                  /* handle the messages */
    {

            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            //cout << 1;
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

主代码的 Heder(命名为 main.h):

#include <windows.h>


/// ---------------------------------------- Codes for Initialize of main GUI -----------------------------------------
#define MainCode int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); // Declare Windows procedure
TCHAR szClassName[ ] = "my_program_root_name";

#define Execute_InitializeWindow                        
                                                        
    MSG msg;                                            
                                                        
    WNDCLASSEX wincl;                                   
    wincl.hInstance = hThisInstance;                    
    wincl.lpszClassName = szClassName;                  
    wincl.lpfnWndProc = WindowProcedure;                
    wincl.style = CS_DBLCLKS;                           
    wincl.cbSize = sizeof (WNDCLASSEX);                 
                                                        
                                                        
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);     
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);   
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);       
    wincl.lpszMenuName = NULL;                          
    wincl.cbClsExtra = 0;                               
    wincl.cbWndExtra = 0;                               
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;    
                                                        
    if (!RegisterClassEx (&wincl)) {return 1;}          

/// --------------------------------------------------------------------------------------------------

问题:排队失败Graphics* hGraphics = Graphics::FromHWND(hwnd,false);

当我尝试运行它时,出现此错误:

mingw32-g++.exe   -c "D:גיבוייםDATADesktopcpp gdi plus problemmain.cpp" -o "D:גיבוייםDATADesktopcpp gdi plus problemmain.o"
mingw32-g++.exe  -o "D:גיבוייםDATADesktopcpp gdi plus problemmain.exe" "D:גיבוייםDATADesktopcpp gdi plus problemmain.o"  -static-libgcc -static-libstdc++  
D:גיבוייםDATADesktopcpp gdi plus problemmain.o:main.cpp:(.text$_ZN7Gdiplus11GdiplusBasenwEj[__ZN7Gdiplus11GdiplusBasenwEj]+0xd): undefined reference to `GdipAlloc@4'
D:גיבוייםDATADesktopcpp gdi plus problemmain.o:main.cpp:(.text$_ZN7Gdiplus11GdiplusBasedlEPv[__ZN7Gdiplus11GdiplusBasedlEPv]+0xd): undefined reference to `GdipFree@4'
D:גיבוייםDATADesktopcpp gdi plus problemmain.o:main.cpp:(.text$_ZN7Gdiplus8GraphicsC1EP6HWND__i[__ZN7Gdiplus8GraphicsC1EP6HWND__i]+0x3a): undefined reference to `GdipCreateFromHWNDICM@8'
D:גיבוייםDATADesktopcpp gdi plus problemmain.o:main.cpp:(.text$_ZN7Gdiplus8GraphicsC1EP6HWND__i[__ZN7Gdiplus8GraphicsC1EP6HWND__i]+0x57): undefined reference to `GdipCreateFromHWND@8'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
4 error(s), 0 warning(s) (0 minute(s), 1 second(s))

我花了很多时间在上面..我不知道该怎么办。我意识到我需要 Gdiplus 的"def"文件。我发现这个:https://sourceforge.net/u/earnie/winapi/winapi/ci/master/tree/lib/gdiplus.def

我试图通过添加以下内容(在编译器 -> 链接器设置 ->其他链接器选项中)在链接器设置中添加它:-def gdiplus.def .在这种情况下,错误是相同的,但这次有所不同 -在编译期间,它会打印大量有关从 dll 中提取函数的错误。它无法从 DLL 中提取所有函数。具有相同错误消息的所有故障... symbol not definded

我感到有点迷茫..我在谷歌上寻找解决方案,但没有帮助..我对C++很陌生

感谢帮手!

-lgdiplus添加到链接器选项。