zoukankan      html  css  js  c++  java
  • CIconListBox带图标的列表框类

    有时候,我们需要在列表框ListBox中插入带图标的文字项,这就需要自己派生一个类出来了,网上的一个CIconListBox类还不错,网站http://www.codeguru.com/Cpp/controls/listbox/article.php/c4749/

    这里介绍下使用方法,源代码我有改动,环境Visual Studio 2008 SP1:
    1.基于对话框的工程,名称为IconListBoxTest;
    2.拖动控件到对话框上,按下图所示排列:

    3.导入IconListBox.cpp和IconListBox.h两个文件到工程,在对话框头文件包含

    #include "IconListBox.h" 

    4.为ListBox控件,改变其属性“Owner Draw”为“Fixed”,其“Has Strings”为“TRUE”添加变量:

    CIconListBox m_ListBox; 

    添加一个图片列表变量:

    CImageList m_imgNormal; 

    导入图片资源,修改其ID号为IDB_LB_IMG,在对话框的初始化函数里面添加如下代码(可自己根据修改):

    CBitmap bmp; 
    m_imgNormal.Create(16
                       16
                       ILC_COLOR32 | ILC_MASK, 
                       10,    // 初始化图片数量 
                       10); 
     
    ASSERT(m_imgNormal.m_hImageList); 
     
    bmp.LoadBitmap(IDB_LB_IMG); 
    m_imgNormal.Add( &bmp, -1); 
    bmp.DeleteObject(); 
    m_ListBox.SetImageList(&m_imgNormal); 
     
    for(int i = 0; i < 10 ; i++ ) 
        m_ListBox.AddString( _T("Hello World") , i ); 

    5.右边按钮的事件如下:

    void CIconListBoxTestDlg::OnBnClickedAdd() 

        if(UpdateData()) 
        { 
            if(!m_Str.IsEmpty()) 
                m_ListBox.AddString(m_Str,m_Img); 
            else 
                AfxMessageBox(_T("输入文本字符串!")); 
        } 

     
    void CIconListBoxTestDlg::OnBnClickedInsert() 

        if(UpdateData()) 
        { 
            if( m_Str.IsEmpty() ) 
            { 
                AfxMessageBox(_T("输入文本字符串!")); 
                return
            } 
            int iSel = m_ListBox.GetCurSel(); 
            if( iSel != LB_ERR ) 
                m_ListBox.InsertString(iSel, m_Str , m_Img); 
            else 
                AfxMessageBox(_T("选择左边列表其中一项!")); 
        }     

     
    void CIconListBoxTestDlg::OnBnClickedDelete() 

        int iSel = m_ListBox.GetCurSel(); 
        if( iSel != LB_ERR ) 
            m_ListBox.DeleteString( iSel ); 
        else 
            AfxMessageBox(_T("选择左边列表其中一项!")); 

     
    void CIconListBoxTestDlg::OnBnClickedChangeicon() 

        if(UpdateData()) 
        { 
            int iSel = m_ListBox.GetCurSel(); 
            if(iSel != LB_ERR ) 
                m_ListBox.SetItemImage(iSel,m_Img); 
            else 
                AfxMessageBox(_T("选择左边列表其中一项!")); 
        } 

     
    void CIconListBoxTestDlg::OnBnClickedImgList() 

        if(UpdateData()) 
        { 
            if( m_Check ) 
                m_ListBox.SetImageList(&m_imgNormal); 
            else 
                m_ListBox.SetImageList(); 
            m_ListBox.RedrawWindow(); 
        } 
        else 
        { 
            ((CButton *)GetDlgItem(IDC_IMG_LIST))->SetCheck(m_Check); 
        } 

    6.编译完成。效果如下:

     

    源代码下载:http://download.csdn.net/source/2816256

  • 相关阅读:
    JPA总结——实体关系映射(一对多@OneToMany)
    JPA——映射持久化对象(Entity)
    jackson annotations注解详解
    Spring Data JPA
    Spring Data JPA初使用
    Spring注解Annotion详解
    JPA概要
    The requested resource is not available错误
    Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签
    Spring——Eclipse上安装springsource-tool-suite与jar包详解
  • 原文地址:https://www.cnblogs.com/lidabo/p/3489651.html
Copyright © 2011-2022 走看看