Click here to Skip to main content
15,943,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Windows 11 Photos app has recently been updated and now opens in the middle of the screen and only remembers the position I move it to until I close it, which has been accepted as a bug and will be fixed.
So I thought I'd dust off the cobwebs and write a bit of code in Visual Studio 2022 using
This works in a Hookutil Class:-
<dllimport("user32.dll", entrypoint:="SetWindowPos" )="">
Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, x As Int32, y As Int32, cx As Int32, cy As Int32, wFlags As Int32) As IntPtr
End Function
Public Shared Sub HookWindow()
    Dim Processes As Process() = Process.GetProcessesByName("Photos")
    For Each p As Process In Processes
        Dim handle As IntPtr = p.MainWindowHandle
        SetWindowPos(handle, HWND_BOTTOM, 500, 0, 1040, 830,SWP_NOZORDER)
End Sub
BUT what I would really like to do is move the Window that was last opened to the same position, but no matter what I try I get no errors but it doesn't work, probably due to my misunderstanding of the relationship between the desktop and other/child windows for an application e.g. there is only 1 Photos process which can have multiple windows.

I created the project as a Console App and run the executable from a shortcut pinned to the taskbar; I changed it's Application Type to Windows Service to stop the console window displaying.

I have tried using GetDesktopWindow(), GetWindow(),

What I have tried:

<dllimport("user32.dll", entrypoint:="GetDesktopWindow" )="">
Public Shared Function GetDesktopWindow() As IntPtr
End Function
Updated 22-May-24 0:16am

1 solution

First off, you don't want a Service - they are designed to operate without any GUI and wouldn't normally be expected to access the desktop. Indeed, you have to specifically allow a service to access the desktop by checking the "Allow service to interact with Desktop" option, but there is no guarantee that there will be a desktop for it to interact with as services can run unattended.

A better way to get rid of the console window is to call it a winforms app, but remove the line Application.Run(new MyForm()); from the Main function (in the program.cs file).

I haven't tried this myself, but I think you would need to use Process.GetProcesses Method (System.Diagnostics) | Microsoft Learn[^] to identify the process behind the Photos app, then use that to access the main window handle - you have then send it messages to location at a specific point on screen.
Share this answer
davepreston 22-May-24 9:06am    
My question does explain that I have got it working exactly as you describe. What I would like to achieve is make it work with whatever application was last opened, so presumably on top and active e.g Explorer, Chrome etc.
Dave Kreskowiak 22-May-24 10:11am    
Windows does not maintain a list of which applications were opened in what order. There is no such thing as "last opened".

The best you're going to get is whatever the top window in the z-order is. Read: Window Features - Win32 apps | Microsoft Learn[^]. It covers the functions used to interrogate the z-order.
davepreston 22-May-24 10:42am    
Probably bad terminology on my part. It's odd that in all the searching I did I never found that reference. Scanning it, it looks as though it refers to windows within your own application, whereas what I want to do is move and resize a window already on the desktop from my command line app?
OriginalGriff 22-May-24 11:32am    
I'm not sure you can do that, but if you can it'll rely on calling the GetForegroundWindow function (winuser.h) - Win32 apps | Microsoft Learn[^]
But ... that may just return the focussed window in the current app, not the whole system (security and all that).

But as Dave says, there is no "focus order" list in Windows so if your target app is minimised or switched away from, you can't find out which app it was at all.
davepreston 22-May-24 11:42am    
I think what's happening is as soon as I run my app it is the foreground even if it has no visible window so it doesn't appear to do anything. If I run it when coding it resizes the Visual Studio window. As Dave said, there is a Z order to windows, but not a last opened but that's a red herring, it was bad wording on my part.
I run it having just opened the target app so shouldn't be a problem.
I also tried the GetDesktopWindow option but no luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900