简体   繁体   English

将过程句柄窗口位置设置为屏幕中心时,它可以工作,但在所有情况下都不是为什么?

[英]When set a process handle window position to screen center it's working but not in all cases why?

In form1 if the process is not yet running then run it bring it to front and center it to the screen center. 在form1中,如果该进程尚未运行,则将其运行在前面并将其居中放在屏幕中心。 If the process already running only bring it to front and center it: 如果该进程已经在运行,则只将其放在前面并居中:

if (AutoIt.AutoItX.WinExists(existingProcessName, "") == 0) // Window not found
            {
                int processId = AutoIt.AutoItX.Run(processFileName, "", AutoIt.AutoItX.SW_SHOW);
                SetProcessWindow.BringToFront(processId);
                SetProcessWindow.CenterProcessWindow(processId);
                Thread.Sleep(10000);
                AutoIt.AutoItX.MouseClick("LEFT", 358, 913, 1, -1);

            }
            else
            {
                Process[] processes = Process.GetProcessesByName(processName);
                SetProcessWindow. BringToFront(processes[0].Id);
                SetProcessWindow.CenterProcessWindow(processes[0].Id);
                Thread.Sleep(10000);
                AutoIt.AutoItX.MouseClick("LEFT", 358, 913, 1, -1);
            }

And the class SetProcessWindow: 和类SetProcessWindow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace UsingAutoIt
{
    class SetProcessWindow
    {
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        private const int SWP_NOSIZE = 0x0001;
        private const int SWP_NOZORDER = 0x0004;
        private const int SWP_SHOWWINDOW = 0x0040;

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

        public static void BringToFront(int processId)
        {
            Process process = Process.GetProcessById(processId);
            IntPtr handle = process.MainWindowHandle;

            if (handle == IntPtr.Zero)
                return;

            SetForegroundWindow(handle);
        }

        public static void CenterProcessWindow(int processId)
        {
            Process process = Process.GetProcessById(processId);
            IntPtr handle = process.MainWindowHandle;

            if (handle != IntPtr.Zero)
            {
                RECT rct;
                GetWindowRect(handle, out rct);
                Rectangle screen = Screen.FromHandle(handle).Bounds;
                Point pt = new Point(screen.Left + screen.Width / 2 - (rct.Right - rct.Left) / 2, screen.Top + screen.Height / 2 - (rct.Bottom - rct.Top) / 2);
                SetWindowPos(handle, IntPtr.Zero, pt.X, pt.Y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
            }


        }
    }
}

The problem is if the process already running and i drag the process window to the one of the screen corners click on empty are in the screen so now the process is in the background and then running my program it will bring the process handle window to the front and center it. 问题是,如果进程已经在运行,并且我将进程窗口拖到屏幕角落之一,则在屏幕上单击空白,因此现在进程在后台运行,然后运行我的程序,它将把进程句柄窗口移至前置并居中。

But if i drag the process window to somewhere in the screen not the center and quit the process then running my program and it's running the process it will bring it to front but will not center it. 但是,如果我将进程窗口拖动到屏幕上某个位置而不是中心位置,然后退出该进程,然后运行我的程序,并且它正在运行该进程,它将把它放在最前面,但不会居中。

Why when the process is not running and my program running the process it dosent center it ? 为什么当进程未运行并且我的程序正在运行该进程时它居中?

Your process window is shown immediately, but it's not fully set up so you won't probably even get results debugging because if you go through debugger enough time will pass and handle will be created so everything will work fine for you. 您的过程窗口将立即显示,但尚未完全设置好,因此您可能甚至无法调试结果,因为如果通过调试器,将经过足够的时间并创建句柄,因此一切将对您正常。 What you need to do is wait for your MainWindowHandle to be set. 您需要做的就是等待MainWindowHandle被设置。 You can modify your CenterProcessWindow method like this: 您可以像这样修改CenterProcessWindow方法:

public static void CenterProcessWindow(int processId)
{
    Process process = Process.GetProcessById(processId);

    while (process.MainWindowHandle == IntPtr.Zero)
        process.Refresh();

    IntPtr handle = process.MainWindowHandle;

    RECT rct;
    GetWindowRect(handle, out rct);
    Rectangle screen = Screen.FromHandle(handle).Bounds;
    Point pt = new Point(screen.Left + screen.Width / 2 - (rct.Right - rct.Left) / 2, screen.Top + screen.Height / 2 - (rct.Bottom - rct.Top) / 2);
    SetWindowPos(handle, IntPtr.Zero, pt.X, pt.Y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM