Вопрос

I'm working on a C++ program that which launches a .NET Winforms application.

If the app is already running, I want to restore the window instead. I grab the .NET app's window handle and use SetForegroundWindow() to bring to to the front.

This works except when the application is minimized.

I've tried combinations of the following:

ShowWindow(windowHandle, SW_SHOW);
ShowWindow(windowHandle, SW_RESTORE);

and

SendMessage(windowHandle, WM_SYSCOMMAND, SC_RESTORE, 0);        

However, when this code is executed, the event becomes stuck. In the tray I see ".NET-BroadcastEventWindow.2.0.0.0.378734a.0" come up as a second window in the tray along with my .NET app, and the app is never restored properly.

This only happens when I try to send a SW/SC_RESTORE or SHOW event. SetForegroundWindow() has no issues.

Has anyone else seen this problem before?

Это было полезно?

Решение

I've had similar problems in the past that I've solved using Get/SetWindowPlacement():

// Ensure that the given window is not minimized.
// If it is minimized, restore it to its normal state.
void EnsureNotMinimized(HWND hWnd)
{
   WINDOWPLACEMENT placement;
   placement.length = sizeof(placement);

   if(!GetWindowPlacement(hWnd, &placement))
      return;

   BOOL minimized = (placement.showCmd & SW_SHOWMINIMIZED) != 0;
   if(!minimized)
      return;

   placement.showCmd = SW_SHOWNORMAL;
   SetWindowPlacement(hWnd, &placement);
}

However, I've only used this for windows that belong to my own application. I don't know if security would allow it to be used on outsiders.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top