Come usare i controlli indicatore di stato
In questo argomento viene illustrato come usare un indicatore di stato per indicare lo stato di avanzamento di un'operazione di analisi di file lunga.
Informazioni importanti
Tecnologie
Prerequisiti
- C/C++
- Programmazione dell'interfaccia utente di Windows
Istruzioni
Creare un controllo indicatore di stato
Il codice di esempio seguente crea una barra di stato e la posiziona nella parte inferiore dell'area client della finestra padre. L'altezza della barra di stato si basa sull'altezza della bitmap freccia usata in una barra di scorrimento. L'intervallo si basa sulle dimensioni del file diviso per 2.048, ovvero le dimensioni di ogni "blocco" di dati letti dal file. Nell'esempio viene inoltre impostato un incremento e viene spostata la posizione corrente dell'indicatore di stato in base all'incremento dopo l'analisi di ogni blocco.
// 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;
}
Osservazioni:
È necessario assicurarsi di usare correttamente la funzione ReadFile per garantire la sicurezza dell'applicazione. Ad esempio, nel codice di esempio, è necessario verificare che ReadFile
in realtà legga tutti i dati richiesti.
Si noti anche che il quarto parametro di CreateFile(LP edizione StandardCURITY_ATTRIBUTES)NULL imposta i valori di sicurezza predefiniti. Se sono necessarie impostazioni di sicurezza specifiche, è necessario impostare i valori appropriati nei membri della struttura. Call sizeof per impostare le dimensioni corrette della struttura LP edizione StandardCURITY_ATTRIBUTES.
Per altre informazioni, vedere Considerazioni sulla sicurezza: Controlli di Microsoft Windows.
Argomenti correlati