📄 listboxex.cpp
字号:
Dropped( GetCurSel(), pInfo->ptCursor );
break;
}
}
else
{
*pLResult = FALSE;
}
return TRUE; // Message handled
}
return CListBox::OnChildNotify( nMessage, wParam, lParam, pLResult );
} // CListBoxEx::OnChildNotify
///*-
// FUNCTION NAME: CListBoxEx::OnKeyDown
//
// DESCRIPTION: Called when it receives a WM_KEYDOWN message.
//
// PARAMETER(S):
// nChar:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Character identifier
//
// nRepCnt:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Repetition count
//
// nFlags:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Flags
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnKeyDown( UINT nChar,
UINT nRepCnt,
UINT nFlags )
{
CDragListBox::OnKeyDown( nChar, nRepCnt, nFlags );
if ( (nChar == VK_F2) &&
(m_iSelected != -1) &&
m_bAllowEditing &&
(OnBeginEditing( m_iSelected ) != FALSE) )
{
// Begin Editing
BeginEditing( m_iSelected );
}
else
if ( (nChar == VK_DELETE) &&
(m_iSelected != -1) )
{
DeleteString( m_iSelected );
}
} // CListBoxEx::OnKeyDown
///*-
// FUNCTION NAME: CListBoxEx::OnSysKeyDown
//
// DESCRIPTION: Called when it receives a WM_SYSKEYDOWN message.
//
// PARAMETER(S):
// nChar:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Character identifier
//
// nRepCnt:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Repetition count
//
// nFlags:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Flags
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnSysKeyDown( UINT nChar,
UINT nRepCnt,
UINT nFlags )
{
if ( ALT_KEY_PRESSED( nFlags ) &&
m_bAllowDrag )
{
switch ( nChar )
{
case VK_UP:
case VK_LEFT:
MoveItemUp( GetCurSel() );
break;
case VK_DOWN:
case VK_RIGHT:
MoveItemDown( GetCurSel() );
break;
}
}
CDragListBox::OnSysKeyDown( nChar, nRepCnt, nFlags );
} // CListBoxEx::OnSysKeyDown
///*-
// FUNCTION NAME: CListBoxEx::OnLButtonDown
//
// DESCRIPTION: Called when it receives a WM_LBUTTONDOWN message.
//
// PARAMETER(S):
// nFlags:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Flags
//
// point:
// TYPE: CPoint class
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Point of action
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnLButtonDown( UINT nFlags,
CPoint point )
{
int iItem;
ClientToScreen( &point );
iItem = ItemFromPt( point, FALSE );
TRACE1( "LButtonDown: %d\n", iItem );
if ( iItem != -1 )
{
if ( m_iSelected != iItem )
{
// Update info
m_iSelected = iItem;
}
}
CDragListBox::OnLButtonDown( nFlags, point );
} // CListBoxEx::OnLButtonDown
///*-
// FUNCTION NAME: CListBoxEx::OnLButtonDblClk
//
// DESCRIPTION: Called when it receives a WM_LBUTTONDBLCLICK message.
//
// PARAMETER(S):
// nFlags:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Flags
//
// point:
// TYPE: CPoint class
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Point of action
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnLButtonDblClk( UINT nFlags,
CPoint point )
{
CDragListBox::OnLButtonDblClk( nFlags, point );
if ( m_bAllowEditing )
{
int iItem;
ClientToScreen( &point );
iItem = ItemFromPt( point, FALSE );
TRACE1( "LButtonDblClk: %d\n", iItem );
if ( (iItem != -1) &&
m_bAllowEditing &&
(OnBeginEditing( iItem ) != FALSE) )
{
// Begin Editing
BeginEditing( iItem );
}
}
} // CListBoxEx::OnLButtonDblClk
///*-
// FUNCTION NAME: CListBoxEx::OnLButtonUp
//
// DESCRIPTION: Called when it receives a WM_LBUTTONUP message.
//
// PARAMETER(S):
// nFlags:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Flags
//
// point:
// TYPE: CPoint class
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Point of action
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnLButtonUp( UINT nFlags,
CPoint point )
{
CDragListBox::OnLButtonUp( nFlags, point );
} // CListBoxEx::OnLButtonUp
///////////////////////////////////////////////
// Overridables //
///////////////////////////////////////////////
///*-
// FUNCTION NAME: CListBoxEx::OnBeginEditing
//
// DESCRIPTION: Called when the item editing phase has been started.
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item.
//
// RETURN: TRUE if the editing action can be performed. FALSE otherwise.
//
// NOTES: None.
//+*/
BOOL CListBoxEx::OnBeginEditing( int iItem )
{
UNUSED_ALWAYS( iItem );
return TRUE;
} // CListBoxEx::OnBeginEditing
///*-
// FUNCTION NAME: CListBoxEx::OnEndEditing
//
// DESCRIPTION: Called when the item editing phase has ended.
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item.
//
// fCancel:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: If the editing was canceled, this parameter is
// TRUE. Otherwise, it is FALSE.
//
// RETURN: TRUE to set the item's label to the edited text.
// Return FALSE to reject the edited text and revert to
// the original label.
//
// NOTES: None.
//+*/
BOOL CListBoxEx::OnEndEditing( int iItem,
BOOL fCanceled )
{
UNUSED_ALWAYS( iItem );
UNUSED_ALWAYS( fCanceled );
return TRUE;
} // CListBoxEx::OnEndEditing
///*-
// FUNCTION NAME: CListBoxEx::OnBrowseButton
//
// DESCRIPTION: Called when the browse button on the item is pressed.
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::OnBrowseButton( int iItem )
{
UNUSED_ALWAYS( iItem );
} // CListBoxEx::OnMoreButtonPressed
///////////////////////////////////////////////
// Public functions //
///////////////////////////////////////////////
///*-
// FUNCTION NAME: CListBoxEx::BeginEditing
//
// DESCRIPTION: Set the edit style
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Item index. If -1, editing is for the currently
// selected item.
//
// RETURN: None.
//
// NOTES: This method does not call the OnBeginEditing function.
//+*/
void CListBoxEx::BeginEditing( int iItem )
{
if ( m_bAllowEditing &&
(m_iEdited == -1) )
{
if ( iItem == -1 )
{
iItem = m_iSelected;
}
else
if ( iItem > GetCount()-1 )
{
iItem = GetCount();
AddString( "" );
}
// Make it the current selection
SetCurSel( iItem );
m_iSelected = iItem;
// Save item index
m_iEdited = iItem;
// Retrieve item text
CString strItemText;
GetText( iItem, strItemText );
// Cache edit/button rectangles
GetItemRect( iItem, m_rcEdit );
m_rcButton.CopyRect( m_rcEdit );
// Adjust for the edit and the button
m_rcEdit.left += 5;
if ( m_dwEditStyle & LBEX_EDITBUTTON )
{
m_rcEdit.right -= 30;
}
else
{
m_rcEdit.right -= 5;
}
m_rcEdit.InflateRect( 0, 2 );
// Initialize the edit control with the item text
m_pEdit->SetWindowText( strItemText );
// Show the edit control
m_pEdit->Show( m_rcEdit );
if ( m_dwEditStyle & LBEX_EDITBUTTON )
{
m_rcButton.left = m_rcEdit.right;
m_rcButton.right -= 5;
m_rcButton.InflateRect( 0, 2 );
m_pBrowseButton->Show( m_rcButton );
}
}
} // CListBoxEx::BeginEditing()s
///*-
// FUNCTION NAME: CListBoxEx::EditNew
//
// DESCRIPTION: Adds a new item and begin editing
//
// PARAMETER(S): None.
//
// RETURN: None.
//
// NOTES: This method does not call the OnBeginEditing function.
//+*/
void CListBoxEx::EditNew()
{
BeginEditing( GetCount() );
} // CListBoxEx::EditNew()
///*-
// FUNCTION NAME: CListBoxEx::EndEditing
//
// DESCRIPTION: Ends the editing of an item.
//
// PARAMETER(S):
// fCancel:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: If this parameter is TRUE, the control cancels
// editing without saving the changes.
// Otherwise, the control saves the changes to the
// label.
//
// RETURN: None.
//
// NOTES: This method does not call the OnEndEditing function.
//+*/
void CListBoxEx::EndEditing( BOOL fCancel )
{
TRACE( "EndEditing\n" );
// Hide the edit box
m_pEdit->Hide();
m_pBrowseButton->Hide();
// Update item text
CString strNewItemText;
m_pEdit->GetWindowText( strNewItemText );
if ( strNewItemText.IsEmpty() )
{
DeleteString( m_iEdited );
}
else
if ( !fCancel )
{
// Replace the text
SetItemText( m_iEdited, LPCTSTR(strNewItemText) );
// Select the edited item
SetCurSel( m_iEdited );
}
m_iEdited = -1;
Invalidate();
} // CListBoxEx::EndEditing
///*-
// FUNCTION NAME: CListBoxEx::SetEditStyle
//
// DESCRIPTION: Set the edit style
//
// PARAMETER(S):
// dwEditStyle:
// TYPE: DWORD
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Edit style.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::SetEditStyle( DWORD dwEditStyle )
{
if ( m_dwEditStyle != dwEditStyle )
{
m_dwEditStyle = dwEditStyle;
delete m_pEdit;
CreateEdit();
}
} // CListBoxEx::SetEditStyle
///*-
// FUNCTION NAME: CListBoxEx::SetEditText
//
// DESCRIPTION: Sets the new edit text
//
// PARAMETER(S):
// strNewText:
// TYPE: CString class
// MODE: In
// MECHANISM: By const reference
// DESCRIPTION: New edit text.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::SetEditText( const CString & strNewText ) const
{
if ( m_pEdit && m_pEdit->IsWindowVisible() )
{
m_pEdit->SetWindowText( LPCTSTR(strNewText) );
m_pEdit->SetFocus();
}
} // CListBoxEx::SetEditText
///*-
// FUNCTION NAME: CListBoxEx::GetEditHandle
//
// DESCRIPTION: Returns the edit handle
//
// PARAMETER(S): None.
//
// RETURN: The handle of the edit tool.
// If no editing operation has never been performed, the function
// returns NULL.
// You cannot destroy the edit control, but you can subclass it.
//
// NOTES: None.
//+*/
HWND CListBoxEx::GetEditHandle() const
{
return m_pEdit ? m_pEdit->m_hWnd : NULL;
} // CListBoxEx::GetEditHandle
///*-
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -