【Windows.h】学习日志 Dat.2 连续加载图片到窗口中 && windows程序设计WinMain()函数几大操作过程


关注公众号程序猿从入门到入土查询更方便哦

1、应用windows程序设计,实现连续加载20图处到窗口当中(图片大小1024*768)?

#include<Windows.h>
#include<tchar.h>
#pragma comment(lib,"winmm.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

HDC g_hdc = NULL, g_mdc = NULL;
HBITMAP g_hSprite[12];
DWORD g_tPre = 0, g_tNow = 0;
int g_iNum = 0;

BOOL InitRes(HWND hwnd);
VOID PaintFunc(HWND hwnd);
BOOL CleanUpFunc(HWND hwnd);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int iCmdShow) {
	
	//窗口类
	TCHAR tcClassName[] = TEXT("MyWindow");

	//窗口类结构
	WNDCLASS WndClass;
	WndClass.lpszClassName = tcClassName;//窗口类名
	WndClass.lpszMenuName = NULL;
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	WndClass.hIcon = (HICON)::LoadImage(NULL, L"./Res/CaptionICO.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);

	//注册窗口
	if (!RegisterClass(&WndClass)) {
		MessageBox(NULL, TEXT("注册失败"), NULL, MB_OK | MB_ICONERROR);
		return 0;
	}
	TCHAR tcWindowCaptionName[] = TEXT("windows lession two");
	CREATESTRUCT cs;
	cs.lpszClass = tcClassName;
	cs.lpszName = tcWindowCaptionName;
	cs.style = WS_OVERLAPPEDWINDOW;
	cs.x = 400;
	cs.y = 100;
	cs.cx = 800;
	cs.cy = 600;
	cs.hwndParent = NULL;
	cs.hMenu = NULL;
	cs.hInstance = hInstance;
	cs.lpCreateParams = NULL;

	//创建窗口对象
	HWND hwnd = CreateWindow(cs.lpszClass, cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
	if (hwnd == NULL) {
		MessageBox(NULL, TEXT("创建窗口失败"), TEXT("提示"), MB_OK | MB_ICONERROR);
		return 0;
	}

	//显示
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	if (!InitRes(hwnd)) {
		MessageBox(NULL, TEXT("资源加载失败"), TEXT("提示"), MB_OK | MB_ICONERROR);
	}

	MSG msg = { 0 };
	while (msg.message!=WM_QUIT) {
		if (PeekMessage(&msg,0,0,0,PM_REMOVE)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else {
			g_tNow = GetTickCount64();
			if (g_tNow - g_tPre >= 500) {
				PaintFunc(hwnd);
			}
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	mciSendString(L"Open ./Music/1.mp3 alias music", 0, 0, 0);
	mciSendString(L"play music repeat", 0, 0, 0);
	switch (message) {
	default:
		break;
	case WM_LBUTTONDOWN: {
		MessageBox(NULL, L"你已选中", L"提示", MB_OK | MB_ICONQUESTION);
		break;
	}
	case WM_DESTROY: {
		PostQuitMessage(0);
		return 0;
	}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);

}

BOOL InitRes(HWND hwnd) {
	g_hdc = GetDC(hwnd);
	wchar_t filename[20];
	for (int i = 0; i < 12; i++) {
		memset(filename, 0, sizeof(filename));
		swprintf_s(filename, L"./Res/%d.bmp", i);
		g_hSprite[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 800, 600, LR_LOADFROMFILE);
	}
	g_mdc = CreateCompatibleDC(g_hdc);
	PaintFunc(hwnd);
	return TRUE;
}

VOID PaintFunc(HWND hwnd) {
	if (g_iNum == 11) {
		g_iNum = 0;
	}
	SelectObject(g_mdc, g_hSprite[g_iNum]);
	BitBlt(g_hdc, 0, 0, 800, 600, g_mdc, 0, 0, SRCCOPY);
	g_tPre = GetTickCount64();
	g_iNum++;
}
BOOL CleanUpFunc(HWND hwnd) {
	for (int i = 0; i < 12; i++) {
		DeleteObject(g_hSprite[i]);
		DeleteDC(g_mdc);
		ReleaseDC(hwnd, g_hdc);

	}
	return TRUE;
}

2、windows程序设计WinMain()函数里面有几大操作过程?

1 定义一个窗口类,并给相关成员赋值
2 注册窗口调用RegisterClass,并判断是否注册成功
3 创建窗口 CreateWindow
4 ShowWindow,UpdateWindow
5 while循环中GetMessage,TranslateMessage,DispatchMessage

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaoYuanSama

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值