GDI+-无法对Gdiplus::Graphics(C++)执行任何操作

GDI+ - Cannot do anything with Gdiplus::Graphics (C++)

本文关键字:C++ 执行 操作 任何 Graphics Gdiplus GDI+-      更新时间:2023-10-16

我是GDI+的新手,不知道问题出在哪里,所以我只发布所有代码。我试图简单地绘制一个图像,但在我的调试器中,我可以看到我试图在WM_PAINT中使用的图形是NULL。我看到很多人都在做和我想做的几乎完全一样的事情,所以我很困惑到底发生了什么

#include "stdafx.h"
#include "GUI.h"
#include <objidl.h>
#include <gdiplus.h>
#include <iostream>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR    lpCmdLine,
_In_ int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_GUI, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GUI));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;
wcex.hInstance      = hInstance;
wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GUI));
wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_GUI);
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
Graphics graphics(hdc);
Image image(L"C:\light.png");
graphics.DrawImage(&image, 0, 0);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

您缺少对GdiplusStartup()GdiplusShutdown()的调用。

参考GdiplusStartup:

在创建任何GDI+对象之前,必须调用GdiplusStartup,并且您必须删除所有GDI+对象(或将它们从scope(,然后调用GdiplusShutdown

。。。和来自GdiplusShutdown:

GdiplusShutdown函数清理Windows GDI+使用的资源。每次对GdiplusStartup的调用都应与对GdiplusShutdown

我使用这样的RAII类来简化任务:

class GdiPlusInit
{
public:
GdiPlusInit()
{
Gdiplus::GdiplusStartupInput startupInput;
Gdiplus::GdiplusStartup( &m_token, &startupInput, NULL );
// NOTE: For brevity I omitted error handling, check function return value!
}
~GdiPlusInit()
{
if( m_token )
Gdiplus::GdiplusShutdown( m_token );
}
// Class is non-copyable.
GdiPlusInit( const GdiPlusInit& ) = delete;
GdiPlusInit& operator=( const GdiPlusInit& ) = delete;
private:
ULONG_PTR m_token = 0;
};

用法:

在要使用GDI+函数的作用域的开头创建一个类的实例(出于性能原因,我不会在频繁调用的函数中这样做(。我通常将它创建为窗口类或其他使用GDI+的类的成员变量,这样我的代码的客户端就不需要被告知初始化GDI+。客户端是否已经单独调用GdiplusStartup()GdiplusShutdown()并不重要,因为如果调用正确配对,它们可以嵌套。

在您的情况下:

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR    lpCmdLine,
_In_ int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
GdiPlusInit gdiplus;  // calls GdiplusStartup() and stores the returned token
// ... remaining code of your application ... 
// When the scope ends, the destructor of GdiPlusInit calls GdiplusShutdown(),
// passing the stored token.
}