图像 (PNG) 未显示在 c++ Win32 项目的最终执行中

Images (PNG) don't show up in final exe of c++ Win32 project

本文关键字:项目 执行 Win32 c++ PNG 显示 图像      更新时间:2023-10-16

首先,我的一小段代码:

// 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+加载图像的方式有关,但我不知道确切的原因,因为训练器不会产生任何错误。

如果有人想知道,我用的是Visual Studio 2013。

您正在尝试从文件(cfr)加载图像。图像GDI+对象构造函数),这意味着图像必须在Windows平台上与应用程序所在的目录相同。