【问题标题】:How to open an application that is in the taskbar?如何打开任务栏中的应用程序?
【发布时间】:2018-10-29 23:15:40
【问题描述】:

我在我的任务栏中打开了 Bluestacks,我想把它放在我的屏幕顶部,我该怎么做?

InitializeComponent();
bool bluestack = false;

// Getting all the processes and then if bluestacks is up 
// switch to it else start bluestacks.

Process[] processlist = Process.GetProcesses();

foreach (Process theprocess in processlist){
    if (theprocess.ProcessName == "Bluestacks")
        bluestack = true;
}

this.Hide();
if (!bluestack) { 
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                            "C:/ProgramData/BlueStacks/Client/Bluestacks.exe");
    Process.Start(new ProcessStartInfo(path)); 
} else {}

【问题讨论】:

    标签: c#


    【解决方案1】:

    让我喂你一次。 请吸取教训

    首先我们需要这些:

    using System.Diagnostics; //PROCESS
    using System.Runtime.InteropServices; //DLL IMPORT
    using System.IO; //PATH
    

    当然 DLLIMPORT 也会出现:

    //FOR FINDING WINDOW
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
    //FOR FOCUSING WINDOW
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    
    //FOR MAXIMIZING WINDOW
    private const int SW_MAXIMIZE = 3;
    [DllImport("USER32.DLL")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    

    让我们更改实际代码中的一些行:

    Process[] processlist = Process.GetProcesses();//getting all the processes and then if bluestacks is up switch to it else start bluestacks
    

    把它改成..

    Process[] processlist = Process.GetProcessesByName("Bluestacks");//YOU SHOULD GET SPECIFIC PROC NAME TO MAKE IT EASIER AND LESSER PERMISSION ISSUE
    

    让我们修改一下:

    foreach (Process theprocess in processlist)
            {
                if (theprocess.ProcessName == "Bluestacks")
                    bluestack = true;
            }
    

    进入..

        foreach (Process theprocess in processlist)
        {
            bluestack = true; //SINCE WE SPECIFIED THE PROCESS, THIS WILL ONLY EXECUTE IF THE TARGET APP IS RUNNING
    
            theprocess.WaitForInputIdle();
            IntPtr extWindow = theprocess.MainWindowHandle;
    
            //FOCUS WINDOW
            SetForegroundWindow(extWindow);
    
            //BONUS: USE THIS TO MAXIMIZE WINDOW
            //ShowWindow(theprocess.MainWindowHandle, SW_MAXIMIZE);
        }
    

    这是完整的代码,伙计们,我在这个答案上付出了一些努力,所以请从中学习

    using System;
    using System.Windows.Forms;
    using System.Diagnostics; //PROCESS
    using System.Runtime.InteropServices; //DLL IMPORT
    using System.IO; //PATH
    
    namespace ShowApp
    {
        public partial class Main : Form
        {
            //FOR FINDING WINDOW
            [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
            public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
            //FOR FOCUSING WINDOW
            [DllImport("USER32.DLL")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    
            //FOR MAXIMIZING WINDOW
            private const int SW_MAXIMIZE = 3;
            [DllImport("USER32.DLL")]
            private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
            public Main()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bool bluestack = false;
    
                Process[] processlist = Process.GetProcessesByName("Bluestacks");//YOU SHOULD GET SPECIFIC PROC NAME TO MAKE IT EASIER AND LESSER PERMISSION ISSUE
    
                foreach (Process theprocess in processlist)
                {
                    bluestack = true; //SINCE WE SPECIFIED THE PROCESS, THIS WILL ONLY EXECUTE IF THE TARGET APP IS RUNNING
    
                    theprocess.WaitForInputIdle();
                    IntPtr extWindow = theprocess.MainWindowHandle;
    
                    //FOCUS WINDOW
                    SetForegroundWindow(extWindow);
    
                    //BONUS: USE THIS TO MAXIMIZE WINDOW
                    //ShowWindow(theprocess.MainWindowHandle, SW_MAXIMIZE);
                }
    
                this.Hide();
    
                if (!bluestack)
                {
                    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "C:/ProgramData/BlueStacks/Client/Bluestacks.exe");
                    Process.Start(new ProcessStartInfo(path));
                }
                else
                {
    
                }
            }
        }
    }
    

    【讨论】:

    • 非常感谢(不过再次拨打Process.Start(new ProcessStartInfo(path)); 更容易)
    • 更容易,但对于未来可能的任务不够灵活。这取决于你。
    【解决方案2】:

    启动进程后,我会先获取window handle of that process 然后,我会使用SetWindowPos 将其设置为topmost

    请注意,这需要使用 PInvoke。这 3 个链接应该为您指明正确的方向。

    【讨论】:

    • 我知道我的进程肯定在运行,如何通过自身名称获取该进程的windows hadle?
    • [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); const UInt32 SWP_NOSIZE = 0x0001; const UInt32 SWP_NOMOVE = 0x0002; const UInt32 SWP_SHOWWINDOW = 0x0040; // Call this way: SetWindowPos(theWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); 我试过了,但是窗口停留在任务栏中...但是当我手动单击它时它可以工作,即使我切换程序它也会保持在最上面!
    • Process.Start(new ProcessStartInfo(path)); 我发现这会成功。事实上,如果它已经在运行,它会从任务栏打开程序
    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多