-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathListBoxWindow.cpp
More file actions
346 lines (241 loc) · 8.38 KB
/
ListBoxWindow.cpp
File metadata and controls
346 lines (241 loc) · 8.38 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ListBoxWindow.cpp
#include "ListBoxWindow.h"
// Global variables
static HWND g_hWndListBox;
BOOL IsListBoxWindow( HWND hWnd )
{
// See if supplied window is list box window
return( hWnd == g_hWndListBox );
} // End of function IsListBoxWindow
int ListBoxWindowAddString( LPCTSTR lpszString )
{
// Add string to list box window
return SendMessage( g_hWndListBox, LB_ADDSTRING, ( WPARAM )NULL, ( LPARAM )lpszString );
} // End of function ListBoxWindowAddString
BOOL ListBoxWindowCreate( HWND hWndParent, HINSTANCE hInstance )
{
BOOL bResult = FALSE;
// Create list box window
g_hWndListBox = CreateWindowEx( LIST_BOX_WINDOW_EXTENDED_STYLE, LIST_BOX_WINDOW_CLASS_NAME, LIST_BOX_WINDOW_TEXT, LIST_BOX_WINDOW_STYLE, 0, 0, 0, 0, hWndParent, ( HMENU )NULL, hInstance, NULL );
// Ensure that list box window was created
if( g_hWndListBox )
{
// Successfully created list box window
// Update return value
bResult = TRUE;
} // End of successfully created list box window
return bResult;
} // End of function ListBoxWindowCreate
BOOL ListBoxWindowGetRect( LPRECT lpRect )
{
// Get list box window rect
return GetWindowRect( g_hWndListBox, lpRect );
} // End of function ListBoxWindowGetRect
BOOL ListBoxWindowHandleCommandMessage( WPARAM wParam, LPARAM, BOOL( *lpStatusFunction )( LPCTSTR lpszItemText ) )
{
BOOL bResult = FALSE;
// Select list box window notification code
switch( HIWORD( wParam ) )
{
case LBN_DBLCLK:
{
// A list box window double click notification code
int nSelectedItem;
// Allocate string memory
LPTSTR lpszSelected = new char[ STRING_LENGTH + sizeof( char ) ];
// Get selected item
nSelectedItem = SendMessage( g_hWndListBox, LB_GETCURSEL, ( WPARAM )NULL, ( LPARAM )NULL );
// Get selected item text
if( SendMessage( g_hWndListBox, LB_GETTEXT, ( WPARAM )nSelectedItem, ( LPARAM )lpszSelected ) )
{
// Successfully got selected item text
// Display selected item text
MessageBox( NULL, lpszSelected, INFORMATION_MESSAGE_CAPTION, ( MB_OK | MB_ICONINFORMATION ) );
} // End of successfully got selected item text
// Free string memory
delete [] lpszSelected;
// Break out of switch
break;
} // End of a list box window double click notification code
case LBN_SELCHANGE:
{
// A list box window selection change notification code
int nSelectedItem;
// Allocate string memory
LPTSTR lpszSelected = new char[ STRING_LENGTH + sizeof( char ) ];
// Get selected item
nSelectedItem = SendMessage( g_hWndListBox, LB_GETCURSEL, ( WPARAM )NULL, ( LPARAM )NULL );
// Get selected item text
if( SendMessage( g_hWndListBox, LB_GETTEXT, ( WPARAM )nSelectedItem, ( LPARAM )lpszSelected ) )
{
// Successfully got selected item text
// Show selected item text on status bar window
( *lpStatusFunction )( lpszSelected );
} // End of successfully got selected item text
// Free string memory
delete [] lpszSelected;
// Break out of switch
break;
} // End of a list box window selection change notification code
default:
{
// Default notification code
// No need to do anything here, just continue with default result
// Break out of switch
break;
} // End of default notification code
}; // End of selection for list box window notification code
return bResult;
} // End of function ListBoxWindowHandleCommandMessage
BOOL ListBoxWindowHandleNotifyMessage( WPARAM, LPARAM lParam, BOOL( *lpStatusFunction )( LPCTSTR lpszItemText ) )
{
BOOL bResult = FALSE;
LPNMHDR lpNmhdr;
// Get notify message handler
lpNmhdr = ( ( LPNMHDR )lParam );
// Select list box window notification code
switch( lpNmhdr->code )
{
default:
{
// Default notification code
// No need to do anything here, just continue with default result
// Break out of switch
break;
} // End of default notification code
}; // End of selection for list box window notification code
return bResult;
} // End of function ListBoxWindowHandleNotifyMessage
BOOL ListBoxWindowMove( int nX, int nY, int nWidth, int nHeight, BOOL bRepaint )
{
// Move list box window
return MoveWindow( g_hWndListBox, nX, nY, nWidth, nHeight, bRepaint );
} // End of function ListBoxWindowMove
int ListBoxWindowLoad( LPCTSTR lpszFileName )
{
int nResult = 0;
HANDLE hFile;
// Open file
hFile = CreateFile( lpszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
// Ensure that file was opened
if( hFile != INVALID_HANDLE_VALUE )
{
// Successfully opened file
DWORD dwFileSize;
// Get file size
dwFileSize = GetFileSize( hFile, NULL );
// Ensure that file size was got
if( dwFileSize != INVALID_FILE_SIZE )
{
// Successfully got file size
// Allocate string memory
LPTSTR lpszFileText = new char[ dwFileSize + sizeof( char ) ];
// Read file text
if( ReadFile( hFile, lpszFileText, dwFileSize, NULL, NULL ) )
{
// Successfully read file text
LPTSTR lpszLine;
// Terminate file text
lpszFileText[ dwFileSize ] = ( char )NULL;
// Get first line in file text
lpszLine = strtok( lpszFileText, NEW_LINE_TEXT );
// Loop through all lines in file text
while( lpszLine )
{
// Add line to list box window
if( SendMessage( g_hWndListBox, LB_ADDSTRING, ( WPARAM )NULL, ( LPARAM )lpszLine ) >= 0 )
{
// Successfully added line to list box window
// Update return value
nResult ++;
// Get next line in file text
lpszLine = strtok( NULL, NEW_LINE_TEXT );
} // End of successfully added line to list box window
else
{
// Unable to add line to list box window
// Force exit from loop
lpszLine = NULL;
} // End of unable to add line to list box window
}; // End of loop through all lines in file text
} // End of successfully read file text
// Free string memory
delete [] lpszFileText;
} // End of successfully got file size
// Close file
CloseHandle( hFile );
} // End of successfully opened file
return nResult;
} // End of function ListBoxWindowLoad
int ListBoxWindowPopulate( LPCTSTR lpszFileName )
{
int nResult = 0;
// Clear list box window
SendMessage( g_hWndListBox, LB_RESETCONTENT, ( WPARAM )NULL, ( LPARAM )NULL );
// Load file
nResult = ListBoxWindowLoad( lpszFileName );
return nResult;
} // End of function ListBoxWindowPopulate
int ListBoxWindowSave( LPCTSTR lpszFileName )
{
int nResult = 0;
HANDLE hFile;
// Create file
hFile = CreateFile( lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
// Ensure that file was created
if( hFile != INVALID_HANDLE_VALUE )
{
// Successfully created file
int nItemCount;
int nWhichItem;
// Allocate string memory
LPTSTR lpszItemText = new char[ STRING_LENGTH + sizeof( char ) ];
// Count items on list box window
nItemCount = SendMessage( g_hWndListBox, LB_GETCOUNT, ( WPARAM )NULL, ( LPARAM )NULL );
// Loop through items on list box window
for( nWhichItem = 0; nWhichItem < nItemCount; nWhichItem ++ )
{
// Get item text
if( SendMessage( g_hWndListBox, LB_GETTEXT, ( WPARAM )nWhichItem, ( LPARAM )lpszItemText) != LB_ERR )
{
// Successfully got item text
// Write item text to file
if( WriteFile( hFile, lpszItemText, lstrlen( lpszItemText ), NULL, NULL ) )
{
// Successfully wrote item text to file
// Write new line text to file
WriteFile( hFile, NEW_LINE_TEXT, lstrlen( NEW_LINE_TEXT ), NULL, NULL );
// Update return value
nResult ++;
} // End of successfully wrote item text to file
else
{
// Unable to write item text to file
// Force exit from loop
nWhichItem = nItemCount;
} // End of unable to write item text to file
} // End of successfully got item text
else
{
// Unable to get item text
// Force exit from loop
nWhichItem = nItemCount;
} // End of unable to get item text
}; // End of loop through items on list box window
// Free string memory
delete [] lpszItemText;
// Close file
CloseHandle( hFile );
} // End of successfully created file
return nResult;
} // End of function ListBoxWindowSave
HWND ListBoxWindowSetFocus()
{
// Focus on list box window
return SetFocus( g_hWndListBox );
} // End of function ListBoxWindowSetFocus
void ListBoxWindowSetFont( HFONT hFont )
{
// Set list box window font
SendMessage( g_hWndListBox, WM_SETFONT, ( WPARAM )hFont, ( LPARAM )TRUE );
} // End of function ListBoxWindowSetFont