1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include "Resource.h"
TCHAR szClsName[] = _T("BasicApp");
TCHAR szWndName[] = _T("BasicApp");
static HINSTANCE g_hInstance;
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
ATOM RegisterMyClass();
void DrawBitmap(HDC hDC, HBITMAP hBitmap, int left, int top, int width, int height);
ATOM RegisterMyClass()
{
WNDCLASSEX WndClsEx;
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = _T(szClsName);
WndClsEx.hInstance = g_hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
return RegisterClassEx(&WndClsEx);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
g_hInstance = hInstance;
if (!RegisterMyClass())
{
MessageBox(0, _T("RegisterClassEx failed"), _T("Error"), MB_OK);
return FALSE;
}
hWnd = CreateWindow(szClsName, szWndName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1000,
700, 0, 0, g_hInstance, 0);
if (!hWnd)
{
MessageBox(0, _T("CreateWindow failed"), _T("Error"), MB_OK);
return FALSE;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (int)Msg.wParam;
}
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
HBITMAP hBitmap;
HDC hDC;
PAINTSTRUCT ps;
switch (Msg)
{
case WM_COMMAND:
MessageBox(hWnd, "Hello World", "Hello", MB_OK);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
// first bitmap
hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
_ASSERTE(hBitmap != NULL);
DrawBitmap(hDC, hBitmap, 0, 0, 480, 640);
DeleteObject(hBitmap);
// second bitmap
hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
_ASSERTE(hBitmap != NULL);
DrawBitmap(hDC, hBitmap, 500, 0, 480, 640);
EndPaint(hWnd, &ps);
DeleteObject(hBitmap);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
void DrawBitmap(HDC hDC, HBITMAP hBitmap, int left, int top, int width, int height)
{
HDC hMemDC;
_ASSERTE(hDC != NULL);
_ASSERTE(hBitmap != NULL);
_ASSERTE(width > 0);
_ASSERTE(height > 0);
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBitmap);
BitBlt(hDC, left, top, width, height, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
}
|