2
votes

I have created a screen saver (C#, .NET 4.0 if that matters) but I have a couple of problems (on a more cosmetic level, though). These are probably easy to solve, but I have prettu much never used the Win32 API, so I can easily miss the obvious when it comes to that.

Q1: I set up the small preview window like this:

SetParent(Handle, hwndParent);
SetWindowLong(Handle, GWL_STYLE, new IntPtr(GetWindowLong(Handle, GWL_STYLE) | WS_CHILD));
Rectangle parentRect;
GetClientRect(hwndParent, out ParentRect);
Size = parentRect.Size;  

where hwndParent is parsed from the 2nd command argument. The problem here is that the preview window steals focus from the screen saver settings tab. As a naive attempt, I called SetFocus(hwndParent) but that didn't have any effect. What's the correct way of keeping the focus where it should be

Q2: If I open the configuration dialog in the "usual way" of creating a form, i.e.

Application.Run(new ConfigurationForm());

the dialog will not be modal to the Control Panel applet. How can this be achieved? I've understood that the HWND of the intended parent is provided with the command argument as "/c:nnnnnnnn" but thats as far I've gotten. (Using SetParent(hwndParent) just behaved weirdly so it is apparently not the correct function.)

1

1 Answers

0
votes

You aren't checking the return values on your API calls... I know, I also "borrowed" that code from CodeProject, and it's broken in any Windows past XP.

Your call to SetWindowsLong is failing, because once you change the parent of the window, you don't have access to change it any more. If you checked your return values and used GetLastError, you would see that GetWindowLong and SetWindowLong are failing with error = 5 (access denied).

I fixed this by moving the call to SetParent to AFTER the call to make the Window a child window. See my code over in this question: Why won't the screen saver control panel kill my form when it dies?

You will also soon be asking the question I asked there :-)