1

Hi I looked into this guy question and got some of my answer but I would like to instead of resizing explorer I would like to resize discord or firefox for instance

    private void windowsPos(string name, Rect pos){
        foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows()){
            if (Path.GetFileNameWithoutExtension(window.FullName).ToLowerInvariant() == name){
                window.Left = pos.Left;
                window.Top = pos.Top;
                window.Width = pos.Width;
                window.Height = pos.Height;
            }
        }
    }

But this works only for explorer I tried other exe but it works only for explorer ... can someone help me adapt this code to my requirements ?

Grez
  • 59
  • 8
  • 1
    This could be done by using some native Win32 methods. First you want to get the main window by enumerating all Windows on the computer that belong to a current processID. Use "EnumWindows" for that. Once you have the main window use "SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE)" to set its size. Testing this with C/C++ first to make sure it works is easier. Then wrap the methods you need in C#. – zezba9000 Dec 31 '19 at 20:14
  • 1
    `SHDocVw.ShellWindows()` only lists IE and Explorer windows which are all the shell controls directly (remnants of IE integration in Windows). You need something like [this answer](https://stackoverflow.com/a/10819641/2557128). – NetMage Dec 31 '19 at 20:38
  • [AutomationElement.FindFirst()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.automationelement.findfirst) (or `FromHandle()` or `FromPoint()` etc.) + [TransformPattern](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.transformpattern) → [Move()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.transformpattern.move), → [Resize()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.transformpattern.resize) – Jimi Dec 31 '19 at 21:09
  • Or [WinfowsPattern.SetWindowVisualState](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.windowpattern.setwindowvisualstate) (minimize, maximize...), [Close()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.windowpattern.close) etc. – Jimi Dec 31 '19 at 21:13

1 Answers1

1

Thank you @zezba9000 & @NetMage your answsers were the ones that worked ... I went on PInvoke - EnumWindows to get the general idea of how it works and the used Reed's answer to get to this code :

        public class SearchData{
            public string Title;
            public IntPtr hWnd;
        }

        private delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref SearchData data);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy,  int wFlags);

        private void test(){
            IntPtr hwnd = SearchForWindow("Discord");
            SetWindowPos(hwnd, 0, 50, 175, 1000, 750, 0x0040);
        }

        public IntPtr SearchForWindow(string title){
            SearchData sd = new SearchData { Title = title };
            EnumWindows(new EnumWindowsProc(EnumProc), ref sd);
            return sd.hWnd;
        }
        public bool EnumProc(IntPtr hWnd, ref SearchData data){
            StringBuilder sb = new StringBuilder(1024);
            GetWindowText(hWnd, sb, sb.Capacity);
            if (sb.ToString().Contains(data.Title)){
                data.hWnd = hWnd;
                return false;
            }
            else return true;
        }

Also @Jimi I tried your approach but I could not work with it because it could not find Automation in the System.Windows namespace

Grez
  • 59
  • 8