/* This is for Rena Shah my Lovely Online Lady Hope this helps. I tried to use the standard library containers as much as possible */ #include "resource.h" #include #include #include #include #include using namespace std; #include #include #include #include #include template class GDIObj { public: T t; GDIObj() { t = 0; } GDIObj(const T& t) { this->t = t; } ~GDIObj() { if(t) DeleteObject(t); } T operator*() { return t; } void operator=(const T &t) { this->t = t; } }; GDIObj bmp = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_BITMAP1)); LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; HWND bBack,bNext,bClear,bSave,hcombo,bFname,bSname,bMname,bAddress,bCard,bMale,bFemale,hNew; char filename[255]; struct Info { string firstname; string lastname; string middlename; string address; string sex; }Info; static int count=0; vector info; HWND info_list = 0; string getData(HWND hwnd) { string data; LRESULT len = SendMessage(hwnd,WM_GETTEXTLENGTH,0,0); char *dat = new char [ len + 2 ]; SendMessage(hwnd,WM_GETTEXT,len+1,(LPARAM)(LPCSTR)dat); data = dat; delete [] dat; return data; } string getSex() { if(SendMessage(bMale,BM_GETSTATE,0,0)) return "Male"; return "Female"; } string substr(string& source, size_t start, size_t stop) { string temp = ""; for(size_t i = start; i < stop && i < source.length(); i++) temp += source[i]; return temp; } void read_list() { fstream fin; fin.open("test.txt", ios::in | ios::out ); string str_temp; char str[1024]; while ( fin.getline(str,1024,char(253)) ) { str_temp = str; struct Info inf; string::size_type pos = 0, pos2 = 0; inf.firstname = substr(str_temp,0,pos=str_temp.find(char(254))); inf.middlename = substr(str_temp,pos+1,pos2=str_temp.find(char(254),pos+1)); inf.lastname = substr(str_temp,pos2+1,pos=str_temp.find(char(254),pos2+1)); inf.address = substr(str_temp,pos+1,pos2=str_temp.find(char(254),pos+1)); inf.sex = substr(str_temp,pos2+1,str_temp.length()); // use string manipulation to fill out the structure then push it into the vector info.push_back(inf); } fin.close(); } bool operator<(const struct Info &i, const struct Info &i2) { if(i.lastname < i2.lastname) return true; return false; } bool operator==(const struct Info &i, const struct Info &i2) { if(i.lastname == i2.lastname && i.firstname == i2.firstname && i.middlename == i2.middlename) return true; return false; } void un_push_back(vector &v, const vector::iterator &p) { vector::iterator i; for(i = v.begin(); i != v.end(); i++) if(*i == *p) { i->address = p->address; i->sex = p->sex; return; } struct Info pp; pp.address = p->address, pp.firstname = p->firstname, pp.lastname = p->lastname, pp.middlename = p->middlename, pp.sex = p->sex; v.push_back(pp); } void rem_dup(vector &info) { vector v; vector::iterator i; for(i = info.begin(); i != info.end(); i++) un_push_back(v, i); info.empty(); info = v; } void write_list() { fstream fout; sort(info.begin(), info.end()); rem_dup(info); fout.open("test.txt", ios::out ); for(size_t i = 0; i < info.size(); i++) fout << info[i].firstname << char(254) << info[i].middlename << char(254) << info[i].lastname << char(254) << info[i].address << char(254) << info[i].sex << char(253); // add more of the structure that you want to save here fout.close(); } void load_list() { sort(info.begin(), info.end()); rem_dup(info); string temp; SendMessage(info_list,LB_RESETCONTENT,0,0); for(size_t i = 0; i < info.size(); i++) { temp = info[i].lastname + " , " + info[i].firstname; SendMessage(info_list,LB_ADDSTRING,temp.length(),(LPARAM)(LPCSTR)temp.c_str()); } } void new_item() { SetWindowText(bFname,""); SetWindowText(bSname,""); SetWindowText(bMname,""); SetWindowText(bAddress,""); SendMessage(bMale,BM_SETCHECK,FALSE,0); SendMessage(bFemale,BM_SETCHECK,FALSE,0); } void load_item(int itm) { SetWindowText(bFname,info[itm].firstname.c_str()); SetWindowText(bSname,info[itm].lastname.c_str()); SetWindowText(bMname,info[itm].middlename.c_str()); SetWindowText(bAddress,info[itm].address.c_str()); if(info[itm].sex == "Male") { SendMessage(bMale,BM_SETCHECK,TRUE,0); SendMessage(bFemale,BM_SETCHECK,FALSE,0); } else { SendMessage(bFemale,BM_SETCHECK,TRUE,0); SendMessage(bMale,BM_SETCHECK,FALSE,0); } } void add_fields() { Info.firstname = getData(bFname); Info.middlename = getData(bMname); Info.lastname = getData(bSname); Info.address = getData(bAddress); Info.sex = getSex(); //info.sex info.push_back(Info); write_list(); load_list(); } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("ScanInfotech") ; static HBITMAP hBitmap ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT ("ScanInfotech"), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 640,580, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return int(msg.wParam); } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static int cxClient,cyClient; switch(iMsg) { case WM_DESTROY: PostQuitMessage(0); break; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); break; case WM_CREATE: { read_list(); HWND hSt = CreateWindow("Static",0,WS_CHILD | WS_VISIBLE | SS_BITMAP , 0,0,640,580,hwnd,0,GetModuleHandle(0),0); SendMessage(hSt,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)(HBITMAP)*bmp); hNew = CreateWindow("Button","New",WS_CHILD | WS_VISIBLE | WS_BORDER ,10,350,50,25,hwnd,(HMENU)22, GetModuleHandle(NULL),0); bBack= CreateWindow("Button","Back",WS_CHILD | WS_VISIBLE | WS_BORDER ,10+60,350,50,25,hwnd,(HMENU)1,GetModuleHandle(NULL),0); bNext = CreateWindow("Button","Next",WS_CHILD | WS_VISIBLE | WS_BORDER ,10+60*2,350,50,25,hwnd,(HMENU)2,GetModuleHandle(NULL),0); bClear= CreateWindow("Button","Clear",WS_CHILD | WS_VISIBLE | WS_BORDER ,10+60*3,350,50,25,hwnd,(HMENU)3,GetModuleHandle(NULL),0); bSave = CreateWindow("Button","Save",WS_CHILD | WS_VISIBLE | WS_BORDER ,10+60*4,350,50,25,hwnd,(HMENU)4,GetModuleHandle(NULL),0); bCard = CreateWindow("Button","Card",WS_BORDER | WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,500,250,100,25,hwnd,(HMENU)5,GetModuleHandle(NULL),0); bMale = CreateWindow("Button","Male", WS_BORDER | WS_CHILD | WS_VISIBLE |BS_AUTORADIOBUTTON ,300,250,100,25,hwnd,(HMENU)6,GetModuleHandle(NULL),0); bFemale = CreateWindow("Button","Female",WS_BORDER | WS_CHILD | WS_VISIBLE |BS_AUTORADIOBUTTON,300,275,100,25,hwnd,(HMENU)7,GetModuleHandle(NULL),0); bFname= CreateWindow( "Edit","First Name",WS_TABSTOP|WS_CHILD|WS_VISIBLE|WS_BORDER| ES_WANTRETURN|ES_CENTER|ES_AUTOVSCROLL|ES_MULTILINE,15,40,130,25,hwnd,(HMENU)8,GetModuleHandle(NULL),0); bMname= CreateWindow( "Edit","Middle Name",WS_CHILD|WS_VISIBLE|WS_BORDER| ES_WANTRETURN|ES_CENTER|ES_AUTOVSCROLL|ES_MULTILINE,150,40,130,25,hwnd,(HMENU)9,GetModuleHandle(NULL),0); bSname= CreateWindow( "Edit","Last Name",WS_CHILD|WS_VISIBLE|WS_BORDER| ES_WANTRETURN|ES_CENTER|ES_AUTOVSCROLL|ES_MULTILINE,285,40,130,25,hwnd,(HMENU)10,GetModuleHandle(NULL),0); bAddress= CreateWindow( "Edit","Address",WS_CHILD|WS_VSCROLL |WS_VISIBLE|WS_BORDER| ES_WANTRETURN|ES_CENTER|ES_AUTOVSCROLL|ES_MULTILINE,15,80,130,100,hwnd,(HMENU)10,GetModuleHandle(NULL),0); hcombo = CreateWindow("ComboBox",NULL,WS_VSCROLL | WS_BORDER|CBS_DISABLENOSCROLL|WS_CHILD|CBS_DROPDOWNLIST|WS_VISIBLE,400,200,150,300,hwnd,(HMENU)11,GetModuleHandle(NULL),0); info_list = CreateWindow("ListBox",NULL,WS_CHILD | WS_VSCROLL |LBS_NOTIFY | WS_VISIBLE | WS_BORDER, 20,400,300,100,hwnd,(HMENU)6,GetModuleHandle(0),0); SendMessage(hcombo,CB_ADDSTRING,255,(LPARAM)(LPCSTR)"City"); SendMessage(hcombo,CB_ADDSTRING,255,(LPARAM)(LPCSTR)"Mumbai"); SendMessage(hcombo,CB_ADDSTRING,255,(LPARAM)(LPCSTR)"etc"); SendMessage(hcombo,CB_SETCURSEL,0,0); load_list(); } break; case WM_COMMAND: { if(HIWORD(wParam)==LBN_SELCHANGE&&LOWORD(wParam)==6) { load_item(int(SendMessage(info_list,LB_GETCURSEL,0,0))); } LRESULT sel; switch(HIWORD(wParam)) { case CBN_SELCHANGE: switch(LOWORD(wParam)) { case 11: sel = SendMessage(hcombo,CB_GETCURSEL,0,0); break; } case BN_CLICKED: switch(LOWORD(wParam)) { case 1: { LRESULT sel = SendMessage(info_list,LB_GETCURSEL,0,0); if(sel > 0) { sel--; SendMessage(info_list,LB_SETCURSEL,sel,0); load_item(int(sel)); } } break; case 2: { LRESULT sel = SendMessage(info_list,LB_GETCURSEL,0,0); if(size_t(sel) < info.size()-1) { sel++; SendMessage(info_list,LB_SETCURSEL,sel,0); load_item(int(sel)); } } break; case 3: { LRESULT sel = SendMessage(info_list,LB_GETCURSEL,0,0); if(sel >= 0 && size_t(sel) < info.size()) { string str =""; str = str + "Are you sure you wish to remove The entry " + info[sel].lastname + ", " + info[sel].firstname + "?"; size_t pos = 0; if(MessageBox(hwnd,str.c_str(),"Remove?",MB_YESNO | MB_ICONQUESTION) == IDYES) if(info.size()==1) info.erase(info.begin(),info.end()); else for(vector::iterator i = info.begin(); i != info.end(); i++,pos++) { if(*i == info[sel]) { info.erase(i); new_item(); break; } } write_list(); load_list(); } } break; case 4: { add_fields(); } break; case 22: { if((getData(bFname).length() != 0 || getData(bMname).length() != 0 || getData(bSname).length() != 0 || getData(bAddress).length() != 0 ) && MessageBox(hwnd,"Do you wish to save what is currently in the fields?", "Save?", MB_YESNO | MB_ICONQUESTION) == IDYES) { add_fields(); } new_item(); } break; } break; } } default: return DefWindowProc(hwnd,iMsg,wParam,lParam); } return (0); }