1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
| ////////////////////////////////////////////////////////////////////////////////
// Simple_TREE_View_INI_File.cpp
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#pragma comment(lib,"comctl32.lib")
char g_szAppname[] = "Simple [TREE View] INI-File";
#define IDC_TREEVIEW 100
#define MAX_SECTION_SIZE 32764
#define MAX_INIFILE_SIZE 65536
#define MAX_SECTION_KEY_SIZE 256
HWND g_hwndTreeView;
HIMAGELIST g_hImage;
int g_nImage, g_nSection, g_nEntry, g_nNonSelected;
BOOL IsSection(LPSTR* svFilename, LPSTR svName)
{
int iFilePtrPos;
int iSectionStringPtr;
BOOL bRetval = FALSE;
iFilePtrPos = iSectionStringPtr = 0;
if( '[' == (*svFilename)[iFilePtrPos] ) {
++iFilePtrPos;
while( (*svFilename)[iFilePtrPos] && (']' != (*svFilename)[iFilePtrPos]) ) {
svName[iSectionStringPtr] = (*svFilename)[iFilePtrPos];
++iSectionStringPtr;
++iFilePtrPos;
}
svName[iSectionStringPtr] = 0;
bRetval = TRUE;
}
while( ((*svFilename)[iFilePtrPos]) && ('\n' != (*svFilename)[iFilePtrPos]) )
++iFilePtrPos;
if( (*svFilename)[iFilePtrPos] ) ++iFilePtrPos;
*svFilename += iFilePtrPos;
return bRetval;
}
void FillKeyItems(HWND hwndTree, HTREEITEM hParent, LPSTR svFileName,
LPSTR svSect, int nEntry, int nNonSelected)
{
static char szData[MAX_SECTION_SIZE];
TV_ITEM tvi;
TV_INSERTSTRUCT tvins;
HTREEITEM hPrev = TVI_FIRST;
GetPrivateProfileSection(svSect, szData, MAX_SECTION_SIZE, svFileName);
LPSTR svPtr = szData;
while( *svPtr ) {
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvi.iImage = nNonSelected;
tvi.iSelectedImage = nEntry;
tvi.pszText = svPtr;
tvi.cchTextMax = lstrlen(svPtr);
tvins.item = tvi;
tvins.hInsertAfter = hPrev;
tvins.hParent = hParent;
hPrev = TreeView_InsertItem(hwndTree, &tvins);
svPtr += lstrlen(svPtr) + 1;
}
}
void FillKeys(HWND hwndTree, HTREEITEM hParent, LPSTR svFileName,
int nSection, int nEntry, int nNonSelected)
{
static char svIni[MAX_INIFILE_SIZE];
char svRaw[MAX_SECTION_KEY_SIZE];
char svSect[MAX_SECTION_KEY_SIZE+2];
TV_ITEM tvi;
TV_INSERTSTRUCT tvins;
HTREEITEM hPrev = TVI_FIRST;
OFSTRUCT of;
HFILE hFile = OpenFile(svFileName, &of, OF_READ);
int nSize = _lread(hFile, svIni, MAX_INIFILE_SIZE-1);
_lclose(hFile);
svIni[nSize] = 0;
LPSTR ptr = svIni;
while( *ptr ) {
if( IsSection(&ptr, svRaw) )
{
wsprintf(svSect, "[%s]", svRaw );
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvi.iImage = tvi.iSelectedImage = nSection;
tvi.pszText = svSect;
tvi.cchTextMax = sizeof(svSect);
tvins.item = tvi;
tvins.hInsertAfter = hPrev;
tvins.hParent = hParent;
hPrev = TreeView_InsertItem(hwndTree, &tvins);
FillKeyItems(hwndTree, hPrev, svFileName, svRaw, nEntry, nNonSelected);
}
}
}
//
// Fill Tree from INI
//
void Fill_TreeView_The_IniFile(HWND hwndTree, int nImage,
int nSection, int nEntry, int nNonSelected)
{
SetCapture(GetParent(hwndTree));
SetCursor(LoadCursor(NULL, IDC_WAIT));
TV_ITEM tvi;
TV_INSERTSTRUCT tvins;
HTREEITEM hPrev = TVI_FIRST;
TreeView_DeleteAllItems(hwndTree);
char svDir[MAX_PATH];
GetWindowsDirectory(svDir, sizeof(svDir));
lstrcat(svDir, "\\*.ini");
WIN32_FIND_DATA FindFile;
HANDLE hFindFile = FindFirstFile(svDir, &FindFile);
if( INVALID_HANDLE_VALUE != hFindFile ) {
do {
tvi.mask = TVIF_TEXT | TVIF_IMAGE;
tvi.iImage = tvi.iSelectedImage = nImage;
tvi.pszText = FindFile.cFileName;
tvi.cchTextMax = MAX_PATH;
tvins.item = tvi;
tvins.hInsertAfter = hPrev;
tvins.hParent = TVI_ROOT;
hPrev = TreeView_InsertItem(hwndTree, &tvins);
FillKeys(hwndTree, hPrev, FindFile.cFileName,
nSection, nEntry, nNonSelected);
}
while( FindNextFile(hFindFile, &FindFile) );
}
ReleaseCapture();
SetCursor(LoadCursor(NULL, IDC_ARROW));
}
HIMAGELIST CreateImageList(PINT ImageName, PINT KeyImage, PINT SelectImage, PINT NonSelectImage)
{
HIMAGELIST Imagelist = ImageList_Create(20, 12, TRUE, 4, 0);
if( !Imagelist ) return NULL;
HBITMAP hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_INI));
if( hBitmap ) {
*ImageName = ImageList_AddMasked(Imagelist, hBitmap, RGB(255,255,255));
DeleteObject (hBitmap);
}
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_KEY));
if( hBitmap ) {
*KeyImage = ImageList_AddMasked(Imagelist, hBitmap, RGB(255,255,255));
DeleteObject (hBitmap);
}
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_ENTRYS));
if( hBitmap ) {
*SelectImage = ImageList_AddMasked(Imagelist, hBitmap, RGB(255,255,255));
DeleteObject (hBitmap);
}
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_ENTRYN));
if( hBitmap ) {
*NonSelectImage = ImageList_AddMasked(Imagelist, hBitmap, RGB(255,255,255));
DeleteObject (hBitmap);
}
return Imagelist;
}
BOOL OnCreate(LPCREATESTRUCT lpCreateStruct)
{
HMENU hMenu = (HMENU)(UINT_PTR)IDC_TREEVIEW;
HWND hwndParent = lpCreateStruct->hwndParent;
HINSTANCE hInstance = lpCreateStruct->hInstance;
InitCommonControls();
DWORD dwStyle = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT
| TVS_HASLINES | WS_VISIBLE | WS_CHILD | WS_BORDER;
RECT Rect;
GetClientRect(hwndParent, &Rect);
g_hwndTreeView = CreateWindowEx(0, WC_TREEVIEW, NULL, dwStyle,
0, 0, Rect.right, Rect.bottom, hwndParent, hMenu, hInstance, NULL);
if( !g_hwndTreeView ) {
MessageBox(hwndParent, "Could not create Tree", g_szAppname,
MB_OK | MB_ICONSTOP | MB_SETFOREGROUND | MB_TOPMOST);
return -1;
}
g_hImage = CreateImageList(&g_nImage, &g_nSection, &g_nEntry, &g_nNonSelected);
if( g_hImage && hwndParent )
TreeView_SetImageList(g_hwndTreeView, g_hImage, 0);// Attach Image to TreeView.
Fill_TreeView_The_IniFile(g_hwndTreeView, g_nImage, g_nSection, g_nEntry, g_nNonSelected);
return 1;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT retval = 0;
LPCREATESTRUCT lpCreateStruct;
switch( uMsg ) {
case WM_CREATE: {
lpCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
lpCreateStruct->hwndParent = hWnd;
lpCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
}
return OnCreate(lpCreateStruct);
case WM_SIZE: {
UINT nType = (UINT)wParam;
if( (0 == nType) || (FALSE == IsWindowVisible(hWnd)) ) {
if( g_hwndTreeView ) {
SetWindowPos(g_hwndTreeView, NULL, 0, 0,
LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER);
InvalidateRect(hWnd, NULL, TRUE);
}
}
}
break;
case WM_DESTROY:
if( g_hImage ) ImageList_Destroy(g_hImage);
PostQuitMessage(0);
break;
default:
retval = DefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
return retval;
}
//
// Entry point.
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
HWND hWnd = HWND_DESKTOP;
WNDCLASSEX wcex = {sizeof(WNDCLASSEX), CS_VREDRAW | CS_HREDRAW, WndProc, 0, 0,
hInstance, LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
(HBRUSH)(COLOR_WINDOW+1), NULL, "WNDCLASS", NULL,};
ATOM Atom = RegisterClassEx(&wcex);
if( !Atom ) {
Atom = RegisterClass((LPWNDCLASS)&wcex.style);
if( !Atom ) {
MessageBox(hWnd, "Cannot RegisterClass", g_szAppname,
MB_OK | MB_ICONSTOP | MB_SETFOREGROUND | MB_TOPMOST);
return EXIT_FAILURE;
}
}
hWnd = CreateWindow(MAKEINTATOM(Atom), g_szAppname,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if( !hWnd ) {
MessageBox(hWnd, "Cannot create window", g_szAppname,
MB_OK | MB_ICONSTOP | MB_SETFOREGROUND | MB_TOPMOST);
return EXIT_FAILURE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
////////////////////////////////////////////////////////////////////////////////
// <<eof>> Simple_TREE_View_INI_File.cpp
//////////////////////////////////////////////////////////////////////////////// |