#include <windows.h>
#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>
//関数の宣言
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE,int,LPTSTR);
void Create();
void Paint(HDC);
void Destroy();
//変数の宣言
IImagingFactory *pImgFactory=NULL;
IImage *pImage=NULL;
//メイン
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,int nShowCmd) {
MSG msg;
BOOL bRet;
//ウィンドウの生成
if (!InitApp(hInstance)) return FALSE;
if (!InitInstance(hInstance,nShowCmd,
_T("ImageFactoryEx"))) return FALSE;
//メインループ
while((bRet=GetMessage(&msg,NULL,0,0))!=0) {
if (bRet==-1) {
break;
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//ウィンドウクラスの登録
ATOM InitApp(HINSTANCE hInst) {
WNDCLASSW wc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst;
wc.hIcon=NULL;
wc.hCursor=NULL;
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
wc.lpszMenuName=NULL;
wc.lpszClassName=_T("win01");
return (RegisterClassW(&wc));
}
//ウィンドウの生成
BOOL InitInstance(HINSTANCE hInst,int nShowCmd,
LPTSTR title) {
HWND hWnd;
hWnd=CreateWindowW(_T("win01"),title,
WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);
if (!hWnd) return FALSE;
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);
return TRUE;
}
//ウィンドウのイベント処理
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) {
PAINTSTRUCT ps;
HDC hdc;
switch (msg){
//生成
case WM_CREATE:
Create();
break;
//描画
case WM_PAINT:{
hdc=BeginPaint(hWnd,&ps);
Paint(hdc);
EndPaint(hWnd,&ps);
}
break;
//破棄
case WM_DESTROY:{
Destroy();
PostQuitMessage(0);
}
break;
//その他
default:
return (DefWindowProc(hWnd,msg,wp,lp));
}
return 0;
}
//生成
void Create() {
//イメージファクトリの生成
CoInitializeEx(NULL,COINIT_MULTITHREADED);
if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory,
NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,
(void **)&pImgFactory))) {
} else {
pImgFactory=NULL;
return;
}
//イメージの読み込み
if (SUCCEEDED(pImgFactory->CreateImageFromFile(
TEXT("\\My Documents\\sorami.jpg"),&pImage))) {
} else {
pImage=NULL;
return;
}
}
//描画処理
void Paint(HDC hdc) {
//イメージの描画
RECT rc={0,0,112,112};
if (pImage!=NULL) pImage->Draw(hdc,&rc,NULL);
}
//破棄
void Destroy() {
//イメージの解放
if (pImage!=NULL) pImage->Release();
//イメージファクトリの解放
pImgFactory->Release();
CoUninitialize();
} |