首页 > 技术文章 > MFC实现 自适应操作系统的CListCtrl控件

Open-Source 2016-08-25 16:52 原文

     新建对话框应用程序,删除自动生成的控件后,拖拽一个CListCtrl控件,绑定变量名为:m_listctrl。在对话框初始化成员函数OnInitDialog()中键入以下代码即可实现自适应系统的CListCtrl控件!

    #include "uxtheme.h"
    #pragma comment(lib,"uxtheme.lib")  

    // 获取当前窗口style
    
    LONG lStyle;
    lStyle = GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE);
    lStyle &= ~LVS_TYPEMASK; //清除显示方式位
    lStyle |= LVS_REPORT; //设置为report风格
    SetWindowLong(m_listctrl.m_hWnd, GWL_STYLE, lStyle);//设置style

    // 设置扩展风格
    
    DWORD dwStyle = m_listctrl.GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮(和report风格配合使用)

    m_listctrl.SetExtendedStyle(dwStyle); 
 
    // 设置windows explorer风格主题
    
    ::SetWindowTheme(m_listctrl.m_hWnd, L"explorer", NULL);

    m_listctrl.InsertColumn(0, L"工资", LVCFMT_LEFT, 100); 
    m_listctrl.InsertColumn(1, L"姓名", LVCFMT_LEFT, 100);    
    m_listctrl.InsertColumn(2, L"学号", LVCFMT_LEFT, 100);
  
    int nRow = m_listctrl.InsertItem(0, L"2400");    
    m_listctrl.SetItemText(nRow, 1, L"ャ玥夜ゞ");   
    m_listctrl.SetItemText(nRow, 2, L"007");

 下面是这两种样式的对比图:

 

推荐阅读