用SDK写的一个简单学生成绩管理系统
功能比较简单。纯属分享交流代码如下#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,WPARAMwParam, LPARAMlParam);
BOOL CALLBACK DlgAdd(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam);
BOOL CALLBACK DlgChange(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam);
BOOL CALLBACK DlgFind(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam);
BOOL CALLBACK DlgShow(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam);
// HDC hdc;
HPEN hpen;
HFONT hF;
HWND hwin;
struct StuMessage{ //为学生信息定义一个结构体
char name;
char ID;
char Grade;
char Score_c ;
char Score_s;
char Score_x;
char Score_h;
char sex;
char class1;
doublesum;
double argv ;
}stuadd;
int flag =0 ;
int flagFind ;
struct StuMessage stu1;
structStuMessage *stuEdit ;
structStuMessage *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,WPARAMwParam, LPARAMlParam)
{
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: //默认时采用系统消息默认处理函数
returnDefWindowProc(hwnd,message,wParam,lParam);
}
return (0);
}
BOOL CALLBACK DlgEdit(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam)
{ FILE *fp;
int i ;
TCHAR mess={""} ;
TCHAR mess1={""} ;
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,LEN,1,fp))
break;
}
for (i = 0;i<num ;i ++)
{
wsprintf( mess,"%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s",stu1.class1,stu1.name,stu1.ID,stu1.sex,stu1.Grade,stu1.Score_c,stu1.Score_h,stu1.Score_x ,stu1.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, LEN, 1, fp))
break;
}
for (i = 0; i < num; i++)
{
wsprintf( mess,"%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s",stu1.class1,stu1.name,stu1.ID,stu1.sex,stu1.Grade,stu1.Score_c,stu1.Score_h,stu1.Score_x ,stu1.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,sizeof(struct StuMessage),1,fp);
else
fwrite(&stu1,sizeof(struct StuMessage),1,fp);
}
else
{
if(flag==0)
fwrite(&stu1,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,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,WPARAMwParam, LPARAMlParam)
{
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,WPARAMwParam, LPARAMlParam)
{
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.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.name);
SetDlgItemText(hwnd,IDC_EDIT2,stu1.ID);
SetDlgItemText(hwnd,IDC_EDIT3,stu1.Grade);
SetDlgItemText(hwnd,IDC_EDIT4,stu1.Score_c);
SetDlgItemText(hwnd,IDC_EDIT5,stu1.Score_s);
SetDlgItemText(hwnd,IDC_EDIT6,stu1.Score_x);
SetDlgItemText(hwnd,IDC_EDIT7,stu1.Score_h);
sex1 ="女";
if (strcmp(stu1.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;
caseWM_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.name,sizeof(stu1.name));
GetDlgItemText(hwnd,IDC_EDIT2,stu1.ID,sizeof(stu1.ID));
GetDlgItemText(hwnd,IDC_EDIT3,stu1.Grade,sizeof(stu1.Grade));
GetDlgItemText(hwnd,IDC_EDIT4,stu1.Score_c,sizeof(stu1.Score_c));
GetDlgItemText(hwnd,IDC_EDIT5,stu1.Score_s,sizeof(stu1.Score_s));
GetDlgItemText(hwnd,IDC_EDIT6,stu1.Score_x,sizeof(stuEdit1.Score_x));
GetDlgItemText(hwnd,IDC_EDIT7,stu1.Score_h,sizeof(stu1.Score_h));
if (IsDlgButtonChecked(hwnd,IDC_RADIO1 ))
{
strcpy(stu1.sex,"男");
}
if (IsDlgButtonChecked(hwnd,IDC_RADIO2))
{
strcpy(stu1.sex,"女");
}
int i ;
i =ComboBox_GetCurSel(hdlglist);
switch (i)
{
case 0:strcpy(stu1.class1,"信安一班");
break;
case 1:
strcpy(stu1.class1 ,"信安二班");
break;
case 2:
strcpy(stu1.class1,"信抗一班");
break;
case 3:
strcpy(stu1.class1,"信抗二班");
break;
case 4:
strcpy(stu1.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,WPARAMwParam, LPARAMlParam)
{
int i =0;
LPTSTRidtemp =NULL;
LPTSTR idtrue =NULL;
TCHAR s1,s2 ;
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.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.name;
if(!strcmp(nametrue,nametemp))
{
DialogBox(hins,(LPCTSTR)IDD_DIALOG5,NULL,DlgShow);
break;
}
}
break ;
}
break;
}
return 0;
}
BOOL CALLBACK DlgShow(HWND hwnd,UINT message,WPARAMwParam, LPARAMlParam)
{
TCHAR mess1;
struct StuMessage stu2 ;
switch (message)
{
case WM_INITDIALOG:
strcpy(stu2.name,stu1.name);
strcpy(stu2.class1,stu1.class1);
strcpy(stu2.ID,stu1.ID);
strcpy(stu2.Score_c,stu1.Score_c);
strcpy(stu2.Score_h,stu1.Score_h);
strcpy(stu2.Score_x,stu1.Score_x);
strcpy(stu2.sex,stu1.sex);
strcpy(stu2.Score_s,stu1.Score_s);
strcpy(stu2.Grade,stu1.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;
}演示:
[
附加:
注:如果是直接运行的话2.txt不用换位置,如果是在调试的话 需要把2.txt换到另外一个Debug里面。
表示不太会MFC所以没有表格来弄格式。所以看起来比较丑。噗。。。{:9_240:}
{:7_131:}泡泡辛苦了,学习、学习~ {:7_139:}其实泡泡已经会MFC了,windowsx.h这个头文件,定义的一些函数、宏。类似MFC。基本上一样哈~ @小甲鱼 @小布丁 @狂潮丶 @Angel丶L @~风介~ @q2692988 @小人 @machimilk 这代码风格 炮姐,你什么时候跑这来了 拈花小仙 发表于 2015-1-4 20:51
@小甲鱼 @小布丁 @狂潮丶 @Angel丶L @~风介~ @q2692988 @小人 @machimilk
{:9_240:}仙仙你怎么@了这么多人 907806863 发表于 2015-1-4 21:17
这代码风格 炮姐,你什么时候跑这来了
{:9_230:}代码风格怎么了 是不是很戳 求指教。 到处混。 chishubiao 发表于 2015-1-4 21:19
顶一下哈哈我用C#
{:9_237:}谢谢大大 kklloo 发表于 2015-1-4 21:36
谢谢大大
{:7_139:}泡泡的SDK经典之作,可惜我@的人数有限,如果无限,想把所有好友全at~ {:7_145:}真是不得不在顶一哈,泡泡是SDK高手呀,我啥时候能写出这样的程序呀~ kklloo 发表于 2015-1-4 21:35
仙仙你怎么@了这么多人
奇怪了,我刚送泡泡勋章了,显示不出来,可能是泡泡的空间设置了吧~ 本帖最后由 皓月长空 于 2015-1-5 18:30 编辑
丫头,我与你的差距好远...........{:5_99:} 拈花小仙 发表于 2015-1-5 11:11
奇怪了,我刚送泡泡勋章了,显示不出来,可能是泡泡的空间设置了吧~
现在应该有了 可能是刚刚没设置上 我重新设置了一下。 皓月长空 发表于 2015-1-5 18:29
丫头,我与你的差距好远...........
{:5_98:}你会那么多不同的东西 。我只会一个领域里面的东西。我还是的跟你学习一些东西。 顶
{:9_228:}好厉害..我还是新手 好厉害..我还是新手{:1_1:} 零度C 发表于 2015-1-5 19:52
好厉害..我还是新手
{:7_123:} 希望你们能在里面能看到一点对你们有用的东西 opqrew99 发表于 2015-1-5 20:04
好厉害..我还是新手
{:7_123:} 希望你们能在里面能看到一点对你们有用的东西