📄 editlist.cpp
字号:
// EditList.cpp : implementation file
//
#include "stdafx.h"
#include "commision.h"
#include "EditList.h"
#include "InPlaceEdit.h"
#include "InPlaceList.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEditList
CEditList::CEditList()
{
}
CEditList::~CEditList()
{
}
BEGIN_MESSAGE_MAP(CEditList, CListCtrl)
//{{AFX_MSG_MAP(CEditList)
ON_WM_PAINT()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEditList message handlers
int CEditList::HitTestEx(CPoint &point, int *col) const
{
int colnum = 0;
int row = HitTest( point, NULL );
if( col ) *col = 0;
// Make sure that the ListView is in LVS_REPORT
if( (GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT )
return row;
// Get the top and bottom row visible
row = GetTopIndex();
int bottom = row + GetCountPerPage();
if( bottom > GetItemCount() )
bottom = GetItemCount();
// Get the number of columns
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
// Loop through the visible rows
for( ;row <= bottom;row++)
{
// Get bounding rect of item and check whether point falls in it.
CRect rect;
GetItemRect( row, &rect, LVIR_BOUNDS );
if( rect.PtInRect(point) )
{
// Now find the column
for( colnum = 0; colnum < nColumnCount; colnum++ )
{
int colwidth = GetColumnWidth(colnum);
if( point.x >= rect.left
&& point.x <= (rect.left + colwidth ) )
{
if( col ) *col = colnum;
return row;
}
rect.left += colwidth;
}
}
}
return -1;
}
void CEditList::OnPaint()
{
// CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// First let the control do its default drawing.
const MSG *msg = GetCurrentMessage();
DefWindowProc( msg->message, msg->wParam, msg->lParam );
// Draw the lines only for LVS_REPORT mode
if( (GetStyle() & LVS_TYPEMASK) == LVS_REPORT )
{
// Get the number of columns
CClientDC dc(this );
CPen pen(PS_SOLID,1,RGB(160,160,160));
CPen* oldPen = dc.SelectObject(&pen);
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
// The bottom of the header corresponds to the top of the line
RECT rect;
pHeader->GetClientRect( &rect );
int top = rect.bottom;
if( !GetItemRect( 0, &rect, LVIR_BOUNDS ))
return;
int height = rect.bottom - rect.top;
int drawheight = top;
for(int i = 1; i <= GetItemCount()/*GetCountPerPage()*/; i++ )
{
drawheight += height;
}
// Now get the client rect so we know the line length and
// when to stop
GetClientRect( &rect );
// The border of the column is offset by the horz scroll
int borderx = 0 - GetScrollPos( SB_HORZ );
for( i = 0; i < nColumnCount; i++ )
{
// Get the next border
borderx += GetColumnWidth( i );
// if next border is outside client area, break out
if( borderx >= rect.right ) break;
// Draw the line.
dc.MoveTo( borderx-1, top);
dc.LineTo( borderx-1, drawheight/*rect.bottom*/ );
}
// Draw the horizontal grid lines
// First get the height
// GetClientRect( &rect );
// int width = rect.right;
for( i = 1; i <= GetItemCount()/*GetCountPerPage()*/; i++ )
{
dc.MoveTo( 0, top + height*i);
dc.LineTo( borderx/*width*/, top + height*i );
}
dc.SelectObject(oldPen);
}
// Do not call CListCtrl::OnPaint() for painting messages
}
// EditSubLabel - Start edit of a sub item label
// Returns - Temporary pointer to the new edit control
// nItem - The row index of the item to edit
// nCol - The column of the sub item.
CEdit* CEditList::EditSubLabel(int nItem, int nCol)
{
// The returned pointer should not be saved
// Make sure that the item is visible
if( !EnsureVisible( nItem, TRUE ) ) return NULL;
// Make sure that nCol is valid
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
if( nCol >= nColumnCount || GetColumnWidth(nCol) < 5 )
return NULL;
// Get the column offset
int offset = 0;
for( int i = 0; i < nCol; i++ )
offset += GetColumnWidth( i );
CRect rect;
GetItemRect( nItem, &rect, LVIR_BOUNDS );
// Now scroll if we need to expose the column
CRect rcClient;
GetClientRect( &rcClient );
if( offset + rect.left < 0 || offset + rect.left > rcClient.right )
{
CSize size;
size.cx = offset + rect.left;
size.cy = 0;
Scroll( size );
rect.left -= size.cx;
}
// Get Column alignment
LV_COLUMN lvcol;
lvcol.mask = LVCF_FMT;
GetColumn( nCol, &lvcol );
DWORD dwStyle ;
if((lvcol.fmt&LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
dwStyle = ES_LEFT;
else if((lvcol.fmt&LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
dwStyle = ES_RIGHT;
else dwStyle = ES_CENTER;
rect.left += offset+4;
rect.right = rect.left + GetColumnWidth( nCol ) - 3 ;
if( rect.right > rcClient.right) rect.right = rcClient.right;
dwStyle |= WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL;
CEdit *pEdit = new CInPlaceEdit(nItem, nCol, GetItemText( nItem, nCol ));
pEdit->Create( dwStyle, rect, this, IDC_IPEDIT );
return pEdit;
}
void CEditList::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if( GetFocus() != this ) SetFocus();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CEditList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if( GetFocus() != this ) SetFocus();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CEditList::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
LV_ITEM *plvItem = &plvDispInfo->item;
if (plvItem->pszText != NULL)
{
SetItemText(plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
}
*pResult = FALSE;
}
void CEditList::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
return;
int index;
CListCtrl::OnLButtonDown(nFlags, point);
int colnum;
if( ( index = HitTestEx( point, &colnum )) != -1 )
{
UINT flag = LVIS_FOCUSED;
if( (GetItemState( index, flag ) & flag) == flag )
{
// Add check for LVS_EDITLABELS
if(index == 0 && colnum == 0)
// {
// if( GetWindowLong(m_hWnd, GWL_STYLE) & LVS_EDITLABELS )
// EditSubLabel( index, colnum );
// }
// else
{
// CStringList lstItems;
// lstItems.AddTail( "First Item");
// lstItems.AddTail( "Second Item");
// lstItems.AddTail( "Third Item");
// lstItems.AddTail( "Fourth Item");
// lstItems.AddTail( "Fifth Item");
// lstItems.AddTail( "Sixth Item");
ShowInPlaceList( index, colnum, m_lstItems, 2 );
}
}
else
SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED ,
LVIS_SELECTED | LVIS_FOCUSED);
}
}
CComboBox* CEditList::ShowInPlaceList(int nItem, int nCol, CStringList &lstItems, int nSel)
{
// The returned pointer should not be saved
// Make sure that the item is visible
if( !EnsureVisible( nItem, TRUE ) ) return NULL;
// Make sure that nCol is valid
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
if( nCol >= nColumnCount || GetColumnWidth(nCol) < 10 )
return NULL;
// Get the column offset
int offset = 0;
for( int i = 0; i < nCol; i++ )
offset += GetColumnWidth( i );
CRect rect;
GetItemRect( nItem, &rect, LVIR_BOUNDS );
// Now scroll if we need to expose the column
CRect rcClient;
GetClientRect( &rcClient );
if( offset + rect.left < 0 || offset + rect.left > rcClient.right )
{
CSize size;
size.cx = offset + rect.left;
size.cy = 0;
Scroll( size );
rect.left -= size.cx;
}
rect.left += offset+4;
rect.right = rect.left + GetColumnWidth( nCol ) - 3 ;
int height = rect.bottom - rect.top;
rect.bottom += 5 * height;
if( rect.right > rcClient.right) rect.right = rcClient.right;
DWORD dwStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL
|CBS_DROPDOWNLIST|CBS_DISABLENOSCROLL;
CComboBox *pList = new CInPlaceList(nItem, nCol, &lstItems, nSel);
pList->Create( dwStyle, rect, this, IDC_IPEDIT );
pList->SetItemHeight( -1, height);
pList->SetHorizontalExtent( GetColumnWidth( nCol ));
return pList;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -