📄 progresswnd.cpp
字号:
CSize ClientSize(nMargin + TextRect.Width() + nMargin, nMargin + TextRect.Height() + nMargin + CancelRect.Height() + nMargin); CRect WndRect, ClientRect; GetWindowRect(WndRect); GetClientRect(ClientRect); WndRect.right = WndRect.left + WndRect.Width() - ClientRect.Width() + ClientSize.cx; WndRect.bottom = WndRect.top + WndRect.Height() - ClientRect.Height() + ClientSize.cy; MoveWindow(WndRect); // Now reposition the controls... m_wndProgress.MoveWindow(ProgressRect); //m_CancelButton.MoveWindow(CancelRect); m_Text.MoveWindow(TextRect);}void RxProgressWnd::Clear() { SetText(_T("")); SetPos(0); m_bCancelled = FALSE; m_nPrevPos = 0; if (::IsWindow(GetSafeHwnd())) UpdateWindow();}void RxProgressWnd::Hide() { if (!::IsWindow(GetSafeHwnd())) return; if (IsWindowVisible()) { ShowWindow(SW_HIDE); ModifyStyle(WS_VISIBLE, 0); }}void RxProgressWnd::Show() { if (!::IsWindow(GetSafeHwnd())) return; if (!IsWindowVisible()) { ModifyStyle(0, WS_VISIBLE); ShowWindow(SW_SHOWNA); RedrawWindow(NULL,NULL,RDW_ERASE|RDW_FRAME|RDW_INVALIDATE); }}void RxProgressWnd::SetRange(int nLower, int nUpper, int nStep /* = 1 */) { if (!::IsWindow(GetSafeHwnd())) return; // To take advantage of the Extended Range Values we use the PBM_SETRANGE32 // message intead of calling CProgressCtrl::SetRange directly. If this is // being compiled under something less than VC 5.0, the necessary defines // may not be available.#ifdef PBM_SETRANGE32 ASSERT(-0x7FFFFFFF <= nLower && nLower <= 0x7FFFFFFF); ASSERT(-0x7FFFFFFF <= nUpper && nUpper <= 0x7FFFFFFF); m_wndProgress.SendMessage(PBM_SETRANGE32, (WPARAM) nLower, (LPARAM) nUpper);#else ASSERT(0 <= nLower && nLower <= 65535); ASSERT(0 <= nUpper && nUpper <= 65535); m_wndProgress.SetRange(nLower, nUpper);#endif m_nMaxValue = nUpper; m_nMinValue = nLower; m_nStep = nStep; m_wndProgress.SetStep(nStep);}int RxProgressWnd::OffsetPos(int nPos){ if (!::IsWindow(GetSafeHwnd())) return m_nPrevPos; Show(); return SetPos(m_nPrevPos + nPos); }int RxProgressWnd::StepIt() { if (!::IsWindow(GetSafeHwnd())) return m_nPrevPos; Show(); return SetPos(m_nPrevPos + m_nStep); }int RxProgressWnd::SetStep(int nStep){ int nOldStep = m_nStep; m_nStep = nStep; if (!::IsWindow(GetSafeHwnd())) return nOldStep; return m_wndProgress.SetStep(nStep); }int RxProgressWnd::SetPos(int nPos) {#ifdef PBM_SETRANGE32 ASSERT(-0x7FFFFFFF <= nPos && nPos <= 0x7FFFFFFF);#else ASSERT(0 <= nPos && nPos <= 65535);#endif if (!::IsWindow(GetSafeHwnd())) return m_nPrevPos; Show(); CString strTitle; int nPercentage; m_nPrevPos = nPos; if (m_nMaxValue > m_nMinValue) nPercentage = (nPos*100)/(m_nMaxValue - m_nMinValue); else nPercentage = 0; if (nPercentage != m_nPrevPercent) { m_nPrevPercent = nPercentage; strTitle.Format(_T("%s [%d%%]"),m_strTitle,nPercentage); SetWindowText(strTitle); } return m_wndProgress.SetPos(nPos); }void RxProgressWnd::SetText(LPCTSTR fmt, ...){ if (!::IsWindow(GetSafeHwnd())) return; va_list args; TCHAR buffer[512]; va_start(args, fmt); _vstprintf(buffer, fmt, args); va_end(args); m_Text.SetWindowText(buffer);}BEGIN_MESSAGE_MAP(RxProgressWnd, CWnd) //{{AFX_MSG_MAP(RxProgressWnd) ON_WM_ERASEBKGND() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_CANCEL, OnCancel)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// RxProgressWnd message handlersBOOL RxProgressWnd::OnEraseBkgnd(CDC* pDC) { // Fill background with Catchment background colour CBrush backBrush(GetSysColor(COLOR_BTNFACE)); CBrush* pOldBrush = pDC->SelectObject(&backBrush); CRect rect; pDC->GetClipBox(&rect); // Erase the area needed pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC->SelectObject(pOldBrush); return TRUE;}void RxProgressWnd::OnCancel() { if (m_bNoCancel) return; m_bCancelled = TRUE; Hide(); if (m_bModal) SendMessage(WM_CLOSE); //RxMainFrame *pMainFrm = (RxMainFrame *)AfxGetMainWnd(); //if (pMainFrm && ::IsWindow(pMainFrm->m_hWnd)) // pMainFrm->SetForegroundWindow(); CWnd *pWnd = RxGetFrameMain(); if (pWnd && ::IsWindow(pWnd->m_hWnd)) int re = pWnd->SetForegroundWindow();}BOOL RxProgressWnd::DestroyWindow() { if (m_bPersistantPosition) SaveCurrentSettings(); if (m_bModal) { m_bModal = FALSE; CWnd *pMainWnd; if (m_bNoCancel) pMainWnd = AfxGetMainWnd(); else pMainWnd = RxGetFrameMain(); if (pMainWnd) pMainWnd->EnableWindow(TRUE); } return CWnd::DestroyWindow();}// Message pumping function that can either be used to pump messages during// long operations. This version will only pass messages to this window (and// all child windows). (Thanks to Michael <mbh-ep@post5.tele.dk> for this)void RxProgressWnd::PeekAndPump(BOOL bCancelOnESCkey /*= TRUE*/){ if (m_bModal && ::GetFocus() != m_hWnd) SetFocus(); MSG msg; while (!m_bCancelled && ::PeekMessage(&msg, NULL,0 ,0 , PM_NOREMOVE)) { if (bCancelOnESCkey && (msg.message == WM_CHAR) && (msg.wParam == VK_ESCAPE)) OnCancel(); // Cancel button disabled if modal, so we fake it. if (m_bModal && (msg.message == WM_LBUTTONUP)) { CRect rect; m_CancelButton.GetWindowRect(rect); if (rect.PtInRect(msg.pt)) OnCancel(); } if (!AfxGetApp()->PumpMessage()) { ::PostQuitMessage(0); return; } }}// Retores the previous window size from the registryvoid RxProgressWnd::GetPreviousSettings(){ int x = AfxGetApp()->GetProfileInt(szSection, szEntryX, -1); int y = AfxGetApp()->GetProfileInt(szSection, szEntryY, -1); if (x >= 0 && x < GetSystemMetrics(SM_CXSCREEN) && y >= 0 && y < GetSystemMetrics(SM_CYSCREEN)) { SetWindowPos(NULL, x,y, 0,0, SWP_NOSIZE|SWP_NOZORDER); } else CenterWindow();}// Saves the current window position registryvoid RxProgressWnd::SaveCurrentSettings(){ if (!IsWindow(m_hWnd)) return; CRect rect; GetWindowRect(rect); AfxGetApp()->WriteProfileInt(szSection, szEntryX, rect.left); AfxGetApp()->WriteProfileInt(szSection, szEntryY, rect.top);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -