Handling Scroll Bar Messages and Updating Window

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey,
Im writing a piece of software that outputs a graph to its own window. Its working well but as the graph is real time it quickly runs out of screen space, so Id like to add a scroll bar so that the graph can continue on indefinitely. The issue Im having
is that when I scroll left or right the window does not update correctly and paints over itself in random locations. Im new to programming graphics with win32 api, so any suggestions are greatly appreciated!!
Heres the Wndproc for the graph that Im building:

<div style="color:Black;background-color:White; <pre>
LRESULT CALLBACK WndProc2(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
<span style="color:Blue; int xold, yold;

<span style="color:Green; // Window Process for Graph Display
HPEN hLinePen;
<span style="color:Blue; switch(msg)
{
<span style="color:Blue; case WM_CREATE:
{
<span style="color:Green; // Timer Set
UINT ret;
ret = SetTimer(hwnd, 1, 1, NULL);
<span style="color:Blue; if(ret == 0)
MessageBox(hwnd, L<span style="color:#A31515; "Could not SetTimer()!", L<span style="color:#A31515; "Error", MB_OK | MB_ICONEXCLAMATION);


}
<span style="color:Blue; break;
<span style="color:Blue; case WM_HSCROLL:
{
<span style="color:Blue; int nScrollCode = (<span style="color:Blue; int)LOWORD(wParam);
<span style="color:Blue; int nPos = (<span style="color:Blue; short <span style="color:Blue; int)HIWORD(wParam);

SCROLLINFO si = {<span style="color:Blue; sizeof(SCROLLINFO),
SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS, 0, 10000, 10000, 0,
0};
GetScrollInfo (hwnd, SB_HORZ, &si);

<span style="color:Blue; int nNewPos = si.nPos;

<span style="color:Blue; switch (nScrollCode)
{
<span style="color:Green; // Include code that checks for other values of nScrollCode.
<span style="color:Green; // ...
<span style="color:Blue; case SB_THUMBPOSITION:
nNewPos = nPos + si.nMin; <span style="color:Green; // Adding si.nMin is the workaround.
<span style="color:Blue; break;
}

si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
si.nPos = nNewPos;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE);

<span style="color:Blue; if (nPosOld-nPos < 0)
ScrollWindow(hwnd, (nNewPos-nPos), 0, (<span style="color:Blue; const RECT*) NULL, (<span style="color:Blue; const RECT*) NULL);
<span style="color:Blue; else
ScrollWindow(hwnd, -(nNewPos-nPos), 0, (<span style="color:Blue; const RECT*) NULL, (<span style="color:Blue; const RECT*) NULL);


nPosOld = nPos;

UpdateWindow(hwnd);

}
<span style="color:Blue; break;
<span style="color:Blue; case WM_CLOSE:
DestroyWindow(hwnd);
<span style="color:Blue; break;
<span style="color:Blue; case WM_PAINT:
{

RECT rc;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
<span style="color:Green; // Draw Axes for Graph
HPEN hPenOld;
COLORREF qLineColor;
qLineColor = RGB(0, 0, 0);
hLinePen = CreatePen(PS_SOLID, 5, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);

MoveToEx(hdc, rc.left+40, rc.top+40, NULL);
LineTo(hdc, rc.left+40, rc.top+540);
MoveToEx(hdc, rc.left+40, rc.top+540, NULL);
LineTo(hdc, rc.left+10000, rc.top+540);

<span style="color:Green; // Gridlines?
qLineColor = RGB(0, 0, 255);
hLinePen = CreatePen(PS_SOLID, 1, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);
<span style="color:Blue; for (<span style="color:Blue; int i = 40; i < 541; i += 50){
MoveToEx(hdc, rc.left+40, rc.top+i, NULL);
LineTo(hdc, rc.left+10000, rc.top+i);
}

SelectObject(hdc, hPenOld);
DeleteObject(hLinePen);

<span style="color:Green; // Y axis labels
TextOut(hdc,rc.left,rc.top+30,L<span style="color:#A31515; "5V",2);
TextOut(hdc,rc.left,rc.top+280,L<span style="color:#A31515; "2.5V",4);
TextOut(hdc,rc.left,rc.top+540,L<span style="color:#A31515; "0V",2);
<span style="color:Green; // X axis labels
TextOut(hdc,rc.left+25,rc.top+550,L<span style="color:#A31515; "0m",2);
TextOut(hdc,rc.left+1025,rc.top+550,L<span style="color:#A31515; "1m",2);
TextOut(hdc,rc.left+2025,rc.top+550,L<span style="color:#A31515; "2m",2);
TextOut(hdc,rc.left+3025,rc.top+550,L<span style="color:#A31515; "3m",2);
TextOut(hdc,rc.left+4025,rc.top+550,L<span style="color:#A31515; "4m",2);
<span style="color:Green; // Title
TextOut(hdc, rc.left+350, rc.top+10, L<span style="color:#A31515; "Voltage v Time", 14);


EndPaint(hwnd, &ps);
}
<span style="color:Blue; break;
<span style="color:Blue; case WM_INPUT:
{
<span style="color:Blue; if (drawtrigger) {

RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);

<span style="color:Green; // Draw Axes for Graph
HPEN hPenOld;
COLORREF qLineColor;
qLineColor = RGB(0, 0, 0);
hLinePen = CreatePen(PS_SOLID, 5, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);

MoveToEx(hdc, 40, 40, NULL);
LineTo(hdc, 40, 540);
MoveToEx(hdc, 40, 540, NULL);
LineTo(hdc, 10000, 540);

<span style="color:Green; // Gridlines?
qLineColor = RGB(0, 0, 255);
hLinePen = CreatePen(PS_SOLID, 1, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);
<span style="color:Blue; for (<span style="color:Blue; int i = 40; i < 541; i += 50){
MoveToEx(hdc, 40, i, NULL);
LineTo(hdc, 10000, i);
}

SelectObject(hdc, hPenOld);
DeleteObject(hLinePen);
<span style="color:Green; // Y axis labels
TextOut(hdc,0,30,L<span style="color:#A31515; "5V",2);
TextOut(hdc,0,280,L<span style="color:#A31515; "2.5V",4);
TextOut(hdc,0,540,L<span style="color:#A31515; "0V",2);
<span style="color:Green; // X axis labels
TextOut(hdc,25,550,L<span style="color:#A31515; "0m",2);
TextOut(hdc,1025,550,L<span style="color:#A31515; "1m",2);
TextOut(hdc,2025,550,L<span style="color:#A31515; "2m",2);
TextOut(hdc,3025,550,L<span style="color:#A31515; "3m",2);
TextOut(hdc,4025,550,L<span style="color:#A31515; "4m",2);
<span style="color:Green; // Title
TextOut(hdc, 350, 10, L<span style="color:#A31515; "Voltage v Time", 14);

<span style="color:Green; // Obtain data points to draw
xold = x;
yold = y;
<span style="color:Blue; if (stoptrigger)
x += 1;
y = 540 - (<span style="color:Blue; int)(voltdata*100);
<span style="color:Green; // Draw data Points to Graph
qLineColor = RGB(255, 0, 0);
hLinePen = CreatePen(PS_SOLID, 1, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);

MoveToEx(hdc, x, y, NULL);
LineTo(hdc, xold, yold);

<span style="color:Green; // Release objects -- NO GDI LEAKS
SelectObject(hdc, hPenOld);
DeleteObject(hLinePen);
ReleaseDC(hwnd, hdc);
}


}
<span style="color:Blue; break;
<span style="color:Blue; case WM_DESTROY:
KillTimer(hwnd, 1);

DeleteObject(hLinePen);

PostQuitMessage(0);
<span style="color:Blue; break;
<span style="color:Blue; default:
<span style="color:Blue; return DefWindowProc(hwnd, msg, wParam, lParam);
}
<span style="color:Blue; return 0;
}
[/code]


Right now the only message being handled is for dragging the Thumb on the scroll bar, once this is working Ill handle the other scrollbar messages.

Also, heres my WindowClassEx declaration:


<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // GRAPH Window Settings and Registration
WndClass2.cbSize = <span style="color:Blue; sizeof(WNDCLASSEX);
WndClass2.style = NULL;
WndClass2.lpfnWndProc = WndProc2;
WndClass2.cbClsExtra = 0;
WndClass2.cbWndExtra = 0;
WndClass2.hInstance = ghInstance;
WndClass2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass2.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass2.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass2.lpszMenuName = NULL;
WndClass2.lpszClassName = (LPCWSTR)gszClassName2;
WndClass2.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

<span style="color:Blue; if(!RegisterClassEx(&WndClass2)) {
MessageBox(0, L<span style="color:#A31515; "Error Registering Window2!", L<span style="color:#A31515; "Error!", MB_ICONSTOP | MB_OK);
<span style="color:Blue; return 0;
}

hwnd2 = CreateWindowEx(
WS_EX_STATICEDGE,
WndClass2.lpszClassName,
L<span style="color:#A31515; "Graph",
WS_OVERLAPPEDWINDOW | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
1100, 700,
NULL, NULL,
ghInstance,
NULL);

<span style="color:Blue; if(hwnd2 == NULL) {
MessageBox(0, L<span style="color:#A31515; "Window Creation Failed2!", L<span style="color:#A31515; "Error!", MB_ICONSTOP | MB_OK);
<span style="color:Blue; return 0;
}


<span style="color:Green; // Display windows
ShowWindow(hwnd2, nCmdShow);
UpdateWindow(hwnd2);
[/code]
<br/>
<br/>


View the full article
 
Back
Top