VC 6.0常用控件使用方法介绍

来源:互联网 发布:数据分析毕业论文题目 编辑:程序博客网 时间:2024/06/15 18:50

除非特别说明,本文中所用控件变量类型为Control
一般控件可用/不可用
EnableWindow(TRUE);
EnableWindow(FALSE);

1、Static Text------------静态控件 --类CStatic 
取值/赋值(变量类型为Control)
m_lbl.GetWindowText(string);
m_lbl.SetWindowText(string);

2、Edit Box---------------编辑控件 --类CEdit 
取值/赋值
m_txt.GetWindowText(string);
m_txt.SetWindowText(string);

3、Check Box------------复选控件 --类CButton 
(1)设置选中/未选中
m_chk.SetCheck(BST_CHECKED);    
m_chk.SetCheck(BST_UNCHECKED);
(2)判断是否选中
int nCur = m_chk.GetCheck();
nCur取值为 BST_CHECKED/BST_UNCHECKED。

4、Radio Box------------单选控件 --类 CButton 
(1)默认选中第一项
m_radio.SetCheck(BST_CHECKED);
(2)选中组中任一项
CWnd::CheckRadioButton 
void CheckRadioButton(int nIDFirstButton, int nIDLastButton, int nIDCheckButton); 
(3)判断哪一项被选中
CWnd::GetCheckedRadioButton 
int GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton);
(4)控件变量类型为Value时,可通过给int型变量赋值0、1、2...选中第1、2、3...个选项。
int型变量默认值为-1,是在构造函数中赋的值。
当然也可通过判断int型变量的值,知道哪一个选项被选中。

5、Combo Box-----------组合框控件 --类CComboBox 
(1)风格
Simple-象List Box一样显示数据
Dropdown-可以输入,也可以选择
Drop List-只能选择
(2)添加数据
a. 属性对话框->Data->Enter listbox items,用Ctrl+Enter换行;
b. m_combo.AddString(string);
c. m_combo.InsertString(index,string);
(3)显示数据 
设计页面,点击Combo Box Control右边的下拉箭头,显示的矩形框就是显示数据的区域。
(4)设置当前选项 
m_combo.SetCurSel(项索引);
(5)获取当前选项
int nIndex = m_combo.GetCurSel();
CString str;
m_combo.GetLBText(nIndex, str);
注:Combo Box Control会自动排序,数据显示顺序可能与预期不同,建议添加数据时用InsertString(索引,值)。

6、List Box---------------列表框控件 --类CListBox 
(1)插入项
m_list.AddString(string);
(2)设置当前选择项
m_list.SetCurSel(项索引);
(3)获取当前选择项
int nIndex = m_list.GetCurSel();
m_list.GetText(nIndex, string);
(4)删除一项
m_list.DeleteString(项索引);
(5)删除所有项
m_list.ResetContent();
(6)获取总项数
m_list.GetCount()
(7)List Box的选项前面加复选框(Check Box)
a.风格
声明时用类CCheckListBox代替CListBox,即CCheckListBox    m_list;而不是CListBox    m_list;
属性对话框->Styles->Owner draw设为Fixed
属性对话框->Styles->勾选Has strings
b.设置选择
void SetCheck(int nIndex, int nCheck);
Parameters
nIndex 
Index of the item whose check box is to be set.
nCheck 
State of the check box: 0 for clear, 1 for checked, and 2 for indeterminate.
c.获取选择
int GetCheck(int nIndex);
Parameters
nIndex 
Index of the item whose check status is to be retrieved.
Return Value 
Zero if the item is not checked, 1 if it is checked, and 2 if it is indeterminate.

7、List Control----------列表框扩展控件 --类CListCtrl 
(1)样式:属性对话框框->Styles->Format有4,分别是Icon/Small Icon/List/Report;
(2)Report格式设置扩展风格
DWORD dwStyle = m_list.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;        // 选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;            // 网格线(只适用与report风格的listctrl)
m_list.SetExtendedStyle(dwStyle);
(3)Report格式插入列
m_list.InsertColumn(1, "列一", LVCFMT_RIGHT, 150);
m_list.InsertColumn(2, "列二", LVCFMT_LEFT, 100);
m_list.InsertColumn(3, "列三", LVCFMT_LEFT, 100);
m_list.InsertColumn(4, "列四", LVCFMT_LEFT, 200);
m_list.InsertColumn(5, "ID", LVCFMT_CENTER, 0);
(4)Report格式插入一行数据
int nIndex = m_list.GetItemCount();
m_list.InsertItem(nIndex, s1);
m_list.SetItemText(nIndex, 1, s2);
m_list.SetItemText(nIndex, 2, s3);
m_list.SetItemText(nIndex, 3, s4);
m_list.SetItemText(nIndex, 4, s5);
(5)Report格式删除所有行
m_list.DeleteAllItems();
(6)Report格式获取某行某列数据
CString sID = m_list.GetItemText(行索引, 列索引);
(7)Report格式删除选择行,多选时可用循环。
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos != NULL)
{
       int nIndex = m_list.GetNextSelectedItem(pos);
       m_list.DeleteItem(nIndex);
}

8、Date Time Picker----日期时间控件--类CDateTimeCtrl 
(1)样式:属性对话框框->Styles->Format有3,分别是Short Date/Long Date/Time,分别显示短日期(2009-03-12)/长日期(2009年3月12日)/时间(20:08:06),日期格式默认有一向下箭头,时间格式默认有一Spin Control;
(2)可编程设置其显示格式,例如年4位,月、日、时、分、秒2位,
CString formatStr= _T("yyyy-MM-dd");
m_txtDate.SetFormat(formatStr);
formatStr= _T("HH:mm:ss");
m_txtTime.SetFormat(formatStr);
(3)取值赋给CString
m_txtDate.GetWindowText(sAddDate);
m_txtTime.GetWindowText(sAddTime);

9、Spin------------------旋转按钮控件 --类CSpinButtonCtrl 
(1)与Edit控件关联
首先,排列控件的Tab键顺序,要让Spin Control的Tab Order紧跟着Edit Control(就是说,Spin Control的Tab Order是Edit Control的Tab Order加1);设置tab order 的方法是 Ctrl+d,然后用鼠标挨个点击,就是按TAB键时焦点在窗体上的移动顺序; 
然后,Spin Control属性对话框中勾选Auto buddy和Set buddy integer。
(2)设置上下限
m_spin.SetRange(1, 60);
(3)设置当前值,可以不用给Edit控件赋值
m_spin.SetPos(3);
(4)获取当前值
int nCur = m_spin.GetPos();

10、Slider-----------------滑动条控件--类CSliderCtrl 
(1)设置上下限、最小滑动值
m_slider.SetRange(5,100);
m_slider.SetTicFreq(1);
(2)设置/获取当前值
m_slider.SetPos(nCur);
int nCur = m_slider.GetPos();
(3)背景色:重写OnCtlColor(),虽然不知道Slider属于nCtlColor的哪一类,但试验表明似乎是属于CTLCOLOR_STATIC。
HBRUSH CDlgOptionVideo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    // 设置透明背景模式
    pDC->SetBkMode(TRANSPARENT);
    
    // TODO: Return a different brush if the default is not desired
    switch(nCtlColor)
    {
        // 设置背景刷子为空
        case CTLCOLOR_STATIC:    // 静态控件
            if(pWnd->GetDlgCtrlID() == IDC_SLIDER_TIME)
                 return ::CreateSolidBrush(RGB(203, 228, 253));
        case CTLCOLOR_DLG:        // 对话框 
            return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
        default:
            return hbr;
    }
}

0 0
原创粉丝点击