📄 editlist.cpp
字号:
//author : Jarry
//E-mail : lansingk@online.sh.cn
// EditList.cpp : implementation file
//
#include "stdafx.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()
{
nLastRow = 0;
nLastColumn = 0;
bLoadImage = false;
}
CEditList::~CEditList()
{
}
BEGIN_MESSAGE_MAP(CEditList, CListCtrl)
//{{AFX_MSG_MAP(CEditList)
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEditList message handlers
int CEditList::HitTestex(CPoint &point, int *col) const
{
int colnum = 0;
int row = HitTest(point);
if (col)
*col = 0;
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;
}
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;
rect.right = rect.left + GetColumnWidth( nCol );
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;
}
CComboBox* CEditList::SelectSubLabel(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;
rect.right = rect.left + GetColumnWidth( nCol );
if( rect.right > rcClient.right) rect.right = rcClient.right;
rect.bottom += 100;
dwStyle |= WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL;
CComboBox *pList = new CInplaceList(nItem, nCol);
pList->Create( dwStyle, rect, this, IDC_IPLIST );
return pList;
}
void CEditList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if( GetFocus() != this ) SetFocus();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CEditList::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if( GetFocus() != this ) SetFocus();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CEditList::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
LV_ITEM *pLvItem = &pDispInfo->item;
if (pLvItem->pszText != NULL)
{
SetItemText(pLvItem->iItem, pLvItem->iSubItem, pLvItem->pszText);
}
*pResult = true;
}
void CEditList::OnLButtonDown(UINT nFlags, CPoint point)
{
int index,colnum;
// CListCtrl::OnLButtonDown(nFlags, point);
if( ( index = HitTestex( point, &colnum )) != -1 )
{
UINT flag = LVIS_FOCUSED;
// if( (GetItemState( index, flag ) & flag) == flag) //DON'T USE , ensure one-click can work
{
SetItemText(nLastRow,0,"");
SetItemText(index,0,">");
if( colnum > 0)
{
if( GetWindowLong(m_hWnd, GWL_STYLE) & LVS_EDITLABELS )
if (colnum == 2)
SelectSubLabel( index, colnum );
else if ((colnum != COL_NULL) && (colnum != COL_KEY))
EditSubLabel( index, colnum );
}
SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED,
LVIS_SELECTED | LVIS_FOCUSED);
nLastRow = index;
nLastColumn = colnum;
}
}
}
void CEditList::OnLButtonUp(UINT nFlags, CPoint point)
{
int index,colnum;
LVITEM lvItem;
// CListCtrl::OnLButtonUp(nFlags, point);
if( ( index = HitTestex( point, &colnum )) != -1 )
{
if ((index == nLastRow) && (colnum == nLastColumn) && ((colnum == COL_NULL) || (colnum == COL_KEY)))
{
lvItem.mask = LVIF_IMAGE;
lvItem.iItem = index;
lvItem.iSubItem = colnum;
GetItem(&lvItem);
if (lvItem.iImage == 0)
{
lvItem.iImage = 1;
SetItem(&lvItem);
lvItem.mask = LVIF_TEXT;
lvItem.pszText = "NO";
SetItem(&lvItem);
}
else
{
lvItem.iImage = 0;
SetItem(&lvItem);
lvItem.mask = LVIF_TEXT;
lvItem.pszText = "YES";
SetItem(&lvItem);
}
if ((colnum == COL_NULL) && (lvItem.iImage == 0)) //If it's null , can't be a key
{
lvItem.mask = LVIF_IMAGE;
lvItem.iSubItem = COL_KEY;
lvItem.iImage = 1; //Must be not key
SetItem(&lvItem);
lvItem.mask = LVIF_TEXT;
lvItem.pszText = "NO";
SetItem(&lvItem);
}
if ((colnum == COL_KEY) && (lvItem.iImage == 0)) //If it's a key , can't be null
{
lvItem.mask = LVIF_IMAGE;
lvItem.iSubItem = COL_NULL;
lvItem.iImage = 1; //Must be not null
SetItem(&lvItem);
lvItem.mask = LVIF_TEXT;
lvItem.pszText = "NO";
SetItem(&lvItem);
}
}
}
}
void CEditList::LoadImageList()
{
SetExtendedStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_GRIDLINES);
if (!bLoadImage)
{
lstImage.Create(16,16, FALSE, 16, 0);
HICON hIcon = ::LoadIcon (AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_ICON_YES));
lstImage.Add(hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_ICON_NO));
lstImage.Add(hIcon);
bLoadImage = true;
}
SetImageList(&lstImage,LVSIL_SMALL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -