1. 程式人生 > >GDI之繪製點陣圖

GDI之繪製點陣圖



#include <Windows.h>
#include "resource.h"
#include <wingdi.h>
#pragma comment(lib,"msimg32.lib")
// 視窗處理函式
HINSTANCE g_hInstance = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP bitmap;
static HBRUSH brush = 0;
switch (uMsg)
{
case WM_CREATE:
bitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
brush = CreatePatternBrush(bitmap);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hwnd, &ps);
HDC hMemDc = CreateCompatibleDC(dc);
auto oldbitmap = SelectObject(hMemDc, bitmap);
BitBlt(dc, 0, 0, 48, 48, hMemDc, 0, 0,SRCCOPY);
SelectObject(hMemDc, oldbitmap);
DeleteObject(bitmap);

EndPaint(hwnd, &ps);


}

break;
case WM_DESTROY:
PostQuitMessage(0);
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
// 視窗註冊函式
void Register(LPCWSTR lpClassName, WNDPROC wndproc)
{
WNDCLASSEX wcx = { 0 };


wcx.cbClsExtra = 0;
wcx.cbSize = sizeof(wcx);
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcx.hCursor = LoadCursor(g_hInstance, IDC_ARROW);
wcx.hIcon = NULL;
wcx.hIconSm = NULL;
wcx.hInstance = g_hInstance;
wcx.lpfnWndProc = wndproc;
wcx.lpszClassName = lpClassName;
wcx.lpszMenuName = NULL;
wcx.style = CS_HREDRAW | CS_VREDRAW;


RegisterClassEx(&wcx);
}


HWND CreateMain(LPCWSTR lpClassName)
{
HWND hwnd = CreateWindowEx(0, lpClassName, L"LEARN", WS_OVERLAPPEDWINDOW, 100, 100, 700, 500, NULL, NULL, g_hInstance, NULL);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return hwnd;
}


int exec()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInstance = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
TCHAR szAppName[] = L"MAIN";
Register(szAppName, WndProc);
CreateMain(szAppName);
//BitBlt
return exec();
}
}
}
}