📄 inplaceedit.cpp
字号:
#include "stdafx.h"
#include "InPlaceEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CTRL_C 0x3
#define CTRL_V 0x16
#define CTRL_X 0x18
/////////////////////////////////////////////////////////////////////////////
// CInPlaceEdit
CInPlaceEdit* CInPlaceEdit::m_pInPlaceEdit = NULL;
CInPlaceEdit::CInPlaceEdit()
{
m_iRowIndex= -1;
m_iColumnIndex = -1;
m_bESC = FALSE;
n_select=-1;
m_strValidChars.Empty();
}
CInPlaceEdit::~CInPlaceEdit()
{
}
BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
//{{AFX_MSG_MAP(CInPlaceEdit)
ON_WM_KILLFOCUS()
ON_WM_CHAR()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// ON_MESSAGE(WM_PASTE, OnPaste)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInPlaceEdit message handlers
/*
void CInPlaceEdit::OnPaste(WPARAM , LPARAM )
{
if (m_strValidChars.IsEmpty())
{
return;
}
CString strFromClipboard;
// get the text from clipboard
if(OpenClipboard()) {
HANDLE l_hData = GetClipboardData(CF_TEXT);
if(NULL == l_hData) {
return;
}
char *l_pBuffer = (char*)GlobalLock(l_hData);
if(NULL != l_pBuffer) {
strFromClipboard = l_pBuffer;
}
GlobalUnlock(l_hData);
CloseClipboard();
}
// Validate the characters before pasting
for(int iCounter_ = 0; iCounter_ < strFromClipboard.GetLength(); iCounter_++)
{
if (-1 == m_strValidChars.Find(strFromClipboard.GetAt(iCounter_)))
{
return;
}
}
//let the individual control handle other processing
CEdit::Default();
}
*/
void CInPlaceEdit::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//为工具条设置响应
RelayEvent(WM_MOUSEMOVE,(WPARAM)nFlags,MAKELPARAM(LOWORD(point.x),LOWORD(point.y)));
CEdit::OnMouseMove(nFlags, point);
}
void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
if(n_select==1)
{
CString str;
GetWindowText(str);
if(str.Find('.')==0)
{
CString strValue;
strValue.Format("0%s", str);
str = strValue;
SetWindowText(str);
}
int n=str.ReverseFind('.');
int n_end=str.Find('.');
int m=str.GetLength();
m--;
if(n==m||n!=n_end)
{
AfxMessageBox("输入有误!请重新输入!");
}
}
// Get the text in the edit ctrl
else if(n_select==-1)
{
CString strEdit;
GetWindowText(strEdit);
// Send Notification to parent of edit 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 = m_iRowIndex;
dispinfo.item.iSubItem = m_iColumnIndex;
dispinfo.item.pszText = m_bESC ? LPTSTR((LPCTSTR)m_strWindowText) : LPTSTR((LPCTSTR)strEdit);
dispinfo.item.cchTextMax = m_bESC ? m_strWindowText.GetLength() : strEdit.GetLength();
GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
// PostMessage(WM_CLOSE);
SendMessage(WM_CLOSE);
}
}
void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if ((m_strValidChars.IsEmpty()) || ((-1 != m_strValidChars.Find(static_cast<TCHAR> (nChar))) ||
(nChar == VK_BACK) || (nChar == CTRL_C) || (nChar == CTRL_V) || (nChar == CTRL_X)))
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
{
MessageBeep(MB_ICONEXCLAMATION);
return;
}
}
BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (WM_KEYDOWN == pMsg->message && (VK_ESCAPE == pMsg->wParam || VK_RETURN == pMsg->wParam))
{
if (VK_ESCAPE == pMsg->wParam)
{
m_bESC = TRUE;
}
GetParent()->SetFocus();
return TRUE;
}
return CEdit::PreTranslateMessage(pMsg);
}
int CInPlaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
// Set the proper font
CFont* pFont = GetParent()->GetFont();
SetFont(pFont);
ShowWindow(SW_SHOW);
SetWindowText(m_strWindowText);
SetSel(0, -1);
SetFocus();
return 0;
}
CInPlaceEdit* CInPlaceEdit::GetInstance()
{
if(m_pInPlaceEdit == NULL)
{
m_pInPlaceEdit = new CInPlaceEdit;
}
return m_pInPlaceEdit;
}
void CInPlaceEdit::DeleteInstance()
{
delete m_pInPlaceEdit;
m_pInPlaceEdit = NULL;
}
BOOL CInPlaceEdit::ShowEditCtrl(DWORD dwStyle, const RECT &rCellRect, CWnd* pParentWnd,
UINT uiResourceID, int iRowIndex, int iColumnIndex,
CString& strValidChars, CString& rstrCurSelection)
{
m_iRowIndex = iRowIndex;
m_iColumnIndex = iColumnIndex;
m_strValidChars = strValidChars;
m_strWindowText = rstrCurSelection;
m_bESC = FALSE;
if(m_pInPlaceEdit->m_hWnd!=NULL)
{
m_pInPlaceEdit->m_hWnd=NULL;
}
if (NULL == m_pInPlaceEdit->m_hWnd)
{
return m_pInPlaceEdit->Create(dwStyle, rCellRect, pParentWnd, uiResourceID);
}
return TRUE;
}
//提示条
void CInPlaceEdit::SetToolTipText(CString s)
{
if(m_tooltip.m_hWnd==NULL){
if(m_tooltip.Create(this)) //first assignment
if(m_tooltip.AddTool(this, (LPCTSTR)s))
m_tooltip.Activate(1);
} else {
m_tooltip.UpdateTipText((LPCTSTR)s,this);
}
}
void CInPlaceEdit::RelayEvent(UINT message, WPARAM wParam, LPARAM lParam)
{
// This function will create a MSG structure, fill it in a pass it to
// the ToolTip control, m_ttip. Note that we ensure the point is in window
// coordinates (relative to the control's window).
if(NULL != m_tooltip.m_hWnd){
MSG msg;
msg.hwnd = m_hWnd;
msg.message = message;
msg.wParam = wParam;
msg.lParam = lParam;
msg.time = 0;
msg.pt.x = LOWORD(lParam);
msg.pt.y = HIWORD(lParam);
m_tooltip.RelayEvent(&msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -