📄 listboxex.cpp
字号:
memDC.SetTextColor( ::GetSysColor(COLOR_WINDOWTEXT) );
// Draw the text
CString strWindowText;
GetWindowText( strWindowText );
memDC.DrawText( strWindowText, rcClient, DT_SINGLELINE | DT_VCENTER );
//
// Buttons
//
// Draw the button bitmap
DrawBitmap( memDC,
m_ButtonBitmap,
rcClient.right - __BMP_WIDTH - 2,
nBmpTopY,
__BMP_WIDTH,
__BMP_HEIGHT );
// Draw the button edge
if ( m_iButton != __BMP_NUMBTN )
{
CRect rcButtonEdge( rcClient.right - (__BMP_NUMBTN - m_iButton)*__BMP_BTNWID - 2,
nBmpTopY,
rcClient.right - (__BMP_NUMBTN - m_iButton - 1)*__BMP_BTNWID - 2,
__BMP_HEIGHT + nBmpTopY );
memDC.DrawEdge( &rcButtonEdge,
m_bButtonPressed ? BDR_SUNKENOUTER : BDR_RAISEDINNER,
BF_RECT );
}
//
// Bit copy
//
dc.BitBlt( 2,
0,
rcClient.Width()-2,
rcClient.Height(),
&memDC,
0,
0,
SRCCOPY );
//
// Tidy up
//
// Select the bitmap out of the device context
memDC.SelectObject( pOldBmp );
// Select the font out of the device context
memDC.SelectObject( pOldFont );
} // CListBoxExBuddy::OnPaint()
///////////////////////////////////////////////
// Mouse Management //
///////////////////////////////////////////////
///*-
// FUNCTION NAME: CListBoxExBuddy::FindButton
//
// DESCRIPTION: Finds a button given a point.
//
// PARAMETER(S):
// point:
// TYPE: CPoint class
// MODE: In
// MECHANISM: By const reference
// DESCRIPTION: Point of action
//
// RETURN: The index of the button, between 0 and 4 (bounds included).
//
// NOTES: None.
//+*/
int CListBoxExBuddy::FindButton( const CPoint & point )
{
// Find the button
for ( UINT iIndex = 0; iIndex < __BMP_NUMBTN; iIndex++ )
{
if ( m_arcButtons[iIndex].PtInRect( point ) )
{
break;
}
}
return iIndex;
} // CListBoxExBuddy::FindButton()
///*-
// FUNCTION NAME: CListBoxExBuddy::InvalidateButton
//
// DESCRIPTION: Called to redraw a button.
//
// PARAMETER(S):
// iIndex:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Button index
//
// bUpdateWindow:
// TYPE: BOOL
// MODE: In
// MECHANISM: By value
// DESCRIPTION: If TRUE, calls UpdateWindow()
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::InvalidateButton( int iIndex,
BOOL bUpdateWindow /*= TRUE */ )
{
if ( iIndex < __BMP_NUMBTN )
{
InvalidateRect( &m_arcButtons[ iIndex ], FALSE );
}
if ( bUpdateWindow ) UpdateWindow();
} // CListBoxExBuddy::InvalidateButton
///*-
// FUNCTION NAME: CListBoxExBuddy::OnMouseMove
//
// DESCRIPTION: Called when it receives a WM_MOUSEMOVE 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 CListBoxExBuddy::OnMouseMove( UINT nFlags,
CPoint point )
{
if ( !m_bButtonPressed )
{
UINT iIndex = FindButton( point );
// If found a button, update info
if ( iIndex != m_iButton )
{
InvalidateButton( m_iButton, FALSE );
m_iButton = iIndex;
InvalidateButton( m_iButton, TRUE );
}
}
// Releay tooltip events
//m_ToolTip.RelayEvent( const_cast<MSG *>( GetCurrentMessage() ) );
//m_ToolTip.Activate( TRUE );
CWnd::OnMouseMove(nFlags, point);
} // CListBoxExBuddy::OnMouseMove
///*-
// FUNCTION NAME: CListBoxExBuddy::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 CListBoxExBuddy::OnLButtonDown( UINT nFlags,
CPoint point )
{
// Capture the mouse
SetCapture();
// Find the button
m_iButton = FindButton( point );
// Redraw the button
if ( m_iButton != __BMP_NUMBTN )
{
m_bButtonPressed = TRUE;
// Redraw only the affected button
InvalidateRect( &m_arcButtons[ m_iButton ], FALSE );
UpdateWindow();
}
CWnd::OnLButtonDown(nFlags, point);
} // CListBoxExBuddy::OnLButtonDown
///*-
// FUNCTION NAME: CListBoxExBuddy::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 CListBoxExBuddy::OnLButtonUp( UINT nFlags,
CPoint point )
{
// Find the button
UINT iButton = FindButton( point );
// Accept only clicks that occur on the same button where the mouse was pressed
if ( iButton == m_iButton )
{
// Take action, if necessary
if ( m_iButton != __BMP_NUMBTN )
{
DoClick( m_iButton );
}
}
// Set default conditions
m_bButtonPressed = FALSE;
// Redraw
Invalidate( FALSE );
// Memorize last
m_iButton = iButton;
// Release mouse capture
ReleaseCapture();
// Call base
CWnd::OnLButtonUp(nFlags, point);
} // CListBoxExBuddy::OnLButtonUp
// FUNCTION NAME: CListBoxExBuddy::DoClick
//
// DESCRIPTION: Called when a click occurs on one of the action button.
//
// PARAMETER(S):
// iIndex:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Zero-based index of the button
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::DoClick( int iIndex )
{
int iSelected = m_pListBoxEx->GetCurSel();
switch ( iIndex )
{
case __BTN_NEW:
m_pListBoxEx->EditNew();
break;
case __BTN_DEL:
if ( iSelected != -1 ) m_pListBoxEx->DeleteString( iSelected );
break;
case __BTN_UP:
if ( iSelected != -1 ) m_pListBoxEx->MoveItemUp( iSelected );
break;
case __BTN_DOWN:
if ( iSelected != -1 ) m_pListBoxEx->MoveItemDown( m_pListBoxEx->GetCurSel() );
break;
}
} // CListBoxExBuddy::DoClick
///*-
// FUNCTION NAME: CListBoxExBuddy::OnNcMouseMove
//
// DESCRIPTION: Called when it receives a WM_NCMOUSEMOVE message.
//
// PARAMETER(S):
// nHitTest:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Hit test
//
// point:
// TYPE: CPoint class
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Point of action
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::OnNcMouseMove( UINT nHitTest,
CPoint point )
{
// Redraw the affected button
InvalidateButton( m_iButton, FALSE );
m_iButton = FindButton( point );
InvalidateButton( m_iButton, TRUE );
// Call base
CWnd::OnNcMouseMove(nHitTest, point);
} // CListBoxExBuddy::OnNcMouseMove
///////////////////////////////////////////////
// Tooltip Management //
///////////////////////////////////////////////
///*-
// FUNCTION NAME: CListBoxExBuddy::CreateTooltips
//
// DESCRIPTION: Creates the tooltip control and assigns the ttols to it.
//
// PARAMETER(S): None.
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::CreateTooltips()
{
// Create the tooltip
m_ToolTip.Create( this );
// Set tip common data
TOOLINFO ttInfo;
ttInfo.cbSize = sizeof( TOOLINFO );
ttInfo.uFlags = TTF_SUBCLASS;
ttInfo.hwnd = m_hWnd;
ttInfo.rect = CRect( 0, 0, 0, 0 ); // OnSize will resize it
ttInfo.hinst = NULL;
ttInfo.lpszText = LPSTR_TEXTCALLBACK;
ttInfo.lParam = 0;
// Add tooltips for each button
for ( int iTip = 0; iTip < __BMP_NUMBTN; iTip++ )
{
ttInfo.uId = iTip+1;
m_ToolTip.SendMessage( TTM_ADDTOOL, 0, (LPARAM)&ttInfo );
m_ToolTip.Activate( TRUE );
}
} // CListBoxExBuddy::CreateTooltips()
///*-
// FUNCTION NAME: CListBoxExBuddy::SetTipText
//
// DESCRIPTION: Sett the appropriate tip text for the button
//
// PARAMETER(S):
// nID:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Button identifier (index)
//
// szTipText:
// TYPE: TCHAR
// MODE: In
// MECHANISM: By pointer
// DESCRIPTION: Pointer to a preallocated buffer to be filled
// with the tip text
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::SetTipText( UINT nID,
LPTSTR szTipText )
{
TCHAR *aszTips[] = { _T("New"),
_T("Delete"),
_T("Move Up"),
_T("Move Down") };
// Set tooltip text
if ( nID < __BMP_NUMBTN )
{
_tcscpy( szTipText, aszTips[ nID ] );
}
} // CListBoxExBuddy::SetTipText()
///////////////////////////////////////////////
// Other Messages //
///////////////////////////////////////////////
///*-
// FUNCTION NAME: CListBoxExBuddy::OnSize
//
// DESCRIPTION: Called when it receives a WM_SIZE message.
//
// PARAMETER(S):
// nType:
// TYPE: unsigned int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Specifies the type of resizing requested.
//
// cx:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: New width of the client area
//
// cy:
// TYPE: int
// MODE: In
// MECHANISM: By value
// DESCRIPTION: New height of the client area
//
// RETURN: None.
//
// NOTES: None.
//+*/
void CListBoxExBuddy::OnSize( UINT nType,
int cx,
int cy )
{
// Get aware of the size of the client area
CRect rcClient;
GetClientRect( &rcClient );
// This is used to center the button bitmap
int nBmpTopY = (rcClient.Height() - __BMP_HEIGHT) / 2;
// Update buttons positions
TOOLINFO ttInfo;
for ( int iIndex = 0; iIndex < __BMP_NUMBTN; iIndex++ )
{
m_arcButtons[ iIndex ].top = nBmpTopY;
m_arcButtons[ iIndex ].left = cx - (__BMP_NUMBTN-iIndex)*__BMP_BTNWID;
m_arcButtons[ iIndex ].bottom = __BMP_HEIGHT + nBmpTopY;
m_arcButtons[ iIndex ].right = cx - (__BMP_NUMBTN-iIndex-1)*__BMP_BTNWID;
// Resize tooltip area
ttInfo.cbSize = sizeof( TOOLINFO );
ttInfo.hwnd = m_hWnd;
ttInfo.uId = iIndex+1;
ttInfo.rect = m_arcButtons[ iIndex ];
m_ToolTip.SendMessage( TTM_NEWTOOLRECT, 0, (LPARAM)&ttInfo );
}
// Call base
CWnd::OnSize( nType, cx, cy );
} // CListBoxExBuddy::OnSize
///*-
// FUNCTION NAME: CListBoxExBuddy::OnNotify
//
// DESCRIPTION: Called when it receives a WM_NOTIFY message.
//
// PARAMETER(S):
// wParam:
// TYPE: WPARAM
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Identifier of the common control sending the message.
//
// lParam:
// TYPE: LPARAM
// MODE: In
// MECHANISM: By value
// DESCRIPTION: Address of an NMHDR structure that contains
// the notification code and additional information.
//
// pResult:
// TYPE: LRESULT
// MODE: In
// MECHANISM: By address
// DESCRIPTION: Message result.
//
// RETURN: None.
//
// NOTES: None.
//+*/
BOOL CListBoxExBuddy::OnNotify( WPARAM wParam,
LPARAM lParam,
LRESULT *pResult )
{
UINT nCode = ((NMHDR *)lParam)->code;
// Get tooltip notification
if ( nCode == TTN_GETDISPINFO )
{
UINT nID = ((NMHDR *)lParam)->idFrom - 1;
SetTipText( nID,
((NMTTDISPINFO *)lParam)->szText );
return TRUE;
}
// Call base
return CWnd::OnNotify( wParam, lParam, pResult );
} // CListBoxExBuddy::OnNotify
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -