📄 listboxex.cpp
字号:
//
// 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;
if (m_pEdit)
{
delete m_pEdit;
delete m_pBrowseButton;
m_pEdit = NULL;
m_pBrowseButton = NULL;
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
///*-
// FUNCTION NAME: CListBoxEx::SetItem
//
// DESCRIPTION: Set item text and data
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item.
//
// szItemText:
// TYPE: char
// MODE: In
// MECHANISM: By address
// DESCRIPTION: Addres of the new item text.
//
// dwItemData:
// TYPE: DWORD
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Custom item data.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::SetItem( int iItem,
LPCTSTR szItemText,
DWORD dwItemData )
{
ASSERT( iItem < GetCount() );
SendMessage( WM_SETREDRAW, FALSE, 0 );
DeleteString(iItem,FALSE);
InsertString(iItem,szItemText,FALSE);
SetItemData(iItem,dwItemData);
SendMessage( WM_SETREDRAW, TRUE, 0 );
} // CListBoxEx::SetItem
///*-
// FUNCTION NAME: CListBoxEx::SetItemText
//
// DESCRIPTION: Set item text
//
// PARAMETER(S):
// iItem:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item.
//
// szItemText:
// TYPE: char
// MODE: In
// MECHANISM: By const address
// DESCRIPTION: Addres of the new item text.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxEx::SetItemText( int iItem,
LPCTSTR szItemText )
{
ASSERT( iItem < GetCount() );
DWORD dwItemData;
dwItemData = GetItemData( iItem );
SetItem( iItem, szItemText, dwItemData );
} // CListBoxEx::SetItemText
///*-
// FUNCTION NAME: CListBoxEx::SwapItems
//
// DESCRIPTION: Called to swap the two items
//
// PARAMETER(S):
// iFirstItem:
// TYPE: int.
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the first item to be swapped.
//
// iSecondItem:
// TYPE: int.
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the second item to be swapped.
//
// RETURN: The new index of the item.
//
// NOTES: None.
//+*/
void CListBoxEx::SwapItems( int iFirstItem,
int iSecondItem )
{
ASSERT( iFirstItem < GetCount() );
ASSERT( iSecondItem < GetCount() );
if ( iFirstItem != iSecondItem )
{
// Cache the first item data
CString strFirstItem;
DWORD dwFirstItemData;
GetText( iFirstItem, strFirstItem );
dwFirstItemData = GetItemData( iFirstItem );
// Cache the second item data
CString strSecondItem;
DWORD dwSecondItemData;
GetText( iSecondItem, strSecondItem );
dwSecondItemData = GetItemData( iSecondItem );
// Insert the items in reverse order
if ( iFirstItem < iSecondItem )
{
SetItem( iFirstItem, strSecondItem, dwSecondItemData );
SetItem( iSecondItem, strFirstItem, dwFirstItemData );
}
else
{
SetItem( iSecondItem, strFirstItem, dwFirstItemData );
SetItem( iFirstItem, strSecondItem, dwSecondItemData );
}
}
} // CListBoxEx::SwapItems
///*-
// FUNCTION NAME: CListBoxEx::MoveItemUp
//
// DESCRIPTION: Called to move the item up.
//
// PARAMETER(S):
// iItem:
// TYPE: int.
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item to be moved.
//
// RETURN: The new index of the item.
//
// NOTES: None.
//+*/
int CListBoxEx::MoveItemUp( int iItem )
{
if(iItem > 0)
{
SwapItems(iItem, iItem-1);
SetCurSel(iItem - 1);
}
return iItem;
} // CListBoxEx::MoveItemUp()
///*-
// FUNCTION NAME: CListBoxEx::MoveItemDown
//
// DESCRIPTION: Called to move the item down.
//
// PARAMETER(S):
// iItem:
// TYPE: int.
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the item to be moved.
//
// RETURN: The new index of the item.
//
// NOTES: None.
//+*/
int CListBoxEx::MoveItemDown( int iItem )
{
ASSERT( iItem >= 0 );
if( iItem != GetCount()-1)
{
SwapItems( iItem, iItem+1 );
SetCurSel( iItem + 1 );
}
return iItem;
} // CListBoxEx::MoveItemDown
///*-
// FUNCTION NAME: CListBoxEx::OnEndEditMessage
//
// DESCRIPTION: Ends the editing of an item.
//
// PARAMETER(S):
// wParam:
// TYPE: WPARAM
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Unused.
//
// lParam:
// TYPE: LPARAM
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Pointer to a IPEENDEDITINFO structure.
//
// RETURN: TRUE to end editing process, FALSE to continue anyway.
//
// NOTES: This message handler calls OnEndEditing callback, if necessary.
//+*/
LRESULT CListBoxEx::OnEndEditMessage( WPARAM wParam,
LPARAM lParam )
{
UNUSED_ALWAYS( wParam );
BOOL bEndEdit;
LPIPCENDEDITINFO lpEndEditInfo = (LPIPCENDEDITINFO)lParam;
switch ( lpEndEditInfo->uKind )
{
case IPEK_KEY:
if ( lpEndEditInfo->nVirtKey == VK_ESCAPE )
{
bEndEdit = TRUE;
OnEndEditing( m_iEdited, TRUE );
EndEditing( TRUE );
}
else
{
bEndEdit = TRUE;
EndEditing( !OnEndEditing( m_iEdited, FALSE ) );
}
break;
case IPEK_ACTION:
bEndEdit = FALSE; // Superfluous
/**OnBrowseButton( m_iEdited );**/
//use message instead of virtual function
GetParent()->PostMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),BL_MESSAGE_BROWSE),(LPARAM)m_hWnd);
break;
case IPEK_FOCUS:
if ( (lpEndEditInfo->hNewWnd == m_pEdit->m_hWnd) ||
(lpEndEditInfo->hNewWnd == m_pBrowseButton->m_hWnd) )
{
bEndEdit = FALSE;
}
else
{
bEndEdit = TRUE;
EndEditing( !OnEndEditing( m_iEdited, FALSE ) );
}
break;
default:
bEndEdit = TRUE;
}
return bEndEdit;
} // CListBoxEx::OnEndEditMessage
///*-
// FUNCTION NAME: CListBoxEx::DeleteSelected
//
// DESCRIPTION: Deletes the currently selected item and selects an
// other item.
//
// PARAMETER(S): None.
//
// RETURN: None.
//
// NOTES:
//+*/
void CListBoxEx::DeleteSelected()
{
m_iSelected = GetCurSel();
if(m_iSelected != -1)
{
DeleteString(m_iSelected);
if(m_iSelected >= GetCount())
{
m_iSelected = GetCount() - 1;
}
SetCurSel(m_iSelected);
}
}
int CListBoxEx::SetCurSel(int nSelect)
{
int result = CListBox::SetCurSel(nSelect);
m_iSelected = nSelect;
if(result != LB_ERR)
GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),LB_MSG_SETCURSEL),(LPARAM)m_hWnd);
return result;
}
int CListBoxEx::AddString(LPCTSTR lpszItem,BOOL bSendMsg /*= TRUE*/)
{
int nResult = CListBox::AddString(lpszItem);
m_nNewlyAdded = nResult;
if(bSendMsg&&nResult != LB_ERR&&nResult !=LB_ERRSPACE)
{
GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),LB_MSG_ITEMADDED),(LPARAM)m_hWnd);
}
return nResult;
}
int CListBoxEx::InsertString(int nIndex,LPCTSTR lpszItem,BOOL bSendMsg /*= TRUE*/)
{
int nResult = CListBox::InsertString(nIndex,lpszItem);
m_nNewlyAdded = nIndex;
if(bSendMsg&&nResult != LB_ERR&&nResult !=LB_ERRSPACE)
GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),LB_MSG_ITEMADDED),(LPARAM)m_hWnd);
return nResult;
}
void CListBoxEx::ResetContent(BOOL bSendMsg /*= TRUE*/)
{
if(bSendMsg)
{
int count = GetCount();
for(int i=0; i<count; i++)
DeleteString(i,TRUE);
}
else
CListBox::ResetContent();
}
int CListBoxEx::DeleteString(UINT nIndex,BOOL bSendMsg /*= TRUE*/)
{
m_nNewlyDeleted = nIndex;
ASSERT((int)nIndex<GetCount());
if(bSendMsg)
GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),LB_MSG_ITEMDEL),(LPARAM)m_hWnd);
return CListBox::DeleteString(nIndex);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -