我在用C++编译一些代码时遇到了麻烦。

码:

#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nShowCmd )
{

    static char name[] = "My Application";
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // Step 1: Registering the Window Class
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW + 1 );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = name;
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

    if ( !RegisterClassEx( & wc ) )
    {

        MessageBox( NULL, "Window Registration Failed!", "Registration Failure", MB_ICONEXCLAMATION | MB_OK );

        return 0;

    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, name, "My First Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL );

    ShowWindow( hwnd, nShowCmd );
    UpdateWindow( hwnd );

    // Step 3: The Message Loop
    while( GetMessage( & Msg, NULL, 0, 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;
    RECT rect;

    switch( msg )
    {

        case WM_PAINT:

            hdc = BeginPaint( hwnd, & ps );

            GetClientRect( hwnd, & rect );

            Rectangle( hdc, rect.right / 2 - 50, rect.bottom / 2 - 20, rect.right / 2 + 50, rect.bottom / 2 + 20 );

            DrawText( hdc, "Hello World!", -1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );

            EndPaint( hwnd, & ps );

            break;

        case WM_CLOSE:

            DestroyWindow( hwnd );

            break;

        case WM_DESTROY:

            PostQuitMessage( 0 );

            break;

    }

    return DefWindowProc( hwnd, msg, wParam, lParam );

}

我遵循的教程说它可以正确编译,没有问题,但这就是我的编译器的样子:
NPP_EXEC: "Compile C++ File"
NPP_SAVE: G:\C++\src\Win32\Hello World! Window.cpp
g++ -o "G:\C++\src\Win32\Hello World! Window" "G:\C++\src\Win32\Hello World! Window.cpp" -static
Process started >>>
C:\Users\Braeden\AppData\Local\Temp\cc3N4Ls1.o:Hello World! Window.cpp:(.text+0x29b): undefined reference to `__imp_Rectangle'
collect2.exe: error: ld returned 1 exit status
<<< Process finished.
================ READY ================

我没有使用任何流行的IDE。我使用Notepad ++作为文本编辑器,并使用MinGW发行版作为编译器。我在Win7 x64上。有人可以告诉我我在做什么错吗?

最佳答案

Rectangle Gdi32.dll提供。您应该是linking against Gdi32.lib

10-06 01:04