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 ?

4
  • 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#. Commented Dec 31, 2019 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. Commented Dec 31, 2019 at 20:38
  • AutomationElement.FindFirst() (or FromHandle() or FromPoint() etc.) + TransformPatternMove(), → Resize() Commented Dec 31, 2019 at 21:09
  • Or WinfowsPattern.SetWindowVisualState (minimize, maximize...), Close() etc. Commented Dec 31, 2019 at 21:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.