📄 noteedit.h
字号:
// NoteEdit.h: interface for the CNoteEdit class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_NOTEEDIT_H__5FE9A853_AFE7_4DB9_8AE9_074DB729D6AF__INCLUDED_)
#define AFX_NOTEEDIT_H__5FE9A853_AFE7_4DB9_8AE9_074DB729D6AF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CNoteEdit : public CWindowImpl<CNoteEdit, CRichEditCtrl>,
public CRichEditCommands<CNoteEdit>
{
private:
COLORREF m_clrBkgnd; // background color
CFont m_fontEdit; // font used by the edit control
CMenu m_menuEdit; // context menu
HCURSOR m_hCursor;
public:
DECLARE_WND_SUPERCLASS(NULL, CRichEditCtrl::GetWndClassName())
BOOL PreTranslateMessage(MSG* pMsg)
{
pMsg;
return FALSE;
}
virtual BOOL OnIdle()
{
return FALSE;
}
CNoteEdit()
{
ATLTRACE(_T("CNoteEdit::CNoteEdit()\n"));
m_hCursor = NULL;
}
~CNoteEdit()
{
ATLTRACE(_T("CNoteEdit::~CNoteEdit()\n"));
if (m_fontEdit.m_hFont)
m_fontEdit.DeleteObject();
if (m_menuEdit.m_hMenu != NULL)
m_menuEdit.DestroyMenu();
}
BEGIN_MSG_MAP(CNoteEdit)
// Take care of unhandled reflected messages
DEFAULT_REFLECTION_HANDLER()
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
MESSAGE_HANDLER(WM_INITMENUPOPUP, OnInitMenuPopup)
CHAIN_MSG_MAP_ALT(CRichEditCommands<CNoteEdit>, 1)
END_MSG_MAP()
// Changes the control's background color
void ChangeBkgndColor(COLORREF clrColor)
{
ATLTRACE(_T("CNoteEdit::ChangeBkgndColor()\n"));
m_clrBkgnd = clrColor;
SetBackgroundColor(m_clrBkgnd);
}
// Changes font
void ChangeFont(LOGFONT * plfNote)
{
ATLTRACE(_T("CNoteEdit::ChangeFont()\n"));
ATLASSERT(m_fontEdit.m_hFont);
// Show the font dialog with 12 point "Times New Roman" as the selected font.
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
CClientDC dc(m_hWnd);
lf.lfHeight = -MulDiv(12, dc.GetDeviceCaps(LOGPIXELSY), 72);
lstrcpy(lf.lfFaceName, _T("Times New Roman"));
CFontDialog dlg(&lf);
if (dlg.DoModal() == IDOK) // success
{
// Delete the old font and create the new one
m_fontEdit.DeleteObject();
if (m_fontEdit.CreateFontIndirect(&dlg.m_lf))
{
// Change the edit control's current font
SetFont(m_fontEdit);
// Return the LOGFONT structure
*plfNote = dlg.m_lf;
}
else
ATLTRACE(_T("New font was not created successfully!\n"));
}
else
ATLTRACE(_T("Font dialog was not created successfully!\n"));
}
// Sets edit control's font
void SetNoteFont(string strFontName, LONG lFontHeight, LONG lWeight, BYTE btItalic)
{
ATLTRACE(_T("CNoteEdit::SetNoteFont()\n"));
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
lstrcpy(lf.lfFaceName, strFontName.c_str());
lf.lfHeight = lFontHeight;
lf.lfWeight = lWeight;
lf.lfItalic = btItalic;
// Create a new font
m_fontEdit.CreateFontIndirect(&lf);
ATLASSERT(m_fontEdit.m_hFont);
SetFont(m_fontEdit);
}
LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
ATLTRACE(_T("CNoteEdit::OnEraseBkgnd()\n"));
// Eliminate the flicker during resizing
return 1;
}
LRESULT OnRButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
ATLTRACE(_T("CNoteEdit::OnRButtonDown()\n"));
// Cursor's horizontal and vertical position
CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
// Convert client coordinates to screen coordinates
ClientToScreen(&pt);
// Load the menu resource
if (!m_menuEdit.LoadMenu(IDR_CONTEXTMENU))
{
ATLTRACE(_T("Menu resource was not loaded successfully!\n"));
return 0;
}
// TrackPopupMenu cannot display the menu bar so get
// a handle to the first shortcut menu.
CMenuHandle menuPopup = m_menuEdit.GetSubMenu(0);
if (menuPopup.m_hMenu == NULL)
{
ATLTRACE(_T("Submenu was not retrieved successfully!\n"));
return 0;
}
// Display the shortcut menu
if (!menuPopup.TrackPopupMenu(TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON,
pt.x, pt.y, m_hWnd))
{
ATLTRACE(_T("Context menu was not displayed successfully!\n"));
return 0;
}
// Destroy the menu and free any memory that the menu occupies
menuPopup.DestroyMenu();
m_menuEdit.DestroyMenu();
m_menuEdit.m_hMenu = NULL;
return 0;
}
// Modifies the menu before it is displayed
LRESULT OnInitMenuPopup(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
ATLTRACE(_T("CNoteEdit::OnInitMenuPopup()\n"));
HMENU hMenuPopup = (HMENU)wParam;
int nPos;
UINT uId;
// Set the cursor shape to an arrow
m_hCursor = ::LoadCursor(NULL, IDC_ARROW);
ATLASSERT(m_hCursor);
::SetCursor(m_hCursor);
int nMenuItems = GetMenuItemCount(hMenuPopup);
for (nPos = 0; nPos < nMenuItems; nPos++)
{
uId = GetMenuItemID(hMenuPopup, nPos);
switch (uId)
{
case ID_EDIT_UNDO:
EnableMenuItem(hMenuPopup, uId, CanUndo() ?
MF_BYCOMMAND | MF_ENABLED :
MF_BYCOMMAND | MF_GRAYED);
break;
case ID_EDIT_SELECT_ALL:
EnableMenuItem(hMenuPopup, uId, (GetWindowTextLength() > 0) ?
MF_BYCOMMAND | MF_ENABLED :
MF_BYCOMMAND | MF_GRAYED);
break;
case ID_EDIT_CUT:
case ID_EDIT_COPY:
case ID_EDIT_CLEAR:
EnableMenuItem(hMenuPopup, uId, HasSelection() ?
MF_BYCOMMAND | MF_ENABLED :
MF_BYCOMMAND | MF_GRAYED);
break;
case ID_EDIT_PASTE:
EnableMenuItem(hMenuPopup, uId, IsClipboardFormatAvailable(CF_TEXT) ?
MF_BYCOMMAND | MF_ENABLED :
MF_BYCOMMAND | MF_GRAYED);
break;
default:
break;
}
}
return 0;
}
};
#endif // !defined(AFX_NOTEEDIT_H__5FE9A853_AFE7_4DB9_8AE9_074DB729D6AF__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -