简单控件基本应用

编辑框EditBox

读写数据

1、  利用向导关联一个CString变量即可。这是最简单,也是用的最多的一种方法。

2、  利用窗口指针,GetWindowText/ SetWindowText

A、  可以关联一个控件型指针(CEdit)。

B、 GetDlgItem(ID)得到窗口指针。例如:GetDlgItem(IDC_EDIT_INPUT)->SetWindowText(_T("7"));

RichEdit

初始化

使用前,一定要初始化控件: 一般在程序初始化函数中

AfxInitRichEdit(); 

关联控件变量:CRichEditCtrl m_richedit;使用其成员函数即可,具体查MSDN

设置文字

m_richedit.SetWindowText(tmp);

选中文字

m_richedit.SetSel(ret,ret+strlen);

滚动

m_richedit.LineScroll(line);

Listbox 控件

添加数据:

CString strAdd;

for (int i = 0; i < 10; i++)

{

strAdd.Format(_T("%d"), i);

     m_listBox.AddString(strAdd);

}

 

读取数据:

     CString strIsMulSel;

     CString strSel;

     int nSelCount = m_listBox.GetSelCount();

     if (nSelCount == LB_ERR)

     {

         strIsMulSel = _T("单选/n");

         m_listBox.GetText(m_listBox.GetCurSel(), strSel);

     }

     else

     {

         strIsMulSel = _T("多选/n");

         CArray<int, int> aryListBoxSel;

         aryListBoxSel.SetSize(nSelCount);

         m_listBox.GetSelItems(nSelCount, aryListBoxSel.GetData());

         for (int i = 0; i < nSelCount; i++)

         {

              CString strTmp;

              m_listBox.GetText(aryListBoxSel[i], strTmp);

              strSel += strTmp + _T("/n");

         }

 

     }

 

     MessageBox(strIsMulSel + strSel);

 

修改数据:

void CStdDlg1Dlg::OnBnClickedButtonXiugai()

{

     int nSel = m_listBox.GetCurSel();

     if (nSel != LB_ERR)

     {   

         m_listBox.DeleteString(nSel);

         m_listBox.InsertString(nSel, _T("aaa"));

     }

   

}

 

添加数据:

void CStdDlg1Dlg::OnBnClickedButtonAdd()

{

     int nSel = m_listBox.GetCurSel();

     if (nSel != LB_ERR)

     {   

         m_listBox.InsertString(nSel + 1, _T("aaa"));

     }

}

 

删除数据:

void CStdDlg1Dlg::OnBnClickedButtonDelete()

{

 

     int nSel = m_listBox.GetCurSel();

     if (nSel != LB_ERR)

     {   

         m_listBox.DeleteString(nSel);

     }

 

}

CListCtrl控件

listbox有些类似,但却又十分不同。

选择多行

         首先要设置Single Selection属性为FALSE

POSITION pos;  

 Int index;  

 pos = m_list.GetFirstSelectedItemPosition();  

 while (pos != NULL)

{  

         index = m_cList.GetNextSelectedItem(pos);  

          // do sth…

 }  

 

ComBo 控件

读取数据:

CString strSel;

     m_cmbBox.GetLBText(m_cmbBox.GetCurSel(), strSel); 

 

     MessageBox(strSel);

 

修改数据:

int nSel = m_cmbBox.GetCurSel();

     m_cmbBox.DeleteString(nSel);

     m_cmbBox.InsertString(nSel, _T("wkwlzhl"));

 

     m_cmbBox.SetCurSel(nSel);

 

添加数据:

int nSel = m_cmbBox.GetCurSel();

     m_cmbBox.InsertString(nSel + 1, _T("wkwlzhl"));

     m_cmbBox.SetCurSel(nSel + 1);

 

 

删除数据:

int nSel = m_cmbBox.GetCurSel();

     m_cmbBox.DeleteString(nSel);

     m_cmbBox.SetCurSel(nSel);

 

 

 

radio button 控件

设置选中:

A. 设置属性:

tab序号相邻,且最小序号控件的group属性设置为TRUE,关联一个int变量m_nRadio,其它radio无法关联。

m_ nRadio的值 与   选中   对应关系:

0                                                 1个选中

1                                                 2个选中

2                                                 3个选中

 

  依次类推

 

注意使用UpdateData()函数更新数据或界面。

 

当分多组时,设置几个group属性,有几组。

如:tab顺序:12345radio;当14groupTRUE时,123为一组;45为另一组。

 

B. 调用函数

// nCheck选中的ID值,读取选中

int nCheck = GetCheckedRadioButton(IDC_RADIO_MONEYIN, IDC_RADIO_MONEYOU);

 

 

// nCheck设置选中的ID值,设置选中

CheckRadioButton(IDC_RADIO_MONEYIN, IDC_RADIO_MONEYOUT, nCheck);

 

其中的下面2个参数:

IDC_RADIO_MONEYIN, IDC_RADIO_MONEYOU

IDIDC_RADIO_MONEYIN开始到IDC_RADIO_MONEYOU结束,中间的radio是一组。

 

 

CCButton类:

  // 设置选中。TRUE,选中;FALSE,不选中。

((CButton*)GetDlgItem(IDC_RADIO_MONEYOUT))->SetCheck(TRUE);

 

// 读取选中。1,选中;0未选中。

int nCheck = ((CButton*)GetDlgItem(IDC_RADIO_MONEYOUT2))->GetCheck();

 

 

 

 

Check box控件

设置选中

关联cbutton变量:

其成员函数:

SetCheck() 设置选中

GetCheck() 读取选中。

 

例:

CString str = _T("");

     if ( m_check_std.GetCheck())

     {

         str += _T("学习/n");

     }       

    

     if (m_check_stop.GetCheck())

     {

         str += _T("休息");

     }   

 

     MessageBox(_T("选中内容:/n") + str);

 

TreeControl控件

关联变量:CTreeCtrl m_tree;

在属性里面可以设置相应属性:如,Has lineHas button

添加节点

HTREEITEM root  = m_tree.InsertItem(_T("Alsmile"), 0);

     HTREEITEM child = m_tree.InsertItem(_T("3"), root, TVI_SORT);

     m_tree.InsertItem(_T("31"), child);

     m_tree.InsertItem(_T("32"), child);

     m_tree.InsertItem(_T("1"), root, TVI_SORT);

     m_tree.InsertItem(_T("2"), root);

 

     m_tree.Expand(root,TVE_EXPAND);

 

插入节点:InsertItem()

第一个参数,节点名;第二个参数,父节点;第三个,在哪个节点后面插入(默认最后),及属性。这里:TVI_SORT 排序。

 

展开:Expand()

第一个参数:哪一个节点展开;第二个,展开TVE_EXPAND,闭合TVE_COLLAPSE,与最后一次相反TVE_TOGGLE

 

添加节点图标

CImageList m_treeImageList; // 注意不要提前析构;一般作为成员变量。

 

// 加载图标

     m_treeImageList.Create(16, 16, ILC_MASK, 4, 1);

     m_treeImageList.Add(theApp.LoadIcon(IDI_ICON1));

     m_treeImageList.Add(theApp.LoadIcon(IDI_ICON2));

     m_treeImageList.Add(theApp.LoadIcon(IDI_ICON3));

     m_treeImageList.Add(theApp.LoadIcon(IDI_ICON4));

     m_tree.SetImageList(&m_treeImageList, LVSIL_NORMAL);

 

     HTREEITEM root  = m_tree.InsertItem(_T("Alsmile"), 0, 1, NULL);

     HTREEITEM child = m_tree.InsertItem(_T("3"), 2, 3, root, TVI_SORT);

     m_tree.InsertItem(_T("31"), 2, 3, child);

     m_tree.InsertItem(_T("32"), 2, 3, child);

     m_tree.InsertItem(_T("1"), 2, 3, root, TVI_SORT);

     m_tree.InsertItem(_T("2"), 2, 3, root);

 

     m_tree.Expand(root,TVE_EXPAND);

 

InsertItem(),多了中间第二个参数和第三个参数,分别表示正常图标和选中时的图标。

 

 

编辑节点

1、设置Edit Label 属性为True

2、为树控件的TVN_ENDLABELEDIT消息添加事件:

void CStdDlg::OnTvnEndlabeleditTree(NMHDR *pNMHDR, LRESULT *pResult)

{

     LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);

 

     // 设置节点文字

     m_tree.SetItemText(pTVDispInfo->item.hItem, pTVDispInfo->item.pszText);

 

     *pResult = 0;

}

 

选中事件

TVN_SELCHANGED消息:(NM_CLICK消息有点“滞后”)

// find the currently selected item

     HTREEITEM hCurSel  = m_tree.GetSelectedItem();

     // report it to the user

     if (hCurSel == NULL)

     {

         AfxMessageBox(_T("There is no selected item"));

     }

     else

     {

         CString str;

         str.Format(_T("The currently selected item is /"%s/""),

              (LPCTSTR)m_tree.GetItemText(hCurSel));

         MessageBox((LPCTSTR)str);

     }

加复选框

1、  设置check boxes属性为True

2、  读取选中内容

 

HTREEITEM item = m_tree.GetRootItem();

     CString str    = _T("");

     if(m_tree.GetCheck(item) != NULL)

     {

         str += m_tree.GetItemText(item) + _T("/n");

     }

 

MessageBox(str);

 

Picture Control控件

显示位图

1、  设置Type属性为bitmap

2、  关联变量CStatic m_picture

3、  加载bmp文件

 

CFileDialog dialog(TRUE, _T("bmp"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("位图文件(*.bmp)|*.bmp||"), this);

     if (dialog.DoModal() == IDOK)

     {

         CString str      = dialog.GetPathName();

         HBITMAP bitmap   = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), str, IMAGE_BITMAP,

              0 ,0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);

 

         if (bitmap != NULL)

         {

              m_picture.SetBitmap(bitmap);

         }

}

CBitmapButton的使用

1、在资源编辑的时候选中按钮的Owner  draw即可,不需要选择Bitmap属性!

2、在程序中定义一个CBitmapButton成员变量。不能使用ClassWizard为按钮映射一个CButton变量,然后改为CBitmapButton,这么做并不能将按钮直接映射为CBitmapButton类的对象,反而会出现初始化错误。

3- 1、使用CBitmapButton::LoadBitmaps装载各种状态的图片,使用SubclassDlgItem关联到想要的按钮,使用 CBitmapButton::SizeToContent函数使按钮适合图片大小。。注意Loadbitmaps一定要在关联到按钮之前进行!

3-2、或者是使用CBitmapButton::AutoLoad函数关联到想要的按钮。需要注意:

A、之前不能使用CBitmapButton::LoadBitmaps装载各种状态的图片,否则会出错。

BAutoLoad函数完成的关联和改变按钮大小的CBitmapButton::SizeToContent函数的功能。

C CBitmapButton::AutoLoad使用的位图是默认资源ID的,即它会自动装载相关资源位图。位图的资源ID格式为:"按钮Caption +U""按钮Caption+D""按钮Caption+F""按钮Caption+X",分别代表UpDownFocusDisable状态。如资源编辑时,希望关联的按钮的CaptionTest,那么其默认装载的位图资源的ID为: "TestU"/"TestD"/"TestF"/"TestX",注意分号""也是其ID的一部分。

 

PS:CBitmapButton要采用动态创建的方式,如:

    m_play = new CBitmapButton;

    m_play->Create("t",BS_PUSHBUTTON,CRect(4,12,100,30),this,IDC_BUTTON1);

    m_play->LoadBitmaps(IDB_BITMAPPLAY1,IDB_BITMAPPLAY3); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值