📄 menubar.cpp.svn-base
字号:
m_arrItem[i]->Update(pDC);
}
// delay draw captions
if (m_pMenuControl) {
if (IsFloating()) {
m_pMenuControl->DelayLayoutAndDraw(pDC, rect.Size(),bFlota);
}
else {
if (m_dwStyle & CBRS_ORIENT_HORZ)
m_pMenuControl->DelayLayoutAndDraw(pDC, CSize(GetClipBoxLength(TRUE), rect.Height()),bFlota);
else
m_pMenuControl->DelayLayoutAndDraw(pDC, CSize(rect.Width(), GetClipBoxLength(FALSE)),bFlota);
}
}
}
//******************************************************************
void CMenuBar::UpdateBar(TrackingState nState, int nNewIndex)
{
if (m_nTrackingState == buttonmouse)
m_bIgnoreAlt = FALSE;// if prev state is BUTTONMOUSE, always should be FALSE!
m_nTrackingState = nState;
// clean up
if (IsValidIndex(m_nCurIndex)) {
CMenuItem* pItem = m_arrItem[m_nCurIndex];
CClientDC dc(this);
pItem->ModifyState(MISTATE_HOT | MISTATE_PRESSED, 0);
pItem->Update(&dc);
// draw captions forcefully
if (m_pMenuControl) {
CRect rcCross = m_pMenuControl->GetRect() & m_arrItem[m_nCurIndex]->GetRect();
if (!rcCross.IsRectEmpty()) {
m_pMenuControl->DrawControl();
if (m_bCombo==TRUE)
DrawCombo();
}
}
m_nCurIndex = -1;
}
if (nState != none) {
ASSERT(IsValidIndex(nNewIndex));
m_nCurIndex = nNewIndex;
CMenuItem* pItem = m_arrItem[m_nCurIndex];
CClientDC dc(this);
if (nState == button || nState == buttonmouse) {
pItem->ModifyState(MISTATE_PRESSED, MISTATE_HOT);
pItem->Update(&dc);
}
else if (nState == popup) {
pItem->ModifyState(MISTATE_HOT, MISTATE_PRESSED);
pItem->Update(&dc);
}
// draw captions forcefully
if (m_pMenuControl) {
CRect rcCross = m_pMenuControl->GetRect() & m_arrItem[m_nCurIndex]->GetRect();
if (!rcCross.IsRectEmpty()) {
m_pMenuControl->DrawControl();
if (m_bCombo==TRUE)
DrawCombo();
}
}
}
else {
// must be default parameter
ASSERT(nNewIndex == -1);
}
m_bProcessRightArrow = m_bProcessLeftArrow = TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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)
{
SetCursor (LoadCursor(NULL, IDC_SIZEALL));
int nIndex = HitTestOnTrack(point);
if (IsValidIndex(nIndex) && (m_arrItem[nIndex]->GetStyle() & MISTYLE_TRACKABLE)) {
TrackPopup(nIndex);
return; // eat it!
}
CControlBar::OnLButtonDown(nFlags, point);
}
//******************************************************************
void CMenuBar::OnMouseMove(UINT nFlags, CPoint point)
{
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)
{
CControlBar::OnLButtonUp(nFlags, point);
}
//******************************************************************
void CMenuBar::CheckActiveChildWndMaximized()
{
if (!bIsTabbed)
{
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;
if (!bIsTabbed)
{
m_pMenuControl->OnActivateChildWnd();
m_pMenuIcon->OnActivateChildWnd();
if(!m_bMDIMaximized )
{
m_pMenuControl->m_arrButton[0].ShowWindow(SW_HIDE);
m_pMenuControl->m_arrButton[1].ShowWindow(SW_HIDE);
m_pMenuControl->m_arrButton[2].ShowWindow(SW_HIDE);
}
else
{
m_pMenuControl->m_arrButton[0].ShowWindow(SW_SHOW);
m_pMenuControl->m_arrButton[1].ShowWindow(SW_SHOW);
m_pMenuControl->m_arrButton[2].ShowWindow(SW_SHOW);
}
}
RefreshBar();
}
}
//******************************************************************
void CMenuBar::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
LTRACE(_T("CMenuBar::OnInitMenuPopup\n"));
CMenu menu;
menu.Attach((HMENU)m_hWindowMenu);
// scan for first window command
int n = menu.GetMenuItemCount();
BOOL bAddSeperator = TRUE;
int iPos;
for (iPos=0; iPos<n; iPos++) {
if (menu.GetMenuItemID(iPos) >= AFX_IDM_FIRST_MDICHILD) {
bAddSeperator = FALSE;
break;
}
}
while (iPos < (int)menu.GetMenuItemCount())
menu.RemoveMenu(iPos, MF_BYPOSITION);
// 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)
{
menu.InsertMenu(iPos++, MF_BYPOSITION | MF_SEPARATOR);
bAddSeperator = FALSE;
}
if (iWin < 10)
{
CWnd::FromHandle(hwnd)->GetWindowText(sWinName);
sMenuItem.Format(_T("&%d %s"), iWin, (LPCTSTR)sWinName);
menu.InsertMenu(iPos, MF_BYPOSITION, nID, sMenuItem);
if (hwnd == hwndActive)
menu.CheckMenuItem(iPos, MF_BYPOSITION | MF_CHECKED);
}
else
{
menu.InsertMenu(iPos, MF_BYPOSITION, nID, _szMoreWindows);
}
iPos++;
}
menu.Detach();
}
//******************************************************************
void CMenuBar::OnSetMenu(HMENU hNewMenu, HMENU hWindowMenu)
{
if (!bIsTabbed)
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
if (!bIsTabbed)
m_pMenuIcon->OnActivateChildWnd();
}
if (!m_hMenu || m_hMenu != hNewMenu) { // menu changed
LTRACE(_T("CMenuBar::OnSetMenu---menu changed\n"));
LoadMenu(hNewMenu, hWindowMenu); // set menubar menu
GetOwner()->SetMenu(NULL); // clear frame menu
}
}
//******************************************************************
BOOL CMenuBar::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
CPoint ptCurPos;
CRect rc;GetClientRect(rc);
GetCursorPos (&ptCurPos);
ScreenToClient (&ptCurPos);
if (m_dwStyle & CBRS_ORIENT_HORZ)
{
rc.right=rc.left+2;
if (ptCurPos.x< 0)
{
SetCursor (LoadCursor(NULL, IDC_SIZEALL));
return TRUE;
}
}
else
{
rc.bottom=rc.top+2;
if (ptCurPos.y< 0)
{
SetCursor (LoadCursor(NULL, IDC_SIZEALL));
return TRUE;
}
}
return CControlBar::OnSetCursor(pWnd, nHitTest, message);
}
//******************************************************************
void CMenuBar::OnNcPaint()
{
EraseNonClientEx();
}
//******************************************************************
LRESULT 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);
if (!(m_dwStyle & CBRS_FLOATING))
{
// adjust non-client area for border space
lpncsp->rgrc[0].left += bHorz?rect.left+4:rect.left+1;
lpncsp->rgrc[0].top += bHorz?rect.top+4:rect.top+2;
}
else
{
lpncsp->rgrc[0].left += rect.left;
lpncsp->rgrc[0].top += rect.top+4;
}
lpncsp->rgrc[0].right += rect.right;
lpncsp->rgrc[0].bottom += rect.bottom;
}
//******************************************************************
void CMenuBar::OnDestroy()
{
if(m_bCombo==TRUE)
cmb->SaveHistory(szNameHistory,bSaveHistory);
CControlBar::OnDestroy();
// TODO:
DeleteItems();
if (m_nIDEvent)
KillTimer(m_nIDEvent);
// for SDI application
if (m_bMDIApp == FALSE && m_hMenu != NULL)
::FreeResource(m_hMenu);
}
//******************************************************************
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(HMENU hMenu, UINT nIndex)
{
// LTRACE(_T("CMenuBar::OnMenuSelect\n"));
if (m_nTrackingState == popup) {
m_bProcessRightArrow = (::GetSubMenu(hMenu, nIndex) == NULL);
HMENU hSubMenu = ::GetSubMenu(hMenu, m_nCurIndex);
if (hSubMenu == NULL)
return;
m_bProcessLeftArrow = (hMenu == hSubMenu);
}
}
//******************************************************************
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;
m_bLoop = TRUE;
while (m_bLoop == TRUE) {
UpdateWindow(); // force to repaint when button hidden by other window
UpdateBar(popup, m_nCurIndex);
// 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;
m_arrItem[m_nCurIndex]->TrackPopup(this, GetTopLevelParent());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -