#include <windows.h>
#include "resource.h"
//関数の宣言
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE,int,LPTSTR);
void Create();
void Paint();
void Destroy();
void DrawImage(HBITMAP,int,int);
void DrawScaledImage(HBITMAP,int,int,int,int,int,int,int,int);
void DrawImage(HBITMAP,HBITMAP,int,int);
void DrawScaledImage(HBITMAP,HBITMAP,int,int,int,int,int,int,int,int);
//変数の宣言
HINSTANCE ghInst; //インスタンス
HDC hdc; //デバイスコンテキスト
HDC hMdc; //メモリDC
HDC hMdcMask;//メモリDC(マスク)
HBITMAP hBmp; //ビットマップ
HBITMAP hBmpMask;//ビットマップ(マスク)
//メイン
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,int nShowCmd) {
MSG msg;
BOOL bRet;
//インスタンスの保持
ghInst=hInstance;
//ウィンドウの生成
if (!InitApp(hInstance)) return FALSE;
if (!InitInstance(hInstance,nShowCmd,
_T("BitmapEx"))) 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;
switch (msg){
//生成
case WM_CREATE:
Create();
break;
//描画
case WM_PAINT:
//前処理
hdc=BeginPaint(hWnd,&ps);
hMdc=CreateCompatibleDC(hdc);
hMdcMask=CreateCompatibleDC(hdc);
//描画
Paint();
//後処理
DeleteDC(hMdc);
DeleteDC(hMdcMask);
EndPaint(hWnd,&ps);
break;
//破棄
case WM_DESTROY:
Destroy();
PostQuitMessage(0);
break;
//その他
default:
return (DefWindowProc(hWnd,msg,wp,lp));
}
return 0;
}
//生成
void Create() {
//ビットマップの読み込み
hBmp =LoadBitmap(ghInst,MAKEINTRESOURCE(IDB_BITMAP1));
hBmpMask=LoadBitmap(ghInst,MAKEINTRESOURCE(IDB_BITMAP2));
}
//描画
void Paint() {
//イメージの描画
DrawImage(hBmp,0,0);
//イメージの拡縮描画
DrawScaledImage(hBmp,hBmpMask,0,150,114*2,114*2,0,0,114,114);
//透過イメージの描画
DrawImage(hBmp,hBmpMask,150,0);
DrawImage(hBmp,hBmpMask,160,10);
}
//破棄
void Destroy() {
//ビットマップの破棄
DeleteObject(hBmp);
DeleteObject(hBmpMask);
}
//イメージの描画
void DrawImage(HBITMAP hBmp,int x,int y) {
BITMAP info;
GetObject(hBmp,sizeof(BITMAP),&info);
(HBITMAP)SelectObject(hMdc,hBmp);
BitBlt(hdc,x,y,info.bmWidth,info.bmHeight,hMdc,0,0,SRCCOPY);
}
//イメージの拡縮描画
void DrawScaledImage(HBITMAP hBmp,int x,int y,int w,int h,
int sx,int sy,int sw,int sh) {
(HBITMAP)SelectObject(hMdc,hBmp);
StretchBlt(hdc,x,y,w,h,hMdc,sx,sy,sw,sh,SRCCOPY);
}
//透過イメージの描画
void DrawImage(HBITMAP hBmp,HBITMAP hBmpMask,int x,int y) {
BITMAP info;
GetObject(hBmp,sizeof(BITMAP),&info);
(HBITMAP)SelectObject(hMdcMask,hBmpMask);
BitBlt(hdc,x,y,info.bmWidth,info.bmHeight,hMdcMask,0,0,SRCAND);
(HBITMAP)SelectObject(hMdc,hBmp);
BitBlt(hdc,x,y,info.bmWidth,info.bmHeight,hMdc,0,0,SRCINVERT);
}
//透過イメージの拡縮描画
void DrawScaledImage(HBITMAP hBmp,HBITMAP hBmpMask,
int x,int y,int w,int h,
int sx,int sy,int sw,int sh) {
(HBITMAP)SelectObject(hMdcMask,hBmpMask);
StretchBlt(hdc,x,y,w,h,hMdcMask,sx,sy,sw,sh,SRCAND);
(HBITMAP)SelectObject(hMdc,hBmp);
StretchBlt(hdc,x,y,w,h,hMdc,sx,sy,sw,sh,SRCINVERT);
} |