码迷,mamicode.com
首页 > Windows程序 > 详细

用Win32 实现进度条

时间:2014-11-18 23:17:18      阅读:700      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

转载:http://www.cctry.com/thread-238862-1-1.html

 

  1     #include <windows.h>
  2     #include <commctrl.h>        //InitCommonControls
  3     #pragma comment(lib, "comctl32") //InitCommonControls
  4     //过程函数的返回值都是LRESULT,类型都是CALLBACK
  5     LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
  6     int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpszCmdLine,int nCmdShow)
  7     {
  8         HWND hwnd;        //主窗口句柄,在CreateWindow中赋值
  9         MSG msg;        //消息变量,在GetMessage中使用
 10         WNDCLASS wndclass;        //窗口类
 11         TCHAR* szAppName = TEXT("ProgressBar");    //类名和窗口名
 12         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    //窗口背景
 13         wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);        //鼠标
 14         wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);    //图标
 15         wndclass.lpszClassName = szAppName;        //类名
 16         wndclass.cbClsExtra = 0;        //类的额外参数
 17         wndclass.cbWndExtra = 0;        //窗口的额外参数.用于基于同一窗口类的窗口各自区分.
 18                                         //在自定义对话框类时必须指定为DLGWINDOWEXTRA的大小
 19         wndclass.lpszMenuName = NULL;    //菜单名.可以用作子窗口的id
 20         wndclass.style = CS_HREDRAW | CS_VREDRAW;    //窗口风格
 21         wndclass.lpfnWndProc = WndProc;        //窗口过程
 22         wndclass.hInstance = hInstance;        //包含窗口过程的实例句柄
 23 
 24         if( !RegisterClass(&wndclass) )        //注册窗口类
 25             return 0;
 26         hwnd = CreateWindow(                //创建窗口
 27                     szAppName,                //窗口类名
 28                     szAppName,                //窗口标题
 29                     WS_OVERLAPPEDWINDOW,    //窗口风格
 30                     CW_USEDEFAULT,            //初始的x坐标
 31                     CW_USEDEFAULT,            //初始的y坐标
 32                     CW_USEDEFAULT,            //初始的宽度
 33                     CW_USEDEFAULT,            //初始的高度
 34                     NULL,                    //父窗口
 35                     NULL,                    //菜单
 36                     hInstance,                //和窗口相关的实例句柄
 37                     NULL                    //额外参数
 38                     );
 39         ShowWindow( hwnd,nCmdShow );        //显示窗口
 40         UpdateWindow( hwnd );                //更新窗口
 41         while( GetMessage(&msg,NULL,0,0) )    //消息循环
 42         {
 43             TranslateMessage(&msg);            //将WM_XXXKEYXXX消息翻译为WM_CHAR消息
 44             DispatchMessage(&msg);            //传递消息到窗口过程
 45         }   
 46         return msg.wParam;                    //返回
 47     }
 48 
 49     DWORD WINAPI PBThreadProc( LPVOID lpParameter )
 50     {
 51         HWND hwndPB = (HWND) lpParameter;    //进度条的窗口句柄
 52         PBRANGE range;                        //进度条的范围
 53        
 54         SendMessage( hwndPB, PBM_SETRANGE,    //设置进度条的范围
 55             (WPARAM)0, (LPARAM)(MAKELPARAM(0,100)) );
 56        
 57         SendMessage( hwndPB, PBM_GETRANGE,    //获取进度条的范围
 58             (WPARAM)TRUE,                    //TRUE 表示返回值为范围的最小值,FALSE表示返回最大值
 59             (LPARAM)&range );
 60 
 61         while(TRUE)
 62         {
 63             SendMessage( hwndPB, PBM_DELTAPOS, //设置进度条的新位置为当前位置加上范围的1/20
 64                 (WPARAM)((range.iHigh-range.iLow)/20), (LPARAM)0 );
 65             if( SendMessage( hwndPB, PBM_GETPOS, (WPARAM)0, (LPARAM)0 ) //取得进度条当前位置
 66                  == range.iHigh )
 67             {
 68                 SendMessage( hwndPB, PBM_SETPOS, (WPARAM)range.iLow, (LPARAM)0 ); //将进度条复位
 69             }
 70             Sleep(1000);
 71         }
 72     }
 73 
 74 
 75     LRESULT WINAPI WndProc( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam )
 76     {
 77         static HWND hwndPB;
 78         switch( message )
 79         {
 80         case WM_CREATE:
 81             {
 82                 InitCommonControls(); //确保已注册了进度条类PROGRESS_CLASS
 83                 hwndPB = CreateWindowEx(
 84                     0,
 85                     PROGRESS_CLASS,
 86                     NULL,
 87                     WS_CHILD | WS_VISIBLE,
 88                     0,0,0,0,            //位置和大小在WM_SIZE中设置
 89                     hwnd,
 90                     (HMENU)0,
 91                     ((LPCREATESTRUCT)lParam)->hInstance,
 92                     NULL);
 93                 CreateThread(            //操作进度条的线程
 94                     NULL,
 95                     0,
 96                     (LPTHREAD_START_ROUTINE)PBThreadProc,
 97                     hwndPB,
 98                     0,
 99                     0
100                     );
101                 return 0;
102             }
103         case WM_SIZE:
104             {
105                 RECT rc;
106                 GetClientRect(hwnd, &rc);
107                 MoveWindow(
108                     hwndPB,
109                     rc.left,
110                     (rc.bottom - rc.top )/4,
111                     rc.right - rc.left,
112                     (rc.bottom - rc.top )/10,
113                     TRUE
114                     );
115                 return 0;
116             }
117         case WM_DESTROY:
118             PostQuitMessage(0);
119             return 0;
120         }
121         return DefWindowProc( hwnd,message,wParam,lParam );
122     }

 

用Win32 实现进度条

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/chechen/p/4106469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!