// WinStd.h
#ifndef _WINSTD_H_
#define _WINSTD_H_
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#include <stdio.h>
#include "resource.h"
#define CHAR_SIZE 1024
#define MAXSTRING 256
// 配置文件
#define CONFIG_FILE "\\Config.ini"
// 数据文件
#define DATA_FILE "\\RECORD"
typedef struct _SRecord
{
char chName[32];
char chSex[3];
char chBirthday[15];
char chContact[20];
char chAddress[80];
}SRecord, *PSRecord;
#endif
// Main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include "WinStd.h"
#include "ReportFrame.h"
#endif _MAIN_H_
// Main.cpp
#include "Main.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, MainProc);
return 0;
}
// ReportFrame.h
#ifndef _REPORTFRAME_H_
#define _REPORTFRAME_H_
#include "WinStd.h"
#include "BaseFun.h"
#include "InsertFrame.h"
//主窗口信息处理函数
//WM_COMMAND - 处理应用程序菜单;WM_PAINT - 绘制主窗口;WM_DESTORY - 发送退出信息并返回
BOOL WINAPI MainProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL MainOnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
//按钮动作响应函数
void MainOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
//退出信息处理函数
void MainOnClose(HWND hwnd);
void MainRightMenu();
VOID CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime);
void PrintRecord(const HWND hListView, const SRecord * sRecord, const size_t iNo, LVITEM lvItem);
void ViewRecord(const HWND hwnd);
#endif
// ReportFram.cpp
#include "ReportFrame.h"
BOOL MainOnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
HWND hListView = GetDlgItem(hwnd, IDC_LIST);
char chCurDir[CHAR_SIZE];
memset((void *)chCurDir, 0, CHAR_SIZE);
GetCurrentDirectoryA(CHAR_SIZE, chCurDir);
strcat(chCurDir, CONFIG_FILE);
FILE * fConfig = NULL;
fConfig = fopen(chCurDir, "r");
if (NULL == fConfig)
{
MessageBoxA(hwnd, chCurDir, "错误", MB_OK);
return FALSE;
}
LVCOLUMNW lvCol;
lvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
memset((void *)chCurDir, 0, CHAR_SIZE);
while (fgets(chCurDir, CHAR_SIZE, fConfig))
{
if (NULL != strstr(chCurDir, "[RECORD_TITLE]"))
{
memset((void *)chCurDir, 0, CHAR_SIZE);
fgets(chCurDir, CHAR_SIZE, fConfig);
break;
}
memset((void *)chCurDir, 0, CHAR_SIZE);
}
fclose(fConfig);
fConfig = NULL;
char * chSection = chCurDir;
char * chDomain = strchr(chCurDir, ':');
int i = 0;
while (NULL != chDomain)
{
char chItem[512];
memset(chItem, 0, sizeof(chItem)/sizeof(char));
strncpy(chItem, chSection, chDomain - chSection); // 列名
lvCol.pszText = ANSIToUnicode(chItem);
++chDomain;
chSection = strchr(chDomain, '|');
if (NULL != chSection)
{
char chSize[64];
memset(chSize, 0, sizeof(chSize)/sizeof(char));
strncpy(chSize, chDomain, chSection - chDomain); // 列长
lvCol.cx = atoi(chSize);
lvCol.iSubItem = i;
ListView_InsertColumn(hListView, i, &lvCol);
}
++i;
++chSection;
chDomain = strchr(chSection, ':');
}
SetTimer (hwnd, IDC_STATIC_TIME, 50, TimerProc);
ViewRecord(hListView);
return TRUE;
}
//按钮动作响应函数
void MainOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case ID_INSERT:
{
DialogBox(NULL, MAKEINTRESOURCE(IDD_INSERT_DIALOG), NULL, InsertProc);
}
break;
case ID_EDIT:
{
MessageBox(hwnd, TEXT("ID_EDIT"), TEXT("TIPS"), MB_YESNO);
}
break;
case ID_DELETE:
{
HWND hListView = GetDlgItem(hwnd, IDC_LIST);
DeleteRecord(hListView);
}
break;
default:
{
MessageBox(hwnd, TEXT("XXXX"), TEXT("TIPS"), MB_YESNO);
}
}
}
//退出信息处理函数
void MainOnClose(HWND hwnd)
{
KillTimer(hwnd, IDC_STATIC_TIME);
EndDialog(hwnd, 0);
}
//主窗口信息处理函数
//WM_COMMAND - 处理应用程序菜单;WM_PAINT - 绘制主窗口;WM_DESTORY - 发送退出信息并返回
BOOL WINAPI MainProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hwnd, WM_INITDIALOG, MainOnInitDialog);//消息分流器,定义在中
HANDLE_MSG(hwnd, WM_COMMAND, MainOnCommand);
HANDLE_MSG(hwnd, WM_CLOSE, MainOnClose);
}
return FALSE;
}
void MainRightMenu()
{
}
void CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
SYSTEMTIME systemtime1;
char chShowTime[MAXSTRING], chShowDate[MAXSTRING];
ZeroMemory(chShowTime, MAXSTRING);
ZeroMemory(chShowDate, MAXSTRING);
GetLocalTime(&systemtime1);
sprintf(chShowDate,"%d-%02d-%02d",systemtime1.wYear, systemtime1.wMonth,systemtime1.wDay);
sprintf(chShowTime,"%s %02d:%02d:%02d", chShowDate, systemtime1.wHour, systemtime1.wMinute,systemtime1.wSecond);
SetDlgItemTextA(hwnd,IDC_STATIC_TIME,chShowTime);
}
void PrintRecord(const HWND hListView, const SRecord * sRecord, const size_t iNo, LVITEM lvItem)
{
if (NULL == sRecord)
{
return ;
}
lvItem.pszText = ANSIToUnicode(sRecord->chName);
lvItem.iItem = iNo;
lvItem.iSubItem = 0;
ListView_InsertItem(hListView, &lvItem);
lvItem.iSubItem = 1;
lvItem.pszText = ANSIToUnicode(sRecord->chSex);
ListView_SetItem(hListView, &lvItem);
lvItem.iSubItem = 2;
lvItem.pszText = ANSIToUnicode(sRecord->chBirthday);
ListView_SetItem(hListView, &lvItem);
lvItem.iSubItem = 3;
lvItem.pszText = ANSIToUnicode(sRecord->chContact);
ListView_SetItem(hListView, &lvItem);
lvItem.iSubItem = 4;
lvItem.pszText = ANSIToUnicode(sRecord->chAddress);
ListView_SetItem(hListView, &lvItem);
}
void ViewRecord(const HWND hListView)
{
char chFile[CHAR_SIZE];
memset((void *)chFile, 0, CHAR_SIZE);
GetCurrentDirectoryA(CHAR_SIZE, chFile);
strcat(chFile, DATA_FILE);
FILE * fConfig = NULL;
fConfig = fopen(chFile, "r+");
if (NULL == fConfig)
{
MessageBoxA(NULL, chFile, "错误", MB_OK);
return ;
}
LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
SRecord sRecord;
ZeroMemory(&sRecord, sizeof(SRecord));
unsigned int iCnt = 0;
while (1 == fread(&sRecord, sizeof(sRecord), 1, fConfig))
{
PrintRecord(hListView, &sRecord, iCnt++, lvItem);
ZeroMemory(&sRecord, sizeof(SRecord));
}
fclose(fConfig);
fConfig = NULL;
}
void DeleteRecord(HWND hListView)
{
if (NULL == hListView)
{
MessageBoxA(NULL, "获取记录信息出错", "错误", MB_OK);
return ;
}
}
// InsertFrame.h
#ifndef _INSERTFRAME_H_
#define _INSERTFRAME_H_
#include "WinStd.h"
#include "BaseFun.h"
//主窗口信息处理函数
//WM_COMMAND - 处理应用程序菜单;WM_PAINT - 绘制主窗口;WM_DESTORY - 发送退出信息并返回
BOOL WINAPI InsertProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL InsertOnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
//按钮动作响应函数
void InsertOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
//退出信息处理函数
void InsertOnClose(HWND hwnd);
void InsertRecord(HWND hwnd);
void AppendRecord(SRecord sRecord);
#endif
// InsertFrame.cpp
#include "InsertFrame.h"
BOOL InsertOnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
CheckRadioButton(hwnd, IDC_SEX1, IDC_SEX2, IDC_SEX1);
return TRUE;
}
//退出信息处理函数
void InsertOnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
//主窗口信息处理函数
//WM_COMMAND - 处理应用程序菜单;WM_PAINT - 绘制主窗口;WM_DESTORY - 发送退出信息并返回
BOOL WINAPI InsertProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hwnd, WM_INITDIALOG, InsertOnInitDialog);//消息分流器,定义在中
HANDLE_MSG(hwnd, WM_COMMAND, InsertOnCommand);
HANDLE_MSG(hwnd, WM_CLOSE, InsertOnClose);
}
return FALSE;
}
//按钮动作响应函数
void InsertOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case ID_OK:
{
InsertRecord(hwnd);
}
break;
case ID_CLEAR:
{
MessageBox(hwnd, TEXT("ID_CLEAR"), TEXT("TIPS"), MB_YESNO);
}
break;
default:
{
;
}
}
}
void InsertRecord(HWND hwnd)
{
if (NULL == hwnd)
{
return ;
}
SRecord sRecord;
ZeroMemory(&sRecord, sizeof(SRecord));
GetDlgItemTextA(hwnd, IDC_EDIT_NAME, sRecord.chName, sizeof(sRecord.chName)/sizeof(char));
GetDlgItemTextA(hwnd, IDC_EDIT_CONTACT, sRecord.chContact, sizeof(sRecord.chContact)/sizeof(char));
GetDlgItemTextA(hwnd, IDC_EDIT_ADDR, sRecord.chAddress, sizeof(sRecord.chAddress)/sizeof(char));
GetDlgItemTextA(hwnd, IDC_BIRTHDAY, sRecord.chBirthday, sizeof(sRecord.chAddress)/sizeof(char));
if (BST_CHECKED == IsDlgButtonChecked(hwnd, IDC_SEX2))
{
sprintf(sRecord.chSex, "%s", "女");
}
else
{
sprintf(sRecord.chSex, "%s", "男");
}
AppendRecord(sRecord);
}
void AppendRecord(SRecord sRecord)
{
char chFile[CHAR_SIZE];
memset((void *)chFile, 0, CHAR_SIZE);
GetCurrentDirectoryA(CHAR_SIZE, chFile);
strcat(chFile, DATA_FILE);
FILE * fConfig = NULL;
fConfig = fopen(chFile, "a+");
if (NULL == fConfig)
{
MessageBoxA(NULL, chFile, "错误", MB_OK);
return ;
}
fwrite(&sRecord, sizeof(sRecord), 1, fConfig);
fclose(fConfig);
fConfig = NULL;
}
// BaseFun.h
#ifndef _BASEFUN_H_
#define _BASEFUN_H_
#include "WinStd.h"
wchar_t * ANSIToUnicode(const char * chSrc);
char * UnicodeToANSI( const wchar_t * wchSrc);
char * SetPathByChar(char * chPath);
wchar_t * SetPathByWChar(wchar_t * chPath);
void GetControlRect(HWND hwndPar,HWND hwndChild,RECT &rect);
#endif
//BaseFun.cpp
#include "BaseFun.h"
wchar_t * ANSIToUnicode(const char * chSrc)
{
if (NULL == chSrc)
{
return NULL;
}
size_t unicodeLen = MultiByteToWideChar(CP_ACP, 0, chSrc, -1, NULL,0);
if (CHAR_SIZE < unicodeLen)
{
return NULL;
}
wchar_t pUnicode[CHAR_SIZE];
memset(pUnicode, 0, CHAR_SIZE);
MultiByteToWideChar(CP_ACP, 0, chSrc, -1, (LPWSTR)pUnicode, unicodeLen);
return pUnicode;
}
char * UnicodeToANSI(const wchar_t * wchSrc)
{
if (NULL == wchSrc)
{
return NULL;
}
int iTextLen = WideCharToMultiByte(CP_ACP, 0, wchSrc, -1, NULL, 0, NULL, NULL);
char * pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
WideCharToMultiByte( CP_ACP, 0, wchSrc, -1, pElementText, iTextLen, NULL, NULL);
return pElementText;
}
char * SetPathByChar(const char * chPath)
{
if (NULL == chPath || CHAR_SIZE < strlen(chPath))
{
return NULL;
}
char chTmp[CHAR_SIZE * 2];
memset(chTmp, 0, CHAR_SIZE * 2);
char * chPoint = const_cast<char *>(chPath);
int i = 0;
while ( i < strlen(chPoint) && '\0' != *chPoint)
{
chTmp[i] = *chPoint;
if ('\\' == *chPoint)
{
chTmp[i] = '\\';
++i;
}
++i;
++chPoint;
}
return chTmp;
}
wchar_t * SetPathByWChar(const wchar_t * chPath)
{
if (NULL == chPath || CHAR_SIZE < wcslen(chPath))
{
return NULL;
}
wchar_t chTmp[CHAR_SIZE * 2];
memset(chTmp, 0, CHAR_SIZE * 2);
wchar_t * chPoint = const_cast<wchar_t *>(chPath);
int i = 0;
while ( i < wcslen(chPoint) && '\0' != *chPoint)
{
chTmp[i] = *chPoint;
if ('\\' == *chPoint)
{
chTmp[i] = '\\';
++i;
}
++i;
++chPoint;
}
return chTmp;
}
void GetControlRect(HWND hwndPar,HWND hwndChild,RECT &rect)
{
POINT pt;
GetWindowRect(hwndChild,&rect);
pt.x=rect.left;
pt.y=rect.top;
ScreenToClient(hwndPar,&pt);
rect.left=pt.x;
rect.top=pt.y;
pt.x=rect.right;
pt.y=rect.bottom;
ScreenToClient(hwndPar,&pt);
rect.right=pt.x;
rect.bottom=pt.y;
}
//resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Inventory.rc
//
#define ID_DELETE 3
#define IDD_MAIN_DIALOG 101
#define IDD_INSERT_DIALOG 102
#define IDC_LIST 1001
#define ID_INSERT 1002
#define ID_EDIT 1003
#define ID_OK 1004
#define ID_CLEAR 1005
#define IDC_EDIT_NAME 1006
#define IDC_SEX1 1007
#define IDC_EDIT_CONTACT 1008
#define IDC_EDIT_ADDR 1009
#define IDC_SEX2 1010
#define IDC_BIRTHDAY 1012
#define IDC_STATIC_TIME 1013
#define ITEM_EDIT 40002
#define ITEM_SELECT_ALL 40003
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40004
#define _APS_NEXT_CONTROL_VALUE 1015
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif