一、添加一张位图
二、相关代码
#include <windows.h>
#include "resource.h"
HINSTANCE g_hInstance = 0;//接收当前程序实例句柄
HANDLE g_输出句柄 = 0;//接收标准输出句柄
int g_x = 128;
int g_y = 128;
void 绘图(HWND hWnd)
{
PAINTSTRUCT p = { 0 };
HDC hdc = BeginPaint(hWnd, &p);
HBITMAP hBmp = LoadBitmap(g_hInstance, (wchar_t*)IDB_BITMAP1);
//到内存dc
HDC hMDc = CreateCompatibleDC(hdc);
HGDIOBJ hOBp=SelectObject(hMDc, hBmp);
StretchBlt(hdc, 0, 0, g_x, g_y, hMDc, 0, 0, 112, 88, SRCCOPY);
//把位图取出来
SelectObject(hMDc, hOBp);
DeleteObject(hBmp);
DeleteDC(hMDc);
EndPaint(hWnd, &p);
}
void 滑轮滚动(HWND hWnd,WPARAM wParam)
{
short nOf = HIWORD(wParam);
g_y += nOf / 120;
g_x += nOf / 120;
InvalidateRect(hWnd,NULL,TRUE);
}
//2、窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID,
WPARAM wParam, LPARAM lParam){
//7、处理消息
switch (msgID)
{
case WM_MOUSEWHEEL:
滑轮滚动(hWnd,wParam);
break;
case WM_PAINT:
绘图(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);//销毁窗口
break;
}
return DefWindowProc(hWnd, msgID, wParam, lParam);
}
//3、注册函数,第一个参数,窗口类名称,第二个参数,指向窗口处理函数的函数指针
void Register(LPCWSTR lpClassName, WNDPROC winProc){
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);//结构体大小
wc.cbClsExtra = 0;//窗口类的申请缓存区,0表示不开启缓存
wc.cbWndExtra = 0;//窗口的申请缓存区,0表示不开启缓存
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);//背景颜色,一般白色
wc.hCursor = NULL;//设置光标。null表示默认
wc.hIcon = NULL;//默认左上角的图标
wc.hInstance = g_hInstance;//第一个参数实例句柄,可以找到进程在那块内存
wc.lpfnWndProc = winProc; //lp 一般都是指针,处理函数名或指针
wc.lpszClassName = lpClassName;//窗口类名称,比如公司名字
wc.lpszMenuName = NULL;//没有菜单用null
wc.style = CS_HREDRAW | CS_VREDRAW;//窗口变化,会重绘,窗口类的一般风格
RegisterClassEx(&wc);
}
//4、创建窗口,(窗口类名称,窗口标题栏名称)
HWND CreateMain(LPCWSTR lpClassName, LPCWSTR lpWindowName){
HWND hWnd = CreateWindowExW(0, lpClassName, lpWindowName,
WS_OVERLAPPEDWINDOW, 100, 100, 500, 600, NULL, NULL, g_hInstance, NULL);
return hWnd;
}
//5、显示窗口(窗口句柄)
void Display(HWND hWnd){
ShowWindow(hWnd, SW_SHOW);//句柄,显示方式
UpdateWindow(hWnd);//调用一次刷新窗口
}
//6、消息循环
void Message(){
MSG nMsg = { 0 };
while (GetMessage(&nMsg, nullptr, 0, 0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);
}
}
//1、入口函数
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpCmdLine, int nCmdShow){
AllocConsole();//显示dos窗口
g_输出句柄 = GetStdHandle(STD_OUTPUT_HANDLE);
g_hInstance = hInstance;
Register(L"主", WndProc);
HWND hWnd = CreateMain(L"主", L"主窗口");
Display(hWnd);
Message();
return 0;
}