이 항목에서는 진행률 표시줄을 사용하여 긴 파일 구문 분석 작업의 진행률을 나타내는 방법을 설명합니다.
알아야 할 사항
기술
필수 구성 요소
- C/C++
- Windows 사용자 인터페이스 프로그래밍
지시
진행률 표시줄 컨트롤 만들기
다음 예제 코드는 진행률 표시줄을 만들고 부모 창의 클라이언트 영역 아래쪽을 따라 배치합니다. 진행률 표시줄의 높이는 스크롤 막대에 사용되는 화살표 비트맵의 높이를 기반으로 합니다. 범위는 파일에서 읽은 데이터의 각 "청크"의 크기인 2,048로 나눈 파일의 크기를 기반으로 합니다. 이 예제에서는 각 청크를 구문 분석한 후, 증분을 설정하고 그 증분만큼 진행률 표시줄의 현재 위치를 이동시킵니다.
// ParseALargeFile - uses a progress bar to indicate the progress of a 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, LPTSTR 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, (LPTSTR) 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);
// TODO: Write an error handler to check that all the
// requested data was read.
//
// 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;
}
발언
애플리케이션의 보안을 보장하려면 ReadFile 함수를 올바르게 사용해야 합니다. 예를 들어 예제 코드에서 ReadFile
요청된 모든 데이터를 실제로 읽는지 확인해야 합니다.
또한 CreateFile의 네 번째 매개 변수인 (LPSECURITY_ATTRIBUTES)NULL는 기본 보안 값을 설정합니다. 특정 보안 설정이 필요한 경우 구조체의 멤버에 적절한 값을 설정해야 합니다. sizeof를 호출하여 LPSECURITY_ATTRIBUTES 구조체의 올바른 크기를 설정합니다.
자세한 내용은 보안 고려 사항: Microsoft Windows 컨트롤참조하세요.
관련 항목