首先,我的一小部分代码示例:
// 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/