⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editcell.cpp

📁 简单数据库管理系统
💻 CPP
字号:
// EditCell.cpp : implementation file
//

#include "stdafx.h"
#include "xkDBMS.h"
#include "EditCell.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CEditCell::CEditCell (CListCtrl* pCtrl, int iItem, int iSubItem, CString sInitText)
:   bEscape (FALSE)
{
    pListCtrl = pCtrl;
    Item = iItem;
    SubItem = iSubItem;
    InitText = sInitText;
}

CEditCell::~CEditCell()
{
}

BEGIN_MESSAGE_MAP(CEditCell, CEdit)
	//{{AFX_MSG_MAP(CEditCell)
	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()

/////////////////////////////////////////////////////////////////////////////
// CEditCell message handlers
void 
CEditCell::SetListText()
{
    CString Text;
    GetWindowText (Text);

    // 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 
CEditCell::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;		    	
			}
    }

    return CEdit::PreTranslateMessage (pMsg);
}

void CEditCell::OnKillFocus(CWnd* pNewWnd) 
{
   CEdit::OnKillFocus(pNewWnd);

    SetListText();

    DestroyWindow();
}

void CEditCell::OnNcDestroy() 
{
   CEdit::OnNcDestroy();
    
    delete this;
	
}

void CEditCell::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    BOOL Shift = GetKeyState (VK_SHIFT) < 0;
    switch (nChar)
    {
		case VK_ESCAPE :
		{
			if (nChar == VK_ESCAPE)
				bEscape = TRUE;
			GetParent()->SetFocus();
			return;
		}
		case VK_RETURN :
		{
			SetListText();
			EditAnother(Item, SubItem+1);
			return;
		}
		case VK_TAB :
		{
			if (Shift)
				EditAnother(Item, SubItem - 1);
			else
				EditAnother(Item, SubItem + 1);
			return;
		}
    }

    CEdit::OnChar (nChar, nRepCnt, nFlags);

}

int CEditCell::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CEdit::OnCreate (lpCreateStruct) == -1)
		return -1;

    CFont* Font = GetParent()->GetFont();
    SetFont (Font);

    SetWindowText (InitText);
    SetFocus();
    SetSel (0, -1);
    return 0;
}

UINT CEditCell::OnGetDlgCode() 
{
	  //UINT code=CEdit::OnGetDlgCode();
	//code=code|DLGC_WANTARROWS | DLGC_WANTTAB;
	//return code;
	return CEdit::OnGetDlgCode();
}

void CEditCell::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
   switch (nChar)
    {
		case VK_NEXT :
		{
			int Count = pListCtrl->GetItemCount();
			int NewItem = Item + pListCtrl->GetCountPerPage();
			if (Count > NewItem)
				EditAnother(NewItem, SubItem);
			else
				EditAnother(Count - 1, SubItem);
			return;
		}
		case VK_PRIOR :
		{
			int NewItem = Item - pListCtrl->GetCountPerPage();
			if (NewItem > 0)
				EditAnother(NewItem, SubItem);
			else
				EditAnother(0, SubItem);
			return;
		}
    }
    
    CEdit::OnKeyUp (nChar, nRepCnt, nFlags);
}

void CEditCell::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
   BOOL Control = GetKeyState (VK_CONTROL) < 0;
    switch (nChar)
    {
		case VK_UP :
			EditAnother(Item - 1, SubItem);
			return;
		case VK_DOWN :
			EditAnother (Item + 1, SubItem);
			return;
		case VK_HOME:
			if (!Control)
				break;
			EditAnother (Item, 1);
			return;
		case VK_END :
			if (!Control)
				break;
			//EditAnother(Count, SubItem);
			return;
    }
    
    CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CEditCell::EditAnother(int nItem, int nSubItem)
{    // Send Notification to parent of ListView ctrl
    CString Text;
    GetWindowText (Text);

	NMITEMACTIVATE dispinfo;
    dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
    dispinfo.hdr.idFrom = GetDlgCtrlID();
    dispinfo.hdr.code = NM_CLICK;
	dispinfo.iItem=nItem;
	dispinfo.iSubItem=nSubItem;
    GetParent()->GetParent()->SendMessage (WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM) &dispinfo);
}

⌨️ 快捷键说明

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