📄 baselist.cpp
字号:
// BaseList.cpp : implementation file
//
#include "stdafx.h"
#include "BaseList.h"
#include "a1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ID_MYEDITPH 134
/////////////////////////////////////////////////////////////////////////////
// CBaseList
CBaseList::CBaseList()
{
m_poplist=NULL;
m_iStopDestroy=0;
m_bSetValue=false;
m_iEditedIndex=-2;
m_Next=false;
m_bWaitPopList=false;
m_iEditedIndex=0;
this->m_nItem=0;
this->m_nSubItem=0;
}
CBaseList::~CBaseList()
{
m_poplist->DestroyWindow();
delete this->m_poplist;
this->m_poplist=NULL;
}
BEGIN_MESSAGE_MAP(CBaseList, CListCtrl)
//{{AFX_MSG_MAP(CBaseList)
ON_WM_LBUTTONDOWN()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
ON_MESSAGE(SETNEXTITEMPHFOCUS,SetNextItemFocus)
ON_MESSAGE(MYMESSAGE_EDIT_MOVED,OnParentMoved)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBaseList message handlers
/////////////////////////////////////////////////////////////////////////////
// CBaseEdit
CBaseEdit::CBaseEdit()
{
m_bInputValid = TRUE;
this->m_breturn=false;
this->m_besc=false;
this->kc_number=0;
this->m_dSetFocusValue=0;
}
CBaseEdit::~CBaseEdit()
{
}
BEGIN_MESSAGE_MAP(CBaseEdit, CEdit)
//{{AFX_MSG_MAP(CBaseEdit)
ON_WM_CREATE()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBaseEdit message handlers
void CBaseList::PreSubclassWindow()
{
ModifyStyle(LVS_EDITLABELS, 0L); //禁止标题编辑
ModifyStyle(0L, LVS_REPORT); //设为Report类型
ModifyStyle(0L, LVS_SHOWSELALWAYS); //始终高亮度被选中的表项
SetBkColor(RGB(0,200,200));
SetTextBkColor(RGB(0,200,200));
ModifyStyle(LVS_OWNERDRAWFIXED ,0L);
SetExtendedStyle( LVS_EX_FULLROWSELECT | //允许整行选中
LVS_EX_HEADERDRAGDROP | //允许整列拖动
LVS_EX_GRIDLINES | //画出网格线
LVS_EX_FLATSB //扁平风格的滚动条
);
CListCtrl::PreSubclassWindow();
}
void CBaseList::OnLButtonDown(UINT nFlags, CPoint point)
{
POSITION pos;
bool flag=false;
int i;
int nItem,nSubItem;
BOOL bSelected = FALSE;
int mark=this->GetSelectionMark();
// if(mark<0) goto defalt_session;
// 检查是否有Item被选中,没有时不进入编辑
pos = GetFirstSelectedItemPosition();
if( pos )
{
// 得到被点击的Item
LVHITTESTINFO testinfo;
testinfo.pt.x = point.x;
testinfo.pt.y = point.y; //点击时的鼠标位置
testinfo.flags = LVHT_ONITEMLABEL; //点击的必须是标题
if( SubItemHitTest(&testinfo)<0 )
goto defalt_session; //没有点在有效区域,不进入编辑
nItem = testinfo.iItem; //被点击表项的行号
nSubItem = testinfo.iSubItem; //被点击表项的列号
if(nItem!=mark)goto defalt_session;
// 判断得到的单元格是否允许被编辑,同时,记录被编辑的编号
for(i=0;i<this->m_iColumnEdited.GetCount();i++)
{
if(nSubItem==m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
flag=true;
this->m_iEditedIndex=i;
}
}
if(!flag)
goto defalt_session;
// 开始编辑
m_nItem=nItem;
m_nSubItem=nSubItem;
BeginEdit();
return;
}
defalt_session:
CListCtrl::OnLButtonDown(nFlags, point);
}
BOOL CBaseList::BeginEdit()
{
m_edit.ShowWindow(SW_SHOW);
TRACE("BOOL CBaseList::MyBeginEdit()\n");
// 得到被编辑表项的区域
CRect rect;
if( GetSubItemRect(m_nItem, m_nSubItem, LVIR_LABEL, rect)==FALSE )
return FALSE;
rect.right-=1;
rect.left+=1;
rect.top+=1;
rect.bottom-=1;
SetValueUnChange("");
m_edit.MoveWindow(&rect);
CString txtItem;
txtItem=this->GetItemText(m_nItem,m_nSubItem);
// 取出的文字填写到编辑控件
SetValueUnChange(txtItem);
this->OnEditSetFocus(m_nItem,m_nSubItem);
m_edit.SetFocus();
m_edit.SetSel( 0, -1 );
m_edit.Invalidate();
m_edit.ShowWindow(SW_SHOW);
return TRUE;
}
void CBaseList::EndEdit( BOOL bValidate )
{
CString txtItem;
m_edit.GetWindowText(txtItem);
if(bValidate)
SetItemText(m_nItem, m_nSubItem, txtItem);
m_edit.ShowWindow(SW_HIDE);
SetValueUnChange("");
if(this->m_poplist!=NULL)
m_poplist->ShowWindow(SW_HIDE);
}
int CBaseEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// 改变字体
LOGFONT logfont;
logfont.lfHeight = -12;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = 400;
logfont.lfItalic = 0;
logfont.lfUnderline = 0;
logfont.lfStrikeOut = 0;
logfont.lfCharSet = 134;
logfont.lfOutPrecision = 0;
logfont.lfClipPrecision = 0;
logfont.lfQuality = 0;
logfont.lfPitchAndFamily = 2;
strcpy( logfont.lfFaceName, "宋体" );
m_font.DeleteObject();
if( m_font.CreateFontIndirect(&logfont) )
SetFont( &m_font );
return 0;
}
void CBaseEdit::OnSetFocus(CWnd* pOldWnd)
{
m_bInputValid=true;
CBaseList *parent = (CBaseList *)GetParent();
parent->OnEditSetFocus(parent->m_nItem,parent->m_nSubItem);
CEdit::OnSetFocus(pOldWnd);
}
void CBaseEdit::OnChange()
{
CBaseList *parent = (CBaseList *)GetParent();
if(parent->m_bSetValue)return;
parent->OnNewEditChange(parent->m_nItem,parent->m_nSubItem);
}
void CBaseEdit::OnKillFocus(CWnd* pNewWnd)
{
TRACE("void CBaseEdit::OnKillFocus(CWnd* pNewWnd) \n");
// 得到父窗口,并通知父窗口结束编辑过程
CBaseList *parent = (CBaseList *)GetParent();
HWND hwnd=pNewWnd->GetSafeHwnd();
if(pNewWnd!=NULL)
{
if(hwnd==parent->m_poplist->GetSafeHwnd()||
hwnd==parent->m_poplist->GetDlgItem(ID_POP_LISTCTRL)->GetSafeHwnd())
afxDump<<"\n ID_POP_LISTCTRL CBaseEdit::OnKillFocus";
else
{
// parent->OnEditKillFocus(
// parent->m_nItem,parent->m_nSubItem);
if(this->m_bInputValid)
parent->OnEditReturn(parent->m_nItem,parent->m_nSubItem);
else
parent->EndEdit(false);
}
}
CEdit::OnKillFocus(pNewWnd);
}
BOOL CBaseEdit::PreTranslateMessage(MSG* pMsg)
{
//拦截ESC键解释为WM_KILLFOCUS消息
if( pMsg->message==WM_KEYDOWN )
{
if(pMsg->wParam==27) //ESC键
{
pMsg->message=MYMESSAGE_EDIT_ESC;
}
}
else if(pMsg->message==WM_CHAR)
{
CBaseList *parent = (CBaseList *)GetParent();
parent->PreOnEditChar(parent->m_nItem,parent->m_nSubItem,pMsg->wParam);
}
return CEdit::PreTranslateMessage(pMsg);
}
void CBaseList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
this->EndEdit(true);
m_edit.ShowWindow(SW_HIDE);
if(m_poplist!=NULL)m_poplist->ShowWindow(SW_HIDE);
UpdateWindow();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CBaseList::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
this->EndEdit(true);
m_edit.ShowWindow(SW_HIDE);
if(m_poplist!=NULL)m_poplist->ShowWindow(SW_HIDE);
UpdateWindow();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
BOOL CBaseList::SetNextItemFocus()
{TRACE("BOOL CBaseList::SetNextItemFocus()\n");
if(m_iEditedIndex==-2)return false;
m_Next=true;
int r;
r=GetItemCount();
m_iEditedIndex++;
if(m_iEditedIndex>this->m_iColumnEdited.GetCount()-1)
{
m_nItem++;
if(m_nItem>(r-1))
{
InsertItem(m_nItem, "" );
}
m_iEditedIndex=0;
}
m_nSubItem=m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(m_iEditedIndex));
this->BeginEdit();
return true;
}
void CBaseList::OnKillFocus(CWnd* pNewWnd)
{
CListCtrl::OnKillFocus(pNewWnd);
}
void CBaseList::OnNewEditChange(const int& item,const int& sub_item)
{
}
bool CBaseList::SetSubItemCanEdited(const int& subitem)
{
int n=m_iColumnEdited.GetCount();
if(subitem>=this->GetSubItemNumber()||subitem<0)return false;
bool flag=false;
for(int i=0;i<this->m_iColumnEdited.GetCount();i++)
{
if(subitem<this->m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
this->m_iColumnEdited.InsertBefore(m_iColumnEdited.FindIndex(i),subitem);
flag=true;
}
}
if(!flag)
m_iColumnEdited.AddTail(subitem);
return true;
}
int CBaseList::GetSubItemNumber()
{
CHeaderCtrl *ct=GetHeaderCtrl();
return ct->GetItemCount();
}
int CBaseList::GetCountCanEdited()
{
return this->m_iColumnEdited.GetCount();
}
bool CBaseList::SetSubItemUnEdited(const int& subitem)
{
if(subitem>=this->GetSubItemNumber()||subitem<0)return false;
bool flag=false;
for(int i=0;i<this->m_iColumnEdited.GetCount();i++)
{
if(subitem==this->m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
this->m_iColumnEdited.RemoveAt(m_iColumnEdited.FindIndex(i));
flag=true;
}
}
return flag;
}
void CBaseList::OnEditKillFocus(const int& item, const int& subitem)
{
}
void CBaseList::OnEditSetFocus(const int& item, const int& subitem)
{
m_edit.m_bInputValid=true;
}
BOOL CBaseList::PreTranslateMessage(MSG* pMsg)
{
HWND hwnd;
if(pMsg->hwnd!=NULL)
hwnd=pMsg->hwnd;
if(hwnd==this->m_edit.GetSafeHwnd()&&
pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==13)
{
OnEditReturn(this->m_nItem,this->m_nSubItem);
pMsg->hwnd=GetSafeHwnd();
pMsg->message=SETNEXTITEMPHFOCUS;
}
else if(pMsg->wParam==VK_UP||pMsg->wParam==VK_DOWN||
pMsg->wParam==VK_NEXT||pMsg->wParam==VK_PRIOR)
{
this->OnEditUDNPMessage(this->m_nItem,this->m_nSubItem,pMsg->wParam);
}
}
else if(hwnd==this->GetSafeHwnd()&&pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam== VK_DELETE)this->DeleteSelected();
else if(pMsg->wParam==13)
{
this->m_iEditedIndex=-1;
this->m_nItem=this->GetSelectionMark();
if(m_nItem<0||m_nItem>=this->GetItemCount())m_nItem=0;
pMsg->hwnd=GetSafeHwnd();
pMsg->message=SETNEXTITEMPHFOCUS;
}
}
return CListCtrl::PreTranslateMessage(pMsg);
}
bool CBaseList::OnEditReturn(const int& item, const int& subitem)
{
if(!IsTheSubItemCanEdited(subitem)) return false;
this->EndEdit(true);
return true;
}
bool CBaseList::IsTheSubItemCanEdited(const int& subitem)
{
if(subitem>=this->GetSubItemNumber()||subitem<0)return false;
if(m_iColumnEdited.Find(subitem)==NULL)
return false;
return true;
}
bool CBaseList::SetTheEditFocus(const int& item, const int& subitem)
{
/* TRACE("CBaseList::SetTheEditFocus(in\n");
if(subitem<=-1) return false;
this->m_nItem=item;
subitem=subitem-1;
this->SetNextItemFocus();
return true;
TRACE("\n");*/
return true;
}
bool CBaseList::DeleteSelected()
{
LVITEM* item=new LVITEM;
int number=this->GetSelectedCount();
for(int i=this->GetItemCount()-1;i>=0;i--)
{
this->m_nItem=i;
item->iItem=i;
item->mask=LVIF_STATE ;
item->stateMask=LVIS_SELECTED ;
GetItem(item);
if(item->state==LVIS_SELECTED)
{
DeleteItem(i);
}
}
if(GetItemCount()==0)
InsertItem(0,"");
this->m_nItem=0;
Invalidate();
return true;
}
void CBaseList::SetValueUnChange(CString str)
{
TRACE("void CBaseList::SetValueUnChange(CString sql)\n");
m_bSetValue=true;
this->m_edit.SetWindowText(str);
m_bSetValue=false;
}
void CBaseList::OnParentMoved()
{
TRACE("void CListXSDJ::OnParentMoved()\n");
if(m_poplist==NULL)return ;
if(m_edit.m_hWnd==NULL)return ;
m_edit.GetWindowRect(&this->m_rectWindow);
if(this->m_poplist->IsWindowVisible())
{
this->m_poplist->SetShowPosition(m_rectWindow,&m_edit);
this->m_poplist->ShowListPop();
}
}
void CBaseList::OnESC(const int& item, const int& subitem)
{
m_edit.m_bInputValid=false;
}
bool CBaseList::Create(DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID)
{
BOOL b=CListCtrl::Create(dwStyle,rect,pParentWnd,nID);
// 创建编辑控件
if( m_edit.Create(WS_CHILD|ES_AUTOHSCROLL , CRect(0,0,0,0), this, ID_MYEDITPH)==FALSE )
return FALSE;
//创建显示提示窗口
m_poplist=new CListCtrlPop;
ASSERT(m_poplist!=NULL);
m_poplist->Create(this);
return b;
}
void CBaseList::OnEditUDNPMessage(const int& item, const int& subitem,UINT key)
{
}
void CBaseList::PreOnEditChar(const int& item, const int& subitem,UINT& key)
{
}
LRESULT CBaseEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==MYMESSAGE_EDIT_ESC)
{
CBaseList *parent = (CBaseList *)GetParent();
parent->OnESC(parent->m_nItem,parent->m_nSubItem);
this->ShowWindow(SW_HIDE);
}
return CEdit::WindowProc(message,wParam,lParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -