-
Notifications
You must be signed in to change notification settings - Fork 77
/
DlgFileTest.cpp
981 lines (835 loc) · 32.4 KB
/
DlgFileTest.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*****************************************************************************/
/* DlgFileTest.cpp Copyright (c) Ladislav Zezula 2009 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 27.05.09 1.00 Lad Created */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// Local variables
#define SC_HELP_ABOUT (SC_CLOSE + 0x800)
static SIZE InitialDialogSize = {0, 0};
static BOOL bDisableCloseDialog = FALSE;
//-----------------------------------------------------------------------------
// Thread moving the dialog
static DWORD WINAPI MoveDialogThread(PVOID pParam)
{
TWindowData * pData = (TWindowData *)pParam;
int nCurrentY = pData->nStartY;
int nAddY;
// Determine the direction
nAddY = (g_dwWinVer >= OSVER_WINDOWS_NT4) ? 1 : 5;
nAddY = (pData->nEndY > pData->nStartY) ? +nAddY : -nAddY;
for(;;)
{
// Move the dialog
nCurrentY += nAddY;
// Check for end of moving
if(pData->nEndY > pData->nStartY && nCurrentY > pData->nEndY)
break;
if(pData->nEndY < pData->nStartY && nCurrentY < pData->nEndY)
break;
// Move the entire dialog
SetWindowPos(pData->hDlg, NULL, pData->nStartX, nCurrentY, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
// We are done; close the thread handle and refresh the dialog position
GetWindowRect(pData->hDlg, &pData->DialogRect);
CloseHandle(pData->hThread);
pData->hThread = NULL;
return 0;
}
//-----------------------------------------------------------------------------
// "APC" thread - performing wait on all queued APCs and sends message
// when an APC is awaken
#define ALERT_REASON_STOP_WORKER 0 // The worker needs to stop
#define ALERT_REASON_UPDATE_WAIT 1 // The wait list needs to be updated
static DWORD WINAPI ApcThread(LPVOID pvParameter)
{
TWindowData * pData = (TWindowData *)pvParameter;
PLIST_ENTRY pHeadEntry;
PLIST_ENTRY pListEntry;
TApcEntry * ApcList[MAXIMUM_WAIT_OBJECTS];
HANDLE WaitHandles[MAXIMUM_WAIT_OBJECTS];
TApcEntry * pApc;
// The first event is always the alert event
WaitHandles[0] = pData->hAlertEvent;
assert(pData->hAlertEvent != NULL);
// Perform a loop until the process does not end
while(pData->hAlertEvent != NULL)
{
DWORD dwWaitCount = 1;
DWORD dwWaitResult;
// Prepare the list of handles to wait
EnterCriticalSection(&pData->ApcLock);
{
pHeadEntry = &pData->ApcList;
for(pListEntry = pHeadEntry->Flink; pListEntry != pHeadEntry; pListEntry = pListEntry->Flink)
{
// Retrieve the APC entry
pApc = CONTAINING_RECORD(pListEntry, TApcEntry, Entry);
// Insert the APC entry to the wait list
WaitHandles[dwWaitCount] = pApc->hEvent;
ApcList[dwWaitCount++] = pApc;
}
}
LeaveCriticalSection(&pData->ApcLock);
// Now when the list if prepared, we can perform wait on all
assert(dwWaitCount < MAXIMUM_WAIT_OBJECTS);
dwWaitResult = WaitForMultipleObjects(dwWaitCount, WaitHandles, FALSE, INFINITE);
// If the first wait broke, it means that we need to exit
if(dwWaitResult == WAIT_OBJECT_0 || dwWaitResult == WAIT_ABANDONED_0)
{
// If we need just to update wait list, do it
if(pData->dwAlertReason == ALERT_REASON_UPDATE_WAIT)
continue;
break;
}
// Check if any of the APCs has triggered
if(WAIT_OBJECT_0 < dwWaitResult && dwWaitResult < (WAIT_OBJECT_0 + dwWaitCount))
{
// Get the pointer to the triggered APC
pApc = ApcList[dwWaitResult - WAIT_OBJECT_0];
assert(pApc != NULL);
// Remove the APC from the list (locked)
EnterCriticalSection(&pData->ApcLock);
{
RemoveEntryList(&pApc->Entry);
pApc->Entry.Flink = NULL;
pApc->Entry.Blink = NULL;
pData->nApcCount--;
}
LeaveCriticalSection(&pData->ApcLock);
// Send the APC to the main dialog
// Note that the main dialog is responsible for freeing the APC
PostMessage(pApc->hWndPage, WM_APC, 0, (LPARAM)pApc);
}
}
// Now we need to free all the APCs
EnterCriticalSection(&pData->ApcLock);
{
pHeadEntry = &pData->ApcList;
for(pListEntry = pHeadEntry->Flink; pListEntry != pHeadEntry; )
{
// Retrieve the APC entry
pApc = CONTAINING_RECORD(pListEntry, TApcEntry, Entry);
pListEntry = pListEntry->Flink;
// Remove the APC from the list and free it
FreeApcEntry(pApc);
}
// Reset the APC list
InitializeListHead(&pData->ApcList);
pData->nApcCount = 0;
}
LeaveCriticalSection(&pData->ApcLock);
return 0;
}
static void AlertApcThread(TWindowData * pData, DWORD dwAlertReason)
{
// Signal the event handle and close it
if(pData->hApcThread && pData->hAlertEvent)
{
pData->dwAlertReason = dwAlertReason;
SetEvent(pData->hAlertEvent);
}
}
static void GetFileTestAppTitle(LPTSTR szTitle, int nMaxChars)
{
TCHAR szUserName[256] = _T("");
DWORD dwSize = _countof(szUserName);
UINT nIDTitle = IDS_APP_TITLE;
BOOL bElevated = FALSE;
// Get the elevation flags. Note that this returns FALSE on pre-Vista
if(GetTokenElevation(&bElevated))
{
nIDTitle = bElevated ? IDS_APP_TITLE_VISTA1 : IDS_APP_TITLE_VISTA2;
}
GetUserName(szUserName, &dwSize);
rsprintf(szTitle, nMaxChars, nIDTitle, szUserName);
}
//-----------------------------------------------------------------------------
// Local functions
static NTSTATUS SafeInitializeCriticalSection(CRITICAL_SECTION & Section)
{
__try
{
InitializeCriticalSection(&Section);
return STATUS_SUCCESS;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
return STATUS_NO_MEMORY;
}
}
static void InitializeTabControl(HWND hDlg, LPARAM lParam)
{
TFileTestData * pData = (TFileTestData *)(lParam);
PROPSHEETHEADER psh = {sizeof(PROPSHEETHEADER)};
PROPSHEETPAGE psp[13];
TCHAR szAppTitle[256];
HWND hTabCtrl = GetDlgItem(hDlg, IDC_TAB);
int nPageNtOpenFile = 0;
int nPageOpenFile = 0;
int nPages = 0;
// Get the title of FileTest application
GetFileTestAppTitle(szAppTitle, _countof(szAppTitle));
// Fill the property sheet header
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP | PSH_MODELESS;
psh.hwndParent = hDlg;
psh.hInstance = g_hInst;
psh.pszIcon = MAKEINTRESOURCE(IDI_FILE_TEST);
psh.pszCaption = szAppTitle;
psh.ppsp = psp;
// Fill the "Transaction" page (Vista only)
if(pfnCreateTransaction != NULL)
{
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE00_TRANSACTION);
psp[nPages].pfnDlgProc = PageProc00;
psp[nPages].lParam = lParam;
nPages++;
}
// Fill the "CreateFile" page
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE01_CREATE);
psp[nPages].pfnDlgProc = PageProc01;
psp[nPages].lParam = lParam;
nPageOpenFile = nPages;
psh.nStartPage = nPages++;
// Fill the "NtCreateFile"
// Note: If the file name looks like an NT name, go to NtCreateFile page
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE02_NTCREATE);
psp[nPages].pfnDlgProc = PageProc02;
psp[nPages].lParam = lParam;
nPageNtOpenFile = nPages;
nPages++;
// Fill the "ReadWrite"
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE03_READWRITE);
psp[nPages].pfnDlgProc = PageProc03;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "Mapping"
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE04_MAPPING);
psp[nPages].pfnDlgProc = PageProc04;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "File Ops".
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE05_FILEOPS);
psp[nPages].pfnDlgProc = PageProc05;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "NtFileInfo" page
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE06_NTFILEINFO);
psp[nPages].pfnDlgProc = PageProc06;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "NtFsInfo" page.
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE07_NTVOLINFO);
psp[nPages].pfnDlgProc = PageProc06;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "EA" page.
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE08_EA);
psp[nPages].pfnDlgProc = PageProc08; // The same like NtFileInfo
psp[nPages].lParam = lParam;
nPages++;
// Fill the "Security".
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE09_SECURITY);
psp[nPages].pfnDlgProc = PageProc09;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "Links".
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE10_LINKS);
psp[nPages].pfnDlgProc = PageProc10;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "Streams" page.
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE11_STREAMS);
psp[nPages].pfnDlgProc = PageProc11;
psp[nPages].lParam = lParam;
nPages++;
// Fill the "Ioctl" page.
ZeroMemory(&psp[nPages], sizeof(PROPSHEETPAGE));
psp[nPages].dwSize = sizeof(PROPSHEETPAGE);
psp[nPages].dwFlags = PSP_DEFAULT;
psp[nPages].hInstance = g_hInst;
psp[nPages].pszTemplate = MAKEINTRESOURCE(IDD_PAGE12_IOCTL);
psp[nPages].pfnDlgProc = PageProc12;
psp[nPages].lParam = lParam;
nPages++;
// Set the start page and number of pages
if((psh.nStartPage = pData->InitialPage) == INVALID_PAGE_INDEX)
psh.nStartPage = IsNativeName(pData->szFileName1) ? nPageNtOpenFile : nPageOpenFile;
psh.nPages = nPages;
// Create Tab Control
TabCtrl_Create(hTabCtrl, &psh);
// Get the currently selected page HWND
pData->hWndPage = TabCtrl_GetSelectedPage(hTabCtrl);
}
static void RefreshScreenSize(HWND hDlg)
{
TWindowData * pData = GetDialogData(hDlg);
int nScreenHeight;
int nDialogHeight;
// Save the screen size
SystemParametersInfo(SPI_GETWORKAREA, 0, &pData->ScreenRect, 0);
GetWindowRect(hDlg, &pData->DialogRect);
// Is the dialog higher than the screen?
nScreenHeight = pData->ScreenRect.bottom - pData->ScreenRect.top;
nDialogHeight = pData->DialogRect.bottom - pData->DialogRect.top;
pData->bDialogBiggerThanScreen = (nDialogHeight > nScreenHeight);
// If the dialog is bigger than the screen, set the timer
if(pData->bDialogBiggerThanScreen)
{
if(pData->CheckMouseTimer == 0)
{
pData->CheckMouseTimer = SetTimer(hDlg, WM_TIMER_CHECK_MOUSE, 500, NULL);
}
}
else
{
if(pData->CheckMouseTimer != 0)
{
KillTimer(hDlg, pData->CheckMouseTimer);
pData->CheckMouseTimer = 0;
}
}
}
//-----------------------------------------------------------------------------
// Original dialog size
#pragma pack(1)
typedef struct _DLGTEMPLATEEX_BEGIN
{
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
} DLGTEMPLATEEX_BEGIN, *PDLGTEMPLATEEX_BEGIN;
#pragma pack()
int GetDialogRectFromTemplate(HWND hDlg, UINT DlgResID, RECT & DlgRect)
{
PDLGTEMPLATEEX_BEGIN pDlgTemplate;
HGLOBAL hDlgRes;
HRSRC hResource;
hResource = FindResource(g_hInst, MAKEINTRESOURCE(DlgResID), RT_DIALOG);
if(hResource != NULL)
{
hDlgRes = LoadResource(g_hInst, hResource);
if(hDlgRes != NULL)
{
pDlgTemplate = (DLGTEMPLATEEX_BEGIN *)LockResource(hDlgRes);
if(pDlgTemplate != NULL)
{
// Check the dialog template
assert(pDlgTemplate->signature == (WORD)-1);
assert(pDlgTemplate->dlgVer == 1);
// Calculate the dialog size in pixels
DlgRect.top = 0;
DlgRect.left = 0;
DlgRect.right = pDlgTemplate->cx;
DlgRect.bottom = pDlgTemplate->cy;
MapDialogRect(hDlg, &DlgRect);
// Append the borders and the caption
DlgRect.right += (GetSystemMetrics(SM_CXSIZEFRAME) * 2);
DlgRect.bottom += GetSystemMetrics(SM_CYCAPTION) + (GetSystemMetrics(SM_CYSIZEFRAME) * 2);
return ERROR_SUCCESS;
}
}
}
return ERROR_RESOURCE_NOT_FOUND;
}
static void FixDialogToOriginalSize(HWND hDlg, UINT DlgResID)
{
RECT OriginalRect;
RECT CurrentRect;
RECT ScreenRect;
int x;
// Get the work area of the screen
SystemParametersInfo(SPI_GETWORKAREA, 0, &ScreenRect, 0);
ScreenRect.bottom = (ScreenRect.bottom - ScreenRect.top);
ScreenRect.right = (ScreenRect.right - ScreenRect.left);
// Get the current dialog size
GetWindowRect(hDlg, &CurrentRect);
CurrentRect.bottom = (CurrentRect.bottom - CurrentRect.top);
CurrentRect.right = (CurrentRect.right - CurrentRect.left);
CurrentRect.left = CurrentRect.top = 0;
// If the current height is greater than height
// of the worker area, we need to fix it
if(CurrentRect.bottom > ScreenRect.bottom)
{
// Get the dialog expected size
GetDialogRectFromTemplate(hDlg, DlgResID, OriginalRect);
// If the dialog has been shrunk, fix its size
if(OriginalRect.bottom > CurrentRect.bottom)
{
x = (ScreenRect.right - OriginalRect.right) / 2;
SetWindowPos(hDlg, NULL, x, 0, OriginalRect.right, OriginalRect.bottom, SWP_NOZORDER | SWP_NOACTIVATE);
}
}
}
static void AddAboutToSystemMenu(HWND hDlg)
{
MENUITEMINFO mii;
HMENU hSysMenu;
TCHAR szItemText[256];
int nSeparatorIndex = -1;
int nMenuCount;
// Retrieve system menu
hSysMenu = GetSystemMenu(hDlg, FALSE);
if(hSysMenu != NULL)
{
// Find the separator
nMenuCount = GetMenuItemCount(hSysMenu);
for(int i = 0; i < nMenuCount; i++)
{
// Retrieve the item type
ZeroMemory(&mii, sizeof(MENUITEMINFO));
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_FTYPE;
GetMenuItemInfo(hSysMenu, i, TRUE, &mii);
// Separator?
if(mii.fType == MFT_SEPARATOR)
{
nSeparatorIndex = i;
break;
}
}
// If we found a separator, we need to add two more items
if(nSeparatorIndex != -1)
{
LoadString(g_hInst, IDS_HELP_ABOUT, szItemText, _countof(szItemText));
InsertMenu(hSysMenu, nSeparatorIndex, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
InsertMenu(hSysMenu, nSeparatorIndex+1, MF_BYPOSITION | MF_STRING, SC_HELP_ABOUT, szItemText);
}
}
}
//-----------------------------------------------------------------------------
// Dialog handlers
static int OnInitDialog(HWND hDlg, LPARAM lParam)
{
TWindowData * pData = (TWindowData *)lParam;
DWORD dwThreadId;
RECT rectDialog;
// Initialize dialog data
SafeInitializeCriticalSection(pData->ApcLock);
InitializeListHead(&pData->ApcList);
pData->hDlg = hDlg;
SetDialogData(hDlg, pData);
// Add "About" in the system menu
AddAboutToSystemMenu(hDlg);
//
// Note: If the screen size is too low at this point (like 800x600),
// the dialog gets shrinked. We need to resize the dialog to the original size
//
FixDialogToOriginalSize(hDlg, IDD_FILE_TEST);
GetWindowRect(hDlg, &rectDialog);
InitialDialogSize.cx = (rectDialog.right - rectDialog.left);
InitialDialogSize.cy = (rectDialog.bottom - rectDialog.top);
// Create the tooltip window
g_Tooltip.Initialize(g_hInst, hDlg);
// Initialize Tab Control
InitializeTabControl(hDlg, lParam);
// Refresh information about screen rect and dialog rect
RefreshScreenSize(hDlg);
// Create the so called "APC" thread that will monitor our list of APC entries
pData->hAlertEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
pData->hApcThread = CreateThread(NULL, 0, ApcThread, pData, 0, &dwThreadId);
return TRUE;
}
static void OnSize(HWND hDlg, LPARAM lParam)
{
TWindowData * pData = GetDialogData(hDlg);
HWND hTabCtrl = GetDlgItem(hDlg, IDC_TAB);
HWND hButton = GetDlgItem(hDlg, IDC_EXIT);
RECT rect;
int nHeight = HIWORD(lParam);
int nWidth = LOWORD(lParam);
int x, y, cx, cy;
// Sanity check for zero size
if(pData != NULL && nWidth != 0 && nHeight != 0)
{
// If the dialog hasn't done the initial resize,
// we have to remember the relative sizes of the tab control
// and the exit button
if(!pData->bInitialResizeDone)
{
// Save the relative position of the tab control
GetWindowRect(hTabCtrl, &rect);
ScreenRectToClientRect(hDlg, &rect);
pData->nTabInnerTop = rect.top;
pData->nTabInnerLeft = rect.left;
pData->nTabInnerRight = pData->nTabInnerTop;
pData->nTabInnerBottom = pData->nTabInnerTop;
// Save position of the "Exit" button
GetWindowRect(hButton, &rect);
pData->nButtonInnerRight = pData->nTabInnerRight + (rect.right - rect.left);
pData->nButtonInnerBottom = pData->nTabInnerBottom + (rect.bottom - rect.top);
// Update the inner bottom margin of the tab cobtrol
pData->nTabInnerBottom = pData->nButtonInnerBottom + 8;
pData->bInitialResizeDone = true;
}
// Resize the tab control
cx = nWidth - (pData->nTabInnerLeft + pData->nTabInnerRight);
cy = nHeight - (pData->nTabInnerTop + pData->nTabInnerBottom);
TabCtrl_Resize(hTabCtrl, pData->nTabInnerLeft, pData->nTabInnerTop, cx, cy);
// Move the Exit button
x = nWidth - pData->nButtonInnerRight;
y = nHeight - pData->nButtonInnerBottom;
SetWindowPos(hButton, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
static void OnGetMinMaxInfo(HWND /* hDlg */, LPARAM lParam)
{
LPMINMAXINFO pmmi = (LPMINMAXINFO)lParam;
pmmi->ptMinTrackSize.x = InitialDialogSize.cx;
pmmi->ptMinTrackSize.y = InitialDialogSize.cy;
}
static void OnTimerCheckMouse(HWND hDlg)
{
TWindowData * pData = GetDialogData(hDlg);
POINT pt;
DWORD ThreadID;
int nTresholdX = 40;
int nTresholdY = pData->ScreenRect.bottom - 40;
// Get the current mouse position
GetCursorPos(&pt);
// If the dialog is currently bigger than the screen, check for limit values
if(pData->bDialogBiggerThanScreen)
{
// Only do something if the mouse cursor is within our rectangle
if(GetActiveWindow() == hDlg && PtInRect(&pData->DialogRect, pt))
{
if(pData->DialogRect.top < pData->ScreenRect.top && pt.y <= nTresholdX)
{
// Create thread that will move the dialog down
if(pData->hThread == NULL)
{
pData->nStartX = pData->DialogRect.left;
pData->nStartY = pData->DialogRect.top;
pData->nEndY = 0;
pData->hThread = CreateThread(NULL, 0, MoveDialogThread, pData, 0, &ThreadID);
}
return;
}
if(pData->DialogRect.bottom > pData->ScreenRect.bottom && pt.y >= nTresholdY)
{
// Create thread that will move the dialog up
if(pData->hThread == NULL)
{
pData->nStartX = pData->DialogRect.left;
pData->nStartY = pData->DialogRect.top;
pData->nEndY = (pData->ScreenRect.bottom - pData->ScreenRect.top) - (pData->DialogRect.bottom - pData->DialogRect.top);
pData->hThread = CreateThread(NULL, 0, MoveDialogThread, pData, 0, &ThreadID);
}
return;
}
}
}
}
static void OnHelpAbout(HWND hDlg)
{
HelpAboutDialog(hDlg);
}
static BOOL OnCommand(HWND hDlg, UINT /* nNotify */, UINT nIDCtrl)
{
switch(nIDCtrl)
{
case IDCANCEL:
case IDC_EXIT:
DestroyWindow(hDlg);
break;
}
return FALSE;
}
static void OnNotify(HWND hDlg, NMHDR * pNMHDR)
{
TWindowData * pData;
if(pNMHDR->code == TCN_SELCHANGE)
{
pData = GetDialogData(hDlg);
pData->hWndPage = TabCtrl_GetSelectedPage(pNMHDR->hwndFrom);
}
}
static void OnDestroy(HWND hDlg)
{
TWindowData * pData = GetDialogData(hDlg);
if(pData != NULL)
{
if(pData->hApcThread != NULL)
{
// Stop the watcher thread, if any
AlertApcThread(pData, ALERT_REASON_STOP_WORKER);
// Wait for the thread to exit. Not more than 5 second
WaitForSingleObject(pData->hApcThread, 5000);
CloseHandle(pData->hApcThread);
}
// Close the alert event handle
if(pData->hAlertEvent != NULL)
CloseHandle(pData->hAlertEvent);
pData->hAlertEvent = NULL;
// Delete the APC critical section
DeleteCriticalSection(&pData->ApcLock);
}
}
static INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Let Tab Control handle messages belonging to it
if(TabCtrl_HandleMessages(GetDlgItem(hDlg, IDC_TAB), uMsg, wParam, lParam))
return TRUE;
// Handle messages that have been passed to us
switch(uMsg)
{
case WM_INITDIALOG:
OnInitDialog(hDlg, lParam);
return TRUE;
case WM_SIZE:
OnSize(hDlg, lParam);
return FALSE;
case WM_GETMINMAXINFO:
OnGetMinMaxInfo(hDlg, lParam);
return FALSE;
case WM_WINDOWPOSCHANGED:
case WM_DISPLAYCHANGE:
RefreshScreenSize(hDlg);
break;
case WM_SETTINGCHANGE:
if(wParam == SPI_SETWORKAREA)
RefreshScreenSize(hDlg);
break;
case WM_TIMER:
if(wParam == WM_TIMER_CHECK_MOUSE)
OnTimerCheckMouse(hDlg);
break;
case WM_COMMAND:
return OnCommand(hDlg, HIWORD(wParam), LOWORD(wParam));
case WM_SYSCOMMAND:
if(wParam == SC_HELP_ABOUT)
OnHelpAbout(hDlg);
break;
case WM_NOTIFY:
OnNotify(hDlg, (NMHDR *)lParam);
break;
case WM_DESTROY:
OnDestroy(hDlg);
break;
}
return FALSE;
}
static BOOL IsMyDialogMessage(HWND hDlg, HWND hTabCtrl, LPMSG pMsg)
{
// Support for navigation keys
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_F2: // Allow tree item editing using F2 accelerator
return FALSE;
// If the dialog messages are disabled, we pass all key messages as-is.
// Example: When editing a tree view item, Enter and Esc key would
// be eaten by the dialog and they would never arrive to the edit box.
case VK_RETURN:
case VK_ESCAPE:
if(bDisableCloseDialog)
return FALSE;
break;
}
}
return TabCtrl_IsDialogMessage(hDlg, hTabCtrl, pMsg);
}
//-----------------------------------------------------------------------------
// Public functions - APC support
TApcEntry * CreateApcEntry(TWindowData * pData, UINT ApcType, size_t cbExtraSize)
{
TApcEntry * pApc = NULL;
// If there is too many APCs queued, do nothing
if(pData->nApcCount < MAXIMUM_WAIT_OBJECTS - 1)
{
// Allocate space for the APC structure
pApc = (TApcEntry *)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, sizeof(TApcEntry) + cbExtraSize);
if(pApc != NULL)
{
// Create new event object for the APC entry
pApc->Overlapped.hEvent =
pApc->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
pApc->ApcType = ApcType;
pApc->hWndPage = pData->hWndPage;
if(pApc->hEvent != NULL)
return pApc;
// Free the (now useless) APC entry
HeapFree(g_hHeap, 0, pApc);
}
}
// Failed
return NULL;
}
bool InsertApcEntry(TWindowData * pData, TApcEntry * pApc)
{
// Only if the APC is valid
if(pApc != NULL)
{
// Sanity check
assert(pApc->hEvent != NULL);
// If there is too many APCs queued, do nothing
if(pData->nApcCount < MAXIMUM_WAIT_OBJECTS - 1)
{
// Mark the APC as complete asynchronously
pApc->bAsynchronousCompletion = TRUE;
// Insert the APC to the APC list.
// The APC thread does not know about it yet
EnterCriticalSection(&pData->ApcLock);
InsertTailList(&pData->ApcList, &pApc->Entry);
pData->nApcCount++;
LeaveCriticalSection(&pData->ApcLock);
// Alert the APC thread so it knows that it needs to update the APC list
AlertApcThread(pData, ALERT_REASON_UPDATE_WAIT);
return true;
}
}
return false;
}
void FreeApcEntry(TApcEntry * pApc)
{
if(pApc != NULL)
{
if(pApc->hEvent != NULL)
CloseHandle(pApc->hEvent);
HeapFree(g_hHeap, 0, pApc);
}
}
//-----------------------------------------------------------------------------
// Public functions
int NtUseFileId(HWND hDlg, LPCTSTR szFileId)
{
TFileTestData * pData = GetDialogData(hDlg);
LPCTSTR szPlainName = szFileId + 3;
HWND hWndParent = GetParent(hDlg);
HWND hTabCtrl = GetDlgItem(hWndParent, IDC_TAB);
// The file ID is expected in the format of "X:\################"
if(szFileId[1] != _T(':') || szFileId[2] != _T('\\'))
return ERROR_BAD_FORMAT;
// The file ID must not contain special characters
if(_tcschr(szPlainName, _T('\\')) || _tcschr(szPlainName, _T('/')) || _tcschr(szPlainName, _T(':')))
return ERROR_BAD_FORMAT;
// Copy the directory name
pData->szDirName[0] = szFileId[0];
pData->szDirName[1] = szFileId[1];
pData->szDirName[2] = szFileId[2];
pData->szDirName[3] = 0;
pData->UseRelativeFile = FALSE;
// Copy the file id
StringCchCopy(pData->szFileName1, MAX_NT_PATH, szPlainName);
// Set the appropriate flags
pData->OpenFile.dwCreateOptions = FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_BY_FILE_ID;
// Switch to the "NtCreateFile" tab
TabCtrl_SelectPageByID(hTabCtrl, MAKEINTRESOURCE(IDD_PAGE02_NTCREATE));
return ERROR_SUCCESS;
}
void DisableCloseDialog(HWND hDlg, BOOL bDisable)
{
HWND hExitButton = GetDlgItem(GetParent(hDlg), IDC_EXIT);
// Initialize the buttons
// Hide "OK" button and change "Cancel" to "Exit"
if(hExitButton != NULL)
EnableWindow(hExitButton, !bDisable);
bDisableCloseDialog = bDisable;
}
INT_PTR FileTestDialog(HWND hParent, TFileTestData * pData)
{
HACCEL hAccelTable = LoadAccelerators(g_hInst, MAKEINTRESOURCE(IDR_ACCELERATORS));
HWND hTabCtrl;
HWND hDlg;
MSG msg;
// Create the property sheet
pData->bEnableResizing = TRUE;
hDlg = CreateDialogParam(g_hInst,
MAKEINTRESOURCE(IDD_FILE_TEST),
hParent,
DialogProc,
(LPARAM)pData);
// Perform the modal loop
if(hDlg != NULL)
{
// Show the dialog
hTabCtrl = GetDlgItem(hDlg, IDC_TAB);
ShowWindow(hDlg, SW_SHOW);
// Get the message. Stop processing if WM_QUIT has arrived
while(IsWindow(hDlg) && GetMessage(&msg, NULL, 0, 0))
{
// We need an alertable sleep to make APCs to work.
// Uncomment this if you want to use the asynchronous "ApcRoutine"
// parameter(s) in some native API
// MsgWaitForMultipleObjectsEx(0,
// NULL,
// INFINITE,
// QS_ALLEVENTS | QS_ALLINPUT | QS_ALLPOSTMESSAGE,
// MWMO_WAITALL | MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
if(!IsMyDialogMessage(hDlg, hTabCtrl, &msg))
{
// Process the accelerator table
if(!TranslateAccelerator(hDlg, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
if(hAccelTable != NULL)
DestroyAcceleratorTable(hAccelTable);
return IDOK;
}