CButto n ID Get Check Set Check CWnd
CButto n类的 成� 函 数 在知道按钮ID的前 Get. Check( ) 提下,还可使用与 Set. Check( ) 按钮有关的CWnd Get. Bitmap() Set. Bitmap() 成员函数来设置或 Get. Button. Styl 查询按钮状态 e() Set. Button. Styl e() Get. Cursor() Check. Dlg. Button Set. Cursor() Check. Radio. Button Get. Checked. Radio. Butto Get. Icon() Is Dlg. Button. Checked Get. Window. Tex Set. Icon() Get. State() Get. Window. Text. Length Set. Window. Text 7 Set. State()
void CMy 9_1 Dlg: : On. Button 1() { m_btn 1. Set. Window. Text(“你已按下了按� !”); } void CMy 9_1 Dlg: : On. Button 2() { m_btn 2. Set. Window. Text(“按� 已被按下! ”); } void CMy 9_1 Dlg: : On. Check 1() { if(m_chk 1. Get. Check()>0) m_chk 1. Set. Check(0); else m_chk 1. Set. Check(1); } 12
(2) 应用程序的代码编程部分 (a) 给滚动条连接变量 ID 变量名 类别 类型 IDC_SCROLLBAR m_Scrollbar CScrollbar IDC_EDITl m_Edit Control CEdit 19
(b) 初始化滚动条 BOOL CMy 9_2 Dlg: : On. Init. Dialog() { CDialog: : On. Init. Dialog(); …… // TODO: Add extra initialization here m_Scrollbar. Set. Scroll. Range(0, 20); m_Scrollbar. Set. Scroll. Pos(10); char s. Pos[10]; itoa(m_Scrollbar. Get. Scroll. Pos(), s. Pos, 10); m_Edit. Sel(0, -1); m_Edit. Replace. Sel(s. Pos); Update. Data(FALSE); return TRUE; } 10进 制数 20
(3) 给滚动条消息添加代码 void CMy 9_2 Dlg: : On. VScroll(……) { // TODO: Add your message handler code here char s. Pos[10]; int i. Now. Pos; switch(n. SBCode) { if(p. Scroll. Bar==&m_Scrollbar) { case SB_THUMBTRACK: //拖动滑块 m_Scrollbar. Set. Scroll. Pos(n. Pos); itoa(n. Pos, s. Pos, 10); m_Edit. Sel(0, -1); 21
case SB_LINEDOWN: //单击滚动条向下的箭 头 i. Now. Pos=m_Scrollbar. Get. Scroll. Pos(); i. Now. Pos=i. Now. Pos+1; if(i. Now. Pos>20) i. Now. Pos=20; m_Scrollbar. Set. Scroll. Pos(i. Now. Pos); itoa(m_Scrollbar. Get. Scroll. Pos(), s. Pos, 10); m_Edit. Sel(0, -1); m_Edit. Replace. Sel(s. Pos); 22 break;
case SB_LINEUP: //单击滚动条向上的 箭头 i. Now. Pos=m_Scrollbar. Get. Scroll. Pos() ; i. Now. Pos=i. Now. Pos-1; if(i. Now. Pos<0) i. Now. Pos=0; m_Scrollbar. Set. Scroll. Pos(i. Now. Pos); itoa(m_Scrollbar. Get. Scroll. Pos(), s. Pos, 10); m_Edit. Sel(0, -1); 23
case SB_PAGEDOWN: //单击箭头与滚块之间的 区域 i. Now. Pos=m_Scrollbar. Get. Scroll. Pos(); i. Now. Pos=i. Now. Pos+3; if(i. Now. Pos>20) i. Now. Pos=20; m_Scrollbar. Set. Scroll. Pos(i. Now. Pos); itoa(m_Scrollbar. Get. Scroll. Pos(), s. Pos, 10); m_Edit. Sel(0, -1); m_Edit. Replace. Sel(s. Pos); 24 break;
case SB_PAGEUP: //单击箭头与滚块之间的 区域 i. Now. Pos=m_Scrollbar. Get. Scroll. Pos(); i. Now. Pos=i. Now. Pos-3; if(i. Now. Pos<0) i. Now. Pos=0; m_Scrollbar. Set. Scroll. Pos(i. Now. Pos); itoa(m_Scrollbar. Get. Scroll. Pos(), s. Pos, 10); m_Edit. Sel(0, -1); m_Edit. Replace. Sel(s. Pos); break; } } 25
(4) 给Exit按钮连接代码 void CMy 9_2 Dlg: : On. Exit. Button() { On. OK(); } (5) 给Up按钮添代码 void CMy 9_2 Dlg: : On. Up. Button() { m_Scrollbar. Set. Scroll. Pos(0); m_Edit. Sel(0, -1); m_Edit. Replace. Sel("0"); } 26
(6) 给 Down按钮添加代码 void CMy 9_2 Dlg: : On. Down. Button() { m_Scrollbar. Set. Scroll. Pos(20); m_Edit. Sel(0, -1); m_Edit. Replace. Sel("20"); } (7) 给Reset按钮添加代码 void CMy 9_2 Dlg: : On. Reset. Button() { m_Scrollbar. Set. Scroll. Pos(10); m_Edit. Sel(0, -1); m_Edit. Replace. Sel("10"); } 27
(4) 在On. Init. Dailog函数中添加如下代码,设置控件 为位图风格,并设置位图 BOOL CMy 9_3 Dlg: : On. Init. Dialog() { …… // TODO: Add extra initialization here m_bmp. Modify. Style(0, SS_BITMAP); HBITMAP h. Bmp=Load. Bitmap(Afx. Get. Instance. Handle(), MAKEINTRESOURCE(IDB_BITMAP 1)); m_bmp. Set. Bitmap(h. Bmp); return TRUE; } 33
(5) 响应鼠标单击静态控件的消息。为static控件添 加BN_CLICKED消息的响应 void CMy 9_3 Dlg: : On. Static. Bmp() { // TODO: Add your control notification handler code here BITMAP bmp; Get. Object(m_bmp. Get. Bitmap(), sizeof(BITMAP ), &bmp); CString msg; msg. Format("Image Size %d*%d", bmp. bm. Width, bmp. bm. Height); Afx. Message. Box(msg); 34
37
(3)在On. Init. Dialog函数中添加初始化列表框内容的代 码 BOOL CMy 9_4 Dlg: : On. Init. Dialog() { CDialog: : On. Init. Dialog(); // TODO: Add extra initialization here 获得当 DWORD cch. Cur. Dir = MAX_PATH; 前目录 LPTSTR lpsz. Cur. Dir; CHAR tch. Buffer[MAX_PATH]; lpsz. Cur. Dir = tch. Buffer; Get. Current. Directory(cch. Cur. Dir, lpsz. Cur. Dir); Dlg. Dir. List(lpsz. Cur. Dir, IDC_LIST_DIR, 设置列表框显示条目为当前目 IDC_STATIC_DIR, 0); 录下所有文件名 52 return TRUE;
(4)为了实现双击条目删除的功能,需要响应列表框的 LBN_DBLCLK消息 void CMy 9_4 Dlg: : On. Dblclk. List. Dir() { // TODO: Add your control notification handler code here int i = m_list. Get. Cur. Sel(); CString str; m_list. Get. Text(i, str); m_list. Delete. String(i); CString msg = "Item "+str+" deleted!"; Afx. Message. Box(msg); 53 }
56
(2) 给 Clear 1按钮连接代码 void CMy 9_5 Dlg: : On. Clear 1 Button() { m_Edit 1. Set. Sel(0, -1); //选中IDC_EDIT 1中的内容 m_Edit 1. Replace. Sel(""); //用空串代替所选文本,即把所选的文本删除 } 68
(3) 给Show 2和Clear 2按钮连接代码 void CMy 9_5 Dlg: : On. Show 2 Button() { m_Edit 2. Set. Sel(0, -1); m_Edit 2. Replace. Sel("This is the second Edit. Box. "); } void CMy 9_5 Dlg: : On. Clear 2 Button() { m_Edit 2. Set. Sel(0, -1); m_Edit 2. Replace. Sel(""); } 69
(4) 给按钮IDC_Transfer_BUTTON连接代码 void CMy 9_5 Dlg: : On. Transfer. Button( { m_Edit 1. Set. Sel(0, -1); m_Edit 1. Copy(); m_Edit 2. Set. Sel(0, -1); m_Edit 2. Replace. Sel(""); m_Edit 2. Paste(); } 70
(5)给 Undo按钮连接代码 void CMy 9_5 Dlg: : On. Undo. Button() { m_Edit 1. Undo(); m_Edit 2. Undo(); }(6)给Exit按钮连接代码 void CMy 9_5 Dlg: : On. Exit. Button() { On. OK(); } 71
(1) 初始化结果编辑框 BOOL CMy 9_6 Dlg: : On. Init. Dialog() { CDialog: : On. Init. Dialog(); …… // TODO: Add extra initialization here Update. Data(TRUE); int i=m_A*m_B; m_Result. Format("%d", i); Update. Data(FALSE); return TRUE; 73
(2)当编辑框中的内容发生改变时,会产生 EN_CHANGE消息,需要为乘数与被乘数两个编辑 框响应该消息 void CMy 9_6 Dlg: : On. Change. Edit. Mul 1( ) { Update. Data(TRUE); int i=m_A*m_B; m_Result. Format("%d", i); Update. Data(FALSE); } void CMy 9_6 Dlg: : On. Change. Edit. Mul 2( ) { Update. Data(TRUE); int i=m_A*m_B; m_Result. Format("%d", i); Update. Data(FALSE); 74 }
(3) 响应Reset按钮的消息 void CMy 9_6 Dlg: : On. Reset() { m_A = 0; m_B = 0; int i=m_A*m_B; m_Result. Format("%d", i); Update. Data(FALSE); } 75
将一个Combo Box控件放到对话框上。取消 Sort风格。否则插入的内容将按照字母顺序排序, 而不是插入的顺序排序。添加CCombo. Box类型的 变量m_cb BOOL CMy 9_7 Dlg: : On. Init. Dialog() //初始化对话框 { CDialog: : On. Init. Dialog(); …… // TODO: Add extra initialization here m_cb. Add. String("Monday"); m_cb. Add. String("Tuesday"); m_cb. Add. String("Wednesday"); m_cb. Add. String("Thursday"); m_cb. Add. String("Friday"); m_cb. Add. String("Saturday"); m_cb. Add. String("Sunday"); 80 return TRUE;
当用户选择的内容发生改变的时候,会产 生CBN_SELCHANGE消息。为控件添加 该消息的响应函数: void CMy 9_7 Dlg: : On. Selchange. Combo() { CString msg; m_cb. Get. LBText(m_cb. Get. Cur. Sel(), msg); Afx. Message. Box(msg); } 81
Visual C++按照放入对话框中的先后顺序,给每个控件赋 一个ID值,所以控件的ID值是连续的。Group属性的控件 之间的控件为一组。 可以从resource. h的资源头文件中得到ID值 #define IDC_SHOW_BUTTON 1006 #define IDC_HIDE_BUTTON 1007 #define #define IDC_Boy_RADIO IDC_Girl_RADIO IDC_Age 1_RADIO IDC_Age 2_RADIO IDC_Age 3_RADIO 1008 1009 1010 1011 1012 #define IDC_Show_Sex_Age_BUTTON #define IDC_Result_EDIT 1013 1014 84
在为相关控件连接变量和方法后,开始编程 (1) 给复选框IDC_DATE_CHECK添加代码 On. Date. Check()方法的实现代码如下: 以当前的屏幕 void CMy 9_8 Dlg: : On. Data. Check() 显示内容更新 { Update. Data(TRUE); 控件的变量 if(m_Date. Check==TRUE) { CTime t. Now ; t. Now=CTime: : Get. Current. Time(); CString s. Now=t. Now. Format("%y. %m. %d"); m_Date. Edit. Sel(0, -1); m_Date. Edit. Replace. Sel(s. Now); } else { m_Date. Edit. Sel(0, -1); m_Date. Edit. Replace. Sel(""); } Update. Data(FALSE); 85 }
(2)为复选框IDC_TIME_CHECK添加代码 void CMy 9_8 Dlg: : On. Time. Check() { Update. Data(TRUE); } if(m_Time. Check==TRUE) { CTime t. Now; t. Now=CTime: : Get. Current. Time(); CString s. Now=t. Now. Format("%I: %M: %S"); m_Time. Edit. Sel(0, -1); m_Time. Edit. Replace. Sel(s. Now); } 函数Format的参 else 数%I是时间的小 { m_Time. Edit. Sel(0, -1); 时表示法(01~ m_Time. Edit. Replace. Sel(""); 12),%m是分的 } 表示法(00~ 59) Update. Data(FALSE); ,%d是秒的表示 86 法(00~ 59)
(3) 给Enable按钮添加代码 void CMy 9_8 Dlg: : On. Enable. Button() { Get. Dlg. Item(IDC_DATE_CHECK)>Enable. Window(TRUE); Get. Dlg. Item(IDC_TIME_CHECK)>Enable. Window(TRUE); m_Date. Edit. Enable. Window(TRUE); m_Time. Edit. Enable. Window(TRUE); } 87
(4) 为Disable按钮连接代码 void CMy 9_8 Dlg: : On. Disable. Button() { Get. Dlg. Item(IDC_DATE_CHECK)>Enable. Window(FALSE); Get. Dlg. Item(IDC_TIME_CHECK)>Enable. Window(FALSE); m_Date. Edit. Enable. Window(FALSE); m_Time. Edit. Enable. Window(FALSE); } 88
(5) 为Show Again按钮添加代码 void CMy 9_8 Dlg: : On. Show. Button() { Get. Dlg. Item(IDC_DATE_CHECK)>Enable. Window(SW_SHOW); Get. Dlg. Item(IDC_TIME_CHECK)>Enable. Window(SW_SHOW); Get. Dlg. Item(IDC_DATE_EDIT)>Enable. Window(SW_SHOW); m_Date. Edit. Show. Window(SW_SHOW); Get. Dlg. Item(IDC_TIME_EDIT)>Enable. Window(SW_SHOW); m_Time. Edit. Show. Window(SW_SHOW); 89
(6) 为Hide the Clock Setting按钮添加实现代码 void CMy 9_8 Dlg: : On. Hide. Button() { Get. Dlg. Item(IDC_DATE_CHECK)>Enable. Window(SW_HIDE); Get. Dlg. Item(IDC_TIME_CHECK)>Enable. Window(SW_HIDE); m_Date. Edit. Show. Window(SW_HIDE); m_Time. Edit. Show. Window(SW_HIDE); } 90
(7) 给Show_the_Sex_and_Age按钮添加代码 void CMy 9_8 Dlg: : On. Show. Sex. Age. BUTTON() { char s. Edit[50]; int i. Sex. RADIO; int i. Age. RADIO; i. Sex. RADIO=Get. Checked. Radio. Button(IDC_Boy_RADIO, IDC_ Girl_RADIO); if(i. Sex. RADIO==IDC_Boy_RADIO) strcpy(s. Edit, "The boy's age is"); if(i. Sex. RADIO==IDC_Girl_RADIO) strcpy(s. Edit, "The girl's age is"); i. Age. RADIO=Get. Checked. Radio. Button(IDC_Age 1_RADIO, IDC_Ag e 3_RADIO); if(i. Age. RADIO==IDC_Age 1_RADIO) strcat(s. Edit, " great than 20"); if(i. Age. RADIO==IDC_Age 2_RADIO) strcat(s. Edit, " between 15 and 20"); if(i. Age. RADIO==IDC_Age 3_RADIO) strcat(s. Edit, " less than 15"); m_Result. Edit. Sel(0, -1); 91
(8) 为Show_Combo按钮添加代码 void CMy 9_8 Dlg: : On. Show. Combo. Button() { Update. Data(TRUE); char s. Course. Edit[30]; char s. Record. Edit[15]; int i. Course. Radio; i. Course. Radio=Get. Checked. Radio. Button(IDC_ENGLISH_RADIO, IDC_NATUR E_RADIO); if(i. Course. Radio==IDC_ENGLISH_RADIO) strcpy(s. Course. Edit, "English record is "); if(i. Course. Radio==IDC_COMPUTER_RADIO) strcpy(s. Course. Edit, "Computer record is "); if(i. Course. Radio==IDC_NATURE_RADIO) strcpy(s. Course. Edit, "Natural record is "); m_Record. Get. Window. Text(s. Record. Edit, 15); strcat(s. Course. Edit, ""); strcat(s. Course. Edit, s. Record. Edit); m_Combo. Edit=_T(s. Course. Edit); Update. Data(FALSE); 92 }
(9) 给On. Exit. Button()方法添加代码 void CMy 9_8 Dlg: : On. Exit. Button() { On. OK(); } 93
初始化单选按钮 BOOL CMy 9_8 Dlg: : On. Init. Dialog() { CDialog: : On. Init. Dialog(); …… // TODO: Add extra initialization here Check. Radio. Button(IDC_Boy_RADIO, IDC_Girl_RADIO, IDC_Boy_RAD IO); Check. Radio. Button(IDC_Age 1_RADIO, IDC_Age 3_RADIO, IDC_ Age 2_RADIO); Update. Data(FALSE); m_English=0; m_Record. Add. String("85"); m_Record. Add. String("90"); m_Record. Add. String("95"); m_Record. Select. String(-1, "95"); Update. Data(FALSE); return TRUE; } 94
向对话框拖放一个Edit控件,置为只读,然后拖放一 个Spin控件紧挨着刚才拖放的Edit控件,两个控件 的ID都是用默认值,设置Spin控件的【Allignment 】属性为“Right”,选中【Auto buddy】属性 BOOL CEx 9_9 Dlg: : On. Init. Dialog() { …… // TODO: Add extra initialization here CSpin. Button. Ctrl* p. Spin =(CSpin. Button. Ctrl*) Get. Dlg. Item(IDC p. Spin->Set. Range(0, 100); p. Spin->Set. Pos(50); p. Spin->Get. Buddy()->Set. Window. Text("5. 0"); return TRUE; } 设置Spin的范围是 0~ 100, 当前位置是 50,同时设置它 的配对控件的显示值 99
在对话框中添加WM_VSCROLL消息的响应: void CEx 9_9 Dlg: : On. VScroll(……) { if (p. Scroll. Bar->Get. Dlg. Ctrl. ID() == IDC_SPIN 1) {CString str. Value; str. Value. Format("%3. 1 f", (double) n. Pos / 10. 0); ((CSpin. Button. Ctrl*)p. Scroll. Bar)->Get. Buddy()>Set. Window. Text(str. Value); } CDialog: : On. VScroll(n. SBCode, n. Pos, p. Scroll. Bar); } 100
添加一个Progress控件,保持默认ID,设置 Smooth属性。在旁边添加一个按钮,设置ID 为“IDC_BUTTON_START”,Caption为“开 始” 在On. Init. Dialog中添加如下代码: CProgress. Ctrl* p. Prog =(CProgress. Ctrl*) Get. Dlg. Item(IDC_PROGRESS 1); p. Prog->Set. Range(0, 100); p. Prog->Set. Pos(50); 为“开始”按钮添加点击事件实现代码: void CEx 9_9 Dlg: : On. Button. Star() {CProgress. Ctrl* p. Prog=(CProgress. Ctrl*)Get. Dlg. Item(IDC_PROGR p. Prog->Set. Pos(0); Set. Timer(1000, 100, NULL); } 102
在CCtrl. Dlg中添加对WM_TIMER消息的响应函 数: void CEx 9_9 Dlg: : On. Timer(UINT n. IDEvent) { if(n. IDEvent == 1000) { CProgress. Ctrl* p. Prog = (CProgress. Ctrl*) Get. Dlg. Item(IDC_PROGRESS 1); 每隔 0. 1秒, p. Prog->Set. Pos(p. Prog->Get. Pos()+1); 进度条前 if(p. Prog->Get. Pos() >= 100) 进一步 { Kill. Timer(n. IDEvent); Afx. Message. Box("进行完毕"); } } CDialog: : On. Timer(n. IDEvent); 103 }
在对话框上增加一个Slider控件,设置 Point属性为“Bottom/Right”,然后在旁边 添加一个Static控件,ID设置为 IDC_STATIC_SLIDER。该控件用来显示滑 块的当前位置。 在On. Init. Dialog函数中添加如下代码: CString str. Text 1; CSlider. Ctrl* p. Slide 1 = (CSlider. Ctrl*) Get. Dlg. Item(IDC_SLIDER 1); p. Slide 1 ->Set. Range(0, 100); p. Slide 1 ->Set. Pos(50); str. Text 1. Format("%d", p. Slide 1 ->Get. Pos()); Set. Dlg. Item. Text(IDC_STATIC_SLIDER, str. Text 1); 105
为了响应滑块移动的消息,添加WM_HSCROLL 消息的响应(Slider是水平的,如果是垂直的, 则需要响应WM_VSCROLL)。实现如下: void CEx 9_9 Dlg: : On. HScroll(……) { if(p. Scroll. Bar->Get. Dlg. Ctrl. ID() == IDC_SLIDER 1) { CSlider. Ctrl* p. Slide = (CSlider. Ctrl*) p. Scroll. Bar; CString str. Text; str. Text. Format("%d", p. Slide->Get. Pos()); Set. Dlg. Item. Text(IDC_STATIC_SLIDER, str. T 106 ext);
在对话框上添加一个Date Time Picker控件, 设置Format为“Short Date”,选择“Use Spin Control”,如果不选择使用Spin控件, 则用户在弹出的日历控件中进行输入。在该 控件旁边添加一个按钮,ID为 “IDC_BUTTON_TIME”,Caption为“报时”。 在On. Init. Dialog中添加如下代码: CDate. Time. Ctrl* p. DT = (CDate. Time. Ctrl*) Get. Dlg. Item(IDC_DATETIMEPICKER 1); CString format. Str= _T("'今天是: 'yy'/'MM'/'dd"); p. DT->Set. Format(format. Str); 108
添加对【报时】按钮的点击事件的响应函数: void CEx 9_9 Dlg: : On. Button. Time() { CDate. Time. Ctrl* p. DT =(CDate. Time. Ctrl*) Get. Dlg. Item(IDC_DATETIMEPICKER 1); CTime t; p. DT->Get. Time(t); CString s = t. Format( "%A, %B %d, %Y %H: %M: %S" ); Afx. Message. Box(s); } 109
9. 8. 6 List Control控件的使用 列表控件 110
为使用列表控件,首先需要创建图标资源,在 Resource. View的Icon一栏内创建 8个图标资 源 在Cex 9_9 Dlg类中增加成员如下: CImage. List m_image. List; 在On. Init. Dialog函数中添加初始化图片列表的代码: HICON h. Icon[8]; int n; m_image. List. Create(16, 0, 8, 8); h. Icon[0] = Afx. Get. App()>Load. Icon(IDI_ICON_WHITE); h. Icon[1] = Afx. Get. App()>Load. Icon(IDI_ICON_BLACK); h. Icon[2] = Afx. Get. App()>Load. Icon(IDI_ICON_RED); h. Icon[3] = Afx. Get. App()>Load. Icon(IDI_ICON_BLUE); h. Icon[4] = Afx. Get. App()>Load. Icon(IDI_ICON_YELLOW); 113 h. Icon[5] = Afx. Get. App()-
为了创建控件,首先在On. Init. Dialog中添加如下代码: CList. Ctrl* p. List = (CList. Ctrl*) Get. Dlg. Item(IDC_LIST 1); // 获得控件对象 p. List->Set. Image. List(&m_image. List, LVSIL_SMALL); // 设置小图标图片列表 for (n = 0; n < 8; n++) { // 第一个参数为项id,第二个为标签文字,第三个为 对应图片列表id p. List->Insert. Item(n, color[n], n); } p. List->Set. Bk. Color(RGB(0, 255)); // 设置 背景色 p. List->Set. Text. Bk. Color(RGB(255, 0, 255)); 115 //设置文字的背景色
在 列 表 控 件 下 添 加 static控 件 , ID为 IDC_STATIC_LIST。 对 列 表 控 件 添 加 对 LVN_ITEMCHANGED消息的响应: void CEx 9_9 Dlg: : On. Itemchanged. List 1(……) { NM_LISTVIEW* p. NMList. View = (NM_LISTVIEW*)p. NMHDR; // TODO: Add your control notification handler code here CList. Ctrl* p. List = (CList. Ctrl*) Get. Dlg. Item(IDC_LIST 1); int n. Selected = p. NMList. View->i. Item; if (n. Selected >= 0) { CString str. Item = p. List>Get. Item. Text(n. Selected, 0); 116 Set. Dlg. Item. Text(IDC_STATIC_LIST, str. Item);
在设置控件属性时,设置了Edit labels属性,下面 是对列表控件的NM_RCLICK消息的响应: void CEx 9_9 Dlg: : On. Rclick. List 1(……) { // TODO: Add your control notification handler code here NM_LISTVIEW* p. NMList. View = (NM_LISTVIEW*)p. NMHDR; CList. Ctrl* p. List = (CList. Ctrl*) Get. Dlg. Item(IDC_LIST 1); int n. Selected = p. NMList. View->i. Item; if (n. Selected >= 0) 117 p. List->Edit. Label(n. Selected);
运行程序,右击某一项,已可以编辑标签了,但无 法保存编辑效果。若要保存,需要响应列表控件的 LVN_ENDLABELEDIT消息,在这里可以判断新输 入的文字是否合法,然后设置标签为编辑得到的文 字: void CEx 9_9 Dlg: : On. Endlabeledit. List 1(……) { LV_DISPINFO* p. Disp. Info = (LV_DISPINFO*)p. NMHDR; // TODO: Add your control notification handler code here LVITEMA item = p. Disp. Info->item; CString str =item. psz. Text; str. Trim. Left(); str. Trim. Right(); if(str. Get. Length() > 0) { CList. Ctrl* p. List = (CList. Ctrl*) Get. Dlg. Item(IDC_LIST 1); 118 p. List->Set. Item. Text(item. i. Item, item. i. Sub. Item,
然后在On. Init. Dialog函数中添加如下代码: CTree. Ctrl* p. Tree = (CTree. Ctrl*) Get. Dlg. Item(IDC_TREE 1); p. Tree->Set. Image. List(&m_image. List, TVSIL_NORMAL); // 设 置图片列表 TV_INSERTSTRUCT tvinsert; //创建待插入的 TV_INSERTSTRUCT结构 tvinsert. h. Parent = NULL; // 无父结点 tvinsert. h. Insert. After = TVI_LAST; // 插入到本层最后 tvinsert. item. mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE| TVIF_TEXT; //掩码: 图标/选中图标/文字 tvinsert. item. h. Item = NULL; // 句柄为空 tvinsert. item. state = 0; // 状态 tvinsert. item. state. Mask = 0; // 状态掩码,不使用这两 121 项
// 创建第一层 tvinsert. item. i. Image = 2; // 一般图标 tvinsert. item. psz. Text = "father"; // 插入第一层第一个节点 HTREEITEM h. Dad = p. Tree ->Insert. Item(&tvinsert); tvinsert. item. psz. Text = "mother"; // 插入第一层第二个节点 HTREEITEM h. Mom = p. Tree->Insert. Item(&tvinsert); // 创建第二层 tvinsert. h. Parent = h. Dad; // 父节点为"father" tvinsert. item. i. Image = 3; // 一般图标 tvinsert. item. psz. Text = "son"; // 插入第二层"father"的第一个 p. Tree->Insert. Item(&tvinsert); tvinsert. item. psz. Text = "daughter"; // 插入第二层的第二个节 p. Tree->Insert. Item(&tvinsert); tvinsert. h. Parent = h. Mom; // 父结点为"mother" tvinsert. item. i. Image = 4; 122
tvinsert. item. psz. Text = "son"; // 插入第二层的第一个节 p. Tree->Insert. Item(&tvinsert); tvinsert. item. psz. Text = "daughter"; // 插入第二层的第二个节 p. Tree->Insert. Item(&tvinsert); tvinsert. item. psz. Text = "cartoon"; // 插入第二层的第三个节 HTREEITEM h. Other = p. Tree->Insert. Item(&tvinsert); // 创建第三层 tvinsert. h. Parent = h. Other; // 父结点为"carto tvinsert. item. i. Image = 7; tvinsert. item. psz. Text = "Tom"; // 插入第三层的第一个节点T p. Tree->Insert. Item(&tvinsert); tvinsert. item. psz. Text = "Jerry"; // 插入第三层的第二个节点J p. Tree->Insert. Item(&tvinsert); 123
下面通过添加一些对树状控件常用消息的响应来说 明树状控件的一般使用方法。首先在树状控件旁边 添加一个static控件,ID设置为IDC_STATIC_TREE。 为树状控件添加对TVN_SELCHANGED消息的响应: void CEx 9_9 Dlg: : On. Selchanged. Tree 1(……) { NM_TREEVIEW* p. NMTree. View = (NM_TREEVIEW*)p. NMHDR; // TODO: Add your control notification handler code here CTree. Ctrl* p. Tree =(CTree. Ctrl*) Get. Dlg. Item(IDC_TREE 1); HTREEITEM h. Selected = p. NMTree. View>item. New. h. Item; if (h. Selected != NULL) { char text[31]; TV_ITEM item; item. mask = TVIF_HANDLE | TVIF_TEXT; item. h. Item = h. Selected; item. psz. Text = text; item. cch. Text. Max = 30; VERIFY(p. Tree->Get. Item(&item)); Set. Dlg. Item. Text(IDC_STATIC_TREE, text); 125 }
在向对话框添加控件时,设置了Edit labels属性,使得树状控 件可编辑,双击节点文字便进入编辑状态,响应编辑状态结束 消息呢的方法与响应列表控件的编辑结束消息完全一样。对 TVN_ENDLABELEDIT消息的响应函数,实现如下: void CEx 9_9 Dlg: : On. Endlabeledit. Tree 1(……) { TV_DISPINFO* p. TVDisp. Info = (TV_DISPINFO*)p. NMHDR; //TODO: Add your control notification handler code here TVITEMA item = p. TVDisp. Info->item; CString str =item. psz. Text; str. Trim. Left(); str. Trim. Right(); if(str. Get. Length() > 0) { CTree. Ctrl* p. Tree =(CTree. Ctrl*) Get. Dlg. Item(IDC_TREE 1); p. Tree->Set. Item. Text(item. h. Item, item. psz. Text); } *p. Result = 0; } 126
127
向对话框添加扩展对话框控件,ID为IDC_COMBOBOXEX 1,类型 (Type)为Dropdown。在On. Init. Dialog函数中添加如下代码: CCombo. Box. Ex * p. Combo. Ex=(CCombo. Box. Ex *)Get. Dlg. Item(IDC_COMBOBOXEX 1); p. Combo. Ex->Set. Image. List(&m_image. List); COMBOBOXEXITEM combo. Item; combo. Item. mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_SELECTEDIMAGE | CBEIF_TEXT ; for(int i=0; i<3; i++) { combo. Item. i. Item = i; combo. Item. i. Image = i; combo. Item. i. Selected. Image = i; combo. Item. i. Indent = i; combo. Item. psz. Text = color[i]; p. Combo. Ex->Insert. Item(&combo. Item); } 129
130
- Slides: 130