Click to See Complete Forum and Search --> : window losing focus at startup


bgianulis
June 16th, 2004, 05:08 PM
Hi, I wasn't sure how to title this question, but I have an application that is full screen, DirectX. If I launch the app and the nquickly click on the desktop before the window is created, the app loses focus and I pretty much have to reboot to get out of it.

Does anyone know of a way, through code, to bring the focus back to my app?

I'm using CreateWindowEX() to create the window.

Thanks for any help.

-brad

Mike Harnad
June 17th, 2004, 08:51 AM
I think your problem is actually due to a lost device. It's a common problem if you don't code for it. From the DirectX help...
A Microsoft® Direct3D® device can be in either an operational state or a lost state. The operational state is the normal state of the device in which the device runs and presents all rendering as expected. The device makes a transition to the lost state when an event, such as the loss of keyboard focus in a full-screen application, causes rendering to become impossible. The lost state is characterized by the silent failure of all rendering operations, which means that the rendering methods can return success codes even though the rendering operations fail. In this situation, the error code D3DERR_DEVICELOST is returned by IDirect3DDevice9::Present. With the above in mind, your code may be making judgements based on the success of code that did not work. The help file has a discussion on how to respond to a lost device.

bgianulis
June 17th, 2004, 09:00 AM
No, I'm handling the lost device ok. In fact, this happens way before I even create the device. After the app is launched, the user clicks on the desktop, then the window is created, then the Direct3D device and textures are created. Rendering occurs and goes through it's render cycle just fine. It's that the app is no longer getting keyboard or mouse events.

Thanks, anyway, though.

bgianulis
June 25th, 2004, 02:21 PM
Ok, usually if you wait long enough, you end up solving your own problem. (Necessity is the mother of invention)

For the DirectX app, I'm creating a windiw using CreateWindow(), with:
dwStyle = WS_POPUP|WS_CLIPSIBLINGS|WS_CLIPCHILDREN;

(the clip siblings andchildren will prevent the mouse cursor from displaying when at the end of the full screen window. Assuming the app uses it's own graphic cursor and calling SetCursor(NULL))

If the user clicks on the desktop after the app is launched, but before the window is created, eventually a WM_ACTIVATEAPP message is received on the queue after the window is created.

To handle this, I call:
ShowWindow(hwnd, SW_RESTORE);

Now, through the course of the running the app, there may be other instances where the activate message occurs, like alt-tab to minimize your app to switch to the next running app, etc. To deal with, I have a startup flag set to true. Once the app is fully started, data loaded, etc., I set the flag to false. I do this when I get the WM_ACTIVATE message. So, here's psuedo code:

WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam):

switch(msg)
{
case WM_ACTIVATEAPP:
if (startingUp && (wParam == 0))
ShowWindow(hWnd, SW_RESTORE);
break;

case WM_ACTIVATE:
{
int activeFlag = LOWORD(wParam);
int minimizedFlag = HIWORD(wParam);
HWND hwndPrevious = (HWND) lParam;

if (! activeFlag)
stop any running processes, update loop, etc.
else
startingUp = false;
break;
}

Andreas Masur
June 25th, 2004, 03:08 PM
[Moved thread]