Using Scroll Bars

Hi,

I'm trying to use a scroll bar(this is my first time) in my programme, but it does not display correctly at all, when I scroll down, and it annoys me mann!

I think it's a little silly mistake I'm making. Please help ASAP.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <windows.h>


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


int WINAPI WinMain(HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR lpCmdLine,
					int iCmdShow)
{
	WNDCLASSEX wcex;
	static TCHAR szAppName[]={TEXT("Programme-2::TextOut on Multiple Lines")};

	wcex.cbClsExtra=0;
	wcex.cbSize=sizeof(WNDCLASSEX);
	wcex.cbWndExtra=0;
	wcex.hbrBackground=static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
	wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hInstance=hInstance;
	wcex.lpfnWndProc=MainWindowProc;
	wcex.lpszClassName=szAppName;
	wcex.lpszMenuName=NULL;
	wcex.style=0;

	if(!RegisterClassEx(&wcex))
	{
		MessageBox(NULL,TEXT("Error: You need to run this Application under Windows NT!"),NULL,
			MB_ICONERROR|MB_OK);
		return 0;
	}
	
	HWND hMainWnd;
	hMainWnd=CreateWindowEx(0,
				szAppName,
				szAppName,
				WS_OVERLAPPEDWINDOW|WS_VSCROLL,
				CW_USEDEFAULT,
				CW_USEDEFAULT,
				CW_USEDEFAULT,
				CW_USEDEFAULT,
				NULL,
				NULL,
				hInstance,
				NULL);

	if(hMainWnd)
	{
		ShowWindow(hMainWnd,iCmdShow);
		UpdateWindow(hMainWnd);

		MSG msg;
		while(GetMessage(&msg,NULL,0,0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		return static_cast<int>(msg.wParam);
	}

	return 0;
}

LRESULT CALLBACK MainWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	TEXTMETRIC tm;
	static long cyChar,cxChar,iScrollThumbPos=0,cyClient;
	int i;
	static TCHAR szBuffer[20];

	switch(uMsg)
	{
	case WM_CREATE:
		hdc=GetDC(hwnd);

		GetTextMetrics(hdc,&tm);
		cyChar=tm.tmHeight+tm.tmExternalLeading;
		cxChar=tm.tmAveCharWidth;

		ReleaseDC(hwnd,hdc);

		SetScrollRange(hwnd,SB_VERT,0,1000-1,FALSE);
		SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
		break;
	case WM_SIZE:
		cyClient=HIWORD(lParam);
		break;
	case WM_VSCROLL:
		switch(LOWORD(wParam))
		{
		case SB_LINEUP:
			if(iScrollThumbPos>0)
			{
				iScrollThumbPos--;
				SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
				InvalidateRect(hwnd,NULL,TRUE);
				UpdateWindow(hwnd);
			}
			break;
		case SB_PAGEUP:
			if(iScrollThumbPos>0)
			{
				iScrollThumbPos-=cyClient/cyChar;
				SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
				InvalidateRect(hwnd,NULL,TRUE);
				UpdateWindow(hwnd);
			}
			break;
		case SB_THUMBPOSITION:
			iScrollThumbPos=HIWORD(wParam);
			SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
			InvalidateRect(hwnd,NULL,TRUE);
			UpdateWindow(hwnd);
			break;
		case SB_PAGEDOWN:
			if(iScrollThumbPos<1000-1)
			{
				iScrollThumbPos+=cyClient/cyChar;
				SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
				InvalidateRect(hwnd,NULL,TRUE);
				UpdateWindow(hwnd);
			}
			break;
		case SB_LINEDOWN:
			if(iScrollThumbPos<1000-1)
			{
				iScrollThumbPos++;
				SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
				InvalidateRect(hwnd,NULL,TRUE);
				UpdateWindow(hwnd);
			}
			break;
		default:
			break;
		}
		break;
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);

		for(i=0;i<1000;i++)
		{
			TextOut(hdc,0,(cyChar-iScrollThumbPos)*i,TEXT("Random Genuis!!!"),16);
			TextOut(hdc,700,(cyChar-iScrollThumbPos)*i,szBuffer,
				wsprintf(szBuffer,L"Line #%i",i));
		}

		EndPaint(hwnd,&ps);
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}

	return 0;
}


Waiting patiently,
Aceix.
Last edited on
Never mind...problem SOLVED!!!--Yeah!

The changed areas(if anyone cares to know):
1
2
3
4
5
6
7
8
iScrollThumbPos = max (0, min (iScrollThumbPos, 1000 - 1)) ;

		if(iScrollThumbPos>=0 && iScrollThumbPos<=1000-1 && iScrollThumbPos!=GetScrollPos(hwnd,SB_VERT))
		{
			SetScrollPos(hwnd,SB_VERT,iScrollThumbPos,TRUE);
			InvalidateRect(hwnd,NULL,TRUE);
			UpdateWindow(hwnd);
		}


1
2
3
TextOut(hdc,0,(i-iScrollThumbPos)*cyChar,TEXT("Random Genuis!!!"),16);
			TextOut(hdc,700,(i-iScrollThumbPos)*cyChar,szBuffer,
				wsprintf(szBuffer,L"Line #%i",i));


God makes a way, when there seems to be no way,
Aceix.
Topic archived. No new replies allowed.