CListBox控件的重写

本文介绍了一个名为CPropertyList的CListBox派生类,用于创建类似VB属性表控件的功能。该控件能处理属性条的高度、颜色、分类位图等,并实现了属性条的重绘,包括绘制分类位图、属性名和数值。文章还展示了如何响应鼠标点击事件,调整列宽,以及处理不同类型的属性值,如颜色、文件和字体选择。
摘要由CSDN通过智能技术生成

//定义行类

class CPropertyItem
{
// Attributes
public:
 CString m_propName;
 CString m_curValue;
 int m_nItemType;
 CString m_cmbItems;

 //新增属性类变量,
 BOOL m_bSeparator;             //是否分隔条(置灰,不能编辑)

 long m_lFontWidth;      //字宽
 long m_lColor;       //颜色

 BOOL m_bDrawExpand;            //是否绘制左侧分类位图
 CRect m_rcBmp;       //分类位图矩形框
 BOOL m_bExpand;       //是否展开

 int m_nClassIndex;             //分类索引(-1 -- 普通<不分类>,其它(>=0) -- 分类的索引号)

public:
 CPropertyItem(CString propName, CString curValue,
  int nItemType, CString cmbItems,bool bSeparator=false,bool bDrawExpand=false,int nClassIndex=-1)
 {
  m_propName = propName;
  m_curValue = curValue;
  m_nItemType = nItemType;
  m_cmbItems = cmbItems;

  //新增属性类变量,
  m_bSeparator = bSeparator;  //分隔条
  m_lFontWidth = 30;    //字宽
  m_lColor = 0;     //颜色

  m_bDrawExpand = bDrawExpand; //是否绘制左侧分类位图
  m_rcBmp = CRect(0,0,0,0);  //分类位图矩形框
  m_bExpand = TRUE;  //默认展开
//  m_bExpand = FALSE;

  m_nClassIndex = nClassIndex;    //分类的索引号
 }
};

/
// CPropertyList window

//派生的列表框类

class CPropertyList : public CListBox
{
// Construction
public:
 CPropertyList();

// Attributes
public:
// LPDRAWITEMSTRUCT lpout;

// Operations
public:
 int AddItem(CString txt);
 int AddPropItem(CPropertyItem* pItem);

 //绘制属性页分类展开位图
 void DrawBmp(CDC *pDC,int bmpID,CPoint pt1,CPoint pt2);

 CPtrList m_PropertyList;   //属性列表
 int nClassIndex;              //临时类索引

// Overrides
 // ClassWizard generated virtual function overrides
 //{ {AFX_VIRTUAL(CPropertyList)
 public:
 virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
 protected:
 virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
 virtual void PreSubclassWindow();
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CPropertyList();

 // Generated message map functions
protected:
 //{ {AFX_MSG(CPropertyList)
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
 afx_msg void OnSelchange();
 afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
 afx_msg void OnKillFocus(CWnd* pNewWnd);
 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 //}}AFX_MSG
 afx_msg void OnKillfocusCmbBox();
 afx_msg void OnSelchangeCmbBox();
 afx_msg void OnKillfocusEditBox();
 afx_msg void OnChangeEditBox();
 afx_msg void OnButton();

 DECLARE_MESSAGE_MAP()

 void InvertLine(CDC* pDC,CPoint ptFrom,CPoint ptTo);
 void DisplayButton(CRect region);

 CComboBox m_cmbBox;
 CEdit m_editBox;
 CButton m_btnCtrl;
 CFont m_SSerif8Font;
 
 int m_curSel,m_prevSel;
 int m_nDivider;
 int m_nDivTop;
 int m_nDivBtm;
 int m_nOldDivX;
 int m_nLastBox;
 BOOL m_bTracking;
 BOOL m_bDivIsSet;
 HCURSOR m_hCursorArrow;
 HCURSOR m_hCursorSize;
};

//派生类的重写

//类名:CPropertyList
//功能:类似VB属性表控件
//
#include "stdafx.h"
#include "PropertyList.h"
#include "resource.h"      //加入资源头文件

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CPropertyList

CPropertyList::CPropertyList()
{
}

CPropertyList::~CPropertyList()
{
 m_PropertyList.RemoveAll();
}


BEGIN_MESSAGE_MAP(CPropertyList, CListBox)
 //{ {AFX_MSG_MAP(CPropertyList)
 ON_WM_CREATE()
 ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
 ON_WM_LBUTTONUP()
 ON_WM_KILLFOCUS()
 ON_WM_LBUTTONDOWN()
 ON_WM_MOUSEMOVE()
 ON_WM_VSCROLL()
 //}}AFX_MSG_MAP
 ON_CBN_KILLFOCUS(IDC_PROPCMBBOX, OnKillfocusCmbBox)
 ON_CBN_SELCHANGE(IDC_PROPCMBBOX, OnSelchangeCmbBox)
 ON_EN_KILLFOCUS(IDC_PROPEDITBOX, OnKillfocusEditBox)
 ON_EN_CHANGE(IDC_PROPEDITBOX, OnChangeEditBox)
 ON_BN_CLICKED(IDC_PROPBTNCTRL, OnButton)
END_MESSAGE_MAP()

/
// CPropertyList message handlers

BOOL CPropertyList::PreCreateWindow(CREATESTRUCT& cs)
{
 if (!CListBox::PreCreateWindow(cs))
  return FALSE;

 cs.style &= ~(LBS_OWNERDRAWVARIABLE | LBS_SORT);
 cs.style |= LBS_OWNERDRAWFIXED;

 m_bTracking = FALSE;
 m_nDivider = 0;
 m_bDivIsSet = FALSE;

 return TRUE;
}

void CPropertyList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
 lpMeasureItemStruct->itemHeight = 50; //默认属性条高度为20个像素
}

//item的重绘(重点在此)
void CPropertyList::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
 CDC dc;
 dc.Attach(lpDIS->hDC);
 CRect rectFull = lpDIS->rcItem;

 //   SetRedraw(FALSE);//先禁止重画操作,
/*****

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值