您的位置:首页 > 其它

GDI之绘制位图

2016-10-17 18:26 288 查看
#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();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: