📄 cdxcdynamiccontrolsmanager.cpp
字号:
}/////////////////////////////////////////////////////////////////////////////// control positioning//////////////////////////////////////////////////////////////////////////////* * Reposition (without current rectangle size) * * rectWin - window rectangle (including border) * rectClient - window rectangle (client area) * Note that since release 6, rectClient.left and .top might be > zero * (for scrolling) * bRedraw - invalidate & update window */void cdxCDynamicControlsManager::ReorganizeControlsAdvanced(const CRect & rectWin, CRect rectClient, bool bRedraw){ ASSERT(IsReady()); if(!GetTotalChildCnt()) return; // // we won't go smaller with the whole window than // m_szMin // if(rectWin.Width() < m_szMin.cx) rectClient.right += (m_szMin.cx - rectWin.Width()); if(rectWin.Height() < m_szMin.cy) rectClient.bottom += (m_szMin.cy - rectWin.Height()); // // we new replace all controls // CSize szDelta; szDelta.cx = rectClient.Width() - m_szClientRelative.cx; szDelta.cy = rectClient.Height() - m_szClientRelative.cy; CPoint pntOffset = rectClient.TopLeft(); // newly added code by Rodger Bernstein AFX_SIZEPARENTPARAMS layout; ControlData *sz; bool bManual = true; if(!( layout.hDWP = ::BeginDeferWindowPos(GetTotalChildCnt()) )) { TRACE(_T("*** ERROR[cdxCDynamicControlsManager::ReorganizeControlsAdvanced()]: BeginDeferWindowPos() failed.\n")); } else { for(sz = m_pFirst; sz; sz = sz->GetNext()) sz->OnSize(szDelta, &layout, &pntOffset); if(!::EndDeferWindowPos(layout.hDWP)) { TRACE(_T("*** ERROR[cdxCDynamicControlsManager::ReorganizeControlsAdvanced()]: EndDeferWindowPos() failed.\n")); } else bManual = false; } if(bManual) for(sz = m_pFirst; sz; sz = sz->GetNext()) sz->OnSize(szDelta, NULL); if(bRedraw && m_pWnd->IsWindowVisible()) { m_pWnd->RedrawWindow(NULL, NULL, RDW_UPDATENOW | RDW_NOERASE); }}/////////////////////////////////////////////////////////////////////////////// misc//////////////////////////////////////////////////////////////////////////////* * change minimum and maximum height of window. * * szMin - new minimum size (use GetMinSize() to leave it as being before) * szMax - new maximum size ( " GetMaxSize() ") * Set to CSize(0,0) if you don't want a maximum size. * bResizeIfNecessary - call FixWindowSize() past calculating new sizes. * * returns false if szMin and szMax are illegal (e.g. szMin > szMax) */bool cdxCDynamicControlsManager::SetMinMaxSize(const CSize & szMin, const CSize & szMax, bool bResizeIfNecessary){ ASSERT(IsReady()); // DoInitWindow() not called ? if((szMax.cx && (szMin.cx > szMax.cx)) || (szMax.cy && (szMin.cy > szMax.cy))) { return false; } m_szMin = szMin; m_szMax = szMax; if(bResizeIfNecessary) FixWindowSize(); return true;}/* * this function ensure that the window's size is between m_szMin and m_szMax. * returns true if window size has been changed */bool cdxCDynamicControlsManager::FixWindowSize(){ ASSERT(IsReady()); // use DoInitWindow() first ! CSize szCurrent = GetWindowSize(*m_pWnd), szDelta; if(m_szMax.cx && (szCurrent.cx > m_szMax.cx)) szDelta.cx = m_szMax.cx - szCurrent.cx; // is negative else if(szCurrent.cx < m_szMin.cx) szDelta.cx = m_szMin.cx - szCurrent.cx; // is positive else szDelta.cx = 0; if(m_szMax.cy && (szCurrent.cy > m_szMax.cy)) szDelta.cy = m_szMax.cy - szCurrent.cy; // is negative else if(szCurrent.cy < m_szMin.cy) szDelta.cy = m_szMin.cy - szCurrent.cy; // is positive else szDelta.cy = 0; if(!szDelta.cx && !szDelta.cy) return false; // nothing to do StretchWindow(*m_pWnd,szDelta); return true;}//////////////////////////////////////////////////////////////////////////////* * hide and show icon */void cdxCDynamicControlsManager::HideSizeIcon(){ if(m_pWndSizeIcon && ::IsWindow(m_pWndSizeIcon->m_hWnd)) m_pWndSizeIcon->ShowWindow(SW_HIDE);}void cdxCDynamicControlsManager::ShowSizeIcon(){ if(m_pWndSizeIcon && ::IsWindow(m_pWndSizeIcon->m_hWnd)) m_pWndSizeIcon->ShowWindow(SW_SHOW);}/////////////////////////////////////////////////////////////////////////////// static functions: window sizing//////////////////////////////////////////////////////////////////////////////* * stretches the window by szDelta (i.e. if szDelta is 100, the window is enlarged by 100 pixels) * stretching means that the center point of the window remains * * returns false if the window would be smaller than (1,1) * * NOTE: this function does NOT care of the min/max dimensions of a window * Use MoveWindow() if you need to take care of it. * * STATIC */bool cdxCDynamicControlsManager::StretchWindow(CWnd & rWnd, const CSize & szDelta){ ASSERT(::IsWindow(rWnd.m_hWnd)); WINDOWPLACEMENT wpl; rWnd.GetWindowPlacement(&wpl); wpl.rcNormalPosition.left -= szDelta.cx / 2; wpl.rcNormalPosition.right += (szDelta.cx + 1) / 2; wpl.rcNormalPosition.top -= szDelta.cy / 2; wpl.rcNormalPosition.bottom += (szDelta.cy + 1) / 2;// wpl.flags = SW_SHOWNA|SW_SHOWNOACTIVATE; if((wpl.rcNormalPosition.left >= wpl.rcNormalPosition.right) || (wpl.rcNormalPosition.top >= wpl.rcNormalPosition.bottom)) return false; VERIFY( rWnd.SetWindowPos( NULL, wpl.rcNormalPosition.left, wpl.rcNormalPosition.top, wpl.rcNormalPosition.right - wpl.rcNormalPosition.left, wpl.rcNormalPosition.bottom - wpl.rcNormalPosition.top, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOZORDER) ); return true;}/* * stretch window by a percent value * the algorithm calculates the new size for both dimensions by: * * newWid = oldWid + (oldWid * iAddPcnt) / 100 * * NOTE: iAddPcnt may even be nagtive, but it MUST be greater than -100. * NOTE: this function does NOT care of the min/max dimensions of a window * * The function will return false if the new size would be empty. * * STATIC */bool cdxCDynamicControlsManager::StretchWindow(CWnd & rWnd, int iAddPcnt){ ASSERT(::IsWindow(rWnd.m_hWnd)); CSize szDelta = GetWindowSize(rWnd); szDelta.cx = (szDelta.cx * iAddPcnt) / 100; szDelta.cy = (szDelta.cy * iAddPcnt) / 100; return StretchWindow(rWnd,szDelta);}/* * get current window's size * * STATIC */CSize cdxCDynamicControlsManager::GetWindowSize(CWnd & rWnd){ ASSERT(::IsWindow(rWnd.m_hWnd)); CRect rect; rWnd.GetWindowRect(&rect); return CSize(rect.Width(),rect.Height());}/////////////////////////////////////////////////////////////////////////////// static functions: window & registry//////////////////////////////////////////////////////////////////////////////* * stores a window's position and visiblity to the registry. * * return false if any error occured * * STATIC */bool cdxCDynamicControlsManager::StoreWindowPosition(CWnd & rWnd, LPCTSTR lpszProfile){ ASSERT(::IsWindow(rWnd.m_hWnd) && lpszProfile && *lpszProfile); // can't use an empty profile section string; see CWinApp::GetProfileInt() for further information WINDOWPLACEMENT wpl; VERIFY( rWnd.GetWindowPlacement(&wpl) ); BOOL bVisible = rWnd.IsWindowVisible(); int iState = REGVAL_NOSTATE; if(rWnd.IsIconic()) iState = REGVAL_ICONIC; else if(rWnd.IsZoomed()) iState = REGVAL_MAXIMIZED; return AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Valid, REGVAL_INVALID) && // invalidate first AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Left, wpl.rcNormalPosition.left) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Right, wpl.rcNormalPosition.right) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Top, wpl.rcNormalPosition.top) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Bottom, wpl.rcNormalPosition.bottom) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Visible, bVisible ? REGVAL_VISIBLE : REGVAL_HIDDEN) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_State, iState) && AfxGetApp()->WriteProfileInt(lpszProfile, lpszRegVal_Valid, REGVAL_VALID); // validate position}/* * load the registry data stored by StoreWindowPosition() * returns true if data have been found in the registry * * STATIC */bool cdxCDynamicControlsManager::RestoreWindowPosition(CWnd & rWnd, LPCTSTR lpszProfile, UINT restoreFlags){ ASSERT(::IsWindow(rWnd.m_hWnd) && lpszProfile && *lpszProfile); // can't use an empty profile section string; see CWinApp::GetProfileInt() for further information // // first, we check whether the position had been saved successful any time before // if( AfxGetApp()->GetProfileInt(lpszProfile,lpszRegVal_Valid,REGVAL_INVALID) != REGVAL_VALID ) return false; // // get old position // WINDOWPLACEMENT wpl; VERIFY( rWnd.GetWindowPlacement(&wpl) ); // // read registry // int iState = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_State, REGVAL_NOSTATE); // // get window's previous normal position // wpl.rcNormalPosition.left = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_Left, wpl.rcNormalPosition.left); wpl.rcNormalPosition.right = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_Right, wpl.rcNormalPosition.right); wpl.rcNormalPosition.top = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_Top, wpl.rcNormalPosition.top); wpl.rcNormalPosition.bottom = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_Bottom, wpl.rcNormalPosition.bottom); if(wpl.rcNormalPosition.left > wpl.rcNormalPosition.right) { long l = wpl.rcNormalPosition.right; wpl.rcNormalPosition.right = wpl.rcNormalPosition.left; wpl.rcNormalPosition.left = l; } if(wpl.rcNormalPosition.top > wpl.rcNormalPosition.bottom) { long l = wpl.rcNormalPosition.bottom; wpl.rcNormalPosition.bottom = wpl.rcNormalPosition.top; wpl.rcNormalPosition.top = l; } // // get restore stuff // UINT showCmd = SW_SHOWNA; if(restoreFlags & rflg_state) { if(iState == REGVAL_MAXIMIZED) showCmd = SW_MAXIMIZE; else if(iState == REGVAL_ICONIC) showCmd = SW_MINIMIZE; } // // use MoveWindow() which takes care of WM_GETMINMAXINFO // rWnd.MoveWindow( wpl.rcNormalPosition.left,wpl.rcNormalPosition.top, wpl.rcNormalPosition.right - wpl.rcNormalPosition.left, wpl.rcNormalPosition.bottom - wpl.rcNormalPosition.top, showCmd == SW_SHOWNA); if(showCmd != SW_SHOWNA) { // read updated position VERIFY( rWnd.GetWindowPlacement(&wpl) ); wpl.showCmd = showCmd; rWnd.SetWindowPlacement(&wpl); } // // get visiblity // if(restoreFlags & rflg_visibility) { int i = AfxGetApp()->GetProfileInt(lpszProfile, lpszRegVal_Visible, REGVAL_NOSTATE); if(i == REGVAL_VISIBLE) rWnd.ShowWindow(SW_SHOW); else if(i == REGVAL_HIDDEN) rWnd.ShowWindow(SW_HIDE); } return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -