📄 autohidebar.cpp
字号:
}
m_rect.bottom = rect->top = pHidePageItem->m_rect.bottom;
}
}
}
}
BOOL CHideItem::OnMouseMove(CPoint pt)
{
CHidePageItem* pHidePageItem;
for(int i =0; i< m_arrPags.GetSize(); i++)
{
pHidePageItem = (CHidePageItem*)m_arrPags[i];
if(NULL != pHidePageItem)
{
if(pHidePageItem->m_rect.PtInRect (pt))
{
Show(i);
if(m_nActivePage != i)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
}
return FALSE;
}
void CHideItem::Show(int nShow)
{
if(m_nActivePage == nShow && m_wndFloat.IsWindowVisible ())
return;
CRect rect;
m_pAutoHideBar->GetWindowRect(rect);
m_wndFloat.GetWindowRect (m_oldWndRect);
if (m_dwStyle & CBRS_ORIENT_HORZ)
{
rect.bottom = rect.top + m_oldWndRect.Height();
if (m_dwStyle & CBRS_ALIGN_TOP)
{
rect.OffsetRect (0, 26);
}
else if (m_dwStyle & CBRS_ALIGN_BOTTOM)
{
rect.OffsetRect (0, -m_oldWndRect.Height());
}
}
else if (m_dwStyle & CBRS_ORIENT_VERT)
{
rect.right = rect.left + m_oldWndRect.Width();
if (m_dwStyle & CBRS_ALIGN_LEFT)
rect.OffsetRect (26, 0);
else if (m_dwStyle & CBRS_ALIGN_RIGHT)
rect.OffsetRect (- m_oldWndRect.Width(), 0);
}
else
{
ASSERT(FALSE); // can never happen
}
m_wndFloat.ShowWindow (SW_HIDE);
m_wndFloat.MoveWindow(0,0,0,0);
((CHidePageItem*)m_arrPags[m_nActivePage])->m_pWnd->ShowWindow (SW_HIDE);
((CHidePageItem*)m_arrPags[m_nActivePage])->m_pWnd->SetParent (m_pDockPageBar);
m_nActivePage = nShow;
CWnd * pchild = ((CHidePageItem*)m_arrPags[m_nActivePage])->m_pWnd;
pchild->SetParent (&m_wndFloat);
m_wndFloat.m_Title = ((CHidePageItem*)m_arrPags[m_nActivePage])->m_sText;
m_wndFloat.MoveWindow (rect.left,rect.top ,rect.Width (), rect.Height ());
CRect clientRect;
m_wndFloat.GetClientRect(clientRect);
pchild->MoveWindow(clientRect);
pchild->ShowWindow (SW_SHOW);
m_wndFloat.ShowWindow (SW_SHOW);
}
void CHideItem::Dock()
{
((CHidePageItem*)m_arrPags[m_nActivePage])->m_pWnd->SetParent (m_pDockPageBar);
RemoveAll();
m_wndFloat.DestroyWindow();
m_pDockPageBar->ShowWindow (SW_SHOW);
m_pDockPageBar->UpdateSize();
((CAutoHideBar*)m_pAutoHideBar)->UpdateBar();
}
/////////////////////////////////////////////////////////////////////////////
// remove all item (2004/04/06)
void CHideItem::RemoveAll()
{
CHidePageItem* pHidePageItem;
int count = m_arrPags.GetSize();
for(int i =0; i< count; i++)
{
pHidePageItem = (CHidePageItem*)m_arrPags[0];
delete pHidePageItem;
pHidePageItem = NULL;
m_arrPags.RemoveAt(0);
}
}
/////////////////////////////////////////////////////////////////////////////
// CAutoHideBar
CAutoHideBar::CAutoHideBar()
{
}
CAutoHideBar::~CAutoHideBar()
{
POSITION pos;
CHideItem* pHideItem;
for(pos=m_listBars.GetHeadPosition();pos!=NULL;)
{
pHideItem = (CHideItem*)m_listBars.GetNext(pos);
if(NULL != pHideItem)
{
pHideItem->RemoveAll ();
delete pHideItem;
pHideItem = NULL;
}
}
m_listBars.RemoveAll ();
}
BEGIN_MESSAGE_MAP(CAutoHideBar, CControlBar)
//{{AFX_MSG_MAP(CAutoHideBar)
ON_MESSAGE(WM_SIZEPARENT, OnSizeParent)
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoHideBar message handlers
BOOL CAutoHideBar::Create(CWnd *pParentWnd, DWORD dwStyle, UINT nID)
{
ASSERT(pParentWnd != NULL);
ASSERT_KINDOF(CFrameWnd, pParentWnd);
m_dwStyle = (dwStyle & CBRS_ALL);
// register and create the window
CString wndclass = ::AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
::GetSysColorBrush(COLOR_BTNFACE), 0);
dwStyle &= ~CBRS_ALL; // keep only the generic window styles
dwStyle |= WS_CLIPCHILDREN; // prevents flashing
if(m_dwStyle & CBRS_ORIENT_HORZ)
{
m_Font.CreateFont(13 ,0, 0,0, FW_NORMAL, 0,0,0, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FF_ROMAN , "Tahoma");
}
else if(m_dwStyle & CBRS_ORIENT_VERT)
{
m_Font.CreateFont(13,0, -900,-900, FW_NORMAL, 0,0,0, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FF_ROMAN , "Tahoma");
}
// Note: Parent must resize itself for control bar to be resized
return CWnd::Create(wndclass, NULL, dwStyle, CRect(0, 0, 0, 0), pParentWnd, nID);
}
LRESULT CAutoHideBar::OnSizeParent(WPARAM wParam, LPARAM lParam)
{
AFX_SIZEPARENTPARAMS* lpLayout = (AFX_SIZEPARENTPARAMS*)lParam;
DWORD dwStyle = m_dwStyle;
if(!(dwStyle & WS_VISIBLE))
{
SetWindowPos(NULL, 0, 0, 0, 0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_HIDEWINDOW);
}
if ((dwStyle & WS_VISIBLE) && (dwStyle & CBRS_ALIGN_ANY) != 0)
{
// align the control bar
CRect rect;
rect.CopyRect(&lpLayout->rect);
CSize sizeAvail = rect.Size(); // maximum size available
// get maximum requested size
DWORD dwMode = lpLayout->bStretch ? LM_STRETCH : 0;
if (dwStyle & CBRS_ORIENT_HORZ)
dwMode |= LM_HORZ | LM_HORZDOCK;
else
dwMode |= LM_VERTDOCK;
CSize size (26,28);
size.cx = min(size.cx, sizeAvail.cx);
size.cy = min(size.cy, sizeAvail.cy);
if (dwStyle & CBRS_ORIENT_HORZ)
{
lpLayout->sizeTotal.cy += size.cy;
lpLayout->sizeTotal.cx = max(lpLayout->sizeTotal.cx, size.cx);
if (dwStyle & CBRS_ALIGN_TOP)
lpLayout->rect.top += size.cy;
else if (dwStyle & CBRS_ALIGN_BOTTOM)
{
rect.top = rect.bottom - size.cy;
lpLayout->rect.bottom -= size.cy;
}
rect.bottom = rect.top + size.cy;
rect.right = lpLayout->rect.right;
}
else if (dwStyle & CBRS_ORIENT_VERT)
{
lpLayout->sizeTotal.cx += size.cx;
lpLayout->sizeTotal.cy = max(lpLayout->sizeTotal.cy, size.cy);
if (dwStyle & CBRS_ALIGN_LEFT)
lpLayout->rect.left += size.cx;
else if (dwStyle & CBRS_ALIGN_RIGHT)
{
rect.left = rect.right - size.cx;
lpLayout->rect.right -= size.cx;
}
rect.right = rect.left + size.cx;
rect.bottom = lpLayout->rect.bottom ;
}
else
{
ASSERT(FALSE); // can never happen
}
m_size = CSize(rect.BottomRight() - rect.TopLeft());
SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER|SWP_NOACTIVATE|SWP_SHOWWINDOW);
}
return 0;
}
void CAutoHideBar::OnUpdateCmdUI(CFrameWnd *pTarget, BOOL bDisableIfNoHndler)
{
}
void CAutoHideBar::OnMouseMove(UINT nFlags, CPoint point)
{
POSITION pos;
CHideItem* pHideItem;
for(pos=m_listBars.GetHeadPosition();pos!=NULL;)
{
pHideItem = (CHideItem*)m_listBars.GetNext(pos);
if(NULL != pHideItem)
{
if(pHideItem->m_rect.PtInRect (point))
{
pHideItem->OnMouseMove (point);
}
//else
// pHideItem->m_wndFloat.SetTimer(1, 1000, NULL);
}
}
Invalidate(FALSE);
CControlBar::OnMouseMove(nFlags, point);
}
void CAutoHideBar::OnPaint()
{
CPaintDC PaintDC(this); // device context for painting
CRect rcBar;
GetClientRect(&rcBar);
CDC dc;
dc.CreateCompatibleDC(&PaintDC);
CBitmap bm;
bm.CreateCompatibleBitmap(&PaintDC, rcBar.Width(), rcBar.Height());
dc.SelectObject(bm);
dc.SetBoundsRect(&rcBar, DCB_DISABLE);
// draw background
CBrush brush(RGB(247,243,233));
dc.FillRect(rcBar, &brush);
dc.SelectObject (m_Font);
//COLORREF crOldText;
//crOldText = dc.SetTextColor(RGB(128,128,128));
UpDateSize();
DrawItem(&dc);
//dc.SetTextColor(crOldText);
PaintDC.BitBlt (rcBar.left, rcBar.top, rcBar.Width(), rcBar.Height(), &dc, 0,0,SRCCOPY);
dc.DeleteDC ();
}
void CAutoHideBar::DrawItem(CDC *pDC)
{
POSITION pos;
CHideItem* pHideItem;
for(pos=m_listBars.GetHeadPosition();pos!=NULL;)
{
pHideItem = (CHideItem*)m_listBars.GetNext(pos);;
if(NULL != pHideItem)
pHideItem->Draw (pDC);
}
}
void CAutoHideBar::HidePageBar(CDockPageBar *pDockPageBar)
{
m_dwStyle |= WS_VISIBLE;
CHideItem* pHideItem = new CHideItem;
pHideItem->AddPageItem(pDockPageBar, this, m_dwStyle);
m_listBars.AddTail (pHideItem);
Invalidate();
}
void CAutoHideBar::UpDateSize()
{
CPaintDC dc(this);
dc.SelectObject (m_Font);
CRect rect;
GetClientRect(&rect);
if(m_dwStyle & CBRS_ORIENT_HORZ)
{
rect.left += 2;
if (m_dwStyle & CBRS_ALIGN_TOP)
rect.bottom -= 2;
else if (m_dwStyle & CBRS_ALIGN_BOTTOM)
rect.top += 2;
}
else if(m_dwStyle & CBRS_ORIENT_VERT)
{
rect.top += 2;
if (m_dwStyle & CBRS_ALIGN_LEFT)
rect.right -= 2;
else if (m_dwStyle & CBRS_ALIGN_RIGHT)
rect.left += 2;
}
else
{
ASSERT(FALSE); // can never happen
}
POSITION pos;
CHideItem* pHideItem;
for(pos=m_listBars.GetHeadPosition();pos!=NULL;)
{
pHideItem = (CHideItem*)m_listBars.GetNext(pos);;
if(NULL != pHideItem)
{
pHideItem->UpDateSize (&dc, &rect);
if(m_dwStyle & CBRS_ORIENT_HORZ)
{
rect.left += 20;
}
else if(m_dwStyle & CBRS_ORIENT_VERT)
{
rect.top += 20;
}
}
}
}
void CAutoHideBar::GetClientRect(CRect *rect)
{
CWnd::GetClientRect (rect);
if(m_dwStyle & CBRS_ORIENT_HORZ)
{
if (m_dwStyle & CBRS_ALIGN_TOP)
rect->bottom -= 2;
else if (m_dwStyle & CBRS_ALIGN_BOTTOM)
{
rect->top += 1;
rect->bottom -= 2;
}
}
else if(m_dwStyle & CBRS_ORIENT_VERT)
{
if (m_dwStyle & CBRS_ALIGN_LEFT)
rect->right -= 2;
else if (m_dwStyle & CBRS_ALIGN_RIGHT)
rect->left += 1;
}
else
{
ASSERT(FALSE); // can never happen
}
}
void CAutoHideBar::UpdateBar()
{
POSITION pos, pos2;
CHideItem* pHideItem;
for(pos=m_listBars.GetHeadPosition();( pos2 = pos ) != NULL;)
{
pHideItem = (CHideItem*)m_listBars.GetNext(pos);;
if(NULL != pHideItem)
{
if(pHideItem->m_arrPags.GetSize () == 0)
{
delete pHideItem;
pHideItem = NULL;
m_listBars.RemoveAt(pos2);
}
}
}
if(m_listBars.GetCount () == 0)
{
m_dwStyle &= ~WS_VISIBLE;
}
Invalidate ();
GetParentFrame()->RecalcLayout();
}
/////////////////////////////////////////////////////////////////////////////
// CAutoHideFrame
#define AHBRS_TOP 0xE82B
#define AHBRS_BOTTOM 0xE82C
#define AHBRS_LEFT 0xE82D
#define AHBRS_RIGHT 0xE82E
const DWORD CAutoHideFrame::dwHideBarMap[4][2] =
{
{ AHBRS_TOP, CBRS_TOP },
{ AHBRS_BOTTOM, CBRS_BOTTOM },
{ AHBRS_LEFT, CBRS_LEFT },
{ AHBRS_RIGHT, CBRS_RIGHT },
};
IMPLEMENT_DYNCREATE(CAutoHideFrame, CFrameWnd)
CAutoHideFrame::CAutoHideFrame()
{
}
CAutoHideFrame::~CAutoHideFrame()
{
}
BEGIN_MESSAGE_MAP(CAutoHideFrame, CFrameWnd)
//{{AFX_MSG_MAP(CAutoHideFrame)
ON_MESSAGE( WM_AUTOHIDE_ON, OnAutoHideOn)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoHideFrame message handlers
void CAutoHideFrame::EnableDocking(DWORD dwDockStyle)
{
// must be CBRS_ALIGN_XXX or CBRS_FLOAT_MULTI only
ASSERT((dwDockStyle & ~(CBRS_ALIGN_ANY|CBRS_FLOAT_MULTI)) == 0);
for (int i = 0; i < 4; i++)
{
if (dwHideBarMap[i][1] & dwDockStyle & CBRS_ALIGN_ANY)
{
CAutoHideBar* pAutoHide = (CAutoHideBar*)GetControlBar(dwHideBarMap[i][0]);
if (pAutoHide == NULL)
{
pAutoHide = new CAutoHideBar;
if (!pAutoHide->Create(this,
WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_CHILD|WS_VISIBLE |
dwHideBarMap[i][1], dwHideBarMap[i][0]))
{
AfxThrowResourceException();
}
}
}
}
CFrameWnd::EnableDocking(dwDockStyle);
}
void CAutoHideFrame::OnDestroy()
{
for (int i = 0; i < 4; i++)
{
CAutoHideBar* pAutoHide = (CAutoHideBar*)GetControlBar(dwHideBarMap[i][0]);
if (pAutoHide != NULL)
{
pAutoHide->DestroyWindow();
delete pAutoHide;
}
}
CFrameWnd::OnDestroy();
}
LRESULT CAutoHideFrame::OnAutoHideOn(WPARAM wParam, LPARAM lParam)
{
CDockPageBar * pWnd = (CDockPageBar *)lParam;
CAutoHideBar * pAutoHideBar;
switch(wParam)
{
case AFX_IDW_DOCKBAR_TOP:
pAutoHideBar = (CAutoHideBar *)GetControlBar(AHBRS_TOP);
break;
case AFX_IDW_DOCKBAR_BOTTOM:
pAutoHideBar = (CAutoHideBar *)GetControlBar(AHBRS_BOTTOM);
break;
case AFX_IDW_DOCKBAR_LEFT:
pAutoHideBar = (CAutoHideBar *)GetControlBar(AHBRS_LEFT);
break;
case AFX_IDW_DOCKBAR_RIGHT:
pAutoHideBar = (CAutoHideBar *)GetControlBar(AHBRS_RIGHT);
break;
}
pAutoHideBar->HidePageBar(pWnd);
RecalcLayout();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -