2017-06-20 153 views
0

我希望在使用下面的程序时,OnPaint函数中的graphics.DrawImage(image,0,0)显示从文件中读取的图像。取而代之的是为应用程序获取一张白色画布。 我在做什么错?gdi plus图像未显示

我在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

当您获取WM_PAINT消息时,'image'变量将再次为NULL。使用调试器很容易看到。你可以声明它是'static'作为解决方法。 –

+0

感谢汉斯。我将“图像”设置为静态,图像按预期显示! – user308879

回答

0

的错误指出由Hans帕桑特 - 在WndProcimage参数重新设置为NULL和graphics.DrawImage(image,0,0)将一无所获显示。