// TEST_PNG.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "TEST_PNG.h"
#include <windows.h>
#include <commctrl.h>
#include "Imaging.h"
#include <initguid.h>
#include <imgguids.h>
#pragma comment (lib,"Ole32.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndCommandBar; // command bar handle
HWND g_hWnd;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void DrawImage(HDC hdc,const void *buffer,UINT size,LPCRECT rect);
void DrawImage2(HWND hwnd,LPCWSTR lpName,LPCWSTR lpType,int left,int top);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST_PNG));
MessageBox(g_hWnd,_T("Ei:请用键盘,请按键盘的任意按键去载入png,按一次载入并且显示一次"),_T("Ei提示"),MB_OK);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST_PNG));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name
g_hInst = hInstance; // Store instance handle in our global variable
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST_PNG, szWindowClass, MAX_LOADSTRING);
if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
g_hWnd=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (g_hWndCommandBar)
{
CommandBar_Show(g_hWndCommandBar, TRUE);
}
//::DrawImage(HDC hdc,const void *buffer,UINT size,LPCRECT rect);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
g_hWndCommandBar = CommandBar_Create(g_hInst, hWnd, 1);
CommandBar_InsertMenubar(g_hWndCommandBar, g_hInst, IDR_MENU, 0);
CommandBar_AddAdornments(g_hWndCommandBar, 0, 0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//::LoaResourceImage(g_hWnd, MAKEINTRESOURCE(IDR_PNG1), TEXT("PNG"), 0, 0, 0, 50,50);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndCommandBar);
PostQuitMessage(0);
break;
case WM_KEYDOWN:
DrawImage2(g_hWnd,MAKEINTRESOURCE(IDR_PNG1),_T("PNG"),rand()%1000,rand()%600);
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)
{
switch (message)
{
case WM_INITDIALOG:
RECT rectChild, rectParent;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
// trying to center the About dialog
if (GetWindowRect(hDlg, &rectChild))
{
GetClientRect(GetParent(hDlg), &rectParent);
DlgWidth = rectChild.right - rectChild.left;
DlgHeight = rectChild.bottom - rectChild.top ;
NewPosX = (rectParent.right - rectParent.left - DlgWidth) / 2;
NewPosY = (rectParent.bottom - rectParent.top - DlgHeight) / 2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;
}
return (INT_PTR)FALSE;
}
//***************************************************************************************
//函数:DrawImage2
//目的: 解码并显示PNG图片
//***************************************************************************************
void DrawImage2(HWND hwnd,LPCWSTR lpName,LPCWSTR lpType,int left,int top)
{
HDC hdc=GetDC(g_hWnd);
HMODULE hModule_Current=GetModuleHandle(NULL);
HRSRC hr=FindResource(hModule_Current,MAKEINTRESOURCE(IDR_PNG1),_T("PNG"));
if(hr==NULL)
return;
DWORD dwsize=SizeofResource(GetModuleHandle(NULL),hr);
HGLOBAL hg=LoadResource(GetModuleHandle(NULL),hr);
LPSTR lp=(LPSTR)LockResource(hg);
RECT rect;
rect.top = top;
rect.left=left;
IImagingFactory *pImageFactory=NULL;
IImage *pImage;
if(SUCCEEDED(CoInitializeEx(NULL,COINIT_MULTITHREADED)))
{
HRESULT hResult=0;
if(SUCCEEDED(hResult=CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**)&pImageFactory)))
{
hResult=0;
if(SUCCEEDED(pImageFactory->CreateImageFromBuffer(lp,dwsize,DISPOSAL_NONE,&pImage)))
{
ImageInfo imgInf;
pImage->GetImageInfo(&imgInf);
rect.right=rect.left+imgInf.Width;
rect.bottom=rect.top+imgInf.Height;
pImage->Draw(hdc,&rect,NULL);
pImage->Release();
}
else
MessageBox(g_hWnd,_T("载入失败"),_T("error"),MB_OKCANCEL);
pImageFactory->Release();
}
else
MessageBox(g_hWnd,_T("CoCreateInstance fail"),_T("error"),MB_OKCANCEL);
if(hResult==REGDB_E_CLASSNOTREG)
MessageBox(g_hWnd,_T("REGDB_E_CLASSNOTREG"),_T("error"),MB_OKCANCEL);
CoUninitialize();
}
DeleteObject(hr);
}
//
#include "stdafx.h"
#include "TEST_PNG.h"
#include <windows.h>
#include <commctrl.h>
#include "Imaging.h"
#include <initguid.h>
#include <imgguids.h>
#pragma comment (lib,"Ole32.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndCommandBar; // command bar handle
HWND g_hWnd;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void DrawImage(HDC hdc,const void *buffer,UINT size,LPCRECT rect);
void DrawImage2(HWND hwnd,LPCWSTR lpName,LPCWSTR lpType,int left,int top);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST_PNG));
MessageBox(g_hWnd,_T("Ei:请用键盘,请按键盘的任意按键去载入png,按一次载入并且显示一次"),_T("Ei提示"),MB_OK);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST_PNG));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name
g_hInst = hInstance; // Store instance handle in our global variable
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST_PNG, szWindowClass, MAX_LOADSTRING);
if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
g_hWnd=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (g_hWndCommandBar)
{
CommandBar_Show(g_hWndCommandBar, TRUE);
}
//::DrawImage(HDC hdc,const void *buffer,UINT size,LPCRECT rect);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
g_hWndCommandBar = CommandBar_Create(g_hInst, hWnd, 1);
CommandBar_InsertMenubar(g_hWndCommandBar, g_hInst, IDR_MENU, 0);
CommandBar_AddAdornments(g_hWndCommandBar, 0, 0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//::LoaResourceImage(g_hWnd, MAKEINTRESOURCE(IDR_PNG1), TEXT("PNG"), 0, 0, 0, 50,50);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndCommandBar);
PostQuitMessage(0);
break;
case WM_KEYDOWN:
DrawImage2(g_hWnd,MAKEINTRESOURCE(IDR_PNG1),_T("PNG"),rand()%1000,rand()%600);
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)
{
switch (message)
{
case WM_INITDIALOG:
RECT rectChild, rectParent;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
// trying to center the About dialog
if (GetWindowRect(hDlg, &rectChild))
{
GetClientRect(GetParent(hDlg), &rectParent);
DlgWidth = rectChild.right - rectChild.left;
DlgHeight = rectChild.bottom - rectChild.top ;
NewPosX = (rectParent.right - rectParent.left - DlgWidth) / 2;
NewPosY = (rectParent.bottom - rectParent.top - DlgHeight) / 2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;
}
return (INT_PTR)FALSE;
}
//***************************************************************************************
//函数:DrawImage2
//目的: 解码并显示PNG图片
//***************************************************************************************
void DrawImage2(HWND hwnd,LPCWSTR lpName,LPCWSTR lpType,int left,int top)
{
HDC hdc=GetDC(g_hWnd);
HMODULE hModule_Current=GetModuleHandle(NULL);
HRSRC hr=FindResource(hModule_Current,MAKEINTRESOURCE(IDR_PNG1),_T("PNG"));
if(hr==NULL)
return;
DWORD dwsize=SizeofResource(GetModuleHandle(NULL),hr);
HGLOBAL hg=LoadResource(GetModuleHandle(NULL),hr);
LPSTR lp=(LPSTR)LockResource(hg);
RECT rect;
rect.top = top;
rect.left=left;
IImagingFactory *pImageFactory=NULL;
IImage *pImage;
if(SUCCEEDED(CoInitializeEx(NULL,COINIT_MULTITHREADED)))
{
HRESULT hResult=0;
if(SUCCEEDED(hResult=CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**)&pImageFactory)))
{
hResult=0;
if(SUCCEEDED(pImageFactory->CreateImageFromBuffer(lp,dwsize,DISPOSAL_NONE,&pImage)))
{
ImageInfo imgInf;
pImage->GetImageInfo(&imgInf);
rect.right=rect.left+imgInf.Width;
rect.bottom=rect.top+imgInf.Height;
pImage->Draw(hdc,&rect,NULL);
pImage->Release();
}
else
MessageBox(g_hWnd,_T("载入失败"),_T("error"),MB_OKCANCEL);
pImageFactory->Release();
}
else
MessageBox(g_hWnd,_T("CoCreateInstance fail"),_T("error"),MB_OKCANCEL);
if(hResult==REGDB_E_CLASSNOTREG)
MessageBox(g_hWnd,_T("REGDB_E_CLASSNOTREG"),_T("error"),MB_OKCANCEL);
CoUninitialize();
}
DeleteObject(hr);
}
本文介绍了一个使用C++实现的PNG图片加载和显示应用,包括窗口注册、初始化、消息处理以及绘制PNG图片的功能。

1279

被折叠的 条评论
为什么被折叠?



