#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);
}
测试结果: