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
|
// ParseALargeFile - parses a large file and uses a progress bar to
// indicate the progress of the parsing operation.
// Returns TRUE if successful or FALSE otherwise.
// hwndParent - parent window of the progress bar
// lpszFileName - name of the file to parse
//
// Global variable
// g_hinst - instance handle
extern HINSTANCE g_hinst;
BOOL ParseALargeFile(HWND hwndParent, LPSTR lpszFileName)
{
RECT rcClient; // client area of parent window
int cyVScroll; // height of scroll bar arrow
HWND hwndPB; // handle of progress bar
HANDLE hFile; // handle of file
DWORD cb; // size of file and count of bytes read
LPCH pch; // address of data read from file
LPCH pchTmp; // temporary pointer
// Ensure that the common control DLL is loaded and create a
// progress bar along the bottom of the client area of the
// parent window. Base the height of the progress bar on
// the height of a scroll bar arrow.
InitCommonControls();
GetClientRect(hwndParent, &rcClient);
cyVScroll = GetSystemMetrics(SM_CYVSCROLL);
hwndPB = CreateWindowEx(0, PROGRESS_CLASS, (LPSTR) NULL,
WS_CHILD | WS_VISIBLE, rcClient.left,
rcClient.bottom - cyVScroll,
rcClient.right, cyVScroll,
hwndParent, (HMENU) 0, g_hinst, NULL);
// Open the file for reading, and retrieve the size of the file.
hFile = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if (hFile == (HANDLE) INVALID_HANDLE_VALUE)
return FALSE;
cb = GetFileSize(hFile, (LPDWORD) NULL);
// Set the range and increment of the progress bar.
SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, cb / 2048));
SendMessage(hwndPB, PBM_SETSTEP, (WPARAM) 1, 0);
// Parse the file.
pch = (LPCH) LocalAlloc(LPTR, sizeof(char) * 2048);
pchTmp = pch;
do {
ReadFile(hFile, pchTmp, sizeof(char) * 2048, &cb,
(LPOVERLAPPED) NULL);
.
. // Include here any code that parses the file.
.
// Advance the current position of the progress bar
// by the increment.
SendMessage(hwndPB, PBM_STEPIT, 0, 0);
} while (cb);
CloseHandle((HANDLE) hFile);
DestroyWindow(hwndPB);
return TRUE;
}
// ParseALargeFile - parses a large file and uses a progress bar to
// indicate the progress of the parsing operation.
// Returns TRUE if successful or FALSE otherwise.
// hwndParent - parent window of the progress bar
// lpszFileName - name of the file to parse
//
// Global variable
// g_hinst - instance handle
extern HINSTANCE g_hinst;
BOOL ParseALargeFile(HWND hwndParent, LPSTR lpszFileName)
{
RECT rcClient; // client area of parent window
int cyVScroll; // height of scroll bar arrow
HWND hwndPB; // handle of progress bar
HANDLE hFile; // handle of file
DWORD cb; // size of file and count of bytes read
LPCH pch; // address of data read from file
LPCH pchTmp; // temporary pointer
// Ensure that the common control DLL is loaded and create a
// progress bar along the bottom of the client area of the
// parent window. Base the height of the progress bar on
// the height of a scroll bar arrow.
InitCommonControls();
GetClientRect(hwndParent, &rcClient);
cyVScroll = GetSystemMetrics(SM_CYVSCROLL);
hwndPB = CreateWindowEx(0, PROGRESS_CLASS, (LPSTR) NULL,
WS_CHILD | WS_VISIBLE, rcClient.left,
rcClient.bottom - cyVScroll,
rcClient.right, cyVScroll,
hwndParent, (HMENU) 0, g_hinst, NULL);
// Open the file for reading, and retrieve the size of the file.
hFile = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if (hFile == (HANDLE) INVALID_HANDLE_VALUE)
return FALSE;
cb = GetFileSize(hFile, (LPDWORD) NULL);
// Set the range and increment of the progress bar.
SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, cb / 2048));
SendMessage(hwndPB, PBM_SETSTEP, (WPARAM) 1, 0);
// Parse the file.
pch = (LPCH) LocalAlloc(LPTR, sizeof(char) * 2048);
pchTmp = pch;
do {
ReadFile(hFile, pchTmp, sizeof(char) * 2048, &cb,
(LPOVERLAPPED) NULL);
.
. // Include here any code that parses the file.
.
// Advance the current position of the progress bar
// by the increment.
SendMessage(hwndPB, PBM_STEPIT, 0, 0);
} while (cb);
CloseHandle((HANDLE) hFile);
DestroyWindow(hwndPB);
return TRUE;
} |
Partager