📄 editcell.cpp
字号:
// EditCell.cpp : implementation file
#include "stdafx.h"
#include "ListCtrl.h"
#include "EditCell.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// EditCell
EditCell::EditCell (EditListCtrl* pCtrl, int iItem, int iSubItem, CString sInitText)
: bEscape (FALSE)
{
pListCtrl = pCtrl;
Item = iItem;
SubItem = iSubItem;
InitText = sInitText;
}
EditCell::~EditCell()
{
}
BEGIN_MESSAGE_MAP(EditCell, CEdit)
//{{AFX_MSG_MAP(EditCell)
ON_WM_KILLFOCUS()
ON_WM_NCDESTROY()
ON_WM_CHAR()
ON_WM_CREATE()
ON_WM_GETDLGCODE()
ON_WM_KEYUP()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// EditCell message handlers
void EditCell::SetListText()
{
CString Text;
GetWindowText (Text);
if(Text.GetLength()==0)
{
Text="0";
}
// Send Notification to parent of ListView ctrl
LV_DISPINFO dispinfo;
dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
dispinfo.hdr.idFrom = GetDlgCtrlID();
dispinfo.hdr.code = LVN_ENDLABELEDIT;
dispinfo.item.mask = LVIF_TEXT;
dispinfo.item.iItem = Item;
dispinfo.item.iSubItem = SubItem;
dispinfo.item.pszText = bEscape ? NULL : LPTSTR ((LPCTSTR) Text);
dispinfo.item.cchTextMax = Text.GetLength();
GetParent()->GetParent()->SendMessage (WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM) &dispinfo);
}
BOOL EditCell::PreTranslateMessage (MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_DELETE ||
pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_TAB ||
pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN || GetKeyState (VK_CONTROL))
{
::TranslateMessage (pMsg);
::DispatchMessage (pMsg);
return TRUE; // DO NOT process further
}
}
return CEdit::PreTranslateMessage (pMsg);
}
void EditCell::OnKillFocus (CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
SetListText();
DestroyWindow();
}
void EditCell::OnNcDestroy()
{
CEdit::OnNcDestroy();
delete this;
}
void EditCell::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
{
// Up and down are in the OnKeyDown so that the user can hold down the arrow
// keys to scroll through the entries.
switch (nChar)
{
case VK_UP :
{
if (Item > 0 && SubItem==1)
pListCtrl->EditSubItem (Item - 1, SubItem);
return;
}
case VK_DOWN :
{
if (SubItem==1)
pListCtrl->EditSubItem (Item + 1, SubItem);
return;
}
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
void EditCell::OnKeyUp (UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_NEXT :
{
int Count = pListCtrl->GetItemCount();
int NewItem = Item + pListCtrl->GetCountPerPage();
if (Count > NewItem)
pListCtrl->EditSubItem (NewItem, SubItem);
else
pListCtrl->EditSubItem (Count - 1, SubItem);
return;
}
case VK_PRIOR :
{
int NewItem = Item - pListCtrl->GetCountPerPage();
if (NewItem > 0)
pListCtrl->EditSubItem (NewItem, SubItem);
else
pListCtrl->EditSubItem (0, SubItem);
return;
}
}
CEdit::OnKeyUp (nChar, nRepCnt, nFlags);
}
void EditCell::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_ESCAPE :
{
if (nChar == VK_ESCAPE)
bEscape = TRUE;
GetParent()->SetFocus();
return;
}
case VK_RETURN :
{
pListCtrl->EditSubItem (Item + 1, 1);
return;
}
}
if(nChar>='0'&&nChar<='9'||nChar==VK_BACK||nChar=='-')
{
CEdit::OnChar (nChar, nRepCnt, nFlags);
}
}
int EditCell::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate (lpCreateStruct) == -1)
return -1;
// Set the proper font
CFont* Font = GetParent()->GetFont();
SetFont (Font);
SetWindowText (InitText);
SetFocus();
SetSel (0,-1);
return 0;
}
UINT EditCell::OnGetDlgCode()
{
return CEdit::OnGetDlgCode() | DLGC_WANTARROWS | DLGC_WANTTAB;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -