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

📄 gfxinedit.cpp

📁 一个多线程的网络数据采集系统(客户端)
💻 CPP
字号:
// GfxInEdit.cpp : implementation file
//

#include "stdafx.h"
#include "WspProcess.h"

#include "GfxInEdit.h"
#include "GfxInCombo.h"

#include "GfxListCtrl.h"

#include "GfxPopupMenu.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGfxInEdit

//列表单元编辑控件类构造函数
CGfxInEdit::CGfxInEdit(int iItem, int iSubItem, CString sInitText, bool _bMouseSelect) : m_sInitText(sInitText)
{
	bMouseSelect = _bMouseSelect;
	m_iItem = iItem;
	m_iSubItem = iSubItem;
	m_bESC = FALSE;
}

//列表单元编辑控件类析构函数
CGfxInEdit::~CGfxInEdit()
{
}

//CEdit)
BEGIN_MESSAGE_MAP(CGfxInEdit, CRichEditCtrl)
	//{{AFX_MSG_MAP(CGfxInEdit)
	ON_WM_CHAR()
	ON_WM_CREATE()
	ON_WM_KILLFOCUS()
	ON_WM_NCDESTROY()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGfxInEdit message handlers

//响应WM_CHAR处理输入
void CGfxInEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	//处理特殊的输入:Escape键、Enter键、Tab键
	if (nChar == VK_ESCAPE || nChar == VK_RETURN || nChar == VK_TAB)
	{
		if (nChar == VK_ESCAPE) m_bESC = TRUE;
		if (nChar == VK_TAB)
		{
			SHORT sh = GetKeyState(VK_SHIFT);

			ProcessSelect();
			CWnd * pParent = GetParent();
			int ip[2] = { m_iItem, m_iSubItem };
			DestroyWindow();
			//Shift键按下
			if (sh < 0) pParent->SendMessage(WM_USER_TAB, 1, (LPARAM) &ip);
			else pParent->SendMessage(WM_USER_TAB, 0, (LPARAM) &ip);
			return;
		}
		//将焦点返还父窗口(列表控件)
		GetParent()->SetFocus();
		return;
	}

	//其他键的输入交由基类函数处理
	CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);

	//如果需要,则改变编辑控件的尺寸
	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;

	CRect rect, parentrect;
	GetClientRect(&rect);
	GetParent()->GetClientRect(&parentrect);

	ClientToScreen(&rect);
	GetParent()->ScreenToClient(&rect);

	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 CGfxInEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1) return -1;
	CFont* font = GetParent()->GetFont();
	SetFont(font);

	SetWindowText(m_sInitText);
	SetFocus();

	SetSel(0, -1);

	if (bMouseSelect)
	{
		CPoint pt;
		GetCursorPos(&pt);
		ScreenToClient(&pt);
		PostMessage(WM_LBUTTONDOWN, 0, MAKELONG(pt.x, pt.y));
		PostMessage(WM_LBUTTONUP, 0, MAKELONG(pt.x, pt.y));
	}

	return 0;
}

//当编辑控件失去焦点时的处理
void CGfxInEdit::OnKillFocus(CWnd* pNewWnd) 
{
	CRichEditCtrl::OnKillFocus(pNewWnd);

	if (m_bESC)
	{
		DestroyWindow();
		return;
	}
	ProcessSelect();
	DestroyWindow();
}

void CGfxInEdit::OnNcDestroy() 
{
	CRichEditCtrl::OnNcDestroy();
//	delete this;
}

BOOL CGfxInEdit::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_LEFT)
		{
			long ls,le;
			GetSel(ls, le);
			if (ls == 0 && ls == le)
			{
				ProcessSelect();
				CWnd * pParent = GetParent();
				int ip[2] = { m_iItem, m_iSubItem };
				DestroyWindow();
				pParent->SendMessage(WM_USER_TAB, 2, (LPARAM) &ip);
				return TRUE;
			}
		}
		if (pMsg->wParam == VK_RIGHT)
		{
			long ls,le;
			GetSel(ls, le);
			CString cs;
			GetWindowText(cs);

			if (ls == cs.GetLength() && ls == le)
			{
				ProcessSelect();
				CWnd * pParent = GetParent();
				int ip[2] = { m_iItem, m_iSubItem };
				DestroyWindow();
				pParent->SendMessage(WM_USER_TAB, 3, (LPARAM) &ip);
				return TRUE;
			}
		}

		if (pMsg->wParam == VK_UP)
		{
			ProcessSelect();
			CWnd * pParent = GetParent();
			int ip[2] = { m_iItem, m_iSubItem };
			DestroyWindow();
			pParent->SendMessage(WM_USER_TAB, 4, (LPARAM) &ip);
			return TRUE;
		}
		if (pMsg->wParam == VK_DOWN)
		{
			ProcessSelect();
			CWnd * pParent = GetParent();
			int ip[2] = { m_iItem, m_iSubItem };
			DestroyWindow();
			pParent->SendMessage(WM_USER_TAB, 5, (LPARAM) &ip);
			return TRUE;
		}


		if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_DELETE || pMsg->wParam == VK_ESCAPE || 
			pMsg->wParam == VK_RIGHT || pMsg->wParam == VK_LEFT || pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN || 
			pMsg->wParam == VK_TAB || GetKeyState(VK_CONTROL))
		{
			::TranslateMessage(pMsg);
			::DispatchMessage(pMsg);
			return TRUE;
		}
	}
	
	return CRichEditCtrl::PreTranslateMessage(pMsg);
}

//若当前被编辑列表单元被修改,通知列表控件处理此修改
void CGfxInEdit::ProcessSelect()
{
	CString str;
	GetWindowText(str);
	
	if (m_sInitText != str) 
	{
		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()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
	}
}

void CGfxInEdit::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	CRichEditCtrl::OnLButtonDblClk(nFlags, point);

	NMHDR nh;
	nh.hwndFrom = GetParent()->GetSafeHwnd();
	nh.idFrom = GetParent()->GetDlgCtrlID();
	nh.code = NM_DBLCLK;

	CWnd * pWnd = ((CGfxListCtrl *)GetParent())->GetReciper();//GetParent();
	pWnd->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM) &nh);
}

void CGfxInEdit::PostNcDestroy() 
{
	CRichEditCtrl::PostNcDestroy();
	delete this;	
}

void CGfxInEdit::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CGfxPopupMenu cMenu;
	cMenu.CreatePopupMenu();

	cMenu.AppendMenu(MF_STRING, ID_EDIT_UNDO, "&Annulla");
	cMenu.AppendMenu(MF_SEPARATOR);
	cMenu.AppendMenu(MF_STRING, ID_EDIT_CUT, "&Taglia");
	cMenu.AppendMenu(MF_STRING, ID_EDIT_COPY, "&Copia");
	cMenu.AppendMenu(MF_STRING, ID_EDIT_PASTE, "&Incolla");

	CPoint pt(GetMessagePos());

	cMenu.LoadToolBarResource(IDR_LISTMENUBMP);//IDR_MAINFRAME);
	cMenu.RemapMenu(&cMenu);
	cMenu.EnableMenuItems(&cMenu, this);

	cMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, this);

	cMenu.DestroyMenu();
}

bool CGfxInEdit::UpdateCopy()
{
	long ns = 0, ne = 0;
	GetSel(ns, ne);
	return (ns != ne) ? true : false;
}

bool CGfxInEdit::UpdatePaste()
{
	return CanPaste() ? true : false;
}

bool CGfxInEdit::UpdateCut()
{
	long ns = 0, ne = 0;
	GetSel(ns, ne);
	return (ns != ne) ? true : false;
}

bool CGfxInEdit::UpdateUndo()
{
	return CanUndo() ? true : false;
}


void CGfxInEdit::OnEditCopy() 
{
	Copy();	
}

void CGfxInEdit::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(UpdateCopy());
}

void CGfxInEdit::OnEditCut() 
{
	Cut();	
}

void CGfxInEdit::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(UpdateCut());
}

void CGfxInEdit::OnEditPaste() 
{
	Paste();	
}

void CGfxInEdit::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(UpdatePaste());
}

void CGfxInEdit::OnEditUndo() 
{
	Undo();
}

void CGfxInEdit::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(UpdateUndo());
}

⌨️ 快捷键说明

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