我在任务栏中打开了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 {}
答案 0 :(得分: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
{
}
}
}
}
答案 1 :(得分:0)
启动过程后,我将首先获得window handle of that process 然后,我将使用SetWindowPos将其设置为topmost
请注意,这需要使用PInvoke。 3个链接应为您指明正确的方向。