进度条的C语言
(来自网络) #include <windows.h>
#include <commctrl.h> //InitCommonControls
//过程函数的返回值都是LRESULT,类型都是CALLBACK
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR 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_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, //窗口风格
CW_USEDEFAULT, //初始的x坐标
CW_USEDEFAULT, //初始的y坐标
480, //初始的宽度
200, //初始的高度
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 WINAPI PBThreadProc( LPVOID lpParameter )
{
HWND hwndPB = (HWND) lpParameter; //进度条的窗口句柄
PBRANGE range; //进度条的范围
SendMessage( hwndPB, PBM_SETRANGE, //设置进度条的范围
(WPARAM)0, (LPARAM)(MAKELPARAM(0,100)) );
SendMessage( hwndPB, PBM_GETRANGE, //获取进度条的范围
(WPARAM)TRUE, //TRUE 表示返回值为范围的最小值,FALSE表示返回最大值
(LPARAM)&range );
while(TRUE)
{
SendMessage( hwndPB, PBM_DELTAPOS, //设置进度条的新位置为当前位置加上范围的1/40
(WPARAM)((range.iHigh-range.iLow)/40), (LPARAM)0 );
if( SendMessage( hwndPB, PBM_GETPOS, (WPARAM)0, (LPARAM)0 ) //取得进度条当前位置
== range.iHigh )
{
SendMessage( hwndPB, PBM_SETPOS, (WPARAM)range.iLow, (LPARAM)0 ); //将进度条复位
}
Sleep(1000); //每1秒更新一次进度条
}
}
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,
0
);
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 );
} 可以用到资源文件(.rc)
//
// SUPPORT FOR WINDOWS XP THEMES:
// THIS WILL MAKE THE PROGRAM USE THE COMMON CONTROLS
// LIBRARY VERSION 6.0 (IF IT IS AVAILABLE)
//
1 24 CtrlProgressBar.exe.Manifest