📄 chevbar.cpp
字号:
{
pTBar->GetButtonInfo ( iCount, id, iStyle, md->m_nBitmap);
SetMenuText(strText, md);
// md->nID = id;
// md->nFlags = iMenuStyle;
pop.AppendMenu ( iMenuStyle|MF_OWNERDRAW, id , (char*)md );
}
}
else if(rbinfo.wID == ID_MENU_BAR)
{
if(pMainFrame!=NULL)
{
CString str;
((CMainFrame*)pMainFrame)->GetMenu()->GetMenuString(iCount, str, MF_BYPOSITION);
CMenu * pPopup;
pPopup = ((CMainFrame*)pMainFrame)->GetMenu()->GetSubMenu(iCount);
if(pPopup!=NULL)
{
HMENU hmenu = CopyMenu(pPopup->m_hMenu);
pop.AppendMenu ( MF_POPUP, (UINT)hmenu, str );
}
}
}
// Yeah, have added one, so can show the menu
bAtleastOne=TRUE;
}
}
}
// Show the menu if atleast one item has been added
if ( bAtleastOne )
pop.TrackPopupMenu ( TPM_LEFTALIGN|TPM_TOPALIGN, ptMenu.x, ptMenu.y, pMainFrame);
if(rbinfo.wID == ID_VIEW_FAVS)
((CMainFrame*)pMainFrame)->m_nFavSize=FavSize;
// Delete our menu
pop.DestroyMenu ();
}
void CChevBar::OnRecalcParent()
{
CFrameWnd* pFrameWnd = GetParentFrame();
ASSERT(pFrameWnd != NULL);
pFrameWnd->RecalcLayout();
}
void CChevBar::OnHeightChange(NMHDR* /*pNMHDR*/, LRESULT* pResult)
{
// does the CChevBar have a frame?
CFrameWnd* pFrameWnd = GetParentFrame();
if (pFrameWnd != NULL)
{
// it does -- tell it to recalc its layout
// if (!pFrameWnd->m_bInRecalcLayout)
// pFrameWnd->RecalcLayout();
// else
PostMessage(WM_RECALCPARENT);
}
*pResult = 0;
}
LRESULT CChevBar::OnShowBand(WPARAM wParam, LPARAM)
{
LRESULT lResult = Default();
if (lResult)
{
// keep window visible state in sync with band visible state
REBARBANDINFO rbBand;
rbBand.cbSize = sizeof(rbBand);
rbBand.fMask = RBBIM_CHILD|RBBIM_STYLE;
VERIFY(DefWindowProc(RB_GETBANDINFO, wParam, (LPARAM)&rbBand));
CControlBar* pBar = DYNAMIC_DOWNCAST(CControlBar, CWnd::FromHandlePermanent(rbBand.hwndChild));
BOOL bWindowVisible;
if (pBar != NULL)
bWindowVisible = pBar->IsVisible();
else
bWindowVisible = (::GetWindowLong(rbBand.hwndChild, GWL_STYLE) & WS_VISIBLE) != 0;
BOOL bBandVisible = (rbBand.fStyle & RBBS_HIDDEN) == 0;
if (bWindowVisible != bBandVisible)
VERIFY(::ShowWindow(rbBand.hwndChild, bBandVisible ? SW_SHOW : SW_HIDE));
}
return lResult;
}
BOOL CChevBar::_AddBar(CWnd* pBar, REBARBANDINFO* pRBBI)
{
ASSERT_VALID(this);
ASSERT(::IsWindow(m_hWnd));
ASSERT(pBar != NULL);
ASSERT(::IsWindow(pBar->m_hWnd));
pRBBI->cbSize = sizeof(REBARBANDINFO);
pRBBI->fMask |= RBBIM_CHILD | RBBIM_CHILDSIZE;
pRBBI->hwndChild = pBar->m_hWnd;
CSize size;
CControlBar* pTemp = DYNAMIC_DOWNCAST(CControlBar, pBar);
if (pTemp != NULL)
{
size = pTemp->CalcFixedLayout(FALSE, m_dwStyle & CBRS_ORIENT_HORZ);
}
else
{
CRect rect;
pBar->GetWindowRect(&rect);
size = rect.Size();
}
//WINBUG: COMCTL32.DLL is off by 4 pixels in its sizing logic. Whatever
// is specified as the minimum size, the system rebar will allow that band
// to be 4 actual pixels smaller! That's why we add 4 to the size here.
// ASSERT(_afxComCtlVersion != -1);
pRBBI->cxMinChild = size.cx ; //(_afxComCtlVersion < VERSION_IE401 ? 4 : 0);
pRBBI->cyMinChild = size.cy ;
BOOL bResult = (BOOL)DefWindowProc(RB_INSERTBAND, (WPARAM)-1, (LPARAM)pRBBI);
CFrameWnd* pFrameWnd = GetParentFrame();
if (pFrameWnd != NULL)
pFrameWnd->RecalcLayout();
return bResult;
}
BOOL CChevBar::AddBar(CWnd* pBar, LPCTSTR pszText, CBitmap* pbmp, DWORD dwStyle)
{
REBARBANDINFO rbBand;
rbBand.fMask = RBBIM_STYLE;
rbBand.fStyle = dwStyle;
if (pszText != NULL)
{
rbBand.fMask |= RBBIM_TEXT;
rbBand.lpText = const_cast<LPTSTR>(pszText);
}
if (pbmp != NULL)
{
rbBand.fMask |= RBBIM_BACKGROUND;
rbBand.hbmBack = (HBITMAP)*pbmp;
}
return _AddBar(pBar, &rbBand);
}
BOOL CChevBar::AddBar(CWnd* pBar, COLORREF clrFore, COLORREF clrBack, LPCTSTR pszText, DWORD dwStyle)
{
REBARBANDINFO rbBand;
rbBand.fMask = RBBIM_STYLE | RBBIM_COLORS;
rbBand.fStyle = dwStyle;
rbBand.clrFore = clrFore;
rbBand.clrBack = clrBack;
if (pszText != NULL)
{
rbBand.fMask |= RBBIM_TEXT;
rbBand.lpText = const_cast<LPTSTR>(pszText);
}
return _AddBar(pBar, &rbBand);
}
CSize CChevBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
ASSERT_VALID(this);
ASSERT(::IsWindow(m_hWnd));
// the union of the band rectangles is the total bounding rect
int nCount = DefWindowProc(RB_GETBANDCOUNT, 0, 0);
REBARBANDINFO rbBand;
rbBand.cbSize = sizeof(rbBand);
int nTemp;
try{
// sync up hidden state of the bands
for (nTemp = nCount; nTemp--; )
{
rbBand.fMask = RBBIM_CHILD|RBBIM_STYLE;
VERIFY(DefWindowProc(RB_GETBANDINFO, nTemp, (LPARAM)&rbBand));
CControlBar* pBar = DYNAMIC_DOWNCAST(CControlBar, CWnd::FromHandlePermanent(rbBand.hwndChild));
BOOL bWindowVisible;
if (pBar != NULL)
bWindowVisible = pBar->IsVisible();
else
bWindowVisible = (::GetWindowLong(rbBand.hwndChild, GWL_STYLE) & WS_VISIBLE) != 0;
BOOL bBandVisible = (rbBand.fStyle & RBBS_HIDDEN) == 0;
if (bWindowVisible != bBandVisible)
VERIFY(DefWindowProc(RB_SHOWBAND, nTemp, bWindowVisible));
}
}catch(...)
{
}
// determine bounding rect of all visible bands
CRect rectBound; rectBound.SetRectEmpty();
try{
for (nTemp = nCount; nTemp--; )
{
rbBand.fMask = RBBIM_STYLE;
VERIFY(DefWindowProc(RB_GETBANDINFO, nTemp, (LPARAM)&rbBand));
if ((rbBand.fStyle & RBBS_HIDDEN) == 0)
{
CRect rect;
VERIFY(DefWindowProc(RB_GETRECT, nTemp, (LPARAM)&rect));
rectBound |= rect;
}
}
// add borders as part of bounding rect
if (!rectBound.IsRectEmpty())
{
CRect rect; rect.SetRectEmpty();
CalcInsideRect(rect, bHorz);
rectBound.right -= rect.Width();
if(m_dwStyle & CBRS_ALIGN_TOP)
rectBound.bottom -= rect.Height()-2;
else
rectBound.bottom -= rect.Height();
}
}
catch(...)
{
}
return CSize((bHorz && bStretch) ? 32767 : rectBound.Width(),
(!bHorz && bStretch) ? 32767 : rectBound.Height());
}
CSize CChevBar::CalcDynamicLayout(int /*nLength*/, DWORD dwMode)
{
if (dwMode & LM_HORZDOCK)
ASSERT(dwMode & LM_HORZ);
return CalcFixedLayout(dwMode & LM_STRETCH, dwMode & LM_HORZ);
}
BOOL CChevBar::Create(CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle, UINT nID)
{
ASSERT_VALID(pParentWnd); // must have a parent
ASSERT (!((dwStyle & CBRS_SIZE_FIXED) && (dwStyle & CBRS_SIZE_DYNAMIC)));
// save the style
m_dwStyle = (dwStyle & CBRS_ALL);
if (nID == AFX_IDW_REBAR)
m_dwStyle |= CBRS_HIDE_INPLACE;
dwStyle &= ~CBRS_ALL;
dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE|RBS_VARHEIGHT;
dwStyle |= dwCtrlStyle;
// initialize common controls
VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTL_COOL_REG));
// _AfxGetComCtlVersion();
// ASSERT(_afxComCtlVersion != -1);
// create the HWND
CRect rect; rect.SetRectEmpty();
if (!CWnd::Create(REBARCLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
return FALSE;
// Note: Parent must resize itself for control bar to be resized
return TRUE;
}
void CChevBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHandler)
{
//UpdateDialogControls(pTarget, bDisableIfNoHandler);
}
BOOL CChevBar::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
{
if (!CControlBar::OnNcCreate(lpCreateStruct))
return FALSE;
// if the owner was set before the rebar was created, set it now
if (m_hWndOwner != NULL)
DefWindowProc(RB_SETPARENT, (WPARAM)m_hWndOwner, 0);
return TRUE;
}
BOOL CChevBar::OnEraseBkgnd(CDC*)
{
return (BOOL)Default();
}
void CChevBar::OnNcCalcSize(BOOL /*bCalcValidRects*/, NCCALCSIZE_PARAMS* lpncsp)
{
// calculate border space (will add to top/bottom, subtract from right/bottom)
CRect rect; rect.SetRectEmpty();
BOOL bHorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
CControlBar::CalcInsideRect(rect, bHorz);
// adjust non-client area for border space
lpncsp->rgrc[0].left += rect.left;
lpncsp->rgrc[0].top += rect.top;
lpncsp->rgrc[0].right += rect.right;
lpncsp->rgrc[0].bottom += rect.bottom;
if(m_dwStyle & CBRS_ALIGN_TOP)
{
lpncsp->rgrc[0].left += 2;
lpncsp->rgrc[0].right += -2;
}
}
void CChevBar::OnNcPaint()
{
EraseNonClient();
Default();
}
void CChevBar::OnPaint()
{
Default();
}
/*int CChevBar::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
ASSERT_VALID(this);
ASSERT(::IsWindow(m_hWnd));
HWND hWndChild = _AfxChildWindowFromPoint(m_hWnd, point);
CWnd* pWnd = CWnd::FromHandlePermanent(hWndChild);
if (pWnd == NULL)
return CControlBar::OnToolHitTest(point, pTI);
ASSERT(pWnd->m_hWnd == hWndChild);
return pWnd->OnToolHitTest(point, pTI);
}*/
LRESULT CChevBar::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// special handling for certain messages (forwarding to owner/parent)
switch (message)
{
case WM_POPMESSAGESTRING:
case WM_SETMESSAGESTRING:
return GetOwner()->SendMessage(message, wParam, lParam);
}
return CControlBar::WindowProc(message, wParam, lParam);
}
IMPLEMENT_DYNAMIC(CChevBar, CControlBar)
HMENU CChevBar::CopyMenu(HMENU hMenu)
{
int iCount;
HMENU hCopyMenu = CreatePopupMenu();
MENUITEMINFO info;
char lpNewItem[200];
UINT state;
for(iCount=0; iCount<GetMenuItemCount(hMenu);iCount++)
{
info.cbSize = sizeof (MENUITEMINFO); // must fill up this field
info.fMask = MIIM_DATA|MIIM_TYPE|MIIM_ID|MIIM_SUBMENU; // get the state of the menu item
info.dwTypeData = NULL;
info.cch = 0;
GetMenuItemInfo(hMenu, iCount ,TRUE , &info);
GetMenuString(hMenu, iCount, lpNewItem, 200, MF_BYPOSITION);
state = GetMenuState( hMenu, iCount, MF_BYPOSITION);
if(IsMenu(info.hSubMenu))
{
HMENU hNewMenu = CopyMenu(info.hSubMenu);
if(info.fType & MFT_OWNERDRAW)
{
CMenuData * md1;
md1 = new CMenuData;
md1->m_nBitmap = ((CMenuData*)info.dwItemData)->m_nBitmap;
md1->m_pImgList = ((CMenuData*)info.dwItemData)->m_pImgList;
md1->m_bIni = ((CMenuData*)info.dwItemData)->m_bIni;
// md1->nFlags = ((CMenuData*)info.dwItemData)->nFlags;
// md1->nID = ((CMenuData*)info.dwItemData)->nID;
SetMenuText (((CMenuData*)info.dwItemData)->m_szMenuText, md1);
//SetURL (((CMenuData*)info.dwItemData)->m_szURL, md1);
AppendMenu(hCopyMenu, MF_POPUP|MF_ENABLED|MF_OWNERDRAW|info.fType, (UINT)hNewMenu, (char*)md1);
}
else
AppendMenu(hCopyMenu, MF_POPUP|MF_ENABLED, (UINT)hNewMenu, lpNewItem);
}
else if(info.fType & MFT_SEPARATOR)
AppendMenu(hCopyMenu, MF_ENABLED|MF_SEPARATOR, 0,NULL);
//else if(mii.fType & MFT_MENUBARBREAK)
// AppendMenu(hCopyMenu, MF_ENABLED|MF_MENUBARBREAK|MF_STRING, GetMenuItemID(hMenu, iCount),lpNewItem);
else if(info.fType & MFT_OWNERDRAW)
{
CMenuData * md1;
md1 = new CMenuData;
md1->m_nBitmap = ((CMenuData*)info.dwItemData)->m_nBitmap;
md1->m_pImgList = ((CMenuData*)info.dwItemData)->m_pImgList;
md1->m_bIni = ((CMenuData*)info.dwItemData)->m_bIni;
// md1->nFlags = ((CMenuData*)info.dwItemData)->nFlags;
// md1->nID = ((CMenuData*)info.dwItemData)->nID;
SetMenuText (((CMenuData*)info.dwItemData)->m_szMenuText, md1);
SetURL (((CMenuData*)info.dwItemData)->m_szURL, md1);
// SetMenuFolder (((CMenuData*)info.dwItemData)->m_szMenuFolder, md1);
AppendMenu(hCopyMenu, MF_OWNERDRAW|info.fType, GetMenuItemID(hMenu, iCount),(char*)md1);
}
else
{ int id = GetMenuItemID(hMenu, iCount);
AppendMenu(hCopyMenu, MF_STRING|info.fType, id,lpNewItem);
CheckMenuItem(hCopyMenu, id, state|MF_BYCOMMAND);
EnableMenuItem(hCopyMenu, id, state);
}
}
return hCopyMenu;
}
void CChevBar::OnParentNotify(UINT message, LPARAM lParam)
{
CControlBar::OnParentNotify(message, lParam);
// TODO: Add your message handler code here
if(message == WM_RBUTTONDOWN)
{
CPoint point;
GetCursorPos(&point);
CWnd * pw = WindowFromPoint(point);
if(pw!=NULL && (pw->m_hWnd == ((CMainFrame*)pMainFrame)->m_wndTab.GetSafeHwnd()
|| ::IsChild(((CMainFrame*)pMainFrame)->m_wndAddress->GetSafeHwnd(), pw->m_hWnd)))
return;
CMenu *pmenu;
pmenu = ((CMainFrame*)pMainFrame)->GetMenu()->GetSubMenu(2)->GetSubMenu(0);
if(pmenu!=NULL)
pmenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON , point.x, point.y, AfxGetMainWnd());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -