让我喂你一次。 请吸取教训。
首先我们需要这些:
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
{
}
}
}
}