#include <windows.h>
#include "resource.h"
#define CLASS_NAME "bitmap sample"
LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM );
BOOL LoadBitmapImage( HWND, HDC *, HBITMAP *, LPCSTR );
HDC memDC;
HBITMAP hBit;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
{
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
wc.hInstance = hInstance;
wc.lpfnWndProc = WinProc;
wc.lpszClassName = CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = 0;
if( !RegisterClass( &wc ) ) return 1;
}
HWND hWnd = CreateWindow(
CLASS_NAME, "Bitmap Sample",
WS_CAPTION | WS_SYSMENU,
0, 0, 400, 400,
NULL, NULL, hInstance, NULL
);
ShowWindow( hWnd, SW_NORMAL );
UpdateWindow( hWnd );
if(
!LoadBitmapImage(
hWnd,
&memDC, &hBit,
MAKEINTRESOURCE( MACHO )
)
) return 1;
while( true )
{
MSG msg;
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT ) break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
HDC hDC = GetDC( hWnd );
BitBlt( hDC, 0, 0, 368, 316, memDC, 0, 0, SRCCOPY );
ReleaseDC( hWnd, hDC );
Sleep( 100 );
}
}
DeleteObject( hBit );
DeleteDC( memDC );
return 0;
}
LRESULT CALLBACK WinProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch( uMsg )
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
BOOL LoadBitmapImage(
HWND hWnd,
HDC *pMemDC,
HBITMAP *pHBit,
LPCSTR lpImageName
)
{
HDC hDC = GetDC( hWnd );
*pMemDC = CreateCompatibleDC( hDC );
*pHBit = (HBITMAP)LoadImage(
(HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE ),
MAKEINTRESOURCE( lpImageName ),
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION
);
if( *pHBit == NULL )
{
*pHBit = (HBITMAP)LoadImage(
NULL,
lpImageName,
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION | LR_LOADFROMFILE
);
if( *pHBit == NULL ) return FALSE;
}
SelectObject( *pMemDC, *pHBit );
ReleaseDC( hWnd, hDC );
return TRUE;
}
|