preferencesshortcutspage.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 599 行 · 第 1/2 页

CPP
599 行
字号
	// validate modifiers but only if a 'main' key has been pressed
	if (wVKeyCode)
	{
		WORD wValidModifiers = m_pShortcutMgr->ValidateModifiers(wModifiers, wVKeyCode);

		if (wValidModifiers != wModifiers)
		{
			wModifiers = wValidModifiers;
			m_hkNew.SetHotKey(wVKeyCode, wModifiers);
		}
	}

	DWORD dwShortcut = MAKELONG(wVKeyCode, wModifiers);

	// if anyone has this shortcut we show who it is
	BOOL bReserved = FALSE;
	HTREEITEM htiOther = NULL;

	m_mapShortcut2HTI.Lookup(dwShortcut, htiOther);

	if (htiOther && m_tcCommands.GetItemData(htiOther) != nCmdID)
		m_sOtherCmdID.Format(IDS_PSP_CURRENTLYASSIGNED, m_tcCommands.GetItemText(htiOther));

	else if (CToDoCtrl::IsReservedShortcut(dwShortcut))
	{
		m_sOtherCmdID.LoadString(IDS_PSP_RESERVED);
		bReserved = TRUE;
	}
	else
		m_sOtherCmdID.Empty();

	GetDlgItem(IDC_ASSIGNSHORTCUT)->EnableWindow(!bReserved);
	UpdateData(FALSE);
}

LRESULT CPreferencesShortcutsPage::OnGutterDrawItem(WPARAM /*wParam*/, LPARAM lParam)
{
	NCGDRAWITEM* pNCGDI = (NCGDRAWITEM*)lParam;

	if (pNCGDI->nColID == PSP_SHORTCUTCOLUMNID)
	{
		CRect rItem(pNCGDI->rItem);
		HTREEITEM hti = (HTREEITEM)pNCGDI->dwItem;

		if (m_tcCommands.ItemHasChildren(hti))
		{
			pNCGDI->pDC->FillSolidRect(rItem, ::GetSysColor(COLOR_3DHIGHLIGHT));
			pNCGDI->pDC->FillSolidRect(rItem.right - 1, rItem.top, 1, rItem.Height(), OTC_GRIDCOLOR);
		}
		else
		{
			BOOL bSelected = pNCGDI->bSelected;
			BOOL bFocused = CTreeCtrlHelper(m_tcCommands).HasFocus();
			COLORREF crTextColor = GetSysColor((bSelected && bFocused) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);

			// bkcolor
			if (bSelected)
			{
				if (bFocused)
					pNCGDI->pDC->FillSolidRect(rItem, GetSysColor(COLOR_HIGHLIGHT));
				else
					pNCGDI->pDC->FillSolidRect(rItem, GetSysColor(COLOR_3DFACE));
			}
			
			UINT nCmdID = m_tcCommands.GetItemData(hti);
			DWORD dwShortcut = 0;
		
			m_mapID2Shortcut.Lookup(nCmdID, dwShortcut);

			if (dwShortcut)
			{
				rItem.left += 3;

				// test for reserved shortcut and mark in red
				if (CToDoCtrl::IsReservedShortcut(dwShortcut))
					pNCGDI->pDC->SetTextColor(255);
				else
					pNCGDI->pDC->SetTextColor(crTextColor);

				pNCGDI->pDC->SetBkMode(TRANSPARENT);

				CString sText = m_pShortcutMgr->GetShortcutText(dwShortcut);

				if (!sText.IsEmpty())
					pNCGDI->pDC->DrawText(sText, rItem, DT_SINGLELINE | DT_VCENTER | DT_LEFT);

				rItem.left -= 3;
			}
			
			// vertical divider
			if (!bSelected)
				pNCGDI->pDC->FillSolidRect(rItem.right - 1, rItem.top, 1, rItem.Height(), OTC_GRIDCOLOR);
		}

		rItem.top = rItem.bottom - 1;
		pNCGDI->pDC->FillSolidRect(rItem, OTC_GRIDCOLOR);
	
		return TRUE; // we handled it
	}

	return FALSE;
}

LRESULT CPreferencesShortcutsPage::OnGutterRecalcColWidth(WPARAM /*wParam*/, LPARAM lParam)
{
	NCGRECALCCOLUMN* pNCRC = (NCGRECALCCOLUMN*)lParam;

	if (pNCRC->nColID == PSP_SHORTCUTCOLUMNID)
	{
		int nLongest = 0;
		HTREEITEM hti = m_tcCommands.GetNextItem(TVI_ROOT, TVGN_CHILD);

		while (hti)
		{
			int nWidth = GetLongestShortcutText(hti, pNCRC->pDC);
			nLongest = max(nLongest, nWidth);

			hti = m_tcCommands.GetNextItem(hti, TVGN_NEXT);
		}

		if (nLongest)
			nLongest += 6; // some padding

		pNCRC->nWidth = max(40, nLongest);

		return TRUE; // we handled it
	}

	return FALSE;
}

int CPreferencesShortcutsPage::GetLongestShortcutText(HTREEITEM hti, CDC* pDC)
{
	int nLongest = 0;
	int nCmdID = m_tcCommands.GetItemData(hti);

	if (nCmdID)
	{
		DWORD dwShortcut = 0;
		m_mapID2Shortcut.Lookup(nCmdID, dwShortcut);

		if (dwShortcut)
		{
			CString sShortcut = m_pShortcutMgr->GetShortcutText(dwShortcut);
			nLongest = sShortcut.IsEmpty() ? 0 : pDC->GetTextExtent(sShortcut).cx;
		}
	}

	HTREEITEM htiChild = m_tcCommands.GetChildItem(hti);

	while (htiChild)
	{
		int nWidth = GetLongestShortcutText(htiChild, pDC);
		nLongest = max(nLongest, nWidth);

		htiChild = m_tcCommands.GetNextItem(htiChild, TVGN_NEXT);
	}

	return nLongest;
}

void CPreferencesShortcutsPage::OnTreeCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMCUSTOMDRAW* pNMCD = (NMCUSTOMDRAW*)pNMHDR;

	if (pNMCD->dwDrawStage == CDDS_PREPAINT)
		*pResult |= CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT;	
		
	else if (pNMCD->dwDrawStage == CDDS_ITEMPREPAINT)
	{
		NMTVCUSTOMDRAW* pTVCD = (NMTVCUSTOMDRAW*)pNMCD;

		// set colors
		HTREEITEM hti = (HTREEITEM)pTVCD->nmcd.dwItemSpec;

		if (m_tcCommands.ItemHasChildren(hti))
		{
			CRect rItem;
			m_tcCommands.GetItemRect(hti, rItem, FALSE);

			CDC* pDC = CDC::FromHandle(pNMCD->hdc);
			pDC->FillSolidRect(rItem, GetSysColor(COLOR_3DHIGHLIGHT));
			pDC->SetBkMode(TRANSPARENT);

			if (pNMCD->uItemState & CDIS_SELECTED)
			{
				pTVCD->clrTextBk = GetSysColor(COLOR_3DSHADOW);
				pTVCD->clrText = 0;//GetSysColor(COLOR_3DDKSHADOW);
			}
			else
			{
				pTVCD->clrText = GetSysColor(COLOR_3DSHADOW);
			}

			*pResult |= CDRF_DODEFAULT;
		}
		else // test for reserved shortcut
		{
			DWORD dwShortcut = 0;
			m_mapID2Shortcut.Lookup(pTVCD->nmcd.lItemlParam, dwShortcut);

			if (CToDoCtrl::IsReservedShortcut(dwShortcut))
			{
				pTVCD->clrText = 255;
				*pResult |= CDRF_DODEFAULT;
			}
		}
	}
	else if (pNMCD->dwDrawStage == CDDS_ITEMPOSTPAINT)
	{
		CDC* pDC = CDC::FromHandle(pNMCD->hdc);
		NMTVCUSTOMDRAW* pTVCD = (NMTVCUSTOMDRAW*)pNMCD;

		CRect rItem(pNMCD->rc);
		rItem.top = rItem.bottom - 1;

		// if the bottom of the text coincides with the bottom of the 
		// item and we have the then take care not to draw over the focus rect
		if (pNMCD->uItemState & CDIS_FOCUS)
		{
			HTREEITEM hti = (HTREEITEM)pTVCD->nmcd.dwItemSpec;

			CRect rText;
			m_tcCommands.GetItemRect(hti, rText, TRUE);

			if (rText.bottom == rItem.bottom)
			{
				pDC->FillSolidRect(rItem.left, rItem.bottom - 1, rText.left - rItem.left, 1, OTC_GRIDCOLOR);
				pDC->FillSolidRect(rText.right, rItem.bottom - 1, rItem.right - rText.right, 1, OTC_GRIDCOLOR);
			}
			else
				pDC->FillSolidRect(rItem, OTC_GRIDCOLOR);
		}
		else
			pDC->FillSolidRect(rItem, OTC_GRIDCOLOR);

		*pResult |= CDRF_DODEFAULT;
	}
}


BOOL CPreferencesShortcutsPage::PreTranslateMessage(MSG* pMsg) 
{
	// special handling for hotkeys
	if (CWinClasses::IsClass(pMsg->hwnd, WC_HOTKEY))
		return FALSE;

	// handle delete key when tree has the focus
	switch (pMsg->message)
	{
	case WM_KEYDOWN:
		switch (pMsg->wParam)
		{
		case VK_DELETE:
			{
				if (GetFocus() == &m_tcCommands)
				{
					HTREEITEM htiSel = m_tcCommands.GetSelectedItem();

					if (htiSel)
					{
						UINT nCmdID = m_tcCommands.GetItemData(htiSel);

						if (nCmdID)
						{
							DWORD dwShortcut = 0;

							if (m_mapID2Shortcut.Lookup(nCmdID, dwShortcut))
								m_mapShortcut2HTI.RemoveKey(dwShortcut);

							m_mapID2Shortcut[nCmdID] = 0;
						}
					}

					m_hkCur.SetHotKey(0, 0);
					m_hkNew.SetHotKey(0, 0);
					m_sOtherCmdID.Empty();

					m_tcCommands.RecalcGutter();
					m_tcCommands.RedrawGutter();
				}
			}
			break;
		}
		break;
	}
	
	return CPreferencesPageBase::PreTranslateMessage(pMsg);
}

void CPreferencesShortcutsPage::LoadPreferences(const CPreferencesStorage& /*prefs*/)
{
}

void CPreferencesShortcutsPage::SavePreferences(CPreferencesStorage& /*prefs*/)
{

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?