A lengthy operation can take many forms. This sample is a function named sieve that computes several hundred prime numbers. The operation begins when the user presses the ENTER key. Add the following statements to the window procedure:
case WM_CHAR:
if (wParam == VK_RETURN) {
SetCapture(hwnd);
/* Set the cursor to an hourglass. */
hSaveCursor = SetCursor(hHourGlass);
lstrcpy(szStr, "Calculating prime numbers...");
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
sprintf(szStr, "Calculated %d primes. ", sieve());
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
SetCursor(hSaveCursor); /* restores previous cursor */
ReleaseCapture();
}
break;
When the user presses ENTER, Windows generates a WM_CHAR message whose wParam parameter contains a value representing a carriage return. Upon receiving the WM_CHAR message, the window procedure checks for this value and carries out the sample lengthy operation, sieve. This function, called Eratosthenes Sieve Prime-Number Program, is from Byte, January 1983. It is defined as follows:
#define NITER 20 /* number of iterations */
#define BUFF_SIZE 8190
BYTE abFlags[BUFF_SIZE + 1] = { 0 };
int PASCAL sieve(void)
{
int i, k;
int iter, count;
for (iter = 1; iter <= NITER; iter++) { /* sieve NITER times */
count = 0;
for (i = 0; i <= BUFF_SIZE; i++) /* sets all flags TRUE */
abFlags[i] = TRUE;
for (i = 2; i <= BUFF_SIZE; i++)
if (abFlags[i]) { /* found a prime? */
for (k = i + i; k <= BUFF_SIZE; k += i)
abFlags[k] = FALSE; /* cancels its multiples */
count++;
}
}
return count;
}