Address Book-Win32GUI production

Making with Win32GUI is a new enhancement to the address book-C ++ .

CodeBlocks Undefined reference to `GetOpenFileNameA @ 'problem solution: need to add lib library for compiler: (add libcomdlg32.a file)

Code:

#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define FILE_MENU_CREATE 1
#define FILE_MENU_UPDATE 2
#define FILE_MENU_READ 3
#define FILE_MENU_DELETE 4
#define FILE_MENU_IMPORT 5
#define FILE_MENU_EXPORT 6
#define FILE_MENU_EXIT 7
#define CREATE 1
#define UPDATE 2
#define READ 3
#define DEL 4


/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

void InitWindow(HWND);
void InitDialog(HWND);
void AddMenus(HWND);
void registerDialogClass(HINSTANCE);
void displayDialogCreate(HWND);
void displayDialogCreateAddControls(HWND);
void displayDialogRead(HWND);
void displayDialogReadAddControls(HWND);
void displayDialogUpdate(HWND);
void displayDialogUpdateAddControls(HWND);
void displayDialogDel(HWND);
void displayDialogDelAddControls(HWND);


struct Person
{
    char num[30];
    char name[30];
    char gender[30];
    char age[30];
    char major[30];
    char phone[30];
    char address[50];
};
const int item=100;
Person PersonBook[item];
int numPerson;

HMENU hMenu;
HWND hNumber,hName,hAge,hGender,hClass,hPhone,hAddress,hMainWindow,hOut;
HWND hwnd;
/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");


int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
{
                  /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
    {
         MessageBox ( NULL, TEXT ("This program requires Windows NT!"),szClassName, MB_ICONERROR) ;
         return 0;
    }

    registerDialogClass(hThisInstance);

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               _T("通讯录"),       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

void open_file(HWND);
void save_file(HWND);
void total()
{
    numPerson=0;
    for(int i=0;i<100;i++)
    {
        if(strcmp(PersonBook[i].name,"")==0)
            break;
        numPerson=i+1;
    }
}
/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int val;
    switch (message)                  /* handle the messages */
    {
    case WM_COMMAND:
        switch(wParam)
        {
        case FILE_MENU_EXIT:
            val=MessageBoxW(hwnd,L"Are you sure?",L"Wait!",MB_YESNO|MB_ICONEXCLAMATION);
            if(val==IDYES)
            {
                DestroyWindow(hwnd);
            }
            else if(val==IDNO)
            {

            }
            break;
        case FILE_MENU_CREATE:
            displayDialogCreate(hwnd);
            //MessageBeep(MB_ICONINFORMATION);
            break;
        case FILE_MENU_READ:
            displayDialogRead(hwnd);
            break;
        case FILE_MENU_UPDATE:
            displayDialogUpdate(hwnd);
            break;
        case FILE_MENU_DELETE:
            displayDialogDel(hwnd);
            break;
        case FILE_MENU_IMPORT:
            open_file(hwnd);
            total();
            InitWindow(hwnd);
            break;
        case FILE_MENU_EXPORT:
            save_file(hwnd);
            break;
        }
        break;
    case WM_CREATE:
        InitDialog(hwnd);
        open_file(hwnd);
        total();
        InitWindow(hwnd);
        AddMenus(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
/* Initialization of Window Menu*/
void AddMenus(HWND hwnd)
{
    hMenu=CreateMenu();
    HMENU hFileMenu=CreateMenu();

    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_CREATE,"添加");
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_UPDATE,"修改");
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_READ,"查询");
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_DELETE,"删除");
    AppendMenu(hFileMenu,MF_SEPARATOR,(UINT_PTR)NULL,NULL);
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_IMPORT,"导入");
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_EXPORT,"导出");
    AppendMenu(hFileMenu,MF_SEPARATOR,(UINT_PTR)NULL,NULL);
    AppendMenu(hFileMenu,MF_STRING,FILE_MENU_EXIT,"退出");
    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFileMenu,"菜单");
    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)NULL,"设置");
    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)NULL,"帮助");

    SetMenu(hwnd,hMenu);
}

/* Initialization of Window Message*/

void InitWindow(HWND hwnd)
{
    int x,y,padding;
    int cxScreen, cyScreen ;
    cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
    cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
    x=10;
    y=50;
    padding=2;
    int cell_width=(cxScreen-50)/12;
    int cell_height=20;

    CreateWindow("Static","通讯录 v2.0.1",WS_VISIBLE|WS_CHILD|ES_CENTER,0,10,cxScreen,30,hwnd,NULL,NULL,NULL);

    CreateWindow("Static","序号",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*0,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","学号",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*1,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","姓名",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*2,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","性别",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*3,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","年龄",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*4,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","班级",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*5,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","电话",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*6,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","地址",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*7,y,cell_width*4,cell_height,hwnd,NULL,NULL,NULL);
    //CreateWindow("Static","操作",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*11,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);

    int num=(cyScreen*0.716666)/22;
    char str[10],str1[10];
    for(int i=1;i<=num;i++)
    {
        itoa(i,str,10);
        CreateWindow("Static",str,WS_VISIBLE|WS_CHILD|ES_LEFT,                          x+(cell_width+padding)*0,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].num,WS_VISIBLE|WS_CHILD|ES_LEFT,     x+(cell_width+padding)*1,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].name,WS_VISIBLE|WS_CHILD|ES_LEFT,    x+(cell_width+padding)*2,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].gender,WS_VISIBLE|WS_CHILD|ES_LEFT,  x+(cell_width+padding)*3,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].age,WS_VISIBLE|WS_CHILD|ES_LEFT,     x+(cell_width+padding)*4,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].major,WS_VISIBLE|WS_CHILD|ES_LEFT,   x+(cell_width+padding)*5,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].phone,WS_VISIBLE|WS_CHILD|ES_LEFT,   x+(cell_width+padding)*6,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
        CreateWindow("Static",(LPSTR)PersonBook[i-1].address,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*7,y+(cell_height+padding)*i,cell_width*4,cell_height,hwnd,NULL,NULL,NULL);
        //CreateWindow("Button","修改",WS_VISIBLE|WS_CHILD|ES_CENTER,                     x+(cell_width+padding)*11,y+(cell_height+padding)*i,cell_width/2,cell_height,hwnd,NULL,NULL,NULL);
        //CreateWindow("Button","删除",WS_VISIBLE|WS_CHILD|ES_CENTER,                     x+(cell_width+padding)*11+cell_width/2,y+(cell_height+padding)*i,cell_width/2,cell_height,hwnd,NULL,NULL,NULL);
    }
    itoa(num,str,10);
    itoa(numPerson,str1,10);
    char s1[30];
    strcpy(s1,"");
    strcat(s1,"每页 ");
    strcat(s1,str);
    strcat(s1," 条数据");
    char s2[30];
    strcpy(s2,"");
    strcat(s2,"总共 ");
    strcat(s2,str1);
    strcat(s2," 条数据");

    CreateWindow("Static","",WS_VISIBLE|WS_CHILD|ES_LEFT,0,y+(cell_height+padding)*(num+1)+8,cxScreen,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Static",s1,WS_VISIBLE|WS_CHILD|ES_LEFT,10,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
    CreateWindow("Static",s2,WS_VISIBLE|WS_CHILD|ES_LEFT,20+cxScreen/12,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Button","上一页",WS_VISIBLE|WS_CHILD|ES_LEFT,cxScreen/2+(cxScreen/12)*2,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Button","第",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Edit","1",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3+cxScreen/36,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Button","页",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3+cxScreen/18,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
    //CreateWindow("Button","下一页",WS_VISIBLE|WS_CHILD|ES_LEFT,cxScreen/2+(cxScreen/12)*4,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
    CreateWindow("Static","仅供学习之用——小浣熊制作",WS_VISIBLE|WS_CHILD|ES_CENTER,0,y+(cell_height+padding)*(num+1)+42,cxScreen,50,hwnd,NULL,NULL,NULL);
    //CreateWindow("Static","vf",WS_VISIBLE|WS_CHILD|WS_HSCROLL|WS_VSCROLL,x,y+(cell_width+padding)*11,12,100,hwnd,NULL,NULL,NULL);
}

/* Initialization of Window Message*/
void InitDialog(HWND hwnd)
{
    MessageBox(hwnd,"欢迎使用通讯录v2.0","通讯录",MB_OK);
    MessageBox(hwnd,"请先导入通讯录文件!","通讯录",MB_OK);
}

LRESULT CALLBACK DialogProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    int val;
    int flag=0;
    int i;
    switch(msg)
    {
    /*case WM_CREATE:
        addControls(hWnd);*/
    case WM_COMMAND:
        switch(wp)
        {
        case CREATE:
            char number[30],name[30],gender[30],age[30],sclass[30],phone[30],address[30];

            GetWindowText(hNumber,number,30);
            GetWindowText(hName,name,30);
            GetWindowText(hGender,gender,30);
            GetWindowText(hAge,age,30);
            GetWindowText(hClass,sclass,30);
            GetWindowText(hPhone,phone,30);
            GetWindowText(hAddress,address,30);

            if(strcmp(number,"")==0||strcmp(name,"")==0||strcmp(gender,"")==0||strcmp(age,"")==0||strcmp(sclass,"")==0||strcmp(phone,"")==0||strcmp(address,"")==0)
            {
                val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
                switch(val)
                {
                    case IDABORT:
                        DestroyWindow(hWnd);
                        break;
                    case IDRETRY:
                        return 0;
                    case IDIGNORE:
                        break;
                }
            }
            else{
                strcpy(PersonBook[numPerson].num,number);
                strcpy(PersonBook[numPerson].name,name);
                strcpy(PersonBook[numPerson].gender,gender);
                strcpy(PersonBook[numPerson].age,age);
                strcpy(PersonBook[numPerson].major,sclass);
                strcpy(PersonBook[numPerson].phone,phone);
                strcpy(PersonBook[numPerson].address,address);
                numPerson++;
                MessageBox(hwnd,"录入成功!","添加联系人",MB_OK);
            }
            break;
        case READ:
            //char number[30],name[30],gender[30],age[30],sclass[30],phone[30],address[30];
            GetWindowText(hNumber,number,30);
            for(i=0;i<numPerson;i++)
            {
                if(strcmp(number,PersonBook[i].num)==0){
                    flag=1;break;
                }
            }
            if(flag==1)
            {
                //SetWindowText(hNumber,number);
                SetWindowText(hName,PersonBook[i].name);
                SetWindowText(hGender,PersonBook[i].gender);
                SetWindowText(hAge,PersonBook[i].age);
                SetWindowText(hClass,PersonBook[i].major);
                SetWindowText(hPhone,PersonBook[i].phone);
                SetWindowText(hAddress,PersonBook[i].address);
            }
            else{
                val=MessageBox(hWnd,"查无此人!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
                switch(val)
                {
                    case IDABORT:
                        DestroyWindow(hwnd);
                        break;
                    case IDRETRY:
                        return 0;
                    case IDIGNORE:
                        break;
                }
            }
            break;
        case UPDATE:
            //MessageBox(hwnd,"修改成功!","修改联系人",MB_OK);
            GetWindowText(hNumber,number,30);
            GetWindowText(hName,name,30);
            GetWindowText(hGender,gender,30);
            GetWindowText(hAge,age,30);
            GetWindowText(hClass,sclass,30);
            GetWindowText(hPhone,phone,30);
            GetWindowText(hAddress,address,30);

            for(i=0;i<numPerson;i++)
            {
                if(strcmp(number,PersonBook[i].num)==0){
                    flag=1;break;
                }
            }

            if(strcmp(number,"")==0||strcmp(name,"")==0||strcmp(gender,"")==0||strcmp(age,"")==0||strcmp(sclass,"")==0||strcmp(phone,"")==0||strcmp(address,"")==0)
            {
                val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
                switch(val)
                {
                    case IDABORT:
                        DestroyWindow(hWnd);
                        break;
                    case IDRETRY:
                        return 0;
                    case IDIGNORE:
                        break;
                }
            }
            else{
                strcpy(PersonBook[i].num,number);
                strcpy(PersonBook[i].name,name);
                strcpy(PersonBook[i].gender,gender);
                strcpy(PersonBook[i].age,age);
                strcpy(PersonBook[i].major,sclass);
                strcpy(PersonBook[i].phone,phone);
                strcpy(PersonBook[i].address,address);
                MessageBox(hwnd,"修改成功!","修改联系人",MB_OK);
            }
            break;
        case DEL:
            //MessageBox(hwnd,"删除成功!","删除联系人",MB_OK);
            GetWindowText(hNumber,number,30);
            GetWindowText(hName,name,30);
            GetWindowText(hGender,gender,30);
            GetWindowText(hAge,age,30);
            GetWindowText(hClass,sclass,30);
            GetWindowText(hPhone,phone,30);
            GetWindowText(hAddress,address,30);

            for(i=0;i<numPerson;i++)
            {
                if(strcmp(number,PersonBook[i].num)==0){
                    flag=1;break;
                }
            }

            if(strcmp(number,"")==0)
            {
                val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
                switch(val)
                {
                    case IDABORT:
                        DestroyWindow(hWnd);
                        break;
                    case IDRETRY:
                        return 0;
                    case IDIGNORE:
                        break;
                }
            }
            else{
                numPerson--;
                strcpy(PersonBook[i].num,PersonBook[numPerson].num);
                strcpy(PersonBook[i].name,PersonBook[numPerson].name);
                strcpy(PersonBook[i].gender,PersonBook[numPerson].gender);
                strcpy(PersonBook[i].age,PersonBook[numPerson].age);
                strcpy(PersonBook[i].major,PersonBook[numPerson].major);
                strcpy(PersonBook[i].phone,PersonBook[numPerson].phone);
                strcpy(PersonBook[i].address,PersonBook[numPerson].address);
                strcpy(PersonBook[numPerson].num,"");
                strcpy(PersonBook[numPerson].name,"");
                strcpy(PersonBook[numPerson].gender,"");
                strcpy(PersonBook[numPerson].age,"");
                strcpy(PersonBook[numPerson].major,"");
                strcpy(PersonBook[numPerson].phone,"");
                strcpy(PersonBook[numPerson].address,"");

                MessageBox(hwnd,"删除成功!","删除联系人",MB_OK);
            }
            break;
        }
        break;
    case WM_CLOSE:
        EnableWindow(hwnd,true);
        DestroyWindow(hWnd);
        InitWindow(hwnd);
        break;
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

void registerDialogClass(HINSTANCE hInst)
{
    WNDCLASSW dialog={0};

    dialog.hbrBackground=(HBRUSH)COLOR_WINDOW;
    dialog.hCursor=LoadCursor(NULL,IDC_CROSS);
    dialog.hInstance=hInst;
    dialog.lpszClassName=L"myDialogClass";
    dialog.lpfnWndProc=DialogProcedure;

    RegisterClassW(&dialog);
}

void displayDialogCreate(HWND hWnd)
{
    char* szStr = "添加联系人";
    WCHAR wszClassName[256];
    memset(wszClassName,0,sizeof(wszClassName));
    MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
    sizeof(wszClassName)/sizeof(wszClassName[0]));

    HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
    //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
    displayDialogCreateAddControls(hDlg);
    EnableWindow(hWnd,false);
}

void displayDialogCreateAddControls(HWND hWnd)
{
    int cxScreen, cyScreen ;
    cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
    cyScreen = GetSystemMetrics (SM_CYSCREEN) ;

    CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hNumber=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hName=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hGender=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAge=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hClass=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hPhone=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAddress=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);

    CreateWindow("Button","添加",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.2,cyScreen*0.05,hWnd,(HMENU)CREATE,NULL,NULL);
    //SendMessageW(hBut,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hGenerateImage);

    //hOut=CreateWindow("Edit","",WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.5,cyScreen*0.1,cxScreen*0.3,cyScreen*0.3,hWnd,NULL,NULL,NULL);
    //CreateWindowW(L"Static",NULL,WS_VISIBLE|WS_CHILD,350,60,100,100,hWnd,NULL,NULL,NULL);
    //SendMessageW(hLogo,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hLogoImage);
}
void displayDialogRead(HWND hWnd)
{
    char* szStr = "查询联系人";
    WCHAR wszClassName[256];
    memset(wszClassName,0,sizeof(wszClassName));
    MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
    sizeof(wszClassName)/sizeof(wszClassName[0]));

    HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
    //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
    displayDialogReadAddControls(hDlg);
    EnableWindow(hWnd,false);
}

void displayDialogReadAddControls(HWND hWnd)
{
    int cxScreen, cyScreen ;
    cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
    cyScreen = GetSystemMetrics (SM_CYSCREEN) ;

    CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hNumber=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hName=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hGender=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAge=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hClass=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hPhone=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAddress=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);

    CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.2,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);

}
void displayDialogUpdate(HWND hWnd)
{
    char* szStr = "修改联系人";
    WCHAR wszClassName[256];
    memset(wszClassName,0,sizeof(wszClassName));
    MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
    sizeof(wszClassName)/sizeof(wszClassName[0]));

    HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
    //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
    displayDialogUpdateAddControls(hDlg);
    EnableWindow(hWnd,false);
}

void displayDialogUpdateAddControls(HWND hWnd)
{
    int cxScreen, cyScreen ;
    cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
    cyScreen = GetSystemMetrics (SM_CYSCREEN) ;

    CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hNumber=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hName=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hGender=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAge=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hClass=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hPhone=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAddress=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);

    CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);
    CreateWindow("Button","修改",WS_VISIBLE|WS_CHILD,cxScreen*0.32,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)UPDATE,NULL,NULL);

}
void displayDialogDel(HWND hWnd)
{
    char* szStr = "删除联系人";
    WCHAR wszClassName[256];
    memset(wszClassName,0,sizeof(wszClassName));
    MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
    sizeof(wszClassName)/sizeof(wszClassName[0]));

    HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
    //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
    displayDialogDelAddControls(hDlg);
    EnableWindow(hWnd,false);
}

void displayDialogDelAddControls(HWND hWnd)
{
    int cxScreen, cyScreen ;
    cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
    cyScreen = GetSystemMetrics (SM_CYSCREEN) ;

    CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hNumber=CreateWindow("Edit","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hName=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hGender=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAge=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hClass=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hPhone=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
    CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
    hAddress=CreateWindow("Static","",   WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);

    CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);
    CreateWindow("Button","删除",WS_VISIBLE|WS_CHILD,cxScreen*0.32,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)DEL,NULL,NULL);

}
void display_file(char* path)
{
    FILE *file;
    file=fopen(path,"rb");
    fseek(file,0,SEEK_END);
    int _size=ftell(file);
    rewind(file);
    //char *data=new char[_size+1];
    fread(PersonBook,_size,1,file);
    //PersonBook[_size]='\0';
    /*char str[10];
    itoa(_size,str,10);
    MessageBox(NULL,str,"",MB_OK);*/
    //SetWindowText(hEdit,data);

    fclose(file);
}
void open_file(HWND hWnd)
{
    OPENFILENAME ofn;
    char file_name[100];

    ZeroMemory(&ofn,sizeof(OPENFILENAME));

    ofn.lStructSize=sizeof(OPENFILENAME);
    ofn.hwndOwner=hWnd;
    ofn.lpstrFile=file_name;
    ofn.lpstrFile[0]='\0';
    ofn.nMaxFile=100;
    ofn.lpstrFilter="All files\0*.*\0Source Files\0*.CPP\0Text Files\0*.TXT\0";
    ofn.nFilterIndex=1;

    GetOpenFileName(&ofn);

    display_file(ofn.lpstrFile);
    MessageBox(NULL,ofn.lpstrFile,"",MB_OK);
}
void write_file(char* path)
{
    FILE *file;
    file=fopen(path,"w");

    /*int _size=GetWindowTextLength(hEdit);
    char *data=new char[_size+1];
    GetWindowText(hEdit,data,_size+1);*/

    fwrite(PersonBook,230*item,1,file);

    fclose(file);
}

void save_file(HWND hWnd)
{
    OPENFILENAME ofn;
    char file_name[100];

    ZeroMemory(&ofn,sizeof(OPENFILENAME));

    ofn.lStructSize=sizeof(OPENFILENAME);
    ofn.hwndOwner=hWnd;
    ofn.lpstrFile=file_name;
    ofn.lpstrFile[0]='\0';
    ofn.nMaxFile=100;
    ofn.lpstrFilter="All files\0*.*\0Source Files\0*.CPP\0Text Files\0*.TXT\0";
    ofn.nFilterIndex=1;

    GetSaveFileName(&ofn);

    write_file(ofn.lpstrFile);
    //MessageBox(NULL,ofn.lpstrFile,"",MB_OK);
}

Screenshot

Main interface

 Delete contact

 add contact

Published 120 original articles · Likes3 · Visits 3750

Guess you like

Origin blog.csdn.net/weixin_43307431/article/details/105341243
Recommended