MFC listctrl列表右键复制菜单, 复制剪切板

环境: vs2010

一、在资源视图–xxxxx.rc下右键添加资源 Menu选项。IDR_MENU
新建一个复制的菜单,Popup设置True 新建一个子项复制。

二、再ListCtrl 列表中右键–添加事件处理程序–选择NM_RCLICK(右键响应事件)
编译程序

void Cview::OnNMRClickReportList(NMHDR *pNMHDR, LRESULT *pRseult)
{
    LPNMITRMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    //TO DO
    CMenu popMenu;
    popMenu.LoadMenu(IDR_MENU);
    CPoint posMouse;
    GetCursorPos(&posMouse);
    CMenu* popup = popMenu.GetSubMenu(0);
    popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, posMouse.x, popMouse.y, this);
    popMenu.Detach();
    popMenu.DestoryMenu();
    *pRseult = 0;
}

三、复制剪切板
在子项复制菜单–右键添加事件处理程序–COMMAND
加入下面的代码:

  //获取单击所在的行号  
  //找出鼠标位置  
  int nItem;
  CString strText;
  DWORD dwPos = GetMessagePos();  
  CPoint point( LOWORD(dwPos), HIWORD(dwPos) );  
  m_listCtrl.ScreenToClient(&point); 

  //定义结构体  
  LVHITTESTINFO lvinfo;  
  lvinfo.pt = point;  
  lvinfo.flag = LVHT_ABOVE;

  //获取行号信息  
  nItem = m_listCtrl.HitTest(&lvinfo);
  //nItem = m_listCtrl.SubItemHitTest(&lvinfo);  
  if(nItem != -1)  
      strText = m_listCtrl.GetItemText(lvinfo.iItem, lvinfo.iSubItem);//获取对应单元格的内容

  //复制剪切板
  if( !strText.IsEmpty() )
  {
      if( OpenClipboard() )
      {
          EmptyCliboard();
          TCHAR* pszData;
          HGLOBAL hClipboardData = GlobalAlloc( GMEM_DDESHARE, ( strText.GetLength() + 1) * sizeof(TCHAR) );
          if( hClipboardData )
          {
              pszData = ( TCHAR* )GlobalLock( hClipboarData );
              _tcscpy( pszData, strText );
              GlobalUnlock( hClipboardData );
              SetClipboardData( CF_UNICODETEXT, hClipboardData);//根据相应的数据选择第一个参数,(CF_TEXT)  
          }
          CloseClipboard();
      }
  }

CTRL+C的文章:
http://blog.csdn.net/energysober/article/details/54410032

四 列表表头排序功能:
1、创建变量m_SortColumn,m_bAsc。
2、在列表中右键添加事件处理程序–LVN_COLUMNCLICK–编辑代码
3、在相应的.h文件的class Cview添加static int CALLBACK Cview::ListComparePorc(LPARAM LParam1, LPARAM LParam2, LPARAM LParamSort);

static int m_SortColumn = 0;
static int m_bAsc = 0;

int CALLBACK Cview::ListComparePorc(LPARAM LParam1, LPARAM LParam2, LPARAM LParamSort)
{
    CListCtrl* pListCtrl = (ClistCtrl*) LParamSort;
    CString strItem1 = pListCtrl->GetItemText(LParam1, m_SortColumn);
    CString strItem2 = pListCtrl->GetItemText(LParam2, m_SortColumn);

    if(m_bAsc)
        return strItem1.ComparamNoCase(strItem2);
    else
        return strItem2.ComparamNoCase(strItem1);
}

void Cview::OnLvnColumnclick(NMHDR *pNMHER, LRESULT* pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    //TODO
    m_SortColumn = pNMLV->iSubItem;
    m_bAsc = !m_bAsc; //取反升降序
    int count = m_number.GetItemCount();
    for( int i = 0; i < count; ++i)
    {
        m_number.SetItemData(i,i);
    }
    m_number.SortItems(ListComparePorc, (LPARAM)&m_number);
    *pResult = 0;
}
展开阅读全文

没有更多推荐了,返回首页