静态文本框
1
| m_stext.SetWindowTextW(TEXT("Hello"));
|
编辑框
基本函数
1 2 3 4 5 6
| m_Edit1.SetSel(0,-1); m_Edit1.ReplaceSel(""); m_Edit1.Copy(); m_Edit2.Paste(); m_Edit1.Undo(); m_Edit1.GetWindowText(str, 10);
|
数值与控件转换
IDC_EDIT1 绑定 int 变量 m_A
1 2 3 4 5 6
| UpdateData(TRUE);
int i=m_A*m_B; m_Result.Format("%d", i);
UpdateData(FALSE);
|
图像框
组框 Group Box
按钮
按下按钮
1
| m_btn1.SetWindowText("你已按下了按钮!");
|
复选框
1 2 3 4
| if(m_chk1.GetCheck()>0) m_chk1.SetCheck(0); else m_chk1.SetCheck(1);
|
单选框
按下单选框
初始化选中
给一组单选按钮中的一个指定按钮加上选中标志,并且清除组中其他按钮的选中标志。
1
| BOOL CheckRadioButton([HWND hDlg,] int nlDFirstButton, int nlDLastBUtton, int nlDCheckButton)
|
获取状态
1 2 3 4 5
| int iSexRADIO = GetCheckedRadioButton(IDC_BOY_RADIO, IDC_GIRL_RADIO); if (iSexRADIO == IDC_BOY_RADIO) ; else ;
|
组合框
初始化时添加下拉列表框项目
1
| m_cb.AddString("Monday");
|
初始化时选中字符串
SelectString
:搜索在组合框的列表框中的字符串,如果找到该字符串,选择列表框和复制的字符串到编辑控件。
1
| m_Combo.SelectString(-1, "a");
|
获取并弹出选中项目
1 2 3
| CString msg; m_cb.GetLBText(m_cb.GetCurSel(), msg); AfxMessageBox(msg);
|
列表框
水平滚动条
垂直滚动条
旋转 Spin
滑块的滚动事件在对话框的事件里,与其他控件不同。
为什么?它就是这样设计的。
初始化
1 2
| CSpinButtonCtrl* pSpin =(CSpinButtonCtrl*) GetDlgItem(IDC_SPIN1); pSpin->SetRange(0, 100);
|
滚动事件
1 2 3 4 5 6 7 8 9 10
| void CTestDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { if (pScrollBar->GetDlgCtrlID() == IDC_SPIN1) { CString strValue; strValue.Format("%3.1f", (double) nPos / 10.0); ( (CSpinButtonCtrl*) pScrollBar )->GetBuddy()->SetWindowText(strValue); } CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }
|
进度条 Progress
1 2 3 4
| CProgressCtrl* pProg = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS1); pSlide1->SetRange(0, 100); pProg->SetPos(0); pProg->SetPos( pProg->GetPos()+1 );
|
滑块
滑块事件也是在对话框滚动事件里面。
1 2 3 4 5 6 7 8 9 10
| void CTestDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { if(pScrollBar->GetDlgCtrlID() == IDC_SLIDER1) { CSliderCtrl* pSlide = (CSliderCtrl*) pScrollBar; CString strText; strText.Format("%d", pSlide->GetPos()); SetDlgItemText(IDC_STATIC_SLIDER, strText); } }
|
热键
列表控件
初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| static char * color[] = {"white","black","red","blue","yellow","cyan","purple","green"}; HICON hIcon[8]; int n; m_imageList.Create(16,16,0,8,8); hIcon[0] = AfxGetApp()->LoadIcon(IDI_ICON_WHITE); hIcon[1] = ...略... for (n=0;n<8;n++) m_imageList.Add(hIcon[n]);
CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST1); pList->SetImageList(&m_imageList, LVSIL_SMALL); for (n = 0; n < 8; n++) pList->InsertItem(n, color[n], n); pList->SetBkColor(RGB(0, 255, 255)); pList->SetTextBkColor(RGB(255, 0, 255));
|
事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| void CTestDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST1); int nSelected = pNMListView->iItem; if (nSelected >= 0) { CString strItem = pList->GetItemText(nSelected, 0); SetDlgItemText(IDC_STATIC_LIST, strItem); } *pResult = 0; }
void CTestDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST1); int nSelected = pNMListView->iItem; if (nSelected >= 0) pList->EditLabel(nSelected); *pResult = 0; }
void CTestDlg::OnEndlabeleditList1(NMHDR* pNMHDR, LRESULT* pResult) { LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR; LVITEMA item = pDispInfo->item; CString str =item.pszText; str.TrimLeft(); str.TrimRight(); if(str.GetLength() > 0) { CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST1); pList->SetItemText(item.iItem, item.iSubItem, item.pszText); } *pResult = 0; }
|
树状控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE1); pTree->SetImageList(&m_imageList, TVSIL_NORMAL);
TV_INSERTSTRUCT tvinsert; tvinsert.hParent = NULL; tvinsert.hInsertAfter = TVI_LAST; tvinsert.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE| TVIF_TEXT; tvinsert.item.hItem = NULL; tvinsert.item.state = 0; tvinsert.item.stateMask = 0; tvinsert.item.cchTextMax = 6; tvinsert.item.iSelectedImage = 1; tvinsert.item.cChildren = 0; tvinsert.item.lParam = 0;
tvinsert.item.iImage = 2;
tvinsert.item.pszText = "father"; HTREEITEM hDad = pTree ->InsertItem(&tvinsert);
tvinsert.item.pszText = "mother"; HTREEITEM hMom = pTree->InsertItem(&tvinsert);
tvinsert.hParent = hDad; tvinsert.item.iImage = 3;
tvinsert.item.pszText = "son"; pTree->InsertItem(&tvinsert);
tvinsert.item.pszText = "daughter"; pTree->InsertItem(&tvinsert);
tvinsert.hParent = hMom; tvinsert.item.iImage = 4;
tvinsert.item.pszText = "cartoon"; HTREEITEM hOther = pTree->InsertItem(&tvinsert);
tvinsert.hParent = hOther;
|
事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| void CTestDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE1); HTREEITEM hSelected = pNMTreeView->itemNew.hItem; if (hSelected != NULL) { char text[31]; TV_ITEM item; item.mask = TVIF_HANDLE | TVIF_TEXT; item.hItem = hSelected; item.pszText = text; item.cchTextMax = 30; VERIFY(pTree->GetItem(&item)); SetDlgItemText(IDC_STATIC_TREE, text); } *pResult = 0; }
void CTestDlg::OnEndlabeleditTree1(NMHDR* pNMHDR, LRESULT* pResult) { TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR; TVITEMA item = pTVDispInfo->item; CString str =item.pszText; str.TrimLeft(); str.TrimRight(); if(str.GetLength() > 0) { CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE1); pTree->SetItemText(item.hItem,item.pszText); } *pResult = 0; }
|
Tab控件
动画
富文本 Rich Edit
月历
日期时间选择器
1 2 3 4 5 6 7 8
| CDateTimeCtrl* pDT =(CDateTimeCtrl*) GetDlgItem(IDC_DATETIMEPICKER1); CString formatStr= _T("'今天是: 'yy'/'MM'/'dd"); pDT->SetFormat(formatStr);
CTime t; pDT->GetTime(t); CString s = t.Format( "%A, %B %d, %Y %H:%M:%S" ); AfxMessageBox(s);
|
IP地址
自定义控件
扩展组合框