LoadCursor not working

Hi all:

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
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	InitCommonControls();

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_CROSS);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MAINMENU);
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
/*
	if(!SetUpMDIChildWindowClass(hInstance))
		return 0;
*/
	hwnd = CreateWindowEx(
		0,//WS_EX_CLIENTEDGE,
		g_szClassName,
		"theForger's Tutorial Application",
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
		CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	g_hMainWindow = hwnd;

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		if (!TranslateMDISysAccel(g_hMDIClient, &Msg))
		{
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
	return Msg.wParam;
}



Above is the code for WinMain.

When I run the app, the cursor is arrow instead of the intended cross.
Why is that? What's wrong with my code?

Thanks.
Try putting this immediately after InitCommonControls() --

SetCursor(LoadCursor(hInstance, IDC_CROSS))
Last edited on
@Lamblion:

It doesn't work.
I'm sorry I didn't give you the big picture.
In fact, the code is the WinMain part of forgers windows api tutorial, page 63.
http://profmsaeed.org/wp-content/uploads/2011/01/forgers-win32-tutorial.pdf

Specifically, I'm trying to change the cursor shape when it passes the MDI client window.

I understand the MDI client window covers the client area of the MDI frame window, so I probably want to look at the code that is related to client window. I went to the WndProc and didn't find anything related to window registration for the client.

So I'm a bit confused. Where do we register the client window class?

Any input is appreciated.
The first argument to "LoadCursor()" is a handle to the instance of the executable that contains the cursor image, you have that set to NULL when it should be "GetCurrentProcess()". Note: Per MSDN this has been superceded by "LoadImage()" with OCR_ as the second argument: http://msdn.microsoft.com/en-us/library/ms648045(VS.85).aspx

LoadCursor(): http://msdn.microsoft.com/en-us/library/ms648391(VS.85).aspx

EDIT: Lamblion was indescript with his answer but ultimatly correct, assuming your exe was compiled with that image in it, his suggestion would work.
Last edited on
@Computergeek01:
Thanks for taking time to reply.

I think I have to set the "handle to instance" parameter to null because IDC_CROSS is a system defined cursor shape.


I'll try to play more with it, but if you have something to point out, please do so.
@All:

I think I've found the reason why I can't change the cursor shape when it passes the MDI client window.
The reason is, "mdiclient" is actually a preregistered window class defined by the system. That is the reason why we can't find window registration in the WM_CREATE case in WndProc.

When we call the function CreatWindowEx() in WinMain, the WM_CREATE message is directly delivered to the window procedure WndProc, without going into the message queue. Then in the WM_CREATE case, we use the system defined mdiclient class to create the mdi client window.

Anyway, thanks to all who took time replying.
Topic archived. No new replies allowed.