图像(PNG)不会出现在c ++ Win32项目的最终exe中

时间:2015-06-08 13:42:43

标签: 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)制作一名教练,最后今天结束。 有一个小问题; 我项目最终版本中的图片(PNG)不会显示。

可以在此处查看图片:http://imgur.com/6mWtETS,0ajBwE9,JlMVUYR,k8AIbAn#0

(前两个是调试教练时得到的。

我认为这与我如何使用GDI +加载图片有关,但我不确切知道是什么,因为培训师不会产生任何错误。

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

1 个答案:

答案 0 :(得分:0)

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