初识windows编程之五

来源:互联网 发布:gif图分解软件 编辑:程序博客网 时间:2024/05/10 18:13
本文是本人看了视频“C语言也能干大事”第四、五节之后所写的总结和感悟,此处附上视频链接http://www.rupeng.com/forum/thread-8801-1-2.html
 
1.简要概述
第四节主要是将第三节的作业讲解了一下,此处就不多赘述了。第五节主要讲的内容是控件组合框的使用。
 
2.重点内容ComboBox的使用,这里主要介绍ComboBox的几个函数:
(1)ComboBox_AddString(hwndCtrl,str);   hwndCtrl表示控件句柄;  
   ComboBox_InsertString(hwndCtrl,index,str);这两个函数的用就是向组合框中添加文本内容;
(2)ComboBox_GetCurSel(hwndCtrl);函数返回值为当前项的编号,从0开始;
(3)ComboBox_GetLBText(hwndCtrl,index,str);得到下标为index的项的值;
(4)ComboBox_DeleteString(hwndCtrl,index);删除下表为index的项;
(5)ComboBox_GetCount(hwndCtrl);返回值是项的个数;
(6)ComboBox_SetCurSel(hwndCtrl,index);选定下标为index的选项,当index=-1是选择为空;
附上这些函数的链接:http://msdn.microsoft.com/en-us/library/cc656426(VS.85).aspx
 
3.四则计算器的实现,此处附上关键代码:
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam){HWND hwndComBoOP= GetDlgItem(hwnd,IDC_COMBOOP);ComboBox_InsertString(hwndComBoOP,-1,"+");ComboBox_InsertString(hwndComBoOP,-1,"-");ComboBox_InsertString(hwndComBoOP,-1,"*");ComboBox_InsertString(hwndComBoOP,-1,"/");    return TRUE;}void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify){    switch(id)    {        case IDC_OK:{TCHAR str1[256];TCHAR str2[256];TCHAR str3[256];
GetDlgItemText(hwnd,IDC_EDITOP1,str1,sizeof(str1));GetDlgItemText(hwnd,IDC_EDITOP2,str2,sizeof(str2));int op1 = atoi(str1),op2 = atoi(str2),op3;HWND hwndComBoOP = GetDlgItem(hwnd,IDC_COMBOOP);int cursel = ComboBox_GetCurSel(hwndComBoOP);switch(cursel){case 0: op3=op1+op2; break;case 1: op3=op1-op2; break;case 2: op3=op1*op2; break;case 3: op3=op1/op2; break;default: break;}wsprintf(str3,"%d",op3);SetDlgItemText(hwnd,IDC_EDITOP3,str3);
}        break;        default:break;    }}

 

4.总结:

看了这节视频应该可以基本掌握ComboBox函数的使用,以及运用组合框进行一些简单的操作。