如何获取进程的所有句柄? [英] How to get all the handles of a process?

查看:418
本文介绍了如何获取进程的所有句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google翻译网站创建了一个新的Chrome窗口,我希望窗口显示为非最大化,以便用户输入文本并退出新窗口。另外如何单独使新窗口无法最大化。



我尝试过:



I have created a new Chrome window with Google Translate website, I want the window to appear unmaximized for the user to type the text and exit the new window. Also how to make the new window alone unmaximized.

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
    var prs = new ProcessStartInfo("chrome.exe");
    prs.Arguments = "http://translate.google.com" + " --new-window";
    Process p = Process.Start(prs);
    Program.setFocusToProcess(p);


    //setFocusToThisProcessChromeName("chrome");
}






public static void setFocusToThisProcessChromeName(string name)
{
    Process[] Processes = Process.GetProcessesByName(name);

    //if (Processes.Length == 0)
    //{
    //    MessageBox.Show("The program: '" + name + "' isn't running and can't be focused on.", "TranslateProgram");
    //    return;
    //}



    foreach (Process process in Processes)
    {
        setFocusToProcess(process);
    }
}


static public void setFocusToProcess(Process process)
{
    //if (process.MainWindowHandle == (IntPtr)0x00000000)
    setFocusToHandleAndUnmaximize(process.MainWindowHandle);
    setFocusToHandleAndUnmaximize(process.Handle);

}

private static void setFocusToHandleAndUnmaximize(IntPtr i)
{
    if (i == IntPtr.Zero)
    {
        return;
    }

    int SWP_SHOWWINDOW = 0x0040;

    const int SW_RESTORE = 9;

    int HWND_TOPMOST = -1;

    //const int SW_SHOWNORMAL = 1;


    //if (IsIconic(process.MainWindowHandle) != 0)
    //{
    //    ShowWindow(process.MainWindowHandle, SW_RESTORE);
    //}

    ShowWindow(i, SW_RESTORE);

    SetForegroundWindow(i);

    BringWindowToTop(i);

    SetFocus(new HandleRef(null, i));

    SetWindowPos(i, 0, Cursor.Position.X - 100, Cursor.Position.Y - 100, 0, 0, SWP_SHOWWINDOW | HWND_TOPMOST| SW_RESTORE);
}

[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr SetFocus(HandleRef hWnd);


[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);


[DllImport("user32.dll")]
private static extern int BringWindowToTop(IntPtr hWnd);


[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

推荐答案

请参阅此处的文章并停止艰难的工作:Google Translator [ ^ ]
See article here and stop doing things the hard way: Google Translator[^]


这篇关于如何获取进程的所有句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆