ScrollBar problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The app runs,but if i try to use the scrollbar it freezes! The code is from Petzolds book,and I check it lots of time for errors,but nothing. It just freezes when I try to use the scrollbar,and thats it! no error or anything!

<div style="color:black; background-color:white
<pre>

#include <span style="color:#a31515 "stdlib.h"
#include <span style="color:#a31515 "stdafx.h"
#include <windows.h>
#include <windef.h>
#include <span style="color:#a31515 "sysmets.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

<span style="color:blue int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, <span style="color:blue int iCmdShow){
<span style="color:blue static TCHAR szAppName[] = TEXT (<span style="color:#a31515 "HellWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

<span style="color:blue if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT (<span style="color:#a31515 "Your windows version is too old! Please upgrade it!"), szAppName, MB_ICONERROR) ;
<span style="color:blue return 0;
}

hwnd = CreateWindow (szAppName, TEXT (<span style="color:#a31515 "Get system Metrics No.1"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

<span style="color:blue while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
<span style="color:blue return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lPara) {
<span style="color:blue static <span style="color:blue int cxChar, cxCaps, cyChar, iMaxWidth;
<span style="color:blue static <span style="color:blue int cxClient, cyClient;
<span style="color:blue int iVscrollPos;

HDC hdc;
<span style="color:blue int i, y, x, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;
PAINTSTRUCT ps;
SCROLLINFO si;
TCHAR szBuffer[10];
TEXTMETRIC tm;


<span style="color:blue switch (message) {

<span style="color:blue case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics (hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading ;

ReleaseDC(hwnd, hdc);

iMaxWidth = 40 * cxChar + 22 * cxCaps;


<span style="color:blue return 0;


<span style="color:blue case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

si.cbSize = <span style="color:blue sizeof (si);
si.fMask = SIF_POS;
GetScrollInfo (hwnd, SB_VERT, &si) ;
iVertPos = si.nPos;
GetScrollInfo (hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;

iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar);
iPaintEnd = min (NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar);
<span style="color:blue for (i = iPaintBeg; i <= iPaintEnd; i++){
x = cxChar * (1 - iHorzPos);
y = cyChar * (i - iVertPos);

TextOut (hdc, x, y,
sysmetrics.szLabel,
lstrlen (sysmetrics.szLabel));

TextOut (hdc, x + 22 * cxCaps, y,
sysmetrics.szDesc,
lstrlen (sysmetrics.szDesc));

SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
wsprintf (szBuffer, TEXT (<span style="color:#a31515 "%5d"),
GetSystemMetrics (sysmetrics.iIndex)));

SetTextAlign (hdc, TA_LEFT | TA_TOP);
}
EndPaint(hwnd, &ps);
<span style="color:blue return 0;

<span style="color:blue case WM_SIZE:
cxClient = LOWORD (lPara);
cyClient = HIWORD (lPara);
<span style="color:green //MessageBox(NULL, TEXT(cxClient "& " cyClient),szAppName,MB_ICONQUESTION);

si.cbSize = <span style="color:blue sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cyClient / cyChar;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE);

si.cbSize = <span style="color:blue sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = 2 + iMaxWidth / cxChar;
si.nPage = cxClient / cxChar;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE);


<span style="color:blue return 0;

<span style="color:blue case WM_VSCROLL:
si.cbSize = <span style="color:blue sizeof (si);
si.fMask = SIF_ALL;
GetScrollInfo (hwnd, SB_VERT, &si) ;
iVertPos = si.nPos;

<span style="color:blue switch (LOWORD (wParam))
{
<span style="color:blue case SB_TOP:
si.nPos = si.nMin;
<span style="color:blue break;
<span style="color:blue case SB_BOTTOM:
si.nPos = si.nMax;
<span style="color:blue break;
<span style="color:blue case SB_LINEUP:
si.nPos -= 1;
<span style="color:blue break;
<span style="color:blue case SB_LINEDOWN:
si.nPos += 1;
<span style="color:blue break;
<span style="color:blue case SB_PAGEUP:
si.nPos -= si.nPage;
<span style="color:blue break;
<span style="color:blue case SB_PAGEDOWN:
si.nPos += si.nPage;
<span style="color:blue break;
<span style="color:blue case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
<span style="color:blue break;
<span style="color:blue default:
<span style="color:blue break;
}
si.fMask = SIF_POS;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
GetScrollInfo (hwnd, SB_VERT, &si);

<span style="color:blue if (si.nPos != iVertPos){
ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), NULL, NULL);
UpdateWindow (hwnd);
}

<span style="color:blue case WM_HSCROLL:
si.cbSize = <span style="color:blue sizeof (si);
si.fMask = SIF_ALL;

GetScrollInfo (hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;
<span style="color:blue switch (LOWORD (wParam)) {
<span style="color:blue case SB_LINELEFT:
si.nPos -= 1;
<span style="color:blue break;
<span style="color:blue case SB_LINERIGHT:
si.nPos += 1;
<span style="color:blue break;
<span style="color:blue case SB_PAGELEFT:
si.nPos -= si.nPage;
<span style="color:blue break;

<span style="color:blue case SB_PAGERIGHT:
si.nPos += si.nPage;
<span style="color:blue break;
<span style="color:blue case SB_THUMBPOSITION:
si.nPos = si.nTrackPos;
<span style="color:blue break;
<span style="color:blue default:
<span style="color:blue break;
}

si.fMask = SIF_POS;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE);
GetScrollInfo (hwnd, SB_HORZ, &si);

<span style="color:blue if (si.nPos != iHorzPos) {
ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, NULL, NULL);
}


<span style="color:blue case WM_DESTROY:
PostQuitMessage (0) ;
<span style="color:blue return 0;
}
<span style="color:blue return DefWindowProc (hwnd, message, wParam, lPara);

}

[/code]

<br/>
<br/>

<br/>

View the full article
 
Back
Top