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

📄 listeditctrl.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		m_nSelItem = GetItemCount () - 1;

	// Edit new item:
	EditSubLabel (m_nSelItem, 2);
	
	return (0);
	}

// **************************************************************************
// OnNextSubLabel ()
//
// Description:
//	On Down Arrow message handler.
//
// Parameters:
//  WPARAM		wParam		Not used.
//	LPARAM		lParam		Not used.
//
// Returns:
//  long - 0.
// **************************************************************************
long CKListEditCtrl::OnNextSubLabel (WPARAM wParam, LPARAM lParam)
	{
	// Next item:
	++m_nSelItem;
	
	// Wrap to start if next is invalid:
	if (m_nSelItem >= GetItemCount ())	
		m_nSelItem = 0;

	// Edit new item:
	EditSubLabel (m_nSelItem, 2);
	
	return (0);
	}


/////////////////////////////////////////////////////////////////////////////
// CKListEdit
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
BEGIN_MESSAGE_MAP (CKListEdit, CEdit)
	//{{AFX_MSG_MAP(CKListEdit)
	ON_WM_CREATE ()
	ON_WM_CHAR ()
	ON_WM_NCDESTROY ()
	ON_WM_KILLFOCUS ()
	ON_MESSAGE (WM_PASTE, OnPaste)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP ()


// **************************************************************************
// CKListEdit ()
//
// Description:
//	Constructor.
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKListEdit::CKListEdit (int iItem, int iSubItem, CString sInitText)
	: m_sInitText (sInitText)
	{
	// Initialize member variables:
	m_iItem = iItem;	
	m_iSubItem = iSubItem;	
	m_bESC = FALSE;
	}

// **************************************************************************
// ~CKListEdit ()
//
// Description:
//	Destructor.
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKListEdit::~CKListEdit ()
	{
	}


/////////////////////////////////////////////////////////////////////////////
// CKListEdit message handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// OnCreate ()
//
// Description:
//	The framework calls this member function when an application requests 
//	that the Windows window be created.
//
// Parameters:
//  LPCREATESTRUCT	lpCreateStruct	Contains information about window to be
//									  created.
//
// Returns:
//  int - 0 if success, -1 if fail
// **************************************************************************
int CKListEdit::OnCreate (LPCREATESTRUCT lpCreateStruct) 
	{
	// Perform default processing:
	if (CEdit::OnCreate (lpCreateStruct) == -1)		
		return (-1);	
	
	// Set the font to our parents current:
	CFont* font = GetParent ()->GetFont ();	
	SetFont (font);

	// Initialize edit text:
	SetWindowText (m_sInitText);	

	// Give the control the focus and select the current text for edit:
	SetFocus ();	
	SetSel (0, -1);	

	// If we make it here, then return 0 to indicate success:
	return (0);
	}

// **************************************************************************
// OnChar ()
//
// Description:
//	Handle keyboard input.  Switch view on tab, etc.
//
// Parameters:
//  UINT		nChar		Character code
//	UINT		nRepCnt		repeat count
//	UINT		nFlags		Flags
//
// Returns:
//  void
// **************************************************************************
void CKListEdit::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) 
	{
	// Filter escape and return:
	if (nChar == VK_ESCAPE || nChar == VK_RETURN)	
		{		
		if (nChar == VK_ESCAPE)
			m_bESC = TRUE;		
		
		GetParent ()->SetFocus ();		
		return;	
		}

	// Perform default processing:
	CEdit::OnChar (nChar, nRepCnt, nFlags);	

	// Get text extent:
	CString strText;
	GetWindowText (strText);	

	CWindowDC dc (this);
	CFont *pFont = GetParent ()->GetFont ();
	CFont *pFontDC = dc.SelectObject (pFont);
	CSize size = dc.GetTextExtent (strText);	
	dc.SelectObject (pFontDC);

	// add some extra buffer	
	size.cx += 5;
	
	// Get client rectangle:
	CRect rect; 
	CRect parentrect;	

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

	// Transform rectangle 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);	
		}

	// Send notification to parent of the list control that edit was made:
	GetParent ()->GetParent ()->SendMessage (UM_LISTEDIT_ITEMCHANGE, m_iSubItem, m_iItem);
	}

// **************************************************************************
// OnNcDestroy ()
//
// Description:
//	Called by the framework when the nonclient area is being destroyed.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKListEdit::OnNcDestroy () 
	{
	// Perform default processing:
	CEdit::OnNcDestroy ();
	
	// Self-delete:
	delete this;	
	}

// **************************************************************************
// OnKillFocus ()
//
// Description:
//	This method is called by the framework immediately before losing the 
//	input focus.
//
// Parameters:
//  CWnd		*pNewWnd	Specifies a pointer to the window that receives
//							  the input focus, this may be NULL or it may be temporary.
//
// Returns:
//  void
// **************************************************************************
void CKListEdit::OnKillFocus (CWnd *pNewWnd) 
	{
	// Perform default processing:
	CEdit::OnKillFocus (pNewWnd);	
	
	// Get current text:
	CString strText;	
	GetWindowText (strText);

	// Send LVN_ENDLABELEDIT notification to parent of list control:
	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 (strText));
	dispinfo.item.cchTextMax = strText.GetLength ();

	GetParent ()->GetParent ()->SendMessage (WM_NOTIFY, GetParent ()->GetDlgCtrlID (), (LPARAM) &dispinfo);	

	// Destroy our window:
	DestroyWindow ();
	}

// **************************************************************************
// PreTranslateMessage ()
//
// Description:
//	Used by class CWinApp to translate window messages before they are 
//	dispatched to theTranslateMessage andDispatchMessage Windows functions.
//	Use opportunity to process up and down arrow messages.
//
// Parameters:
//  MSG			*pMsg
//
// Returns:
//	BOOL - Nonzero if the message was translated and should not be dispatched,
//	0 if the message was not translated and should be dispatched.
// **************************************************************************
BOOL CKListEdit::PreTranslateMessage (MSG *pMsg) 
	{
	// Process key down events:
	if (pMsg->message == WM_KEYDOWN)	
		{
		// Send parent window notification of up arrow message:
		if (pMsg->wParam == VK_UP)
			{
			GetParent ()->SendMessage (UM_ONUPARROW, 0, 0);
			return (TRUE);
			}

		// Send parent window notification of down arrow event:
		if (pMsg->wParam == VK_DOWN)
			{
			GetParent ()->SendMessage (UM_ONDNARROW, 0, 0);
			return (TRUE);
			}

		// Do not dispatch return, delete, escape, and control presses:
		if (pMsg->wParam == VK_RETURN || 
			pMsg->wParam == VK_DELETE || 
			pMsg->wParam == VK_ESCAPE || 
			GetKeyState (VK_CONTROL))		
			{			
			::TranslateMessage (pMsg);
			::DispatchMessage (pMsg);			

			return (TRUE);		    	
			}	
		}

	// Perform default processing and return result:
	return (CEdit::PreTranslateMessage (pMsg));
	}

// **************************************************************************
// OnPaste ()
//
// Description:
//	Handles pastes into the edit control by allowing default processing
//  to occur and then notifies the parent that a change has been made.
//
// Parameters:
//  WPARAM		wParam		Not used.
//	LPARAM		lParam		Not used.
//
// Returns:
//  long - 0.
// **************************************************************************
long CKListEdit::OnPaste (WPARAM wParam, LPARAM lParam) 
	{
	// Perform default message processing:
	Default ();

	// Send notification to parent of the list control that edit was made:
	GetParent ()->GetParent ()->SendMessage (UM_LISTEDIT_ITEMCHANGE, m_iSubItem, m_iItem);
	return (0);
	}

⌨️ 快捷键说明

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