#include <windows.h>
#include<tchar.h>
#include "resource.h"
#include <stdio.h>
#include <windowsx.h>
#pragma warning(disable:4996)
//窗口函数说明
#define DATASIZE 20
#define LEN sizeof(struct StuMessage)
#define MAX 100
HINSTANCE hins ;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DlgEdit(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgAdd(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgChange(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgFind(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgShow(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam);
// HDC hdc;
HPEN hpen;
HFONT hF;
HWND hwin;
struct StuMessage{ //为学生信息定义一个结构体
char name[DATASIZE];
char ID[DATASIZE];
char Grade[20];
char Score_c[5] ;
char Score_s[5];
char Score_x[5];
char Score_h[5];
char sex[4];
char class1[10];
double sum;
double argv ;
}stuadd;
int flag =0 ;
int flagFind ;
struct StuMessage stu1[MAX];
struct StuMessage *stuEdit ;
struct StuMessage *stuEdit1 ;
int num;
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
hins = hInstance;
DialogBox(hInstance,(LPCTSTR)IDD_DIALOG1,NULL,DlgEdit);
}
//窗口函数
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
hwin =hwnd ;
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0); //调用PostQuitMessage发出WM_QUIT消息
case WM_COMMAND:
switch (LOWORD(wParam))
{
case WM_DESTROY:
DestroyWindow(hwnd);
break;
default:
break;
}
break;
default: //默认时采用系统消息默认处理函数
return DefWindowProc(hwnd,message,wParam,lParam);
}
return (0);
}
BOOL CALLBACK DlgEdit(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{ FILE *fp;
int i ;
TCHAR mess[128]={""} ;
TCHAR mess1[128]={""} ;
HWND hdlglist1=GetDlgItem(hwnd,IDC_LIST2);
HWND hdlglist=GetDlgItem(hwnd,IDC_COMBO1 );
LPSTR temp1 =NULL;
LPSTR temp2 =NULL ;
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break ;
case WM_INITDIALOG:
if((fp=fopen("2.txt","rb"))==NULL)
{
MessageBox(hwnd,"文件打开失败!",NULL,MB_OK );
exit(0);
}
for (num =0;1;num++)
{
if (!fread(&stu1[num],LEN,1,fp))
break;
}
for (i = 0;i<num ;i ++)
{
wsprintf( mess,"%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s",stu1[i].class1,stu1[i].name,stu1[i].ID,stu1[i].sex,stu1[i].Grade,stu1[i].Score_c,stu1[i].Score_h,stu1[i].Score_x ,stu1[i].Score_s);
ListBox_AddString(hdlglist1,mess);
}
ComboBox_AddString(hdlglist,"信安一班");
ComboBox_AddString(hdlglist,"信安二班");
ComboBox_AddString(hdlglist,"信抗一班");
ComboBox_AddString(hdlglist,"信抗二班");
ComboBox_AddString(hdlglist,"信安实验");
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON5: //刷新
if ((fp = fopen("2.txt", "rb")) == NULL)
{
MessageBox(hwnd, "文件打开失败!", NULL, MB_OK);
exit(0);
}
for (i = num-1; i >= 0; i--)
ListBox_DeleteString(hdlglist1, i);
for (num = 0; 1; num++)
{
if (!fread(&stu1[num], LEN, 1, fp))
break;
}
for (i = 0; i < num; i++)
{
wsprintf( mess,"%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s",stu1[i].class1,stu1[i].name,stu1[i].ID,stu1[i].sex,stu1[i].Grade,stu1[i].Score_c,stu1[i].Score_h,stu1[i].Score_x ,stu1[i].Score_s);
ListBox_AddString(hdlglist1, mess);
}
break;
case IDC_BUTTON1: //添加
DialogBox(hins,(LPCTSTR)IDD_DIALOG2,NULL,DlgAdd);
break ;
case IDC_BUTTON2: //修改数据
flag = ListBox_GetCurSel(hdlglist1);
if (flag ==0xffffffff)
MessageBox(hwnd,"请选择数据!",NULL,MB_OK);
else
DialogBox(hins,(LPCTSTR)IDD_DIALOG3,NULL,DlgChange);
break;
case IDC_BUTTON3://删除
flag = ListBox_GetCurSel(hdlglist1);
stuEdit =(struct StuMessage *)malloc((num-1)*sizeof(struct StuMessage));
if (flag ==0xffffffff)
MessageBox(hwnd,"请选择数据!",NULL,MB_OK);
else
{
if((fp = fopen("2.txt","w+b")) ==NULL)
{
MessageBox(hwnd,TEXT("打开或者写入文件失败!"),TEXT("错误"),MB_OK);
exit(0);
}
else
{
if(num==1)
;
else if(num==2)
{
if(flag)
fwrite(&stu1[0],sizeof(struct StuMessage),1,fp);
else
fwrite(&stu1[1],sizeof(struct StuMessage),1,fp);
}
else
{
if(flag==0)
fwrite(&stu1[1],sizeof(struct StuMessage),num-1,fp);
else if(flag==num-1)
fwrite(stu1,sizeof(struct StuMessage),num-1,fp);
else
{
fwrite(stu1,sizeof(struct StuMessage),flag,fp);
fwrite(&stu1[flag+1],sizeof(struct StuMessage),(num-1)-flag,fp);
}
}
fclose(fp);
}
}
break;
case IDC_BUTTON4:
DialogBox(hins,(LPCTSTR)IDD_DIALOG4,NULL,DlgFind);
break ;
default:
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
}
return 0;
}
BOOL CALLBACK DlgAdd(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
LPSTR sex=NULL ;
FILE *fp;
HWND hdlglist=GetDlgItem(hwnd,IDC_COMBO1);
switch (message)
{
case WM_CLOSE:
ShowWindow(hwnd,SW_HIDE);
break ;
case WM_INITDIALOG:
SetDlgItemText(hwnd,IDC_EDIT3 ,"0");
SetDlgItemText(hwnd,IDC_EDIT4 ,"0");
SetDlgItemText(hwnd,IDC_EDIT5 ,"0");
SetDlgItemText(hwnd,IDC_EDIT6 ,"0");
SetDlgItemText(hwnd,IDC_EDIT7 ,"0");
ComboBox_AddString(hdlglist,"信安一班");
ComboBox_AddString(hdlglist,"信安二班");
ComboBox_AddString(hdlglist,"信抗一班");
ComboBox_AddString(hdlglist,"信抗二班");
ComboBox_AddString(hdlglist,"信安实验");
ComboBox_SetCurSel(hdlglist,0);
CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
break ;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
GetDlgItemText(hwnd,IDC_EDIT1,stuadd.name,sizeof(stuadd.name));
GetDlgItemText(hwnd,IDC_EDIT2,stuadd.ID,sizeof(stuadd.ID));
GetDlgItemText(hwnd,IDC_EDIT3,stuadd.Grade,sizeof(stuadd.Grade));
GetDlgItemText(hwnd,IDC_EDIT4,stuadd.Score_c,sizeof(stuadd.Score_c));
GetDlgItemText(hwnd,IDC_EDIT5,stuadd.Score_s,sizeof(stuadd.Score_s));
GetDlgItemText(hwnd,IDC_EDIT6,stuadd.Score_x,sizeof(stuadd.Score_x));
GetDlgItemText(hwnd,IDC_EDIT7,stuadd.Score_h,sizeof(stuadd.Score_h));
if (IsDlgButtonChecked(hwnd,IDC_RADIO1 ))
{
strcpy(stuadd.sex,"男");
}
if (IsDlgButtonChecked(hwnd,IDC_RADIO2))
{
strcpy(stuadd.sex,"女");
}
int i ;
i =ComboBox_GetCurSel(hdlglist);
switch (i)
{
case 0:strcpy(stuadd.class1,"信安一班");
break;
case 1:
strcpy(stuadd.class1 ,"信安二班");
break;
case 2:
strcpy(stuadd.class1,"信抗一班");
break;
case 3:
strcpy(stuadd.class1,"信抗二班");
break;
case 4:
strcpy(stuadd.class1,"信安实验");
break;
}
if (lstrlen(stuadd.name) <= 0 ||lstrlen(stuadd.ID) <= 0)
{
MessageBox(hwnd,"学号跟姓名均不能为空!","出错",MB_OK);
}、
else{
if((fp = fopen("2.txt","ab+")) ==NULL)
{
MessageBox(hwnd,TEXT("打开或者写入文件失败!"),TEXT("错误"),MB_OK);
exit(0);
}
else
{
fwrite(&stuadd,sizeof(stuadd),1,fp);
fclose(fp);
}
MessageBox(hwnd,"添加成功","恭喜",MB_OK );
ShowWindow(hwnd,SW_HIDE);
}
break ;
case IDCANCEL:
ShowWindow(hwnd,SW_HIDE);
break ;
}
break ;
}
return 0;
}
BOOL CALLBACK DlgChange(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
FILE *fp ;
HWND hdlglist=GetDlgItem(hwnd,IDC_COMBO1);
LPSTR sex1=NULL ;
LPSTR class1 = NULL;
stuEdit1 =(struct StuMessage *)malloc(num*sizeof(struct StuMessage));
switch (message)
{
case WM_INITDIALOG:
ComboBox_AddString(hdlglist,"信安一班");
ComboBox_AddString(hdlglist,"信安二班");
ComboBox_AddString(hdlglist,"信抗一班");
ComboBox_AddString(hdlglist,"信抗二班");
ComboBox_AddString(hdlglist,"信安实验");
class1 = stu1[flag].class1 ;
if (!strcmp(class1,"信安一班"))
ComboBox_SetCurSel(hdlglist,0);
if (!strcmp(class1,"信安二班"))
ComboBox_SetCurSel(hdlglist,1);
if (!strcmp(class1,"信抗一班"))
ComboBox_SetCurSel(hdlglist,2);
if (!strcmp(class1,"信抗二班"))
ComboBox_SetCurSel(hdlglist,3);
if (!strcmp(class1,"信安实验"))
ComboBox_SetCurSel(hdlglist,4);
SetDlgItemText(hwnd,IDC_EDIT1,stu1[flag].name);
SetDlgItemText(hwnd,IDC_EDIT2,stu1[flag].ID);
SetDlgItemText(hwnd,IDC_EDIT3,stu1[flag].Grade);
SetDlgItemText(hwnd,IDC_EDIT4,stu1[flag].Score_c);
SetDlgItemText(hwnd,IDC_EDIT5,stu1[flag].Score_s);
SetDlgItemText(hwnd,IDC_EDIT6,stu1[flag].Score_x);
SetDlgItemText(hwnd,IDC_EDIT7,stu1[flag].Score_h);
sex1 ="女";
if (strcmp(stu1[flag].sex,sex1))
CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
else
CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO2,IDC_RADIO2);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
EndDialog(hwnd,TRUE);
break;
case IDOK:
if((fp = fopen("2.txt","w+b")) ==NULL)
{
MessageBox(hwnd,TEXT("打开或者写入文件失败!"),TEXT("错误"),MB_OK);
exit(0);
}
else
{
GetDlgItemText(hwnd,IDC_EDIT1,stu1[flag].name,sizeof(stu1[flag].name));
GetDlgItemText(hwnd,IDC_EDIT2,stu1[flag].ID,sizeof(stu1[flag].ID));
GetDlgItemText(hwnd,IDC_EDIT3,stu1[flag].Grade,sizeof(stu1[flag].Grade));
GetDlgItemText(hwnd,IDC_EDIT4,stu1[flag].Score_c,sizeof(stu1[flag].Score_c));
GetDlgItemText(hwnd,IDC_EDIT5,stu1[flag].Score_s,sizeof(stu1[flag].Score_s));
GetDlgItemText(hwnd,IDC_EDIT6,stu1[flag].Score_x,sizeof(stuEdit1[flag].Score_x));
GetDlgItemText(hwnd,IDC_EDIT7,stu1[flag].Score_h,sizeof(stu1[flag].Score_h));
if (IsDlgButtonChecked(hwnd,IDC_RADIO1 ))
{
strcpy(stu1[flag].sex,"男");
}
if (IsDlgButtonChecked(hwnd,IDC_RADIO2))
{
strcpy(stu1[flag].sex,"女");
}
int i ;
i =ComboBox_GetCurSel(hdlglist);
switch (i)
{
case 0:strcpy(stu1[flag].class1,"信安一班");
break;
case 1:
strcpy(stu1[flag].class1 ,"信安二班");
break;
case 2:
strcpy(stu1[flag].class1,"信抗一班");
break;
case 3:
strcpy(stu1[flag].class1,"信抗二班");
break;
case 4:
strcpy(stu1[flag].class1,"信安实验");
break;
}
fwrite(stu1,sizeof(struct StuMessage),num,fp);
fclose(fp);
}
MessageBox(hwnd,"修改成功","恭喜",MB_OK );
ShowWindow(hwnd,SW_HIDE);
break;
}
break;
}
return 0;
}
BOOL CALLBACK DlgFind(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
int i =0;
LPTSTR idtemp =NULL;
LPTSTR idtrue =NULL;
TCHAR s1[20],s2[20] ;
LPTSTR nametemp =NULL,nametrue =NULL;
switch (message)
{
case WM_CLOSE:
ShowWindow(hwnd,SW_HIDE);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON1: //学号
GetDlgItemText(hwnd,IDC_EDIT1 ,s1,sizeof(s1));
idtemp =s1 ;
for (i=0;i<num ; i++)
{
idtrue = stu1[i].ID;
if(!strcmp(idtrue,idtemp))
{flagFind =i;
DialogBox(hins,(LPCTSTR)IDD_DIALOG5,NULL,DlgShow);
break;
}
}
break;
case IDC_BUTTON2:
GetDlgItemText(hwnd,IDC_EDIT1 ,s2,sizeof(s2));
nametemp =s2 ;
for (i=0;i<num ; i++)
{
flagFind =i;
nametrue = stu1[i].name;
if(!strcmp(nametrue,nametemp))
{
DialogBox(hins,(LPCTSTR)IDD_DIALOG5,NULL,DlgShow);
break;
}
}
break ;
}
break;
}
return 0;
}
BOOL CALLBACK DlgShow(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
TCHAR mess1[128];
struct StuMessage stu2 ;
switch (message)
{
case WM_INITDIALOG:
strcpy(stu2.name,stu1[flagFind].name);
strcpy(stu2.class1,stu1[flagFind].class1);
strcpy(stu2.ID,stu1[flagFind].ID);
strcpy(stu2.Score_c,stu1[flagFind].Score_c);
strcpy(stu2.Score_h,stu1[flagFind].Score_h);
strcpy(stu2.Score_x,stu1[flagFind].Score_x);
strcpy(stu2.sex,stu1[flagFind].sex);
strcpy(stu2.Score_s,stu1[flagFind].Score_s);
strcpy(stu2.Grade,stu1[flagFind].Grade);
wsprintf( mess1,TEXT("姓名 学号 班级 %-10s%-10s%-10s%-10s%-10s%-10s%-10s "),stu2.name,stu2.ID,stu2.Grade,stu2.Score_c,stu2.Score_h,stu2.Score_x ,stu2.Score_s);
SetDlgItemText(hwnd,IDC_EDIT1,mess1);
break ;
case WM_CLOSE:
ShowWindow(hwnd,SW_HIDE);
break;
}
return 0;
}
演示: