Win32 控件篇(5)

6.21 如何使用组合框

初始化,即ComboBox控件操作,相关方法参考

for (int n = 0; n < 10; n++)
{
    CString strText = _T("");
    strText.Format(_T("字符串%d"), n);

    //向组合框中列表框添加文本
    m_ctrlComboBox.AddString(strText);
}

//设置组合框当前选择项
m_ctrlComboBox.SetCurSel(0);

SetDlgItemText(IDC_EDIT, _T("字符串0"));

SelectChanged事件OnSelchangeCombo

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

    //获得组合框文本
    m_ctrlComboBox.GetWindowText(strText);

    SetDlgItemText(IDC_EDIT, strText);
}

6.22 如何实现带自动完成功能的组合框

6.23 如何设置组合框中列表框的宽度

先计算每个项的宽度,然后取最大值的,最后调用SetDroppedWidth方法设置宽度

int nWidth = 0;

CDC* pDC = m_ctrlComboBox.GetDC();

for (int n = 0; n < m_ctrlComboBox.GetCount(); n++)
{
    CString strText = _T("");
    int nTextWidth = 0;

    //获得组合框中列表框文本
    m_ctrlComboBox.GetLBText(n, strText);

    //获得组合框中列表框文本宽度
    nTextWidth = pDC->GetTextExtent(strText).cx;

    nWidth = max(nWidth, nTextWidth);
}

m_ctrlComboBox.ReleaseDC(pDC);

//加滚动条宽度和左右边界宽度
nWidth += ::GetSystemMetrics(SM_CXVSCROLL)
    + ::GetSystemMetrics(SM_CXEDGE) * 2;

//设置组合框中列表框宽度
m_ctrlComboBox.SetDroppedWidth(nWidth);

6.24 如何在组合框中显示文件列表

提供方便的Dir方法

//在组合框中显示文件列表
m_ctrlComboBox.Dir(DDL_DRIVES, _T(""));    

6.25 如何使用列表框

即ListBox操作,基本上与ComboBox操作是雷同的

初始化

for (int n = 0; n < 10; n++)
{
    CString strText = _T("");
    strText.Format(_T("字符串%d"), n);

    //向列表框1添加文本
    m_ctrlList1.AddString(strText);
}

GetDlgItem(IDC_TEST1)->EnableWindow(TRUE);
GetDlgItem(IDC_TEST3)->EnableWindow(TRUE);
GetDlgItem(IDC_TEST2)->EnableWindow(FALSE);
GetDlgItem(IDC_TEST4)->EnableWindow(FALSE);

测试代码:

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

    //获得列表框1当前选中项
    int nSel1 = m_ctrlList1.GetCurSel();
    if (nSel1 == LB_ERR)
    {
        return;
    }
    //获得列表框1文本
    m_ctrlList1.GetText(nSel1, strText);
    //从列表框1删除文本
    m_ctrlList1.DeleteString(nSel1);
    //设置列表框1当前选中项
    m_ctrlList1.SetCurSel(-1);

    //获得列表框2当前选中项
    int nSel2 = m_ctrlList2.GetCurSel();
    if (nSel2 == LB_ERR)
    {
        //向列表框2添加文本
        m_ctrlList2.AddString(strText);
    }
    else
    {
        //向列表框2插入文本
        m_ctrlList2.InsertString(nSel2, strText);
    }
    //设置列表框2当前选中项
    m_ctrlList2.SetCurSel(-1);

    if (m_ctrlList1.GetCount() == 0)
    {
        GetDlgItem(IDC_TEST1)->EnableWindow(FALSE);
        GetDlgItem(IDC_TEST3)->EnableWindow(FALSE);
    }
    GetDlgItem(IDC_TEST2)->EnableWindow(TRUE);
    GetDlgItem(IDC_TEST4)->EnableWindow(TRUE);
}

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

    //获得列表框2当前选中项
    int nSel2 = m_ctrlList2.GetCurSel();
    if (nSel2 == LB_ERR)
    {
        return;
    }
    //获得列表框2文本
    m_ctrlList2.GetText(nSel2, strText);
    //从列表框2删除文本
    m_ctrlList2.DeleteString(nSel2);
    //设置列表框2当前选中项
    m_ctrlList2.SetCurSel(-1);

    //获得列表框1当前选中项
    int nSel1 = m_ctrlList1.GetCurSel();
    if (nSel1 == LB_ERR)
    {
        //向列表框1添加文本
        m_ctrlList1.AddString(strText);
    }
    else
    {
        //向列表框1插入文本
        m_ctrlList1.InsertString(nSel1, strText);
    }
    //设置列表框1当前选中项
    m_ctrlList1.SetCurSel(-1);

    if (m_ctrlList2.GetCount() == 0)
    {
        GetDlgItem(IDC_TEST2)->EnableWindow(FALSE);
        GetDlgItem(IDC_TEST4)->EnableWindow(FALSE);
    }
    GetDlgItem(IDC_TEST1)->EnableWindow(TRUE);
    GetDlgItem(IDC_TEST3)->EnableWindow(TRUE);
}

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

    //获得列表框2当前选中项
    int nSel2 = m_ctrlList2.GetCurSel();
    if (nSel2 == LB_ERR)
    {
        for (int n = 0; n < m_ctrlList1.GetCount(); n++)
        {
            //获得列表框1文本
            m_ctrlList1.GetText(n, strText);
            //向列表框2添加文本
            m_ctrlList2.AddString(strText);
        }
    }
    else
    {
        for (int n = 0; n < m_ctrlList1.GetCount(); n++)
        {
            //获得列表框1文本
            m_ctrlList1.GetText(n, strText);
            //向列表框2插入文本
            m_ctrlList2.InsertString(nSel2, strText);
        }    
    }

    //设置列表框2当前选中项
    m_ctrlList2.SetCurSel(-1);

    //清除列表框1内容
    m_ctrlList1.ResetContent();

    GetDlgItem(IDC_TEST1)->EnableWindow(FALSE);
    GetDlgItem(IDC_TEST3)->EnableWindow(FALSE);
    GetDlgItem(IDC_TEST2)->EnableWindow(TRUE);
    GetDlgItem(IDC_TEST4)->EnableWindow(TRUE);
}

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

    //获得列表框1当前选中项
    int nSel1 = m_ctrlList1.GetCurSel();
    if (nSel1 == LB_ERR)
    {
        for (int n = 0; n < m_ctrlList2.GetCount(); n++)
        {
            //获得列表框2文本
            m_ctrlList2.GetText(n, strText);
            //向列表框1添加文本
            m_ctrlList1.AddString(strText);
        }
    }
    else
    {
        for (int n = 0; n < m_ctrlList2.GetCount(); n++)
        {
            //获得列表框2文本
            m_ctrlList2.GetText(n, strText);
            //向列表框1插入文本
            m_ctrlList1.InsertString(nSel1, strText);
        }    
    }

    //设置列表框1当前选中项
    m_ctrlList1.SetCurSel(-1);

    //清除列表框2内容
    m_ctrlList2.ResetContent();

    GetDlgItem(IDC_TEST2)->EnableWindow(FALSE);
    GetDlgItem(IDC_TEST4)->EnableWindow(FALSE);
    GetDlgItem(IDC_TEST1)->EnableWindow(TRUE);
    GetDlgItem(IDC_TEST3)->EnableWindow(TRUE);
}

6.26 如何在列表框中添加水平滚动条

使用SendDlgItemMessage发送消息

CString strText = _T("");
for (int n = 0; n < 5; n++)
{
    strText += _T("Hello World! ");    
}

//向List添加文本
m_ctrlList.AddString(strText);

//发送LB_SETHORIZONTALEXTENT消息
SendDlgItemMessage(IDC_LIST, LB_SETHORIZONTALEXTENT, 400, 0);
原文地址:https://www.cnblogs.com/Clingingboy/p/1990035.html