图像 (PNG) 未显示在 c++ Win32 项目的最终执行中
Images (PNG) don't show up in final exe of c++ Win32 project
首先,我的一小段代码:
// 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平台上与应用程序所在的目录相同。
相关文章:
- 在现有的C++可执行项目CMAKE中构建Apache Arrow
- 无法在 Linux 上运行C++ ZMQ 项目的可执行文件
- C++项目编译强制使用 /clr 选项,尽管在没有公共语言运行时支持的情况下执行它
- 使用 Premake5 构建项目不会生成可执行文件或中间体
- CMake:防止在子目录库项目中生成测试可执行目标
- 如何在解决方案中生成多个项目之前执行流程
- 如何将图像文件夹添加到CMake项目以便在c ++可执行文件中使用这些图像?
- CMake 项目结构:如何正确地将库合并在一起并将它们包含在多个可执行文件中
- 如何使用可执行文件将我的C++代码合并到我的 Xcode 项目中
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 如何配置 CMake,以便生成的 Visual Studio 项目找到可执行文件
- WXWIDGETS样本项目可产生巨大的可执行文件(90MB)
- 从电子项目运行的可执行文件未加载资源
- CMAKE:构建项目时执行C 文件
- GTEST测试项目链接链接到其他可执行文件
- 在Eclipse(Ubuntu)创建C Makefile项目时,如何生成.bin(可执行文件)
- 如何在单个 Visual Studio 2010 项目中执行多个主文件
- 如何在 basic 中从 Visual Studio 项目调用或执行 C++ 代码
- 哪些VS2010 C项目设置导致执行器需要兼容模式
- qt QAbstractItemModel 拖放用于移动项目执行删除/插入