📄 bcgpkeymapdlg.cpp
字号:
{
m_KeymapList.SetFocus ();
CopyKeyMap ();
}
//*************************************************************************************
void CBCGPKeyMapDlg::OnPrint()
{
m_KeymapList.SetFocus ();
PrintKeyMap ();
}
//*************************************************************************************
void CBCGPKeyMapDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (m_KeymapList.GetSafeHwnd () == NULL)
{
return;
}
//---------------------------------------------------------------
// List of keys should cover the whole bottom part of the dialog:
//---------------------------------------------------------------
CRect rectList;
m_KeymapList.GetClientRect (rectList);
m_KeymapList.MapWindowPoints (this, &rectList);
CRect rectClient;
GetClientRect (rectClient);
rectList.right = rectClient.right;
rectList.bottom = rectClient.bottom;
m_KeymapList.SetWindowPos (NULL, -1, -1,
rectList.Width (), rectList.Height (),
SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
//--------------------------
// Adjust the columns width:
//--------------------------
SetColumnsWidth ();
}
//*************************************************************************************
BOOL CBCGPKeyMapDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR* pNMHDR = (NMHDR*) lParam;
if (pNMHDR != NULL && pNMHDR->code == HDN_ITEMCLICK)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
if (phdn->iButton == 0) // Left button
{
if (phdn->iItem == m_nSortedCol)
m_bSortAscending = !m_bSortAscending;
else
m_bSortAscending = TRUE;
m_nSortedCol = phdn->iItem;
m_KeymapList.SortItems (listCompareFunc, (LPARAM) this);
m_KeymapList.SetSortColumn (m_nSortedCol, m_bSortAscending);
}
}
return CDialog::OnNotify(wParam, lParam, pResult);
}
//*************************************************************************************
void CBCGPKeyMapDlg::CopyKeyMap ()
{
int i = m_KeymapList.GetSelectedCount();
if (i <= 0)
{
MessageBeep ((UINT)-1);
return;
}
CString strText;
int nItem = -1;
int nFlag = (m_KeymapList.GetSelectedCount () > 0) ? LVNI_SELECTED : LVNI_ALL;
while ((nItem = m_KeymapList.GetNextItem (nItem, nFlag)) >= 0)
{
strText += FormatItem (nItem) + _T("\r\n");
}
if (OpenClipboard ())
{
EmptyClipboard ();
HGLOBAL hClipbuffer = ::GlobalAlloc (GMEM_DDESHARE, strText.GetLength () + 1);
LPTSTR lpszBuffer = (LPTSTR) GlobalLock (hClipbuffer);
_tcscpy (lpszBuffer, (LPCTSTR) strText);
::GlobalUnlock (hClipbuffer);
::SetClipboardData (CF_TEXT, hClipbuffer);
CloseClipboard();
}
}
//*************************************************************************************
void CBCGPKeyMapDlg::PrintKeyMap ()
{
CWaitCursor WaitCursor;
int nItem = -1;
int nFlag = (m_KeymapList.GetSelectedCount () > 0) ? LVNI_SELECTED : LVNI_ALL;
CPrintDialog dlgPrint (FALSE, PD_ALLPAGES | PD_RETURNDC | PD_NOSELECTION, NULL);
if (dlgPrint.DoModal() != IDOK)
{
return;
}
// Obtain a handle to the device context.
HDC hdcPrn = dlgPrint.GetPrinterDC ();
if (hdcPrn == NULL)
{
ASSERT (FALSE);
return;
}
CDC dc;
dc.Attach (hdcPrn);
CSize szPage (dc.GetDeviceCaps (HORZRES), dc.GetDeviceCaps (VERTRES));
dc.StartDoc(_T("BCGKeyMapDlg")); // begin a new print job
dc.StartPage ();
int nPage = 1;
int y = OnPrintHeader (dc, nPage, szPage.cx);
while ((nItem = m_KeymapList.GetNextItem (nItem, nFlag)) >= 0)
{
int nItemHeight = OnPrintItem (dc, nItem, y, szPage.cx, TRUE /* Calc height */);
if (y + nItemHeight > szPage.cy)
{
dc.EndPage();
dc.StartPage ();
y = OnPrintHeader (dc, ++nPage, szPage.cx);
}
y += OnPrintItem (dc, nItem, y, szPage.cx, FALSE /* Draw */);
}
dc.EndPage();
dc.EndDoc();
}
//*************************************************************************************
int CBCGPKeyMapDlg::OnPrintHeader (CDC& dc, int nPage, int cx) const
{
TEXTMETRIC tm;
dc.GetTextMetrics (&tm);
int yMargin = tm.tmHeight * 2;
CString strAppName = (AfxGetApp ()->m_pszAppName == NULL) ? _T("") :
AfxGetApp ()->m_pszAppName;
CString strTitle;
GetWindowText (strTitle);
CString strCaption;
strCaption.Format (_T("- %d -\r\n%s: %s"), nPage, strAppName, strTitle);
CRect rectText (0, yMargin, cx, 32767);
return dc.DrawText (strCaption, rectText, DT_WORDBREAK | DT_CENTER) + 2 * yMargin;
}
//*************************************************************************************
int CBCGPKeyMapDlg::OnPrintItem (CDC& dc, int nItem, int y, int cx, BOOL bCalcHeight) const
{
ASSERT_VALID (this);
ASSERT (nItem >= 0);
TEXTMETRIC tm;
dc.GetTextMetrics (&tm);
int xMargin = tm.tmMaxCharWidth * 2;
CBCGPToolbarButton* pButton = (CBCGPToolbarButton*) m_KeymapList.GetItemData (nItem);
ASSERT_VALID (pButton);
CString strCommand = pButton->m_strText;
CString strKeys = m_KeymapList.GetItemText (nItem, iColumnKeys);
CString strDescr = m_KeymapList.GetItemText (nItem, iColumnDescr);
// Define column width:
int nKeyColumWidth = dc.GetTextExtent (CString (_T("Ctrl+Shift+W"))).cx + xMargin;
int nRestOfWidth = cx - nKeyColumWidth - 2 * xMargin;
int nHeight = 1;
for (int iStep = 0; iStep < (bCalcHeight ? 1 : 2); iStep ++)
{
UINT uiFormat = iStep == 0 ?
(DT_CALCRECT | DT_WORDBREAK) :
(DT_WORDBREAK | DT_END_ELLIPSIS);
CRect rectCmd (CPoint (xMargin, y), CSize (nRestOfWidth / 3, 32676));
int nCmdHeight = dc.DrawText (strCommand, rectCmd, uiFormat);
CRect rectKey (CPoint (rectCmd.right + xMargin, y), CSize (nKeyColumWidth, 32676));
int nKeyHeight = dc.DrawText (strKeys, rectKey, uiFormat);
CRect rectDescr (rectKey.right + xMargin, y, cx, 32676);
int nDescrHeight = dc.DrawText (strDescr, rectDescr, uiFormat);
nHeight = max (nCmdHeight, max (nKeyHeight, nDescrHeight));
}
return nHeight;
}
//*************************************************************************************
void CBCGPKeyMapDlg::SetColumnsWidth ()
{
CRect rectList;
m_KeymapList.GetClientRect (rectList);
CClientDC dc (this);
CFont* pOldFont = dc.SelectObject (m_KeymapList.GetFont ());
ASSERT_VALID (pOldFont);
int nKeyColumWidth = dc.GetTextExtent (CString (_T("Ctrl+Shift+W"))).cx + 10;
dc.SelectObject (pOldFont);
int nRestOfWidth = rectList.Width () - nKeyColumWidth - ::GetSystemMetrics (SM_CXHSCROLL);
m_KeymapList.SetColumnWidth (iColumnCommand, nRestOfWidth / 3);
m_KeymapList.SetColumnWidth (iColumnKeys, nKeyColumWidth);
m_KeymapList.SetColumnWidth (iColumnDescr, nRestOfWidth * 2 / 3);
}
//*************************************************************************************
int CBCGPKeyMapDlg::DoModal()
{
m_hInstDefault = AfxGetResourceHandle ();
CBCGPLocalResource locaRes;
return CDialog::DoModal();
}
//*************************************************************************************
BOOL CBCGPKeyMapDlg::Create(CWnd* pParentWnd)
{
m_hInstDefault = AfxGetResourceHandle ();
CBCGPLocalResource locaRes;
return CDialog::Create(IDD, pParentWnd);
}
//*************************************************************************************
void CBCGPKeyMapDlg::OnDestroy()
{
//----------------------------------
// Save window position and size:
//----------------------------------
if (GetWorkspace () != NULL)
{
CRect rectPosition;
GetWindowRect (rectPosition);
CBCGPRegistrySP regSP;
CBCGPRegistry& reg = regSP.Create (FALSE, FALSE);
if (reg.CreateKey (GetWorkspace ()->GetRegSectionPath (strWindowPlacementRegSection)))
{
reg.Write (strRectKey, rectPosition);
}
}
CDialog::OnDestroy();
}
//*************************************************************************************
CString CBCGPKeyMapDlg::FormatItem (int nItem) const
{
ASSERT_VALID (this);
CString strKeys = m_KeymapList.GetItemText (nItem, iColumnKeys);
if (strKeys.IsEmpty ())
{
strKeys = _T("-");
}
CString strItem;
strItem.Format (_T("%-30s\t%-20s\t%s"),
m_KeymapList.GetItemText (nItem, iColumnCommand),
strKeys,
m_KeymapList.GetItemText (nItem, iColumnDescr));
return strItem;
}
//***************************************************************************************
void CBCGPKeyMapDlg::OnSetColumns ()
{
CString strCaption;
strCaption.LoadString (IDS_BCGBARRES_COMMAND);
m_KeymapList.InsertColumn (iColumnCommand, strCaption);
strCaption.LoadString (IDS_BCGBARRES_KEYS);
m_KeymapList.InsertColumn (iColumnKeys, strCaption);
strCaption.LoadString (IDS_BCGBARRES_DESCRIPTION);
m_KeymapList.InsertColumn (iColumnDescr, strCaption);
}
//***************************************************************************************
void CBCGPKeyMapDlg::OnInsertItem (CBCGPToolbarButton* pButton, int nItem)
{
ASSERT_VALID (this);
ASSERT_VALID (pButton);
//------------------
// Set command name:
//------------------
CString strText = pButton->m_strTextCustom.IsEmpty () ?
pButton->m_strText : pButton->m_strTextCustom;
int iIndex = m_KeymapList.InsertItem (nItem, strText, -1);
m_KeymapList.SetItemData (iIndex, (DWORD) pButton);
m_KeymapList.SetItemText (iIndex, iColumnKeys, GetCommandKeys (pButton->m_nID));
//-------------------------
// Set command description:
//-------------------------
CString strDescr;
CFrameWnd* pParent = GetParentFrame ();
if (pParent != NULL && pParent->GetSafeHwnd () != NULL)
{
pParent->GetMessageString (pButton->m_nID, strDescr);
}
m_KeymapList.SetItemText (iIndex, iColumnDescr, strDescr);
}
//**************************************************************************************
CString CBCGPKeyMapDlg::GetCommandKeys (UINT uiCmdID) const
{
//--------------------------------------------
// Fill keys associated with selected command:
//--------------------------------------------
CString strKey;
for (int i = 0; i < m_nAccelSize; i ++)
{
if (uiCmdID == m_lpAccel [i].cmd)
{
ASSERT (&m_lpAccel [i] != NULL);
CBCGPKeyHelper helper (&m_lpAccel [i]);
CString sNewKey;
helper.Format (sNewKey);
if (!strKey.IsEmpty())
{
strKey += _T("; ");
}
strKey += sNewKey;
}
}
return strKey;
}
#endif // BCG_NO_CUSTOMIZATION
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -