Possible to control other Programs/Windows [duplicate]

血红的双手。 提交于 2019-12-08 05:02:30

问题


I was wondering whether it is possible to resize programs other than the actual application itself. IE, I want to resize and move Word and my application to fill 70% and 30% of the screen respectively.

Private Sub MinimiseButton_Copy_Click(sender As Object, e As RoutedEventArgs) Handles MinimiseButton_Copy.Click
    Me.Left = SystemParameters.PrimaryScreenWidth - Me.Width + 14
    Me.Top = -14
    Me.Height = SystemParameters.PrimaryScreenHeight

    Dim parry As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("winword")
    Dim word As System.Diagnostics.Process = parry(0)
    SetWindowPos(word.Handle, 0, 0, 0, SystemParameters.PrimaryScreenWidth - Me.Width, SystemParameters.PrimaryScreenHeight - 28, &H10)
End Sub

<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, W As Integer, H As Integer, uFlags As UInteger) As Boolean
End Function

回答1:


That is indeed possible, try this function:

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int WPARAM, int LPARAM);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int, Y, int cx, int cy, uint uFlags);

public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_NEXTWINDOW = 0xF040;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public const UInt32 TOPMOST_FLAGS = 0x0002 | 0x0001;

Public void resisezeWindow(String procesname, int Width, int Height, Boolean bringtofront)
{
    foreach (Process proc in Process.GetProcesses())
    {
        IntPtr id = proc.MainWindowHandle;
        Rectangle rect = new Rectangle();
        GetWindowRect(id, ref rect);
        if (proc.MainWindowTitle.Contains(procesname))
        {
            PostMessage(proc.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
                    MoveWindow(id, 0, 0, Width, Height, true);
                    if(bringtofront) SetWindowPos(id, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
                    proc.Refresh();
        }
    }
}

If there is a problem, please notify me!



来源:https://stackoverflow.com/questions/13945923/possible-to-control-other-programs-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!