【问题标题】:Possible to control other Programs/Windows [duplicate]可以控制其他程序/Windows [重复]
【发布时间】:2012-12-20 15:57:29
【问题描述】:

我想知道是否可以调整实际应用程序本身以外的程序大小。 IE,我想调整并移动 Word 和我的应用程序以分别填充 70% 和 30% 的屏幕。

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

【问题讨论】:

  • 当您的应用程序执行时,Word 是否应该已经加载到内存中,或者您是否打算从您的进程中调用 word?如果您通过自动化调用单词,那么将其放置在您想要的位置应该很容易。如果它已经在运行,你仍然可以使用自动化,但你可能想看看如何获​​得 MS Word 的 HWnd。
  • 它已经在运行了。如果没有,它会启动它。
  • 这就是我想要做的。我不知道如何获得 Word 的 HWND。它必须是单词的第一个实例。
  • 很久以前我使用 dde ​​或 ole 容器做过类似的事情,我创建了一个包含单词 ole 对象的表单,并与保存和关闭等接收器事件相关联。我确信 VS2010 字模板包含类似的控制机制。我无法回答关于尺寸的问题,因为我没有使用过这些项目。
  • 这是一个适合您使用 FindWindow 和 SetWindowLong 或 SetWindowPos...forums.codeguru.com/… 。如果你得到一个句柄,你“可能”能够 setWindowPos()

标签: c# wpf vb.net aero


【解决方案1】:

确实可以,试试这个功能:

[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();
        }
    }
}

如果有问题,请通知我!

【讨论】:

  • 我不太确定我是否理解如何使用它来显示 Word 和我的应用程序...您能否使用我编辑的参数展示您将如何执行此操作?如果没有,那很好。
  • 在应用程序选项卡上打开任务管理器。您将看到所有打开的窗口的列表。在“Microsoft Word”上设置过程名称,它会找到所有的 Word 窗口。
  • 谢谢!我很快就会试一试:D
  • 我想这会给你排序,让我知道!
  • 伙伴,你是一个伟大的传奇!!!我有一个重复的问题(不是真正正确地命名)如果你在那里发帖,我会标记为答案。最后一个问题,如果 MS Word 窗口最小化到任务栏,我将如何显示它?是ShowWindow吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2012-07-16
  • 1970-01-01
相关资源
最近更新 更多