clsitctrl

1  、获取行列

CHeaderCtrl   *pmyHeaderCtrl= pList->GetHeaderCtrl(); //获取表头  ;
int   m_cols   = pmyHeaderCtrl-> GetItemCount(); //获取列数  

int   m_rows =pList->GetItemCount();  //获取行数  

2. 可编辑的clistctrl

当你点击击CListCtrl控件时,将一个CEdit(编辑框)控件移动到你所双击的列中(CEdit控件中显示你所单击列的内容)所在的位置。并使CEdit控件获取焦点。然后,你可以修改CEdit中的内容。当你的鼠标点击其他地方,CEdit将失去焦点,同时,我们将CEdit中的内容更新到CListCtrl中,从而完成了对CListCtrl的子项的修改。

MyListCtrl.h

#pragma once
#include "MyEdit.h"
// CMyListCtrl


class CMyListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CMyListCtrl)
public:
CMyListCtrl();
virtual ~CMyListCtrl();
protected:
DECLARE_MESSAGE_MAP()
afx_msg LRESULT OnEndEdit(WPARAM wParam,LPARAM lParam);
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg LRESULT OnMyKeyDown(WPARAM wParam,LPARAM lParam);
CMyEdit m_Edit_ctlItem;
int m_nItem;
int m_nSubItem;
CFont m_FontItem;
void EditSubEdit(int nItem, int nSubItem);
};

MyListCtrl.cpp

// MyListCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "MyListCtrl.h"
#define MY_LVN_ENDEDIT WM_USER+100
#define MY_LVN_KEYDOEN WM_USER+101//响应键盘左右键
// CMyListCtrl
IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)
CMyListCtrl::CMyListCtrl()
{

}
CMyListCtrl::~CMyListCtrl()
{
}

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
ON_WM_LBUTTONDOWN()
ON_MESSAGE(MY_LVN_ENDEDIT, OnEndEdit)
ON_MESSAGE(MY_LVN_KEYDOEN,OnMyKeyDown)
END_MESSAGE_MAP()
// CMyListCtrl message handlers


void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

LVITEM lvi = {0};
lvi.mask = LVIF_STATE;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.iItem = lpDrawItemStruct->itemID;
BOOL bGet = GetItem(&lvi);

BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) && ((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS))));

CRect rcBack = lpDrawItemStruct->rcItem;

if( bHighlight ) //高亮显示;
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
}
else
{
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_WINDOW)));
}
//绘制文本
CString strText=_T("");
CRect rcItem;
if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
{
for (int i = 0; i < GetHeaderCtrl()-> GetItemCount();i++)
{            
if ( !GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem ))
continue;
strText = GetItemText( lpDrawItemStruct->itemID, i );
pDC->DrawText(strText,strText.GetLength(), &rcItem, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_BOTTOM);
}
}
// TODO:  Add your code to draw the specified item
}

void CMyListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CListCtrl::OnLButtonDown(nFlags, point);
LVHITTESTINFO lvhti;
lvhti.pt = point;
m_nItem =SubItemHitTest(&lvhti);
if (m_nItem == -1) return;
m_nSubItem = lvhti.iSubItem;
EditSubEdit(m_nItem,m_nSubItem);

}
LRESULT CMyListCtrl::OnEndEdit(WPARAM wParam,LPARAM lParam)

{
CString strText;
m_Edit_ctlItem.GetWindowText(strText);
SetItemText(m_nItem,m_nSubItem,strText);
m_Edit_ctlItem.ShowWindow(SW_HIDE);
if(2==m_nSubItem)
{
CString NextText=GetItemText(m_nItem,3);

if(NextText!=_T("")&&strText!=_T(""))
{
int BuyPrice=_ttoi(strText);
int SellPrice=_ttoi(NextText);
float rate =(SellPrice-BuyPrice)/float(BuyPrice)*100;
CString csrate;
csrate.Format(_T("%.2f%%"),rate);
SetItemText(m_nItem,4,csrate);
}


}
if(3==m_nSubItem)
{
CString NextText=GetItemText(m_nItem,2);
if(NextText!=_T("")&&strText!=_T(""))
{
int BuyPrice=_ttoi(NextText);
int SellPrice=_ttoi(strText);
float rate =(SellPrice-BuyPrice)/float(BuyPrice)*100.0;
CString csrate;
csrate.Format(_T("%.2f%%"),rate);
SetItemText(m_nItem,4,csrate);
}
}
return 0;

}
void CMyListCtrl::EditSubEdit(int nItem, int nSubItem)
{
m_nItem=nItem;
m_nSubItem=nSubItem;
CRect rcCtrl;
GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rcCtrl);
if(m_nSubItem!=4)
{
if (m_Edit_ctlItem.m_hWnd == NULL)
{
m_Edit_ctlItem.Create(ES_AUTOHSCROLL|WS_CHILD|ES_LEFT|ES_WANTRETURN|WS_BORDER,CRect(0,0,0,0),this,99);
m_Edit_ctlItem.ShowWindow(SW_HIDE);


m_FontItem.CreateStockObject(DEFAULT_GUI_FONT);
m_Edit_ctlItem.SetFont(&m_FontItem);
m_FontItem.DeleteObject();


}
CString strItem=GetItemText(nItem,nSubItem);
m_Edit_ctlItem.MoveWindow(&rcCtrl);
m_Edit_ctlItem.ShowWindow(SW_SHOW);
m_Edit_ctlItem.SetWindowText(strItem);
m_Edit_ctlItem.SetFocus();
m_Edit_ctlItem.SetSel(-1); 
}
}
LRESULT CMyListCtrl::OnMyKeyDown(WPARAM wParam,LPARAM lParam)
{

if(wParam==1)
{
if(m_nSubItem>0)
{
if(m_nSubItem==5)
EditSubEdit(m_nItem,m_nSubItem-2);
else
  EditSubEdit(m_nItem,m_nSubItem-1);
}   
}
else
{
int nAllitem=GetHeaderCtrl()->GetItemCount();
if(m_nSubItem<nAllitem-1)
{
if(m_nSubItem==3)
EditSubEdit(m_nItem,m_nSubItem+2);
else
           EditSubEdit(m_nItem,m_nSubItem+1);
 
}
}
return 0;
}
MyEdit.h

#pragma once
// CMyEdit
class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit)
public:
CMyEdit();
virtual ~CMyEdit();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};

MyEdit.cpp

// MyEdit.cpp : implementation file
//
#include "stdafx.h"
#include "MyEdit.h"
#define MY_LVN_ENDEDIT WM_USER+100
#define MY_LVN_KEYDOEN WM_USER+101
// CMyEdit

IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

CMyEdit::CMyEdit()
{
}
CMyEdit::~CMyEdit()
{
}

BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_KILLFOCUS()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
// CMyEdit message handlers
void CMyEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
CWnd* pParent = this->GetParent();
::SendMessage(pParent->GetSafeHwnd(),MY_LVN_ENDEDIT,0,0);
// TODO: Add your message handler code here
}
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch (nChar)
{
case VK_LEFT :
{
::SendMessage(this->GetParent()->GetSafeHwnd(),MY_LVN_ENDEDIT,0,0);
::SendMessage(this->GetParent()->GetSafeHwnd(),MY_LVN_KEYDOEN,1,0);
return;
}
case VK_RIGHT  :
{
::SendMessage(this->GetParent()->GetSafeHwnd(),MY_LVN_ENDEDIT,0,0);
::SendMessage(this->GetParent()->GetSafeHwnd(),MY_LVN_KEYDOEN,2,0);
return;
}
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值