CComboBox重载在CListCtrl中使用

1、CComboBox的重载

.h

#pragma once
#include "afxwin.h"
/*
List Ctrl使用
只有再次点击别的地方的时候才会隐藏
*/

class CComboBoxList :
  public CComboBox
{

public:
  CComboBoxList();
  ~CComboBoxList();

protected:
  DECLARE_MESSAGE_MAP()

protected:
  afx_msg void OnKillFocus(CWnd* pNewWnd);
  afx_msg void OnSelectChange();

public:
  void SetData(CRect& rc, int& nItem, int& nSubItem, bool  bUse = false);

public:
  CRect mRcItemList;
  int   mnSel;
  int   mnItem;
  int   mnSubItem;
  bool  mbUse;
};

.cpp(位置获取不大对,需要以后考虑下 因为存在下拉)

#include "stdafx.h"
#include "ComboBoxList.h"


CComboBoxList::CComboBoxList()
{
  mbUse = false;
}


CComboBoxList::~CComboBoxList()
{
}

BEGIN_MESSAGE_MAP(CComboBoxList, CComboBox)
  ON_WM_KILLFOCUS()
  ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelectChange)
END_MESSAGE_MAP()


void CComboBoxList::OnKillFocus(CWnd* pNewWnd)
{
  // 编辑框失去焦点获取相关数据
  CComboBox::OnKillFocus(pNewWnd);
  POINT pt;
  GetCursorPos(&pt);
  ScreenToClient(&pt);
  if (!mRcItemList.PtInRect(pt))
  {
    ShowWindow(SW_HIDE);//将组合框隐藏
  }
}

void CComboBoxList::OnSelectChange()
{
  // TODO:  在此添加控件通知处理程序代码
  mnSel = GetCurSel();
}

void  CComboBoxList::SetData(CRect& rc, int& nItem, int& nSubItem, bool  bUse)
{
  mRcItemList = rc;
  mnItem = nItem;
  mnSubItem = nSubItem;
  mbUse = bUse;
  mnSel = -2;
}

2、CListCtrl点击某一列Dlg的

void Dlg::OnNMClickList(NMHDR *pNMHDR, LRESULT *pResult)
{
  LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);

  // TODO:  在此添加控件通知处理程序代码
  mnItem = pNMItemActivate->iItem;
  mnSubItem = pNMItemActivate->iSubItem;
  if (mnItem != -1 && mnSubItem != 0)
  {
    _DataBtn(); // 下次调用前将上次数据刷新到CtrlLis中,别处也需做相同修改 可在 PreTranslateMessage 处理
    switch (mnSubItem)
    {
    case  2:
       break;
    case  3:
      _SetTrasp(); 
      break;
    case 4:
    case 5:
    {
      _ModfyListCtrlCell();
    } break;
    }
  }
  *pResult = 0;
}


void  Dlg::_SetTrasp()
{
  CRect rc;
  mListCtrl.GetSubItemRect(mnItem, mnSubItem, LVIR_BOUNDS, rc);
  if (mBoxTsp.m_hWnd == NULL)
  {
    mBoxTsp.Create(
      WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
      rc, GetDlgItem(IDC_LIST_Layer), IDC_ComBoxTmp);
    mBoxTsp.AddString(L"Ma");
    mBoxTsp.AddString(L"Hong");
    mBoxTsp.AddString(L"Kai");
  } 

  mBoxTsp.SetData(rc, mnItem, mnSubItem);
  CString str = mListCtrl.GetItemText(mnItem, mnSubItem);
  EnTrasp eTsp = ColorTool::GetTraspEn(str);
  mBoxTsp.SetCurSel(eTsp);
  mBoxTsp.MoveWindow(&rc, TRUE);
  mBoxTsp.ShowWindow(SW_NORMAL);
  mBoxTsp.BringWindowToTop();
  mBoxTsp.SetFocus();//使组合框聚焦
}

你可能感兴趣的:(MFC,CComboBox,CtrList)