Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Last active September 5, 2015 14:07
Show Gist options
  • Save ynkdir/18b8bb9e517d94ebc747 to your computer and use it in GitHub Desktop.
Save ynkdir/18b8bb9e517d94ebc747 to your computer and use it in GitHub Desktop.
ScrollWindowEx() does not scroll out text occationally.
#include <windows.h>
#include <stdio.h>
#include <string.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
HWND hwnd;
MSG msg;
WNDCLASSEX cls;
cls.cbSize = sizeof(cls);
cls.style = CS_HREDRAW | CS_VREDRAW;
cls.lpfnWndProc = WndProc;
cls.cbClsExtra = 0;
cls.cbWndExtra = 0;
cls.hInstance = hInstance;
cls.hIcon = NULL;
cls.hCursor = NULL;
cls.hbrBackground = NULL;
cls.lpszMenuName = NULL;
cls.lpszClassName = "TestWindow";
cls.hIconSm = NULL;
RegisterClassEx(&cls);
hwnd = CreateWindow("TestWindow", "TestWindow", WS_OVERLAPPEDWINDOW, 0, 0, 300, 300, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
for (;;) {
int r = GetMessage(&msg, NULL, 0, 0);
if (r == 0 || r == -1)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static int fontheight = 0;
switch (msg) {
case WM_CREATE:
{
HDC hdc;
RECT rc;
hdc = GetDC(hwnd);
GetClientRect(hwnd, &rc);
DrawText(hdc, "0123456789", -1, &rc, DT_CALCRECT);
ReleaseDC(hwnd, hdc);
fontheight = rc.bottom;
}
return 0;
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
DrawText(hdc, "Press key or scroll wheel randomely.\nYou can see \"EUREKA\".", -1, &rc, 0);
EndPaint(hwnd, &ps);
}
return 0;
case WM_KEYDOWN:
case WM_MOUSEWHEEL:
{
HDC hdc;
/*
* This text is not erased occasionally.
*/
hdc = GetDC(hwnd);
TextOut(hdc, 0, 0, "EUREKA", 6);
ReleaseDC(hwnd, hdc);
ScrollWindowEx(hwnd, 0, -fontheight, NULL, NULL, NULL, NULL, 0);
UpdateWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment