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();
}
}
}
}
#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();
}
}
}
}
相关文章推荐
- GDI对位图的绘制
- GDI 总结二: 位图的绘制
- GDI的 点 线 面 双缓冲 位图的绘制
- 【备忘】GDI位图绘制四部曲
- GDI绘制透明位图
- GDI C++(2) 位图的绘制
- Delphi GDI对象之绘制位图
- Delphi GDI对象之绘制位图
- GDI 总结二: 位图的绘制
- 使用C++绘制GDI位图的基本编写实例
- GDI C++ 位图的绘制
- C#使用GDI绘制直线的方法
- 绘制半透明位图以及如何画透明位图
- Windows GDI:CDC绘制ICON
- 绘制“透明”位图
- As3 通过绘制多张位图合并成一个超大的位图
- 【Visual C++】游戏开发笔记之五——游戏画面绘图(二)绘制位图
- VC MFC GDI 位图旋转算法
- Android绘制进阶之四:在位图上绘制文本并旋转
- 游戏开发笔记之五——游戏画面绘图(二)绘制位图