📄 menubar.cpp
字号:
m_pMenuControl->ForceDrawControl(&dc);
}
}
}
else {
// must be default parameter
ASSERT(nNewIndex == -1);
}
if (m_arrDepth<=2)
m_bProcessRightArrow = m_bProcessLeftArrow = TRUE;
else
m_bProcessRightArrow = m_bProcessLeftArrow = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CMenuBar message handler
int CMenuBar::HitTestOnTrack(CPoint point)
{
for (int i = 0; i < GetItemCount(); ++i) {
CMenuItem* pItem = m_arrItem[i];
CRect rcItem = pItem->GetRect();
if ((pItem->GetStyle() & MISTYLE_TRACKABLE) &&
rcItem.PtInRect(point))
return i;
}
return -1;
}
void CMenuBar::OnLButtonDown(UINT nFlags, CPoint point)
{
// LTRACE("CMenuBar::OnLButtonDown\n");
if (m_pMenuControl && m_pMenuControl->OnMouseMsg(WM_LBUTTONDOWN, nFlags, point)) {
return; // eat it!
}
int nIndex = HitTestOnTrack(point);
if (IsValidIndex(nIndex) && (m_arrItem[nIndex]->GetStyle() & MISTYLE_TRACKABLE))
{
RefreshMenuPainting(m_pMenu,nIndex);
TrackPopup(nIndex);
return; // eat it!
}
CControlBar::OnLButtonDown(nFlags, point);
}
void CMenuBar::OnMouseMove(UINT nFlags, CPoint point)
{
// LTRACE(_T("CMenuBar::OnMouseMove\n"));
if (m_pMenuControl && m_pMenuControl->OnMouseMsg(WM_MOUSEMOVE, nFlags, point)) {
CControlBar::OnMouseMove(nFlags, point);
return;
}
int nIndex = HitTestOnTrack(point);
if (IsValidIndex(nIndex)) {
if (m_nCurIndex == -1 || m_nCurIndex != nIndex) {// if other button
UpdateBar(buttonmouse, nIndex);// button made hot with mouse
if (!m_nIDEvent)
m_nIDEvent = SetTimer(1, 100, NULL);
}
}
else {
UpdateBar();
}
CControlBar::OnMouseMove(nFlags, point);
}
LRESULT CMenuBar::OnSetMenuNull(WPARAM wParam, LPARAM)
{
HWND hWnd = (HWND)wParam;
ASSERT(::IsWindow(hWnd));
::SetMenu(hWnd, NULL);
return 0;
}
LRESULT CMenuBar::OnSettingChange(WPARAM wParam, LPARAM lParam)
{
// reinitialize fonts etc
if (!_InitCommonResources(TRUE)) {
TRACE(_T("Failed to create bar resource\n"));
return FALSE;
}
VERIFY(InitItems());
CFrameWnd* pFrame = GetParentFrame();
ASSERT_VALID(pFrame);
pFrame->RecalcLayout();
return 0;
}
void CMenuBar::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO:
// LTRACE(_T("CMenuBar::OnLButtonUp\n"));
if (m_pMenuControl && m_pMenuControl->OnMouseMsg(WM_LBUTTONUP, nFlags, point)) {
CControlBar::OnLButtonUp(nFlags, point);
return;
}
CControlBar::OnLButtonUp(nFlags, point);
}
void CMenuBar::CheckActiveChildWndMaximized()
{
//LTRACE(_T("CMenuBar::CheckActiveChildWndMaximized\n"));
ASSERT(m_pMenuControl);
ASSERT(m_pMenuIcon);
BOOL bMaximized = FALSE;
GetActiveChildWnd(bMaximized);
if (m_bMDIMaximized != bMaximized) {
LTRACE(_T("CMenuBar::CheckActiveChildWndMaximized---state changed, refreshing\n"));
m_bMDIMaximized = bMaximized;
m_pMenuControl->OnActivateChildWnd();
m_pMenuIcon->OnActivateChildWnd();
RefreshBar();
}
}
void CMenuBar::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
LTRACE(_T("CMenuBar::OnInitMenuPopup\n"));
CMenuEx &menu=*m_pWindowMenu;
// scan for first window command
int n = menu.GetMenuItemCount();
BOOL bAddSeperator = TRUE;
for (int iPos=0; iPos<n; iPos++) {
UINT Id=menu.GetMenuItemID(iPos);
if (Id >= AFX_IDM_FIRST_MDICHILD)
{
bAddSeperator = FALSE;
break;
}
}
// iPos is either first window item, or end if none found.
// delete everything after.
while (iPos < (int)n)
{
menu.DeleteMenu(iPos, MF_BYPOSITION);
n--;
}
// get active window so I can check its menu item
ASSERT(m_hWndMDIClient);
HWND hwndActive = (HWND)::SendMessage(m_hWndMDIClient,
WM_MDIGETACTIVE, 0, NULL);
// append window names in the form "# title"
// *****fixed by VORGA, thanks!*****
int iWin;
int nID = AFX_IDM_FIRST_MDICHILD;
CString sWinName, sMenuItem;
HWND hwnd;
for (iWin = 1; iWin <= 10; iWin++, nID++)
{
hwnd = ::GetDlgItem(m_hWndMDIClient, nID);
if (hwnd == NULL)
break;
if (bAddSeperator)
{
iPos++;
menu.AppendMenu(MF_SEPARATOR,0,NULL,0x8000);
bAddSeperator = FALSE;
}
else
{
for (int h=iPos-2;h>=0;h--)
{
if (menu.GetMenuItemID(h)!=ID_SEPARATOR &&
(menu.GetFlagsEx(h)&MF_DRAWGRAYED)==0)
{
menu.SetFlagsEx(iPos-1,menu.GetFlagsEx(iPos-1)&(~MF_DRAWGRAYED));
break;
}
else if (menu.GetMenuItemID(h)==ID_SEPARATOR &&
(menu.GetFlagsEx(h)&MF_DRAWGRAYED)==0)
{
break;
}
}
}
if (iWin < 10)
{
CWnd::FromHandle(hwnd)->GetWindowText(sWinName);
sMenuItem.Format(_T("&%d %s"), iWin, (LPCTSTR)sWinName);
menu.AppendMenu(MF_STRING, nID, sMenuItem);
if (hwnd == hwndActive)
menu.CheckMenuItem(iPos, MF_BYPOSITION | MF_CHECKED);
}
else
{
menu.AppendMenu(MF_STRING, nID, sMenuItem);
}
iPos++;
}
RefreshMenuPainting(m_pMenu);
}
void CMenuBar::OnSetMenu(CMenuEx *pNewMenu, CMenuEx* pWindowMenu)
{
if(!m_bEnableChange) return;
//if(!m_pMenuIcon && !m_pMenuControl) return;
ASSERT(m_pMenuIcon && m_pMenuControl);
// We can get active MDI child window on this message!
BOOL bMax = FALSE;
HWND hWndChild = GetActiveChildWnd(bMax);
if (!m_hWndActiveChild || m_hWndActiveChild != hWndChild) {// an active child window changed
LTRACE(_T("CMenuBar::OnSetMenu---an active child window changed\n"));
m_hWndActiveChild = hWndChild;
// tell MenuIcon child window has been changed
m_pMenuIcon->OnActivateChildWnd();
}
if (!m_pMenu || m_pMenu != pNewMenu) { // menu changed
LTRACE(_T("CMenuBar::OnSetMenu---menu changed\n"));
LoadMenu(pNewMenu, pWindowMenu); // set menubar menu
GetOwner()->SetMenu(NULL); // clear frame menu
}
}
void CMenuBar::OnNcPaint()
{
EraseNonClientEx();
}
UINT CMenuBar::OnNcHitTest(CPoint point)
{
// make nonclient area clickable
return HTCLIENT;
}
void CMenuBar::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* 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;
_CalcInsideRect(rect, bHorz);
// ASSERT(_afxComCtlVersion != -1);
// ASSERT(_afxComCtlVersion >= VERSION_IE4 || rect.top >= 2);
// adjust non-client area for border space
lpncsp->rgrc[0].left += rect.left;
lpncsp->rgrc[0].top += rect.top;
// previous versions of COMCTL32.DLL had a built-in 2 pixel border
// if (_afxComCtlVersion < VERSION_IE4)
// lpncsp->rgrc[0].top -= 2;
lpncsp->rgrc[0].right += rect.right;
lpncsp->rgrc[0].bottom += rect.bottom;
}
void CMenuBar::OnDestroy()
{
CControlBar::OnDestroy();
// TODO:
DeleteItems();
if (m_nIDEvent)
KillTimer(m_nIDEvent);
if (m_hookFrame.m_bTimerSet)
{
m_pParent->KillTimer((UINT)&m_hookFrame);
m_hookFrame.m_bTimerSet=FALSE;
EnableGrayedDraw(FALSE);
}
// for SDI application
if (m_bMDIApp == FALSE && m_pMenu != NULL && m_bLoadedFromResource)
{
m_pMenu->DestroyMenu();
delete m_pMenu;
m_pMenu=NULL;
m_bLoadedFromResource=FALSE;
}
}
void CMenuBar::OnTimer(UINT nIDEvent)
{
if (m_nTrackingState == buttonmouse) {
CPoint pt; ::GetCursorPos(&pt);
CRect rect;
GetWindowRect(&rect);
if (!rect.PtInRect(pt)) {
UpdateBar();
if (m_nIDEvent) {
KillTimer(m_nIDEvent);
m_nIDEvent = NULL;
}
}
}
CControlBar::OnTimer(nIDEvent);
}
HWND CMenuBar::GetActiveChildWnd(BOOL& bMaximized)
{
if (!m_hWndMDIClient)
return NULL;
BOOL bMax = FALSE;
HWND hWnd = (HWND)::SendMessage(m_hWndMDIClient,
WM_MDIGETACTIVE, 0, (LPARAM)&bMax);
bMaximized = bMax;
return hWnd;
}
/////////////////////////////////////////////////////////////////////////////
// CMenuBar system hook
void CMenuBar::OnMenuSelect(CMenuEx* pMenu, UINT nIndex)
{
// LTRACE(_T("CMenuBar::OnMenuSelect\n"));
if (!pMenu) return;
if (m_nTrackingState == popup)
{
CMenuEx* pSubMenu = (CMenuEx*)pMenu->CMenu::GetSubMenu(nIndex);//m_nCurIndex);
int i;
BOOL m_bDec=FALSE;
int p=m_arrDepth;
for(;m_arrDepth>=0;m_arrDepth--,m_bDec=TRUE)
if(m_arrSel[m_arrDepth]==pMenu)
{
m_arrDepth++;
if(m_arrDepth>=m_arrSel.GetSize())
{
m_arrSel.SetSize(m_arrDepth+5);
m_arrNSel.SetSize(m_arrDepth+5);
}
m_arrSel[m_arrDepth]=pSubMenu;
if(!pSubMenu)
{
for(i=0;i<(signed)pMenu->CMenu::GetMenuItemCount();i++)
if (LOWORD(pMenu->CMenu::GetMenuItemID(i))==(signed)LOWORD(nIndex))
{
m_arrNSel[m_arrDepth]=i;
break;
}
}
else
m_arrNSel[m_arrDepth]=nIndex;
#ifdef DEBUG
CString str;
HMENU tmp;
tmp=(m_arrSel[m_arrDepth]!=NULL)?m_arrSel[m_arrDepth]->m_hMenu:0;
str.Format("Level: %d - hMenu: %x - Index: %d \n",m_arrDepth,tmp,m_arrNSel[m_arrDepth]);
TRACE (str);
#endif
break;
}
UINT fex;
if(m_arrDepth<=0)
{
m_arrDepth=0;
fex=0;
}
else
fex=m_arrSel[m_arrDepth-1]->GetFlagsEx(m_arrNSel[m_arrDepth]);
if(m_pMenu->CheckIfGrayedDraw())
{
#ifdef DEBUG
CString str;
str.Format("Depth=%d;LastArrow=%d\n",m_arrDepth,(fex&MF_LASTARROW)!=0);
TRACE (str);
#endif
if (fex&MF_LASTARROW)
{
PostMessage(WM_KEYDOWN,m_nDirectionKey,0);
}
}
else
{
#ifdef DEBUG
CString str;
str.Format("Depth=%d;Grayed=%d\n",m_arrDepth,(fex&MF_DRAWGRAYED)!=0);
TRACE (str);
#endif
if (fex&MF_DRAWGRAYED)
{
PostMessage(WM_KEYDOWN,m_nDirectionKey,0);
}
}
m_nDirectionKey=0;
//if (pSubMenu == NULL)
// return;
m_bProcessRightArrow = (m_arrDepth<=2) && (pMenu->CMenu::GetSubMenu(nIndex) == NULL);
m_bProcessLeftArrow = (m_arrDepth<=2) && (pMenu == m_pMenu->GetSubMenu(m_nCurIndex-1));//pSubMenu);
}
}
void CMenuBar::OnFrameNcActivate(BOOL bActive)
{
CFrameWnd* pFrame = GetTopLevelFrame();
ASSERT_VALID(pFrame);
if (pFrame->m_nFlags & WF_STAYACTIVE)
bActive = TRUE;
if (!pFrame->IsWindowEnabled())
bActive = FALSE;
if (bActive == m_bFrameActive)
return;
if (!bActive) {
for (int i = 0; i < GetItemCount(); ++i) {
m_arrItem[i]->ModifyState(0, MISTATE_INACTIVE);
}
}
else {
for (int i = 0; i < GetItemCount(); ++i) {
m_arrItem[i]->ModifyState(MISTATE_INACTIVE, 0);
}
}
m_bFrameActive = bActive;
// InvalidateRect(NULL); is better, but too late
// while clicking the application title bar (like IE5)
// so we have to redraw now!
CClientDC dc(this);
DoPaint(&dc);
}
LRESULT CALLBACK CMenuBar::MenuInputFilter(int code, WPARAM wParam, LPARAM lParam)
{
return (
code == MSGF_MENU &&
g_pMenuBar &&
g_pMenuBar->OnMenuInput( *((MSG*)lParam) )
) ? TRUE : CallNextHookEx(g_hMsgHook, code, wParam, lParam);
}
void CMenuBar::TrackPopup(int nIndex)
{
ASSERT_VALID(this);
m_nCurIndex = nIndex;
if(nIndex>=0) m_nLastIndex = nIndex;
m_bLoop = TRUE;
while (m_bLoop == TRUE) {
UpdateWindow(); // force to repaint when button hidden by other window
UpdateBar(popup, m_nCurIndex);
if(!g_pMenuBar)
{
// install hook
ASSERT(g_pMenuBar == NULL);
g_pMenuBar = this;
ASSERT(g_hMsgHook == NULL);
m_bLoop = FALSE;
g_hMsgHook = ::SetWindowsHookEx(WH_MSGFILTER,
MenuInputFilter, NULL, AfxGetApp()->m_nThreadID);// m_bLoop may become TRUE
}
// popup!!
m_nTrackingState = popup;
_nPrevIndexForCmd = m_nCurIndex;
RefreshMenuPainting(m_pMenu,m_nCurIndex);
m_arrItem[m_nCurIndex]->TrackPopup(this, m_pParent/*GetTopLevelParent()*/);
// uninstall hook
::UnhookWindowsHookEx(g_hMsgHook);
g_hMsgHook = NULL;
g_pMenuBar = NULL;
}
UpdateBar();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -