I followed an example in Charles Petzold’s book “Programming Windows”. That example shows the keyboard message when keyboard event occurs. It scrolls automatically to show the display. Here is part of the code (with some small modification). The problem is that: normally, when a keyboard message is received, it should display 0 at the bottom. When another keyboard message is received, it scrolls down by a line, display 0 at the bottom, and above the bottom, it should display 1.
But what I have is that it displays always 0. Only when I resize the window, I get the correct result like: …. 4, 3, 2, 1, 0. I think the problem is that something is not repainted when it called ScrollWindow, but I don’t know what’s exactly the problem, since I followed the book.
case WM_SIZE:
if (message == WM_SIZE)
{ cxClient = LOWORD (lParam);
cyClient = HIWORD (lParam);
}
// Calculate scrolling rectangle
rectScroll.left = 0;
rectScroll.right = cxClient;
rectScroll.top = cyChar;
rectScroll.bottom = cyChar * ( cyClient / cyChar);
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
for ( i = cLinesMax - 1; i > 0 ; i-- )
{
pmsg[i] = pmsg[i - 1];
}
// Store new message
pmsg[0].hwnd = hwnd;
pmsg[0].message = message;
pmsg[0].wParam = wParam;
pmsg[0].lParam = lParam;
cLines = min(cLines + 1, cLinesMax);
// Scroll up the display
ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
break; // call DefWindowProc so System messages work
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for (i = 0; i < min(cLines, cyClient / cyChar - 1); i++)
{
TextOut(hdc, 0, (cyClient / cyChar - 1 - i) * cyChar, szBuffer,
wsprintf(szBuffer, TEXT("%5d"), i));
}
The whole point of
ScrollWindow()is to move the existing contents of a window without needing to repaint everything. The fundamental assumption is that the content that is moved does not change as a result of the scrolling action, and only the “uncovered” region needs to be painted.In your example you want to change the content (by renumbering the lines), not just move it. That means it needs to be redrawn – which the
ScrollWindow()in your key message processing doesn’t cause, but theInvalidateRect()in yourWM_SIZEhandler does.