tags:

views:

538

answers:

2

Weird issue:

  1. Open a large notepad window
  2. create a toolwindow (style WS_EX_TOOLWINDOW)
  3. create 2 more windows (normal overlapped) (WS_OVERLAPPED)
  4. close those 2 overlapped windows (child of desktop or the toolwindow)
  5. the toolwindow jumps behind the notepad window

Does anyone know why this is the case? Or what I could be doing wrong? I would say 'bug in windows', but that is rarely the case.

To answer questions:

It is not a dialog window, but a full window. If i make it have correct children (ie: not a child of desktop), the taskbar entry for the children do not appear (probably easily fixable), but either way, the bug still happens.

I have included example code that shows the issue. I am hoping I am just creating the window wrong or required to respond to a message I am not responding to.

In this example, a tool window will open (no task bar entry, which is what is wanted). Then you click on that window, a subwindow will open. You click on the subwindow, another window will open. Then close both new subwindows and the original window, instead of getting focus, jumps immediately to behind other windows (notepad, etc).

Thanks for any help!

Example code to clarify:

// WindowToback.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WindowToback.h"

// Global Variables:
HINSTANCE g_instance;
HWND g_mainWnd = NULL;

wchar_t *szWindowClass = L"WindowToBackSub";
wchar_t *szWindowClass2 = L"WindowToBackSub2";

ATOM       MyRegisterClass(HINSTANCE hInstance);
BOOL       InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    WndProc2(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MSG msg;

    MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
 return FALSE;
}

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
 TranslateMessage(&msg);
 DispatchMessage(&msg);
}

return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style   = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra  = 0;
wcex.cbWndExtra  = 0;
wcex.hInstance  = hInstance;
wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWTOBACK));
wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINDOWTOBACK);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

RegisterClassEx(&wcex);

wcex.lpfnWndProc = WndProc2;
wcex.lpszClassName = szWindowClass2;

return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   g_instance = hInstance; 
   g_mainWnd = CreateWindowEx(WS_EX_TOOLWINDOW,szWindowClass, szWindowClass,WS_OVERLAPPED,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!g_mainWnd) return FALSE;

   ShowWindow(g_mainWnd, nCmdShow);
   UpdateWindow(g_mainWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
 case WM_LBUTTONDOWN:
 {
  HWND l_hwnd = CreateWindow(szWindowClass2, szWindowClass2, WS_VISIBLE | WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, g_instance, NULL);
  ShowWindow(l_hwnd,SW_SHOW);
  break;
 }

 case WM_DESTROY:
 {
  PostQuitMessage(0);
  return 0;
 }
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

LRESULT CALLBACK WndProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
 case WM_LBUTTONDOWN:
 {
  HWND l_hwnd = CreateWindow(szWindowClass2, szWindowClass2, WS_VISIBLE | WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, g_instance, NULL);
  ShowWindow(l_hwnd,SW_SHOW);
 }
 break;
}

return DefWindowProc(hWnd, message, wParam, lParam);

}

A: 

Are the three windows dialogs to another main window or are they applications in their own right?

If they are dialog windows then I would check that their parent window is correctly assigned.

If they are application windows then I would check that they are appearing in the taskbar.

Without more information about the problem it is hard to give a more meaningful answer.

jussij
I have updated the question to include more information. Thanks for the response so far!
Bob
+1  A: 

This isn't surprising. In fact, it's exactly the behavior I'd expect.

You're tool window isn't jumping down; rather Notepad is jumping up.

You closed the window that had activation. The system is going to activate the next-highest top-level window in the z-order. Your tool window doesn't a count as a top-level window in this regard (that's part of what being a tool window means). So Notepad gets activated, and it comes to the top.

If you want your tool window to get activated instead, you probably don't really want a tool window.

Adrian McCarthy