1 // MainFrm.h 2 3 //对话框(用来显示进度条) 4 5 #include "ProgressDlg.h" 6 7 #define WM_WORKPROGRESS WM_USER+100 8 #define CN_PROGRESS 1 9 #define CN_DONE 3 10 11 12 struct ThreadParam{ 13 HWND m_NotifyWnd; 14 }; 15 16 class CMainFrame : public CFrameWnd 17 { 18 19 ![](/Images/dot.gif) .. 20 21 protected: 22 23 CProgressDlg m_ProgressDlg; 24 25 public: 26 // 处理线程消息 27 LRESULT OnThreadProgress(WPARAM wParam, LPARAM lParam); 28 29 CWinThread* m_pAssistantThread ; 30 ThreadParam gThreadParam; 31 32 33 } 34 35 36 // MainFrm.cpp 37 38 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) 39 40 . 41 ON_MESSAGE(WM_WORKPROGRESS, OnThreadProgress) 42 END_MESSAGE_MAP() 43 44 CMainFrame::CMainFrame() 45 { 46 // TODO: 在此添加成员初始化代码 47 m_pAssistantThread = NULL; 48 } 49 50 CMainFrame::~CMainFrame() 51 { 52 if( m_pAssistantThread!=NULL ) 53 delete m_pAssistantThread; m_pAssistantThread = NULL; 54 } 55 56 57 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 58 59 { 60 61 ![](/Images/dot.gif) ![](/Images/dot.gif) . 62 63 m_ProgressDlg.Create(CProgressDlg::IDD, this); 64 65 return 0; 66 67 } 68 69 //辅助线程 70 static UINT ThreadFunc(LPVOID pParam) 71 { 72 ThreadParam *ptParam = (ThreadParam*)pParam; 73 HWND hNotifyWnd = ptParam->m_NotifyWnd; 74 int nSum = 1000; 75 for(int i=0;i<nSum;i++) 76 { 77 if(::IsWindow(hNotifyWnd)) 78 { 79 ::PostMessage( hNotifyWnd,WM_WORKPROGRESS,MAKEWPARAM(CN_PROGRESS,0),(LPARAM)i ); 80 } 81 Sleep(1); 82 } 83 84 if(::IsWindow(hNotifyWnd)) 85 { 86 ::PostMessage(hNotifyWnd,WM_WORKPROGRESS,MAKEWPARAM(CN_DONE,0),(LPARAM)nSum ); 87 } 88 return 0; 89 } 90 91 void CMainFrame::OnTestthreadShowprogressdlg() 92 { 93 //m_ProgressDlg.ShowWindow(SW_SHOW); 94 if(m_pAssistantThread != NULL ) 95 { 96 DWORD dwExitCode = -1; 97 VERIFY(GetExitCodeThread(*m_pAssistantThread,&dwExitCode)); 98 if(dwExitCode == STILL_ACTIVE) 99 { 100 return ; 101 } 102 delete m_pAssistantThread; m_pAssistantThread = NULL; 103 } 104 105 106 m_ProgressDlg.m_ProgressCtrl.SetRange(0,1000); 107 gThreadParam.m_NotifyWnd = (AfxGetApp()->m_pMainWnd)->m_hWnd; 108 m_pAssistantThread = AfxBeginThread( ThreadFunc, 109 &gThreadParam, 110 THREAD_PRIORITY_NORMAL, 111 0, 112 CREATE_SUSPENDED, 113 NULL); 114 ASSERT( m_pAssistantThread ); 115 m_pAssistantThread->m_bAutoDelete = FALSE; 116 m_pAssistantThread->ResumeThread(); 117 } 118 119 // 处理线程消息 120 LRESULT CMainFrame::OnThreadProgress(WPARAM wParam, LPARAM lParam) 121 { 122 UINT nID = LOWORD(wParam); 123 if(nID == CN_PROGRESS) 124 { 125 int nProgress = (int)lParam; 126 m_ProgressDlg.ShowWindow(SW_SHOW); 127 m_ProgressDlg.m_ProgressCtrl.SetPos(nProgress); 128 } 129 else if(nID == CN_DONE) 130 { 131 int nSum = (int)lParam; 132 m_ProgressDlg.ShowWindow(SW_HIDE); 133 } 134 return 0; 135 } 136 137
|