关注公众号程序猿从入门到入土查询更方便哦
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