📄 inplaceedit.cpp
字号:
//author : Jarry
//E-mail : lansingk@online.sh.cn
// InplaceEdit.cpp : implementation file
//
#include "stdafx.h"
#include "InplaceEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CInplaceEdit
CInplaceEdit::CInplaceEdit(int iItem, int iSubitem, CString strInText)
{
m_strInText = strInText;
m_iItem = iItem;
m_iSubitem = iSubitem;
m_bEsc = false;
}
CInplaceEdit::~CInplaceEdit()
{
}
BEGIN_MESSAGE_MAP(CInplaceEdit, CEdit)
//{{AFX_MSG_MAP(CInplaceEdit)
ON_WM_KILLFOCUS()
ON_WM_NCDESTROY()
ON_WM_CHAR()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInplaceEdit message handlers
BOOL CInplaceEdit::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message == WM_KEYDOWN )
{
if(pMsg->wParam == VK_RETURN
|| pMsg->wParam == VK_DELETE
|| pMsg->wParam == VK_ESCAPE
|| GetKeyState( VK_CONTROL)
)
{
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return true; // do not process further
}
}
return CEdit::PreTranslateMessage(pMsg);
}
void CInplaceEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
CString str;
GetWindowText(str);
// 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 = m_iItem;
dispinfo.item.iSubItem = m_iSubitem;
dispinfo.item.pszText = m_bEsc ? NULL : LPTSTR((LPCTSTR)str);
dispinfo.item.cchTextMax = str.GetLength();
GetParent()->GetParent()->SendMessage( WM_NOTIFY, GetParent()->GetDlgCtrlID(),
(LPARAM)&dispinfo );
DestroyWindow();
}
void CInplaceEdit::OnNcDestroy()
{
CEdit::OnNcDestroy();
delete this;
}
void CInplaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar == VK_ESCAPE || nChar == VK_RETURN)
{
if( nChar == VK_ESCAPE )
m_bEsc = true;
GetParent()->SetFocus();
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
// resize edit control if needed
// get text extent
CString str;
GetWindowText( str );
CWindowDC dc(this);
CFont *pFont = GetParent()->GetFont();
CFont *pFontDC = dc.SelectObject( pFont );
CSize size = dc.GetTextExtent( str );
dc.SelectObject( pFontDC );
size.cx += 5; // add some extra buffer
// get client rect
CRect rect, parentrect;
GetClientRect( &rect );
GetParent()->GetClientRect( &parentrect );
// transform rect to parent coordinates
ClientToScreen( &rect );
GetParent()->ScreenToClient( &rect );
// check whether control needs to be resized
// and whether there is space to grow
if( size.cx > rect.Width() )
{
if( size.cx + rect.left < parentrect.right )
rect.right = rect.left + size.cx;
else
rect.right = parentrect.right;
MoveWindow( &rect );
}
}
int CInplaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// set the proper font
CFont* font = GetParent()->GetFont();
SetFont(font);
SetWindowText( m_strInText );
SetFocus();
SetSel( 0, -1 );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -