Code for a child window

Hi there.

I'm attempting to to learn WinAPI but I don't understand how to create a simple blank child window with a scroll bar.

I managed to create a window with a scroll bar, but it's something wrong with it. I've drawn some images in the main window but they are drawn multiple times in the child window aswell even though the images isn't anywhere near the child Window.

Could someone please show the code for creating a simple white child window with a scroll bar?
The code for making it scroll is not nessasary, I just want to create it.
I guess not.

What is wrong with the following code, why won't the rectangle be drawn?

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
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HINSTANCE hInstance;
	static HWND hWindow;

	switch(message)
	{
	case WM_CREATE:
		{
			hInstance = (HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE);

			hWindow = CreateWindowEx(NULL, L"Class", NULL,
						WS_CHILD | WS_VISIBLE | WS_VSCROLL,
						0, 0, 500, 80,
						hWnd, (HMENU) 1, hInstance, NULL);
		} break;

	case WM_PAINT:
		{
			HDC hdc;
			PAINTSTRUCT ps;

			hdc = BeginPaint(hWindow, &ps);
			Rectangle(hdc, 10, 10, 40, 40);	// Why won't this be drawn?
			EndPaint(hWindow, &ps);
		} break;

	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
	}
	return DefWindowProc (hWnd, message, wParam, lParam);
}
I would suggest you read basic tutorials on Windows programming, and if you're even moderately serious about it, also buy Charles Petzold's book on Programming Windows, 5th edition.

In short, you first create a basic window and then you either place the scroll bar in it via your resource file, or you create it on the fly with CreateWindowEx() using the scrollbar class.

In the above code, you don't need the first lilne after WM_CREATE, i.e., hInstance = etc., and you actual window is not typed as a scrollbar class. If you've created a parent window previous to this, it doesn't matter.

In other words, you have to learn at least the basics of window programming.

I don't know where you got your code, but you should go to this site and at least learn the basics --

http://www.winprog.org/tutorial/
1
2
3
4
hWindow = CreateWindowEx(NULL, L"Class", NULL,
						WS_CHILD | WS_VISIBLE | WS_VSCROLL,
						0, 0, 500, 80,
						hWnd, (HMENU) 1, hInstance, NULL);


Where did you get the name of a class named "Class"? As far as I know there is no such Windows control. What goes in the second parameter of the CreateWindowEx() function is a null terminated string containing the name of some class Windows recognizes, such as a standard windows control such as "edit", "button", "scrollbar", "listbox", "static", etc. Or, a common control such as the calendar or tab control. Or, even further, a registered ActiveX control or what not. Or, even further, a Window Class you yourself have registered with RegisterClassEx(). The key word in all of this is 'Registered'.

Also, you must clearly understand that there are two basic ways of having a scroll bar (either verticle or horiz or both) in your window.

First, it can be done without creating the scrollbar yourself by simply creating a top level WS_OVERLAPPEDWINDOW window and or'ing the WS_VSCROLL style in the dwStyles parameter. If you do that however, the scroll bar might not show up or only show up inactivated until you set various scroll bar metrics using perhaps SetScrollInfo().

Secondly, you can create a scroll bar control whose parent is any window you choose. I do this occasionally, but its usually easier to attach the scroll bar through styles as described above.

As Lamblion intimated, you really need about 6 months with nPetzold to grasp all this. Its somewhat involved.

As a matter of fact, I've written very detailed tutorials on all this but the code & tutorials are in various PowerBASIC related forums. I don't have any C/C++ translations around fit to post. However, the PowerBASIC translations are straight Sdk Api code. If you want the links let me know.
I hate to not answer direct questions though that do have an easy answer, so here is an example of a CreateWindow() call from one of my programs that creates a blank white window with a scrollbar. Note that there is no CreateWindow() call to create a scrollbar control; rather, the window is created with the WS_VSCROLL window style or'ed into place.

1
2
3
4
5
6
7
8
9
10
11
12
hWnd=
CreateWindow
(
 szClassName,
 TEXT("ODBCDemo"),
 WS_OVERLAPPEDWINDOW|WS_VSCROLL,
 200,100,300,228,
 HWND_DESKTOP,
 0,
 hIns,
 0
);


When such a window is created with the necessary metrics set so that the scrollbar is active and enabled, Windows will send various scroll notification messages as part of the WM_VSCROLL message when the user uses the scroll bar. What you need to do then within your Window Preocedure is handle the various SB_LINEUP, SB_LINEDOWN, SB_PAGEUP, SB_PAGEDOWN, SB_THUMBTRACK, etc., notification messages packaged along with the WM_VSCROLL. Its rather tricky.

I must emphasize though that the code above does not create a scrollbar control but rather a standard top level OVERLAPPEDWINDOW with a scroll bar.
Last edited on
Topic archived. No new replies allowed.