📄 window.c
字号:
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include"resource.h"//资源文件
#include "list.h"
#define COL 6
#define ROW 14
#define Prompt "通迅管理提示您"
HINSTANCE hInst;//声明应用程序实例
HWND hWnd;//窗口句柄
HWND g_hWnd; //主窗口句柄
Node * pHead=NULL; //全局指针变量
char FileName[255]; //文件名
int QueryFlat=-1;
Node * pNode1=NULL;
//新建
LRESULT CALLBACK DlgProc_New(HWND hDlgWnd,//对话框句柄
UINT message,//消息参数
WPARAM wParam,
LPARAM lParam)
{
HWND hTopWnd;
RECT rc,Drc;
int wmID;
switch (message)
{
case WM_INITDIALOG://对话框初始消息
hTopWnd = GetDesktopWindow();
GetWindowRect(hTopWnd,&rc);
GetWindowRect(hDlgWnd,&Drc);
SetWindowPos(hDlgWnd,HWND_TOP,(rc.right - Drc.right + Drc.left)/2,(rc.bottom - Drc.bottom + Drc.top )/2,Drc.right -Drc.left,Drc.bottom - Drc.top,SWP_SHOWWINDOW);
return 1;
break;
case WM_COMMAND:
wmID=LOWORD(wParam);
switch(wmID)
{
case IDOK:
{
FILE * fp;
HWND hFileWnd;
hFileWnd = GetDlgItem(hDlgWnd, IDC_EDIT1);
GetWindowText(hFileWnd, FileName, 255);
if (strlen(FileName)==0)
MessageBox(hDlgWnd,"文件为空,请重新输入文件名!",Prompt,MB_OK | MB_ICONERROR);
else
{
fp=fopen(FileName,"r");
if(fp!=NULL)
{
if(MessageBox(hDlgWnd,"文件已存在,是否覆盖?",Prompt,MB_OKCANCEL | MB_ICONQUESTION)==IDOK)
{
fp=fopen(FileName,"w");
fclose(fp);
}
else if(MessageBox(hDlgWnd,"该文件已存在,是否打开?",Prompt,MB_OKCANCEL | MB_ICONQUESTION)==IDOK)
{
pHead=LoadList(pHead,FileName); //加载文件
InvalidateRect(g_hWnd,NULL,TRUE);
}
}
else
{
fp=fopen(FileName,"wb");
fclose(fp);
}
EndDialog(hDlgWnd,IDOK);
}
}
break;
case IDCANCEL:
EndDialog(hDlgWnd,IDOK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CLOSE : //对话框关闭消息
EndDialog(hDlgWnd,0);
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
break;
default:
break;
}
return 0;
}
//打开
LRESULT CALLBACK DlgProc_Open(HWND hDlgWnd, //对话框句柄
UINT message, //消息参数
WPARAM wParam,
LPARAM lParam)
{
HWND hTopWnd;
RECT rc,Drc;
int wmID;
switch (message)
{
case WM_INITDIALOG: //对话框初始消息
hTopWnd = GetDesktopWindow();
GetWindowRect(hTopWnd,&rc);
GetWindowRect(hDlgWnd,&Drc);
SetWindowPos(hDlgWnd,HWND_TOP,(rc.right - Drc.right + Drc.left)/2,(rc.bottom - Drc.bottom + Drc.top )/2,Drc.right -Drc.left,Drc.bottom - Drc.top,SWP_SHOWWINDOW);
return 1;
break;
case WM_COMMAND:
wmID=LOWORD(wParam);
switch(wmID)
{
case IDOK:
{
FILE * fp;
HWND hFileWnd;
hFileWnd = GetDlgItem(hDlgWnd, IDC_EDIT1);
GetWindowText(hFileWnd, FileName, 255);
if (strlen(FileName)==0)
MessageBox(hDlgWnd,"文件名不能为空,请重新输入文件名!",Prompt,MB_OK | MB_ICONERROR);
else
{
fp=fopen(FileName,"r");
if(fp!=NULL)
{
pHead=LoadList(pHead,FileName); //读取文件
InvalidateRect(g_hWnd,NULL,TRUE);
}
else
{
if(MessageBox(hDlgWnd,"该文件不存在,是否创建一个新文件?",Prompt,MB_OKCANCEL | MB_ICONQUESTION)==IDOK)
{
fp=fopen(FileName,"wb");
}
else
break;
}
fclose(fp);
EndDialog(hDlgWnd,IDOK);
}
}
break;
case IDCANCEL:
EndDialog(hDlgWnd,IDOK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CLOSE : //对话框关闭消息
EndDialog(hDlgWnd,0);
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
break;
default:
break;
}
return 0;
}
//添加学生信息
LRESULT CALLBACK DlgProc_Add(HWND hDlgWnd, //对话框句柄
UINT message, //消息参数
WPARAM wParam,
LPARAM lParam)
{
HWND hTopWnd;
RECT rc,Drc;
int wmID;
Student std;
Node * pNode=NULL;
char No[20];
switch (message)
{
case WM_INITDIALOG: //对话框初始消息
hTopWnd = GetDesktopWindow();
GetWindowRect(hTopWnd,&rc);
GetWindowRect(hDlgWnd,&Drc);
SetWindowPos(hDlgWnd,HWND_TOP,(rc.right - Drc.right + Drc.left)/2,(rc.bottom - Drc.bottom + Drc.top )/2,Drc.right -Drc.left,Drc.bottom - Drc.top,SWP_SHOWWINDOW);
return 1;
break;
case WM_COMMAND:
wmID=LOWORD(wParam);
switch(wmID)
{
case IDOK:
{
HWND hNameWnd,hStdIdWnd,hSexWnd,hAddressWnd,hZipWnd,hPhoneWnd;
if(strlen(FileName)==0)
MessageBox(hDlgWnd,"文件未打开!",Prompt,MB_OK|MB_ICONERROR);
else
{
hStdIdWnd = GetDlgItem(hDlgWnd, IDC_EDIT2);
GetWindowText(hStdIdWnd,No,10);
pNode=searchnode(pHead,No);
if(searchnode(pHead,No)!=NULL)
MessageBox(hDlgWnd,"该学号已存在!",Prompt,MB_OK | MB_ICONERROR);
else
{
//姓名
hNameWnd = GetDlgItem(hDlgWnd, IDC_EDIT1);
GetWindowText(hNameWnd,std.name,sizeof(std.name));
//学号
hStdIdWnd = GetDlgItem(hDlgWnd, IDC_EDIT2);
GetWindowText(hStdIdWnd,std.no,sizeof(std.no));
//性别
hSexWnd = GetDlgItem(hDlgWnd, IDC_EDIT3);
GetWindowText(hSexWnd,std.sex,sizeof(std.sex));
//家庭地址
hAddressWnd = GetDlgItem(hDlgWnd, IDC_EDIT4);
GetWindowText(hAddressWnd,std.address,sizeof(std.address));
//邮政编码
hZipWnd = GetDlgItem(hDlgWnd, IDC_EDIT5);
GetWindowText(hZipWnd,std.zip,sizeof(std.zip));
//电话
hPhoneWnd = GetDlgItem(hDlgWnd, IDC_EDIT6);
GetWindowText(hPhoneWnd,std.phone,sizeof(std.phone));
if (strlen(std.no)==0 ||strlen(std.name)==0 || strlen(std.address)==0 || strlen(std.zip)==0 || strlen(std.phone)==0)
MessageBox(hDlgWnd,"信息输入不能为空!",Prompt,MB_OK | MB_ICONERROR);
else
{
pHead = InsertList(pHead,std);
SaveList(pHead,FileName); //保存文件
MessageBox(hDlgWnd,"添加成功!",Prompt,MB_OK|MB_ICONINFORMATION);
InvalidateRect(g_hWnd,NULL,TRUE); //强制重绘
}
}
}
}
break;
case IDCANCEL:
EndDialog(hDlgWnd,IDOK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CLOSE : //对话框关闭消息
EndDialog(hDlgWnd,0);
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
break;
default:
break;
}
return 0;
}
//查找
LRESULT CALLBACK DlgProc_Find(HWND hDlgWnd, //对话框句柄
UINT message, //消息参数
WPARAM wParam,
LPARAM lParam)
{
HWND hTopWnd;
HWND hStdIdWnd,hNameWnd,hAddressWnd,hSexWnd,hZipWnd,hPhoneWnd,hEditWnd;
RECT rc,Drc;
int wmID;
Node * pNode=NULL;
char No[20];
hStdIdWnd = GetDlgItem(hDlgWnd, IDC_Del_Id); //得到句柄
hNameWnd = GetDlgItem(hDlgWnd, IDC_Name);
hAddressWnd = GetDlgItem(hDlgWnd, IDC_Address);
hSexWnd = GetDlgItem(hDlgWnd, IDC_Sex);
hZipWnd = GetDlgItem(hDlgWnd, IDC_Zip);
hPhoneWnd = GetDlgItem(hDlgWnd, IDC_Phone);
hEditWnd=GetDlgItem(hDlgWnd,IDC_QUERY);
switch (message)
{
case WM_INITDIALOG: //对话框初始消息
hTopWnd = GetDesktopWindow();
GetWindowRect(hTopWnd,&rc);
GetWindowRect(hDlgWnd,&Drc);
SetWindowPos(hDlgWnd,HWND_TOP,(rc.right - Drc.right + Drc.left)/2,(rc.bottom - Drc.bottom + Drc.top )/2,Drc.right -Drc.left,Drc.bottom - Drc.top,SWP_SHOWWINDOW);
return 1;
break;
case WM_COMMAND:
wmID=LOWORD(wParam);
switch(wmID)
{
case IDOK:
{
GetWindowText(hStdIdWnd,No,10);
if(strlen(FileName)==0)
MessageBox(hDlgWnd,"你还未新建或打开一个文件,请先进行此操作!",Prompt,MB_OK|MB_ICONERROR);
else
{
if(strlen(No)==0)
MessageBox(hDlgWnd,"学号不能为空,请重新输入所要删除的学号!",Prompt,MB_OK|MB_ICONERROR);
else
{
pNode=searchnode(pHead,No);
if( searchnode(pHead,No)==NULL)
MessageBox(hDlgWnd,"该学号不存在,请重新输入!",Prompt,MB_OK | MB_ICONERROR);
else
{
//显示所要修改学生的信息(姓名、性别、地址、邮编、电话)
SetWindowText(hNameWnd,pNode->m_stu.name );
SetWindowText(hSexWnd,pNode->m_stu.sex);
SetWindowText(hAddressWnd,pNode->m_stu.address);
SetWindowText(hZipWnd,pNode->m_stu.zip);
SetWindowText(hPhoneWnd,pNode->m_stu.phone);
}
}
}
}
break;
case IDCANCEL:
EndDialog(hDlgWnd,IDOK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CLOSE ://对话框关闭消息
EndDialog(hDlgWnd,0);
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
break;
default:
break;
}
return 0;
}
//删除 学生信息对话框回调函数
LRESULT CALLBACK DlgProc_Del(HWND hDlgWnd,//对话框句柄
UINT message,//消息参数
WPARAM wParam,
LPARAM lParam)
{
HWND hTopWnd,hDelWnd,hStdIdWnd;
RECT rc,Drc;
int wmID;
Node * pNode=NULL;
char No[20];
hStdIdWnd = GetDlgItem(hDlgWnd, IDC_EDIT1);
hDelWnd = GetDlgItem(hDlgWnd,IDD_DIALOG_DEL);
switch (message)
{
case WM_INITDIALOG://对话框初始消息
hTopWnd = GetDesktopWindow();
GetWindowRect(hTopWnd,&rc);
GetWindowRect(hDlgWnd,&Drc);
SetWindowPos(hDlgWnd,HWND_TOP,(rc.right - Drc.right + Drc.left)/2,(rc.bottom - Drc.bottom + Drc.top )/2,Drc.right -Drc.left,Drc.bottom - Drc.top,SWP_SHOWWINDOW);
return 1;
break;
case WM_COMMAND:
wmID=LOWORD(wParam);
switch(wmID)
{
case IDOK:
{
GetWindowText(hStdIdWnd,No,10);
if(strlen(FileName)==0)
MessageBox(hDlgWnd,"你未新建或打开一个通迅文件,请先进行此操作!",Prompt,MB_OK|MB_ICONERROR);
else
{
if(strlen(No)==0)
MessageBox(hDlgWnd,"学号不能为空,请输入所要删除的学号!",Prompt,MB_OK|MB_ICONERROR);
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -