السلام عليكم و رحمة الله و بركاته
كنت بقدم على و ظيفة فى أحد الشركات و كان هناك ورشة عمل لابد من القيام بها
المهم قمت بعمل المهمه المطلوبة و هى كانت كالأتى
"عمل برنامج لقرأة Struct من ملف Binary و عرض محتوياتة فى شكل Tree المهم هو أن الـStruct عبارة عن Nested structs"
و اليكم الحل و أرجوا أن تستفيدوا منه
#include <windows.h>
#include "resource\resource.h"
#include "Struct.h"
#include <commctrl.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
HINSTANCE hInst;
#define WIN32_LEAN_AND_MEAN
TV_ITEM tvi;
TV_INSERTSTRUCT tvinsert;
HIMAGELIST hImageList;
HBITMAP hBitMap;
HWND hTree;
fstream F;
HTREEITEM Master;
HWND hEdit;
int strLen(char *target);
void DelteAllTree(HWND hWnd_);
void IntToChar(int target,char *returnChar);
void FloatToChar(float target,char *returnChar);
char *Concat(char *AddTo,char *Addthis);
void TerminateCharArray(char *target);
HTREEITEM AddNode(HTREEITEM Parent_ ,char *Title, HWND hWnd_);
HTREEITEM AddRoot(char *Title,HWND hWnd_);
BOOL CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
InitCommonControls();
hTree=GetDlgItem(hWnd,IDC_TREE1);
hImageList=ImageList_Create(16,16,ILC_COLOR16,3,10);
hBitMap=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_TREE));
ImageList_Add(hImageList,hBitMap,NULL);
DeleteObject(hBitMap);
SendDlgItemMessage(hWnd,IDC_TREE1,TVM_SETIMAGELIST,0,(LPARAM)hImageList);
tvinsert.hParent=NULL;
tvinsert.hInsertAfter=TVI_ROOT;
tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvinsert.item.pszText="Parent";
tvinsert.item.iImage=0;
tvinsert.item.iSelectedImage=1;
}
break;
case WM_PAINT:
{
return 0;
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_ADDROOT:
{
int count=0;
DelteAllTree(hWnd);
HTREEITEM Parent_1;
HTREEITEM struct_1;
HTREEITEM struct_2;
HTREEITEM struct_3;
HTREEITEM struct_4;
HTREEITEM struct_5;
HTREEITEM struct_6;
F.open(".\\Input.bin",ios::binary|ios::in|ios::out);
while (!F.eof())
{
count++;
MainStruct MainStructInstance,MainStructInstance_;
F.read((char*)&MainStructInstance,56);
MainStructInstance_=MainStructInstance;
char MS_yu,c1,c2,c3;
c1=MainStructInstance.b.hh;
c2=MainStructInstance.c.b.hh;
c3=MainStructInstance.io;
char *tempp;
IntToChar(count,&MS_yu);
tempp=Concat(" MainStruct #",&MS_yu);
Parent_1 = AddRoot(tempp,hWnd);
struct_1=AddNode(Parent_1," A ",hWnd);
IntToChar(MainStructInstance.a.nn,&MS_yu);
tempp = " nn = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_1,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.a.ii,&MS_yu);
tempp = " ii = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_1,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
struct_2=AddNode(Parent_1," B ",hWnd);
struct_4=AddNode(struct_2," A ",hWnd);
IntToChar(MainStructInstance.b.a.nn,&MS_yu);
tempp = " nn = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_4,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.b.a.ii,&MS_yu);
tempp = " ii = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_4,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
tempp = &c1; tempp++; *tempp=0;
tempp = " hh = ";
tempp =Concat(tempp,&c1);
AddNode(struct_2,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
struct_3=AddNode(Parent_1," C ",hWnd);
struct_5=AddNode(struct_3," A ",hWnd);
IntToChar(MainStructInstance.c.a.nn,&MS_yu);
tempp = " nn = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_5,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.c.a.ii,&MS_yu);
tempp = " ii = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_5,tempp ,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
struct_6=AddNode(struct_3," B ",hWnd);
struct_4=AddNode(struct_6," A ",hWnd);
IntToChar(MainStructInstance.c.b.a.nn,&MS_yu);
tempp = " nn = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_4,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.c.b.a.ii,&MS_yu);
tempp = " ii = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_4,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
tempp = &c2; tempp++; *tempp=0;
tempp = " hh = ";
tempp =Concat(tempp,&c2);
AddNode(struct_6,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.c.gg,&MS_yu);
tempp = " gg = ";
tempp =Concat(tempp,&MS_yu);
AddNode(struct_3,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
IntToChar(MainStructInstance.hj,&MS_yu);
tempp = " hj = ";
tempp =Concat(tempp,&MS_yu);
AddNode(Parent_1,tempp,hWnd);
TerminateCharArray(tempp);TerminateCharArray(&MS_yu);MainStructInstance=MainStructInstance_;
FloatToChar(MainStructInstance.yu,&MS_yu);
tempp = " yu = ";
tempp =Concat(tempp,&MS_yu);
AddNode(Parent_1,tempp,hWnd);
tempp = &c3; tempp++; *tempp=0;
tempp = " io = ";
tempp =Concat(tempp,&c3);
AddNode(Parent_1,tempp,hWnd);
}
F.close();
UpdateWindow(GetDlgItem(hWnd,IDC_TREE1));
}
break;
case IDC_DELALL:
{
DelteAllTree(hWnd);
UpdateWindow(hWnd);
}
break;
}
break;
case WM_CLOSE:
{
EndDialog(hWnd,0);
}
break;
}
break;
}
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
hInst=hInstance;
DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc,0);
return 0;
}
HTREEITEM AddRoot(char *Title,HWND hWnd_)
{
tvinsert.hParent=NULL;
tvinsert.hInsertAfter=TVI_ROOT;
tvinsert.item.pszText=Title;
HTREEITEM retVal=(HTREEITEM)SendDlgItemMessage(hWnd_,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
TreeView_SelectDropTarget(GetDlgItem(hWnd_,IDC_TREE1),retVal);
return retVal;
}
HTREEITEM AddNode(HTREEITEM Parent_ ,char *Title, HWND hWnd_)
{
tvinsert.hParent=Parent_;
tvinsert.hInsertAfter=TVI_LAST;
tvinsert.item.pszText=Title;
HTREEITEM retVal=(HTREEITEM)SendDlgItemMessage(hWnd_,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
TreeView_SelectDropTarget(GetDlgItem(hWnd_,IDC_TREE1),retVal);
return retVal;
}
void DelteAllTree(HWND hWnd_)
{
int TreeCount=TreeView_GetCount(GetDlgItem(hWnd_,IDC_TREE1));
for(int i=0;i<TreeCount;i++)
TreeView_DeleteAllItems(GetDlgItem(hWnd_,IDC_TREE1));
}
void IntToChar(int target,char *returnChar)
{
itoa(target,returnChar, 10);
}
void FloatToChar(float target,char *returnChar)
{
gcvt((double)target,20,returnChar);
}
char *Concat(char *AddTo,char *Addthis)
{
int ValOfAddTo =strLen(AddTo),ValOfAddthis =strLen(Addthis),i,j;
char *temp;
temp=new char[ValOfAddTo+ValOfAddthis];
for (i=0;i<ValOfAddTo;i++)
{
temp=*AddTo;
AddTo++;
}
for (j=0;j<ValOfAddthis;j++)
{
temp[i+j]=*Addthis;
Addthis++;
}
return temp;
}
int strLen(char *target)
{
int i=0;
while(*target!=0)
{
target++;
i++;
}
return i;
}
void TerminateCharArray(char *target)
{
while (*target=0)
{
*target = 0;
target++;
}
}
مرفق مع الموضوع ملف الذى يحتوى على كل المشروع
أرجوا منكم الأطلاع و المناقشة
