Minimize App when all you have is AppUserModelId

ScottSharp 0 Reputation points
2024-07-05T21:35:14.5066667+00:00

I get all the apps from AppsFolder:

`ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(AppEntry.AppsKnownFolderId);`
```which I enumerate and display each app in a list (name and icon from the shell object). The user selects an app from this list.

Using the AppUserModelId for the app selected from the list above, I enumerate all the processes and call

```ruby
`GetApplicationUserModelId(process.Handle, ref length, sb);`
```If the app isn't running (list is empty), I launch it. This works.

If the app is running (list is NOT empty), I get all the windows for the processes in the list:

```sql
`var handles = EnumerateProcessWindowHandles(process.Id);`
```I add all these to a list of window handles.

If none of these windows matches the `GetForegroundWindow();` (or foreground window's process ID is NOT among the ids of processes in the list), I restore the app windows enumerated for the processes in the list if necessary and set those windows (or the owner window) to foreground. This also appears to work.

However, for some AppUserModelIds (e.g. Calculator), if this list DOES contain the process id of the foreground (there's no problem when the foreground window is in the list of window handles), I don't seem to be able to `ShowWindow(hwnd, SW_MINIMIZE);` for the windows found for the processes, whether hwnd is in the list of window hands or retrieved via `GetWindow(windowHandle, GetWindowType.GW_OWNER);` for the handles in the list of window handles. (For others, like Excel, this works, but not for what appear to be packaged apps like Calculator.)

If it matters, I'm on Win11 (I need a solution that also works on Win10), and the app is using WinUI/WinAppSDK in c#.

I've been chasing this problem for the better part of a week. Answers with "This worked for me..." are more useful than "Have you tried..." since I've already tried a dozen approaches.

Anyone know how to accomplish this?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,657 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 86,041 Reputation points
    2024-07-06T09:50:43.11+00:00

    For UWP apps, the hierarchy of windows changes when minimized (suspended)

    You can see this test I had done with Calc : https://learn.microsoft.com/en-us/answers/questions/219889/bring-uwp-app-window-to-front-and-move-it?orderBy=Helpful

    It was the reverse (similar to IPackageDebugSettings::Resume), activating calc when minimized, but you can replace the 2 last lines by ShowWindow(epret.hWnd, SW_SHOWMINIMIZED);

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.