Optimized Table redraw and fixed redraw-on-resize.

This commit is contained in:
Pietro Gagliardi 2014-10-19 21:49:27 -04:00
parent 3c4021d7d6
commit ec22c573ba
1 changed files with 17 additions and 3 deletions

View File

@ -31,24 +31,36 @@ struct table {
intptr_t count; intptr_t count;
}; };
static void drawItems(struct table *t, HDC dc) static void drawItems(struct table *t, HDC dc, RECT cliprect)
{ {
HFONT thisfont, prevfont; HFONT thisfont, prevfont;
TEXTMETRICW tm; TEXTMETRICW tm;
LONG y; LONG y;
intptr_t i; intptr_t i;
RECT r; RECT r;
intptr_t first, last;
// TODO eliminate the need (only use cliprect)
if (GetClientRect(t->hwnd, &r) == 0) if (GetClientRect(t->hwnd, &r) == 0)
abort(); abort();
thisfont = t->font; // in case WM_SETFONT happens before we return thisfont = t->font; // in case WM_SETFONT happens before we return
prevfont = (HFONT) SelectObject(dc, thisfont); prevfont = (HFONT) SelectObject(dc, thisfont);
if (prevfont == NULL) if (prevfont == NULL)
abort(); abort();
if (GetTextMetricsW(dc, &tm) == 0) if (GetTextMetricsW(dc, &tm) == 0)
abort(); abort();
// see http://blogs.msdn.com/b/oldnewthing/archive/2003/07/29/54591.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/07/30/54600.aspx
first = cliprect.top / tm.tmHeight;
if (first < 0)
first = 0;
last = (cliprect.bottom + tm.tmHeight - 1) / tm.tmHeight;
if (last >= t->count)
last = t->count;
y = 0; y = 0;
for (i = 0; i < t->count; i++) { for (i = first; i < last; i++) {
RECT rsel; RECT rsel;
HBRUSH background; HBRUSH background;
@ -69,6 +81,7 @@ static void drawItems(struct table *t, HDC dc)
TextOutW(dc, r.left, y, L"Item", 4); TextOutW(dc, r.left, y, L"Item", 4);
y += tm.tmHeight; y += tm.tmHeight;
} }
if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont)) if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont))
abort(); abort();
} }
@ -99,7 +112,7 @@ t->selected = 5;t->count=100;//TODO
dc = BeginPaint(hwnd, &ps); dc = BeginPaint(hwnd, &ps);
if (dc == NULL) if (dc == NULL)
abort(); abort();
drawItems(t, dc); drawItems(t, dc, ps.rcPaint);
EndPaint(hwnd, &ps); EndPaint(hwnd, &ps);
return 0; return 0;
case WM_SETFONT: case WM_SETFONT:
@ -128,6 +141,7 @@ void makeTableWindowClass(void)
wc.hCursor = LoadCursorW(NULL, IDC_ARROW); wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.hIcon = LoadIconW(NULL, IDI_APPLICATION); wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // TODO correct? wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // TODO correct?
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = GetModuleHandle(NULL); wc.hInstance = GetModuleHandle(NULL);
if (RegisterClassW(&wc) == 0) if (RegisterClassW(&wc) == 0)
abort(); abort();