WIN32 代码测试(BitMap)

#include <windows.h>
#include <stdio.h>
#include "resource.h"

#define  MAX_LOADSTRING 256

WCHAR szTitle[MAX_LOADSTRING] = L"demo";

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool  InitInstance(HINSTANCE hInstance, int   nCmdShow);
ATOM RegisterWndClass(HINSTANCE hInstance);

HINSTANCE g_hInstance = nullptr;

int APIENTRY wWinMain(
	_In_ HINSTANCE hInstance,
	_In_ HINSTANCE hPrevInstance,
	_In_ LPWSTR lpCmdLine,
	_In_ int   nCmdShow) {

	g_hInstance = hInstance;

	RegisterWndClass(hInstance);
	if (!InitInstance(hInstance, nCmdShow)) {
		return -1;
	}

	MSG msg;
	while (GetMessage(&msg, nullptr, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

ATOM RegisterWndClass(HINSTANCE hInstance) {
	WNDCLASS  wnd;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
	wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);

	wnd.lpfnWndProc = WndProc;
	wnd.lpszClassName = szTitle;
	wnd.lpszMenuName = NULL;
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	wnd.hInstance = hInstance;
	return RegisterClass(&wnd);
}

bool  InitInstance(HINSTANCE hInstance, int   nCmdShow) {
	HWND hWnd = CreateWindow(szTitle, L"windows", WS_OVERLAPPEDWINDOW, 0, 0, 300, 300,
		NULL, NULL, hInstance, NULL);
	if (!hWnd)
		return false;

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return true;
}
void onPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	PAINTSTRUCT ps;
	HDC hDC = BeginPaint(hWnd, &ps);

	//HBITMAP hbitmap = LoadBitmap(g_hInstance, (LPCWCHAR)IDB_BITMAP);

	HBITMAP hbitmap = (HBITMAP)LoadImage(g_hInstance, L"./bitmap1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	HDC hMemDC =  CreateCompatibleDC(hDC);
	HGDIOBJ hOldObj = SelectObject(hMemDC, hbitmap);
	//1:1 成像
	BitBlt(hDC, 100, 100, 48, 48, hMemDC, 0, 0, SRCCOPY);

	//缩放成像
	StretchBlt(hDC, 150, 150, 96, 96, hMemDC, 0, 0, 48, 48, SRCCOPY);

	SelectObject(hMemDC, hOldObj);
	DeleteObject(hbitmap);
	DeleteDC(hMemDC);

	EndPaint(hWnd, &ps);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message){
	case WM_PAINT:
		onPaint(hWnd, message, wParam, lParam);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	default:
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值