--- mstools/samples/cursor/cursor.c 2018/08/09 18:20:01 1.1 +++ mstools/samples/cursor/cursor.c 2018/08/09 18:21:41 1.1.1.3 @@ -11,7 +11,7 @@ InitInstance() - saves instance handle and creates main window MainWndProc() - processes messages About() - processes messages for "About" dialog box - sieve() - time consuming function, generates primes + sieve() - time consuming function, generates primes ****************************************************************************/ @@ -50,7 +50,7 @@ int APIENTRY WinMain( int nCmdShow ) { - MSG msg; /* message */ + MSG msg; /* message */ UNREFERENCED_PARAMETER( lpCmdLine ); @@ -59,15 +59,15 @@ int APIENTRY WinMain( return (FALSE); if (!InitInstance(hInstance, nCmdShow)) - return (FALSE); + return (FALSE); MessageBox ( - GetFocus () - , "Use the mouse button in this program for an example of graphics " - "selection, or the key for an example of " - "using a special cursor to reflect a program state." - , "Cursor Sample Application" - , MB_ICONASTERISK | MB_OK + GetFocus () + , "Use the mouse button in this program for an example of graphics " + "selection, or the key for an example of " + "using a special cursor to reflect a program state." + , "Cursor Sample Application" + , MB_ICONASTERISK | MB_OK ); while (GetMessage(&msg, NULL, NULL, NULL)) { @@ -126,21 +126,21 @@ BOOL InitInstance( hHourGlass = LoadCursor(NULL, IDC_WAIT); hWnd = CreateWindow( - "CursorWClass", - "Cursor Sample Application", - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - NULL, - NULL, - hInstance, - NULL + "CursorWClass", + "Cursor Sample Application", + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + NULL, + NULL, + hInstance, + NULL ); if (!hWnd) - return (FALSE); + return (FALSE); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); @@ -157,29 +157,29 @@ BOOL InitInstance( MESSAGES: WM_COMMAND - application menu (About dialog box) - WM_CHAR - ASCII key value received - WM_LBUTTONDOWN - left mouse button - WM_MOUSEMOVE - mouse movement - WM_LBUTTONUP - left button released - WM_KEYDOWN - key pressed - WM_KEYUPS - key released - WM_PAINT - update window + WM_CHAR - ASCII key value received + WM_LBUTTONDOWN - left mouse button + WM_MOUSEMOVE - mouse movement + WM_LBUTTONUP - left button released + WM_KEYDOWN - key pressed + WM_KEYUPS - key released + WM_PAINT - update window WM_DESTROY - destroy window COMMENTS: - When the left mouse button is pressed, btrack is set to TRUE so that - the code for WM_MOUSEMOVE will keep track of the mouse and update - the box accordingly. Once the button is released, btrack is set to - FALSE, and the current position is saved. Holding the SHIFT key - while pressing the left button will extend the current box rather - then erasing it and starting a new one. - - When an arrow key is pressed, the cursor is repositioned in the - direction of the arrow key. A repeat count is kept so that the - longer the user holds down the arrow key, the faster it will move. - As soon as the key is released, the repeat count is set to 1 for - normal cursor movement. + When the left mouse button is pressed, btrack is set to TRUE so that + the code for WM_MOUSEMOVE will keep track of the mouse and update + the box accordingly. Once the button is released, btrack is set to + FALSE, and the current position is saved. Holding the SHIFT key + while pressing the left button will extend the current box rather + then erasing it and starting a new one. + + When an arrow key is pressed, the cursor is repositioned in the + direction of the arrow key. A repeat count is kept so that the + longer the user holds down the arrow key, the faster it will move. + As soon as the key is released, the repeat count is set to 1 for + normal cursor movement. ****************************************************************************/ @@ -198,7 +198,7 @@ LONG APIENTRY MainWndProc(HWND hWnd, UIN DialogBox(hInst, "AboutBox", hWnd, - (WNDPROC)lpProcAbout); + lpProcAbout); FreeProcInstance(lpProcAbout); break; @@ -206,196 +206,196 @@ LONG APIENTRY MainWndProc(HWND hWnd, UIN else return (DefWindowProc(hWnd, message, wParam, lParam)); - case WM_CHAR: - if (wParam == '\r') { - SetCapture(hWnd); - - /* Set the cursor to an hourglass */ - - hSaveCursor = SetCursor(hHourGlass); - - strcpy (str, "Calculating prime numbers..."); - InvalidateRect (hWnd, NULL, TRUE); - UpdateWindow (hWnd); - wsprintf(str, "Calculated %d primes. ", sieve()); - InvalidateRect (hWnd, NULL, TRUE); - UpdateWindow (hWnd); - - SetCursor(hSaveCursor); /* Restores previous cursor */ - ReleaseCapture(); - } - break; - - case WM_LBUTTONDOWN: - bTrack = TRUE; - strcpy (str, ""); - PrevX = LOWORD(lParam); - PrevY = HIWORD(lParam); - if (!(wParam & MK_SHIFT)) { /* If shift key is not pressed */ - OrgX = LOWORD(lParam); - OrgY = HIWORD(lParam); - } - InvalidateRect (hWnd, NULL, TRUE); - UpdateWindow (hWnd); - - /* Capture all input even if the mouse goes outside of window */ - - SetCapture(hWnd); - break; - - case WM_MOUSEMOVE: - { - RECT rectClient; - INT NextX; - INT NextY; - - if (bTrack) { - NextX = LOWORD(lParam); - NextY = HIWORD(lParam); - - /* Do not draw outside the window's client area */ - - GetClientRect (hWnd, &rectClient); - if (NextX < rectClient.left) { - NextX = rectClient.left; - } else if (NextX >= rectClient.right) { - NextX = rectClient.right - 1; - } - if (NextY < rectClient.top) { - NextY = rectClient.top; - } else if (NextY >= rectClient.bottom) { - NextY = rectClient.bottom - 1; - } - - /* If the mouse position has changed, then clear the */ - /* previous rectangle and draw the new one. */ - - if ((NextX != PrevX) || (NextY != PrevY)) { - hDC = GetDC(hWnd); - SetROP2(hDC, R2_NOT); /* Erases the previous box */ + case WM_CHAR: + if (wParam == '\r') { + SetCapture(hWnd); + + /* Set the cursor to an hourglass */ + + hSaveCursor = SetCursor(hHourGlass); + + strcpy (str, "Calculating prime numbers..."); + InvalidateRect (hWnd, NULL, TRUE); + UpdateWindow (hWnd); + wsprintf(str, "Calculated %d primes. ", sieve()); + InvalidateRect (hWnd, NULL, TRUE); + UpdateWindow (hWnd); + + SetCursor(hSaveCursor); /* Restores previous cursor */ + ReleaseCapture(); + } + break; + + case WM_LBUTTONDOWN: + bTrack = TRUE; + strcpy (str, ""); + PrevX = LOWORD(lParam); + PrevY = HIWORD(lParam); + if (!(wParam & MK_SHIFT)) { /* If shift key is not pressed */ + OrgX = LOWORD(lParam); + OrgY = HIWORD(lParam); + } + InvalidateRect (hWnd, NULL, TRUE); + UpdateWindow (hWnd); + + /* Capture all input even if the mouse goes outside of window */ + + SetCapture(hWnd); + break; + + case WM_MOUSEMOVE: + { + RECT rectClient; + INT NextX; + INT NextY; + + if (bTrack) { + NextX = LOWORD(lParam); + NextY = HIWORD(lParam); + + /* Do not draw outside the window's client area */ + + GetClientRect (hWnd, &rectClient); + if (NextX < rectClient.left) { + NextX = rectClient.left; + } else if (NextX >= rectClient.right) { + NextX = rectClient.right - 1; + } + if (NextY < rectClient.top) { + NextY = rectClient.top; + } else if (NextY >= rectClient.bottom) { + NextY = rectClient.bottom - 1; + } + + /* If the mouse position has changed, then clear the */ + /* previous rectangle and draw the new one. */ + + if ((NextX != PrevX) || (NextY != PrevY)) { + hDC = GetDC(hWnd); + SetROP2(hDC, R2_NOT); /* Erases the previous box */ MoveToEx(hDC, OrgX, OrgY, NULL); - LineTo(hDC, OrgX, PrevY); - LineTo(hDC, PrevX, PrevY); - LineTo(hDC, PrevX, OrgY); - LineTo(hDC, OrgX, OrgY); + LineTo(hDC, OrgX, PrevY); + LineTo(hDC, PrevX, PrevY); + LineTo(hDC, PrevX, OrgY); + LineTo(hDC, OrgX, OrgY); - /* Get the current mouse position */ + /* Get the current mouse position */ - PrevX = NextX; - PrevY = NextY; + PrevX = NextX; + PrevY = NextY; MoveToEx(hDC, OrgX, OrgY, NULL); /* Draws the new box */ - LineTo(hDC, OrgX, PrevY); - LineTo(hDC, PrevX, PrevY); - LineTo(hDC, PrevX, OrgY); - LineTo(hDC, OrgX, OrgY); - ReleaseDC(hWnd, hDC); - } - } - } - break; + LineTo(hDC, OrgX, PrevY); + LineTo(hDC, PrevX, PrevY); + LineTo(hDC, PrevX, OrgY); + LineTo(hDC, OrgX, OrgY); + ReleaseDC(hWnd, hDC); + } + } + } + break; - case WM_LBUTTONUP: - bTrack = FALSE; /* No longer creating a selection */ - ReleaseCapture(); /* Releases hold on mouse input */ + case WM_LBUTTONUP: + bTrack = FALSE; /* No longer creating a selection */ + ReleaseCapture(); /* Releases hold on mouse input */ - X = LOWORD(lParam); /* Saves the current value */ - Y = HIWORD(lParam); - break; + X = LOWORD(lParam); /* Saves the current value */ + Y = HIWORD(lParam); + break; - case WM_KEYDOWN: - if (wParam != VK_LEFT && wParam != VK_RIGHT - && wParam != VK_UP && wParam != VK_DOWN) - break; + case WM_KEYDOWN: + if (wParam != VK_LEFT && wParam != VK_RIGHT + && wParam != VK_UP && wParam != VK_DOWN) + break; - MPOINT2POINT(ptCursor, pt); - GetCursorPos(&pt); + MPOINT2POINT(ptCursor, pt); + GetCursorPos(&pt); - /* Convert screen coordinates to client coordinates */ + /* Convert screen coordinates to client coordinates */ - ScreenToClient(hWnd, &pt); - POINT2MPOINT(pt, ptCursor); - repeat++; /* Increases the repeat rate */ + ScreenToClient(hWnd, &pt); + POINT2MPOINT(pt, ptCursor); + repeat++; /* Increases the repeat rate */ - switch (wParam) { + switch (wParam) { - /* Adjust cursor position according to which key was pressed. */ + /* Adjust cursor position according to which key was pressed. */ - case VK_LEFT: - ptCursor.x -= repeat; - break; + case VK_LEFT: + ptCursor.x -= repeat; + break; - case VK_RIGHT: - ptCursor.x += repeat; - break; + case VK_RIGHT: + ptCursor.x += repeat; + break; - case VK_UP: - ptCursor.y -= repeat; - break; + case VK_UP: + ptCursor.y -= repeat; + break; - case VK_DOWN: - ptCursor.y += repeat; - break; + case VK_DOWN: + ptCursor.y += repeat; + break; - } + } - /* Get the client boundaries */ + /* Get the client boundaries */ - GetClientRect(hWnd, &Rect); + GetClientRect(hWnd, &Rect); - /* Do not draw outside the window's client area */ + /* Do not draw outside the window's client area */ - MPOINT2POINT(ptCursor, pt); - if (pt.x >= Rect.right) - pt.x = Rect.right - 1; - else if (pt.x < Rect.left) - pt.x = Rect.left; - if (pt.y >= Rect.bottom) - pt.y = Rect.bottom - 1; - else if (pt.y < Rect.top) - pt.y = Rect.top; + MPOINT2POINT(ptCursor, pt); + if (pt.x >= Rect.right) + pt.x = Rect.right - 1; + else if (pt.x < Rect.left) + pt.x = Rect.left; + if (pt.y >= Rect.bottom) + pt.y = Rect.bottom - 1; + else if (pt.y < Rect.top) + pt.y = Rect.top; - /* Convert the coordinates to screen coordinates */ + /* Convert the coordinates to screen coordinates */ - ClientToScreen(hWnd, &pt); - SetCursorPos(pt.x, pt.y); - break; + ClientToScreen(hWnd, &pt); + SetCursorPos(pt.x, pt.y); + break; - case WM_KEYUP: - repeat = 1; /* Clears the repeat count. */ - break; + case WM_KEYUP: + repeat = 1; /* Clears the repeat count. */ + break; - case WM_ACTIVATE: + case WM_ACTIVATE: if (!GetSystemMetrics(SM_MOUSEPRESENT)) { if (!HIWORD(wParam)) { // LOWORD added for portability if (LOWORD(wParam)) { - SetCursor(LoadCursor(hInst, "bullseye")); - pt.x = X; - pt.y = Y; - ClientToScreen(hWnd, &pt); - SetCursorPos(pt.x, pt.y); - } - ShowCursor(wParam); - } - } - break; - - case WM_PAINT: - { - PAINTSTRUCT ps; + SetCursor(LoadCursor(hInst, "bullseye")); + pt.x = X; + pt.y = Y; + ClientToScreen(hWnd, &pt); + SetCursorPos(pt.x, pt.y); + } + ShowCursor(wParam); + } + } + break; + + case WM_PAINT: + { + PAINTSTRUCT ps; - hDC = BeginPaint (hWnd, &ps); - if (OrgX != PrevX || OrgY != PrevY) { + hDC = BeginPaint (hWnd, &ps); + if (OrgX != PrevX || OrgY != PrevY) { MoveToEx(hDC, OrgX, OrgY, NULL); - LineTo(hDC, OrgX, PrevY); - LineTo(hDC, PrevX, PrevY); - LineTo(hDC, PrevX, OrgY); - LineTo(hDC, OrgX, OrgY); - } - TextOut (hDC, 1, 1, str, strlen (str)); - EndPaint (hWnd, &ps); - } - break; + LineTo(hDC, OrgX, PrevY); + LineTo(hDC, PrevX, PrevY); + LineTo(hDC, PrevX, OrgY); + LineTo(hDC, OrgX, OrgY); + } + TextOut (hDC, 1, 1, str, strlen (str)); + EndPaint (hWnd, &ps); + } + break; case WM_DESTROY: PostQuitMessage(0); @@ -458,8 +458,8 @@ BOOL APIENTRY About(HWND hDlg, UINT mess ****************************************************************************/ -#define NITER 20 /* number of iterations */ -#define SIZE 8190 +#define NITER 20 /* number of iterations */ +#define SIZE 8190 CHAR flags[SIZE+1]={ 0}; @@ -467,15 +467,15 @@ INT sieve() { INT i,k; INT iter, count; - for (iter = 1; iter <= NITER; iter++) { /* Does sieve NITER times */ + for (iter = 1; iter <= NITER; iter++) { /* Does sieve NITER times */ count = 0; - for (i = 0; i <= SIZE; i++) /* Sets all flags TRUE */ + for (i = 0; i <= SIZE; i++) /* Sets all flags TRUE */ flags[i] = TRUE; for (i = 2; i <= SIZE; i++) { - if (flags[i] ) { /* Found a prime? */ + if (flags[i] ) { /* Found a prime? */ for (k = i + i; k <= SIZE; k += i) - flags[k] = FALSE; /* Cancelsits multiples */ + flags[k] = FALSE; /* Cancelsits multiples */ count++; } }