Reputation: 59
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 ?
Upvotes: 1
Views: 808
Reputation: 59
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
Upvotes: 1