进度条 游戏中Win32进度条ProcessBar的写法
#include <Windows.h>
#include <commctrl.h> //InitCommonControls
#pragma comment(lib, "comctl32") //InitCommonControls
//过程函数的返回值都是LRESULT,类型都是CALLBACK
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL procflag = TRUE;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd; //主窗口句柄,在CreateWindow中赋值
MSG msg; //消息变量,在GetMessage中使用
WNDCLASS wndclass; //窗口类
TCHAR* szAppName = TEXT("ProgressBar"); //类名和窗口名
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗口背景
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //鼠标
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //图标
wndclass.lpszClassName = szAppName; //类名
wndclass.cbClsExtra = 0; //类的额外参数
wndclass.cbWndExtra = 0; //窗口的额外参数.用于基于同一窗口类的窗口各自区分.
//在自定义对话框类时必须指定为DLGWINDOWEXTRA的大小
wndclass.lpszMenuName = NULL; //菜单名.可以用作子窗口的id
wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口风格
wndclass.lpfnWndProc = WndProc; //窗口过程
wndclass.hInstance = hInstance; //包含窗口过程的实例句柄
if (!RegisterClass(&wndclass)) //注册窗口类
return 0;
hwnd = CreateWindow( //创建窗口
szAppName, //窗口类名
szAppName, //窗口标题
WS_OVERLAPPEDWINDOW, //窗口风格
CW_USEDEFAULT, //初始的x坐标
CW_USEDEFAULT, //初始的y坐标
CW_USEDEFAULT, //初始的宽度
CW_USEDEFAULT, //初始的高度
NULL, //父窗口
NULL, //菜单
hInstance, //和窗口相关的实例句柄
NULL //额外参数
);
ShowWindow(hwnd, nCmdShow); //显示窗口
UpdateWindow(hwnd); //更新窗口
while (GetMessage(&msg, NULL, 0, 0)) //消息循环
{
TranslateMessage(&msg); //将WM_XXXKEYXXX消息翻译为WM_CHAR消息
DispatchMessage(&msg); //传递消息到窗口过程
}
return msg.wParam; //返回
}
DWORD ip;
DWORD WINAPI PBThreadProc(LPVOID lpParameter)
{
HWND hwndPB = (HWND)lpParameter; //进度条的窗口句柄
PBRANGE range; //进度条的范围
SendMessage(hwndPB, PBM_SETRANGE, //设置进度条的范围
(WPARAM)0, (LPARAM)(MAKELPARAM(0, 150)));
SendMessage(hwndPB, PBM_GETRANGE, //获取进度条的范围
(WPARAM)TRUE, //TRUE 表示返回值为范围的最小值,FALSE表示返回最大值
(LPARAM)&range);
while (TRUE)
{
COLORREF color = RGB(1, 255, 1);
SendMessage(hwndPB, PBM_SETBKCOLOR, 0, (LPARAM)color);
SendMessage(hwndPB, PBM_DELTAPOS, //设置进度条的新位置为当前位置加上范围的1/20
(WPARAM)((range.iHigh - range.iLow) / 20), (LPARAM)0);
if (SendMessage(hwndPB, PBM_GETPOS, (WPARAM)0, (LPARAM)0) //取得进度条当前位置
== range.iHigh)
{
SendMessage(hwndPB, PBM_SETPOS, (WPARAM)range.iLow, (LPARAM)0); //将进度条复位
//ExitThread(ip);
}
Sleep(100);
}
}
LRESULT WINAPI WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndPB;
switch (message)
{
case WM_CREATE:
{
InitCommonControls(); //确保已注册了进度条类PROGRESS_CLASS
hwndPB = CreateWindowEx(
0,
PROGRESS_CLASS,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, //位置和大小在WM_SIZE中设置
hwnd,
(HMENU)0,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
CreateThread( //操作进度条的线程
NULL,
0,
(LPTHREAD_START_ROUTINE)PBThreadProc,
hwndPB,
0,
&ip
);
return 0;
}
case WM_SIZE:
{
RECT rc;
GetClientRect(hwnd, &rc);
MoveWindow(
hwndPB,
rc.left,
(rc.bottom - rc.top) / 4,
rc.right - rc.left,
(rc.bottom - rc.top) / 10,
TRUE
);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}