博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mfc 可编辑 list control
阅读量:4946 次
发布时间:2019-06-11

本文共 5668 字,大约阅读时间需要 18 分钟。

维护到一个古老的gm工具的时候 需要这个功能 在网上找到一份很好用的代码 贴到这里 再次感谢那位同僚

#pragma once//#include "OrangeMessage.h"// COrangeEditclass COrangeEdit : public CEdit{    DECLARE_DYNAMIC(COrangeEdit)public:    COrangeEdit();    virtual ~COrangeEdit();protected:    DECLARE_MESSAGE_MAP()    virtual void PreSubclassWindow();public:    afx_msg void OnKillFocus(CWnd* pNewWnd);};
// OrangeEdit.cpp : 实现文件//#include "stdafx.h"//#include "ListDemo.h"#include "OrangeEdit.h"#include "WindowManager.h"// COrangeEditIMPLEMENT_DYNAMIC(COrangeEdit, CEdit)COrangeEdit::COrangeEdit(){}COrangeEdit::~COrangeEdit(){}BEGIN_MESSAGE_MAP(COrangeEdit, CEdit)    ON_WM_KILLFOCUS()END_MESSAGE_MAP()// COrangeEdit 消息处理程序void COrangeEdit::PreSubclassWindow(){    // TODO: 在此添加专用代码和/或调用基类    CEdit::PreSubclassWindow();}void COrangeEdit::OnKillFocus(CWnd* pNewWnd){    CEdit::OnKillFocus(pNewWnd);    // TODO: 在此处添加消息处理程序代码    CWnd* pParent = this->GetParent();    ::SendMessage(pParent->GetSafeHwnd(),ORANGE_LVN_ENDEDIT,0,0);}
#pragma once#include "orangeedit.h"//#include "OrangeMessage.h"// COrangeListCtrlclass COrangeListCtrl : public CListCtrl{    DECLARE_DYNAMIC(COrangeListCtrl)public:    COrangeListCtrl();    virtual ~COrangeListCtrl();protected:    DECLARE_MESSAGE_MAP()public:    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);protected:    virtual void PreSubclassWindow();    afx_msg LRESULT OnEndEdit(WPARAM wParam,LPARAM lParam);    void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);    afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);private:    int m_nItem;//主项    int m_nSubItem;//子项    COrangeEdit m_Edit_ctlItem;    CFont m_FontItem;    int m_nRowHeight;public:    void SetRowHeigt(int nHeight);};
// OrangeListCtrl.cpp : 实现文件//#include "stdafx.h"#include "WindowManager.h"#include "OrangeListCtrl.h"// COrangeListCtrlIMPLEMENT_DYNAMIC(COrangeListCtrl, CListCtrl)COrangeListCtrl::COrangeListCtrl(){    m_nRowHeight=0;}COrangeListCtrl::~COrangeListCtrl(){}BEGIN_MESSAGE_MAP(COrangeListCtrl, CListCtrl)    ON_WM_LBUTTONDOWN()    ON_MESSAGE(ORANGE_LVN_ENDEDIT, OnEndEdit)    ON_WM_MEASUREITEM()    ON_WM_MEASUREITEM_REFLECT()END_MESSAGE_MAP()// COrangeListCtrl 消息处理程序void COrangeListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct){    // TODO:  添加您的代码以绘制指定项    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=L""; 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); } }}void COrangeListCtrl::OnLButtonDown(UINT nFlags, CPoint point){ // TODO: 在此添加消息处理程序代码和/或调用默认值 CListCtrl::OnLButtonDown(nFlags, point); CRect rcCtrl; LVHITTESTINFO lvhti; lvhti.pt = point; m_nItem =SubItemHitTest(&lvhti); if (m_nItem == -1) return; m_nSubItem = lvhti.iSubItem; GetSubItemRect(m_nItem,m_nSubItem,LVIR_LABEL,rcCtrl); 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(m_nItem,m_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); }void COrangeListCtrl::PreSubclassWindow(){ // TODO: 在此添加专用代码和/或调用基类 ModifyStyle(0,LVS_OWNERDRAWFIXED); CListCtrl::PreSubclassWindow();}LRESULT COrangeListCtrl::OnEndEdit(WPARAM wParam,LPARAM lParam){ CString strText; m_Edit_ctlItem.GetWindowText(strText); SetItemText(m_nItem,m_nSubItem,strText); m_Edit_ctlItem.ShowWindow(SW_HIDE); return 0;}void COrangeListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct){ if (m_nRowHeight>0) { lpMeasureItemStruct->itemHeight = m_nRowHeight; }}void COrangeListCtrl::SetRowHeigt(int nHeight){ m_nRowHeight = nHeight; CRect rcWin; GetWindowRect(&rcWin); WINDOWPOS wp; wp.hwnd = m_hWnd; wp.cx = rcWin.Width(); wp.cy = rcWin.Height(); wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER; SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);}void COrangeListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct){ // TODO: 在此添加消息处理程序代码和/或调用默认值 CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);}

 

转载于:https://www.cnblogs.com/rexzhao/p/5254868.html

你可能感兴趣的文章
Android 开发者福利Google Developers中国网站发布
查看>>
【模板】线段树 2
查看>>
《零基础入门学习Python》学习过程笔记【017函数】
查看>>
Block Demo
查看>>
LintCode Coins in a Line III
查看>>
Oracle定义varchar2()类型存储汉字的长度问题
查看>>
python 2.7 pip install plt 报错,应该是 pip install matplotlib
查看>>
C# 解压缩
查看>>
Centos7安装教程
查看>>
ABAP术语-ALE
查看>>
删除SVN信息
查看>>
IDEA 转移C盘 .IntelliJIdea 索引目录
查看>>
CentOS 6通过yum升级Git
查看>>
python接口自动化测试三:代码发送HTTP请求
查看>>
POJ2386:Lake Counting(DFS)
查看>>
poj2411_状压dp
查看>>
Error running 'run': data.userName must not be null
查看>>
kindeditor去掉网络图片
查看>>
Unity拖拽旋转2D物体
查看>>
软工结队第二次作业
查看>>