Gdi+显示问题
在窗口中显示png无法成功,代码如下
#include "StdAfx.h"
#include "images.h"
images::images(void)
{
init();
}
images::images(HWND parent):
baseControl(parent)
{
init();
}
images::~images(void)
{
}
void images::init()
{
mImagePath = _T("2.png");
HINSTANCE hInst = AfxGetInstanceHandle();
WNDCLASS wndclass; //窗体类
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wndclass.hCursor = LoadCursor( 0, IDC_HAND );
wndclass.hIcon = LoadIcon( 0, IDI_INFORMATION );
wndclass.hInstance = AfxGetInstanceHandle();
wndclass.lpfnWndProc = MyWindowProc;
wndclass.lpszClassName = TEXT("hg");
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW ; // 指定窗体风格
//注册类
if (!RegisterClass( &wndclass ) )
{
ShowLog(Priority::WARN, "image window class register error.");
}
//创建窗体
mHwnd = CreateWindow( TEXT("hg"), TEXT("Helloworldhhh"), WS_CHILD, 0, 0, 300, 600, mParentWindow,NULL,AfxGetInstanceHandle(), NULL );
if ( !mHwnd)
{
DWORD lastError = GetLastError();
ShowLog(Priority::WARN, "image window create error, error code : %d.", lastError);
}
}
LRESULT CALLBACK images::MyWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
images *image = reinterpret_cast<images *>(wParam);
PAINTSTRUCT ps;
HDC hdc;
switch(uMsg)
{
case WM_CONTROL_SHOW:
if (image)
{
ShowLog(Priority::NOTICE, "image wm_control_show.");
image->Show();
}
break;
case WM_PAINT:
{
ShowLog(Priority::NOTICE, "image wm_paint.");
if (image)
{
image->Show();
RedrawWindow(hwnd,NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW);
}
else
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
//return DefWindowProc( hwnd, uMsg, wParam, lParam );
}
}
break;
default:
return DefWindowProc( hwnd, uMsg, wParam, lParam );
}
return 0;
}
void images::Show()
{
PAINTSTRUCT ps; // 点结构
HDC hdc; // DC句柄
hdc = BeginPaint (mHwnd , &ps); // 获得DC句柄,开始绘制,其中hWnd为窗口句柄
RECT rc;
GetClientRect(mHwnd,&rc);
Graphics graphics( hdc);
Image image(mImagePath.c_str(), FALSE);
graphics.DrawImage(&image, 0, 0, rc.right, rc.bottom); //绘制背景
EndPaint(mHwnd, &ps) ; // 绘制结束
ShowLog(Priority::NOTICE, "Image Show Finish.");
}