MFC 在界面这块,不得不说比较难看,虽然如此,但是它的简洁性(当然,不是说它的代码,而是相对于程序员,减少了自己敲写代码的工作量)。
下面的例子是新建对话框,在新的对话框里添加列表控件,在列表控件里显示必要信息。
效果如图:
新建 MFC 工程,命名为 Draw,添加对话框,新建类 MyPanel
<pre name="code" class="cpp">//CDrawDlg.h public: afx_msg void OnBnClickedBtStart(); afx_msg void Dlg2_Init(); public: MyPanel * panel;
//CDrawDlg.cpp UINT Func_Thread(LPVOID lpParam) { CDrawDlg * dlg = (CDrawDlg *)lpParam; Text_Show(lpParam); return 0; } void CDrawDlg::OnBnClickedBtStart() { // TODO: 在此添加控件通知处理程序代码 Dlg2_Init(); panel->ShowWindow(SW_SHOW); AfxBeginThread(Func_Thread, this); } void CDrawDlg::Dlg2_Init() { panel->Create(IDD_MY_DIALOG); }
MyPanel.h #pragma once #include "afxcmn.h" int Text_Show(LPVOID lpParam); // MyPanel 对话框 class MyPanel : public CDialogEx { DECLARE_DYNAMIC(MyPanel) public: MyPanel(CWnd* pParent = NULL); // 标准构造函数 virtual ~MyPanel(); // 对话框数据 enum { IDD = IDD_MY_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 //自己添加的,初始化Teechart virtual BOOL OnInitDialog(); DECLARE_MESSAGE_MAP() public: void OnTimer(UINT_PTR nIDEvent); void OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult ); public: CListCtrl m_List1; int count; afx_msg void OnBnClickedBtStart(); };
MyPanel.cpp BEGIN_MESSAGE_MAP(MyPanel, CDialogEx) ON_WM_TIMER() ON_NOTIFY ( NM_CUSTOMDRAW,IDC_LIST1, OnCustomdrawMyList ) ON_BN_CLICKED(IDC_BT_START, &MyPanel::OnBnClickedBtStart) END_MESSAGE_MAP() // Videodecode 消息处理程序 BOOL MyPanel::OnInitDialog() { CDialogEx::OnInitDialog(); count = 0; //整行选择;有表格线;表头;单击激活 DWORD dwExStyle=LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE; //报表风格;单行选择;高亮显示选择行 //视频 m_List1.ModifyStyle(0,LVS_SINGLESEL|LVS_REPORT|LVS_SHOWSELALWAYS); m_List1.SetExtendedStyle(dwExStyle); m_List1.InsertColumn(0,"L0",LVCFMT_CENTER,60,0); m_List1.InsertColumn(1,"L1",LVCFMT_CENTER,60,0); m_List1.InsertColumn(2,"L2",LVCFMT_CENTER,60,0); m_List1.InsertColumn(3,"L3",LVCFMT_CENTER,70,0); m_List1.InsertColumn(4,"L4",LVCFMT_CENTER,60,0); return TRUE; } // MyPanel 消息处理程序 int Text_Show(LPVOID lpParam) { dlg = (CDrawDlg *)lpParam; return 0; } void MyPanel::OnTimer(UINT_PTR nIDEvent) { CString f_index; f_index.Format(_T("%d"),count++); //获取当前记录条数 int nIndex = m_List1.GetItemCount(); m_List1.InsertItem(nIndex,f_index); m_List1.SetItemText(nIndex, 1, "1"); m_List1.SetItemText(nIndex, 2, "2"); m_List1.SendMessage(WM_VSCROLL, SB_BOTTOM, NULL); } //ListCtrl加颜色 void MyPanel::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult ) { NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR ); // Take the default processing unless we set this to something else below. *pResult = CDRF_DODEFAULT; if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage ) { *pResult = CDRF_NOTIFYITEMDRAW; } else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage ) { *pResult = CDRF_NOTIFYSUBITEMDRAW; } else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage ) { COLORREF clrNewTextColor, clrNewBkColor; int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec ); CString strTemp = m_List1.GetItemText(nItem,1); //if(strTemp.Compare(_T("I"))==0) if(nItem%6 == 0) { clrNewTextColor = RGB(0,0,0); //Set the text clrNewBkColor = RGB(255,0,0); //背景设置成红色 } //else if(strTemp.Compare(_T("P"))==0) else if(nItem%6 == 2) { clrNewTextColor = RGB(0,0,0); clrNewBkColor = RGB(0,255,255); //背景设置成青色 } //else if(strTemp.Compare(_T("B"))==0) else if(nItem%6 == 4) { clrNewTextColor = RGB(0,0,0); clrNewBkColor = RGB(0,255,0); //背景设置成绿色 } else { clrNewTextColor = RGB(0,0,0); clrNewBkColor = RGB(255,255,255); } pLVCD->clrText = clrNewTextColor; pLVCD->clrTextBk = clrNewBkColor; *pResult = CDRF_DODEFAULT; } } void MyPanel::OnBnClickedBtStart() { // TODO: 在此添加控件通知处理程序代码 SetTimer(1, 1000, NULL); }
原文地址:http://blog.csdn.net/u010477528/article/details/42007409