我希望OnPaint函数中的graphics.DrawImage(image,0,0)能够在使用下面的程序时显示从文件中读取的图像。而是为应用程序获取白色画布。 我做错了什么?
我在Windows 10上使用visual studio 2017社区。 谢谢!
/* https://msdn.microsoft.com/en-us/library/vs/alm/ms533895(v=vs.85).aspx */
#define UNICODE
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.Lib")
#pragma comment (lib,"User32.Lib")
#pragma comment (lib,"Gdi32.Lib")
VOID OnPaint(HDC hdc, Image * image)
{
Graphics graphics(hdc);
graphics.DrawImage(image,0,0);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT("GettingStarted");
RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("GettingStarted"), // window class name
TEXT("Getting Started"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;
} // WinMain
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
Image *image = NULL;
switch(message)
{
case WM_CREATE:
//create image
MessageBox(NULL, L"small.png", L"File Path", MB_OK);
image = new Image(L"small.png");
if (image)
return 0;
else
return -1;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc,image);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc
答案 0 :(得分:0)
Hans Passant指出了这个错误 - image
中的WndProc
参数重置为NULL,graphics.DrawImage(image,0,0)
无法显示任何内容。