代码改变世界

Win32 控件篇(8)

2011-03-21 11:11  Clingingboy  阅读(2940)  评论(0编辑  收藏  举报

 

6.38 如何在树控件中插入项

构建TVINSERTSTRUCT 结构体,hParent 指向父级节点,用InsertItem方法插入节点

void CDemoDlg::InitTree()
{
    CString strText = _T("");

    TVINSERTSTRUCT tvInsert;

    tvInsert.hParent = TVI_ROOT;
    tvInsert.hInsertAfter = NULL;
    tvInsert.item.mask = TVIF_TEXT;
    tvInsert.item.pszText = _T("Root");

    //在树控件中插入项
    HTREEITEM hRoot = m_ctrlTree.InsertItem(&tvInsert);

    for (int i = 0; i < 4; i++)
    {
        strText.Format(_T("Item %d"), i);

        tvInsert.hParent = hRoot;
        tvInsert.hInsertAfter = TVI_LAST;
        tvInsert.item.mask = TVIF_TEXT;
        tvInsert.item.pszText = strText.GetBuffer(strText.GetLength());

        //在树控件中插入项
        HTREEITEM hParent = m_ctrlTree.InsertItem(&tvInsert);

        strText.ReleaseBuffer();

        for(int j = 0; j < 5; j++)
        {
            strText.Format(_T("SubItem %d %d"), i, j);

            tvInsert.hParent = hParent;
            tvInsert.hInsertAfter = TVI_LAST;
            tvInsert.item.mask = TVIF_TEXT;
            tvInsert.item.pszText = strText.GetBuffer(strText.GetLength());

            //在树控件中插入项
            HTREEITEM hParent = m_ctrlTree.InsertItem(&tvInsert);

            strText.ReleaseBuffer();
        }
    }
}

效果:
image

6.39 如在树控件中添加连线和按钮

用ModifyStyle方法修改样式,调用InsertItem的重载方法,比上面的方法简单多了

void CDemoDlg::InitTree()
{
    //在树控件中添加连线和按钮
    m_ctrlTree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);

    CString strText = _T("");

    //在树控件中插入项
    HTREEITEM hRoot = m_ctrlTree.InsertItem(_T("Root"));

    for (int i = 0; i < 4; i++)
    {
        strText.Format(_T("Item %d"), i);

        //在树控件中插入项
        HTREEITEM hParent = m_ctrlTree.InsertItem(strText, hRoot);

        for(int j = 0; j < 5; j++)
        {
            strText.Format(_T("SubItem %d %d"), i, j);

            //在树控件中插入项
            m_ctrlTree.InsertItem(strText, hParent);
        }
    }
}

效果:
image

6.40 如何从树控件中删除项

  1. GetSelectedItem
  2. ItemHasChildren
  3. DeleteItem
  4. DeleteAllItems
void CDemoDlg::OnTest1() 
{
    //获得树控件的当前选择项;
    HTREEITEM hSel = m_ctrlTree.GetSelectedItem();

    if( hSel == NULL)
    {
        return;
    }

    //判断该项是否有子项
    if (m_ctrlTree.ItemHasChildren(hSel))
    {
        CString strMessage = _T("当前选择项包含子项,是否删除?");
        if (MessageBox(strMessage, _T(""), MB_YESNO) == IDNO)
        {
            return;
        }
    }

    //从树控件中删除该项
    m_ctrlTree.DeleteItem(hSel);
}

void CDemoDlg::OnTest2() 
{
    //从树控件中删除全部项
    m_ctrlTree.DeleteAllItems();
}

void CDemoDlg::OnTest3() 
{
    //从树控件中删除全部项
    m_ctrlTree.DeleteAllItems();

    //重新初始化树控件
    InitTree();
}

 

6.41 如何展开和选择树项

  1. Expand
  2. Select
void CDemoDlg::InitTree()
{
    //在树控件中添加连线和按钮
    m_ctrlTree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);

    CString strText = _T("");

    //在树控件中插入项
    HTREEITEM hRoot = m_ctrlTree.InsertItem(_T("Root"));

    for (int i = 0; i < 4; i++)
    {
        strText.Format(_T("Item %d"), i);

        //在树控件中插入项
        HTREEITEM hParent = m_ctrlTree.InsertItem(strText, hRoot);

        for(int j = 0; j < 5; j++)
        {
            strText.Format(_T("SubItem %d %d"), i, j);

            //在树控件中插入项
            m_ctrlTree.InsertItem(strText, hParent);
        }

        //展开树项
        m_ctrlTree.Expand(hParent, TVE_EXPAND);    
    }

    //展开树项
    m_ctrlTree.Expand(hRoot, TVE_EXPAND);

    //选择树项
    m_ctrlTree.Select(hRoot, TVGN_CARET);
}

 

6.42 如何设置树控件中项的图像

创建一个CImageList

//初始化图像列表
m_ImageList.Create(IDB_TREE, 16, 0, RGB(0, 255, 0));

使用SetImageList方法填充图像

//在树控件中添加连线和按钮
m_ctrlTree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);

//设置树控件的图像列表
m_ctrlTree.SetImageList(&m_ImageList, TVSIL_NORMAL);

6.43 如何遍历树控件

递归遍历

void CDemoDlg::OnTest() 
{
    //清除列表框的内容
    m_ctrlList.ResetContent();

    //获得树控件的根项
    HTREEITEM hRoot = m_ctrlTree.GetRootItem();

    if (hRoot == NULL)
    {
        return;
    }

    //获得根项的文本
    CString strText = m_ctrlTree.GetItemText(hRoot);

    //在列表框中添加文本
    m_ctrlList.AddString(strText);

    //该项是否有子项
    if (m_ctrlTree.ItemHasChildren(hRoot))
    {
        //遍历下级子项
        Search(hRoot, 1);
    }
}

void CDemoDlg::Search(HTREEITEM hParent, int nOffset)
{
    CString strText = _T("");

    HTREEITEM hChild = NULL;

    //获得该项的子项
    hChild = m_ctrlTree.GetChildItem(hParent);

    while (hChild != NULL)
    {
        //获得子项的文本
        strText = m_ctrlTree.GetItemText(hChild);

        //根据偏移量在字符串中插入空格
        for (int n = 0; n < nOffset; n++)
        {
            strText.Insert(0, _T("   "));
        }

        //在列表框中添加文本
        m_ctrlList.AddString(strText);

        //子项是否有子项
        if (m_ctrlTree.ItemHasChildren(hChild))
        {
            //遍历下级子项
            Search(hChild, nOffset + 1);
        }

        //获得子项的兄弟项
        hChild = m_ctrlTree.GetNextItem(hChild, TVGN_NEXT);
    }
}

 

6.44 如何使用标签控件

Tab选项卡控件

void CDemoDlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
    //获得当前标签项
    int nSel = m_ctrlTab.GetCurSel();

    CString strText = _T("");
    strText.Format(_T("Tab %d"), nSel + 1);

    SetDlgItemText(IDC_EDIT, strText);
}

void CDemoDlg::InitTab()
{
    //向标签控件中插入项
    for (int n = 0 ; n < 5; n++)
    {
        CString strText = _T("");
        strText.Format(_T("Tab %d"), n + 1);

        m_ctrlTab.InsertItem(n, strText);
    }
}

 

6.45 如何使用日期时间控件

DateTimePicker控件(CDateTimeCtrl)

初始化:

//选择短日期显示格式
m_ctrlDateTime1.ModifyStyle(0, DTS_SHORTDATEFORMAT, 0);
//选择时间显示格式
m_ctrlDateTime2.ModifyStyle(0, DTS_TIMEFORMAT, 0);

//获得当前时间
CTime time = CTime::GetCurrentTime();

//设置时间
m_ctrlDateTime1.SetTime(&time);
m_ctrlDateTime2.SetTime(&time);

Test:

void CDemoDlg::OnTest() 
{
    CTime time1;
    CTime time2;

    //获得日期和时间
    m_ctrlDateTime1.GetTime(time1);
    m_ctrlDateTime2.GetTime(time2);

    CString strText = _T("");
    strText.Format(_T("%04d-%02d-%02d\n%02d:%02d:%02d"),
        time1.GetYear(), time1.GetMonth(), time1.GetDay(), 
        time2.GetHour(), time2.GetMinute(), time2.GetSecond());
    AfxMessageBox(strText);
}

效果:

image

6.46 如何显示RichEdit控件

 

CRichEditCtrl* pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT);
CString strText = _T("");
strText += _T("昨夜星辰昨夜风,\r\n");
strText += _T("画楼西畔桂堂东。\r\n");
strText += _T("身无彩凤双飞翼,\r\n");
strText += _T("心有灵犀一点通。");
pRichEdit->SetWindowText(strText);

效果:
image