static int count_list=0; //定义全局变量,记录添加的记录个数;
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDC_ADD://增加记录功能;
{
HWND hwndcomb1 = GetDlgItem(hwnd,IDC_COMB_INFO);
TCHAR ADD_STR[256];
int add_c = GetDlgItemText(hwnd,IDC_DATA_ADD,ADD_STR,sizeof(ADD_STR));
if ((0 == count_list) && 0 != add_c)
{
ComboBox_InsertString(hwndcomb1,-1,ADD_STR);
MessageBox(hwnd,TEXT("增加成功!"),TEXT("提示"),MB_OK|MB_ICONWARNING);
count_list++;
}
else
{
if (0 != add_c)
{
TCHAR str_tmp[256];
int sear_id;
for (sear_id = 0;sear_id<=count_list;sear_id++)
{
ComboBox_GetLBText(hwndcomb1,sear_id,str_tmp);
if (!strcmp(str_tmp,ADD_STR))
{
MessageBox(NULL,TEXT("不能增加相同的记录!"),TEXT("出错"),MB_ICONERROR);
return;
}
else
{
ComboBox_InsertString(hwndcomb1,-1,ADD_STR);
MessageBox(hwnd,TEXT("增加成功!"),TEXT("提示"),MB_OK|MB_ICONWARNING);
count_list++;
return;
}
}
}
else
{
MessageBox(hwnd,TEXT("不能增加空信息!"),TEXT("出错"),MB_OK|MB_ICONERROR);
}
}
}
break;
case IDC_SEARCH: //查询记录功能;
{
HWND hwndcomb1 = GetDlgItem(hwnd,IDC_COMB_INFO);
TCHAR SEARCH_STR[256];
if (count_list>0)
{
int sear_input =GetDlgItemText(hwnd,IDC_DATA_SEARCH,SEARCH_STR,sizeof(SEARCH_STR));//将要查询字符输入到缓冲区数组中;
if (0 != sear_input)
{
int index;
TCHAR STR_TMP[256];
int last_id = count_list - 1;
for (index = 0; index {
ComboBox_GetLBText(hwndcomb1,index,STR_TMP);
if (!strcmp(STR_TMP,SEARCH_STR))
{
ComboBox_SetCurSel(hwndcomb1,index);
MessageBox(NULL,TEXT("查询成功!"),TEXT("结果"),MB_OK);
return;
}
else
{
index ++;
if (index == count_list)
{
MessageBox(NULL,TEXT("查询失败,未找到匹配项!"),TEXT("结果"),MB_OK);
return;
}
}
}
}
else
{
MessageBox(NULL,TEXT("请输入要查询的记录!"),TEXT("注意"),MB_ICONERROR);
return;
}
}
else
{
MessageBox(NULL,TEXT("记录为空,不可查询!"),TEXT("消息"),MB_ICONERROR);
}
}
break;
case IDC_DEL_SEL: //删除所选择的记录;
{
HWND hwndcomb1 =GetDlgItem(hwnd,IDC_COMB_INFO);
TCHAR DEL_SEARC[256];
int id_del = ComboBox_GetCurSel(hwndcomb1);//Get the Current Selection id
if(count_list>0)
{
if (CB_ERR == id_del)
{
MessageBox(NULL,TEXT("请选择要删除的项!"),TEXT("出错"),MB_ICONERROR);
}
else
{
ComboBox_GetLBText(hwndcomb1,id_del,DEL_SEARC);
int sure_del = MessageBox(NULL,TEXT("确认删除该项?"),TEXT("删除确认"),MB_YESNO);
if (IDYES == sure_del)
{
ComboBox_DeleteString(hwndcomb1,id_del);
MessageBox(NULL,TEXT("删除成功"),TEXT("成功"),MB_ICONWARNING|MB_OK);
count_list--;
}
else
{
return;
}
}
}
else
{
MessageBox(hwnd,TEXT("没有内容可删除!"),TEXT("ERROR"),MB_ICONERROR);
}
}
break;
case IDC_QUITWINDOW: //退出程序
{
EndDialog(hwnd, 0);
}
break;
default:
break;
}
}
以上程序中有很多重复代码,都可以通过集成为函数,缩短代码长度,由于时间原因,没有简化,等有时间再细化一下。
阅读(1650) | 评论(0) | 转发(0) |