c++ - 图像 (PNG) 不显示在 C++ Win32 项目的最终 exe 中

标签 c++ gdi+

首先,我的一小部分代码示例:

// Includes and namespaces
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <string>
#include <tchar.h>
#include <objidl.h>
#include <gdiplus.h>

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

/*---------------------------------------------------------------------------------------------*/
// Forward declarations

static TCHAR szWindowClassIntro[] = _T("codbo2_trainer_intro");
static TCHAR szTitle[] = _T("Call of Duty Black Ops II Trainer");

HINSTANCE hInst = NULL;

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

const int INTRO_WIDTH = 850;
const int INTRO_HEIGHT = 534;

/*---------------------------------------------------------------------------------------------*/
// Following function is used to paint everything on intro window

void onPaintIntro(HDC hdc)
{
    Graphics graphics(hdc);
    graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
    FontFamily fontFamily(L"Calibri");

    // Draw background image for intro
    Image introBg(L"Intro_border.png"); // IMAGE CAN BE FOUND HERE: http://i.imgur.com/H5Jc4Fj.jpg
    UINT introBgWidth = introBg.GetWidth();
    UINT introBgHeight = introBg.GetHeight();
    Rect introBgRect(0, 0, introBgWidth, introBgHeight);
    graphics.DrawImage(&introBg, introBgRect, 0, 0, introBgWidth, introBgHeight, UnitPixel);
}

/*---------------------------------------------------------------------------------------------*/
// Main function of trainer

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // Initialize GDI+.
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    hInst = hInstance;

    WNDCLASSEX wcexIntro;
    wcexIntro.cbSize = sizeof(WNDCLASSEX);
    wcexIntro.style = CS_HREDRAW | CS_VREDRAW;
    wcexIntro.lpfnWndProc = WndProcIntro;
    wcexIntro.cbClsExtra = 0;
    wcexIntro.cbWndExtra = 0;
    wcexIntro.hInstance = hInst;
    wcexIntro.hIcon = NULL;
    wcexIntro.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcexIntro.hbrBackground = CreateSolidBrush(RGB(5, 0, 0));
    wcexIntro.lpszMenuName = NULL;
    wcexIntro.lpszClassName = szWindowClassIntro;
    wcexIntro.hIconSm = NULL;

    if (!RegisterClassEx(&wcexIntro))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Call of Duty Black Ops II Trainer"),
            NULL);

        return 1;
    }

    HWND hWndIntro = CreateWindow(
        szWindowClassIntro,
        szTitle,
        WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        INTRO_WIDTH, INTRO_HEIGHT,
        NULL,
        NULL,
        hInst,
        NULL
        );

    SetWindowLong(hWndIntro, GWL_STYLE, WS_VISIBLE);
    SetWindowLong(hWndIntro, GWL_EXSTYLE, GetWindowLong(hWndIntro, GWL_EXSTYLE) | WS_EX_LAYERED);
    SetLayeredWindowAttributes(hWndIntro, RGB(5, 0, 0), 0, LWA_COLORKEY);

    if (!hWndIntro)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Call of Duty Black Ops II Trainer"),
            NULL);

        return 1;
    }

    // Show window
    ShowWindow(hWndIntro, nCmdShow);
    UpdateWindow(hWndIntro);

    // Main message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    GdiplusShutdown(gdiplusToken);
    return (int)msg.wParam;
}

/*---------------------------------------------------------------------------------------------*/
// Callback function to process input on intro window

LRESULT CALLBACK WndProcIntro(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    WNDCLASSEX wcex;
    wcex.hCursor = LoadCursor(NULL, IDC_HAND);

    switch (message)
    {    
    // WM_PAINT
    case WM_PAINT:
    {
        hdc = BeginPaint(hWnd, &ps);

        onPaintIntro(hdc);

        EndPaint(hWnd, &ps);
        break;
    }

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }

    default:
    {
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    }

    return 0;
}

我一直在用 c++ 为使命召唤做一个培训师(第一次 c++ 体验,抱歉 n00b),今天终于完成了。 有一个小问题; 我的项目的最终 exe 中的图像 (PNG) 未显示。

图像可以在这里查看:http://imgur.com/6mWtETS,0ajBwE9,JlMVUYR,k8AIbAn#0

(前两个是我在调试训练器时得到的。

我认为这与我使用 GDI+ 加载图像的方式有关,但我不知 Prop 体是什么,因为训练器不会生成任何错误。

如果有人想知道,我正在使用 Visual Studio 2013。

最佳答案

您正在尝试从文件加载图像(参见 Image GDI+ object constructor ),这意味着该图像必须与您的应用程序位于 Windows 平台上的同一目录中。

关于c++ - 图像 (PNG) 不显示在 C++ Win32 项目的最终 exe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710750/

相关文章:

c# - 在多个显示器上绘制所有窗口

c++ - boost::split(结果,输入,boost::is_any_of (“(, )”))无法分割空白

c++ - 如何在C++中搜索字符串数组

c++ - 矩阵类 : "No instance of overloaded function push_back matches the argument list"

c# - 我如何在 GDI+ 中将一个位图图像叠加到另一个位图图像上?

c# - CreateGraphics() 方法和 Paint 事件参数

c++ - 多重继承模板类

c++ - 使用 boost 求解(密集)线性系统 Ax=b

c# - 带有图形转换的 TextRenderer

.net - 在多线程中使用 GDI+ 的问题 (VB.NET)