-2

I have Bluestacks opened in my taskbar, and I wanted to put it at the top of my screen, how can i do it?

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 {}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

1

Let me spoonfeed you once. Please learn from this.

First we will be needing these:

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

And of course DLLIMPORT will enter the scene:

//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);

And Lets change some lines on your actual code:

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

change it to..

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

And lets modify this:

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

Into..

    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);
    }

Here is the complete code, again guys, I put a little effort on this answer so Please learn from this.

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
            {

            }
        }
    }
}
0

After starting the proccess, I would start by getting the window handle of that process Then, I would use SetWindowPos to set it to be the topmost

Notice that this requires using PInvoke. The 3 links should point you in the right direction.

Mr.feel
  • 315
  • 1
  • 3
  • 12
  • I know that my process is running for sure, how can get the windows hadle of that process through the name of itself? – Deity Pogchamp Oct 29 '18 at 23:35
  • `[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);` I tried this but the window stay in the taskbar... However when i click manually it works, it stays topmost even if I switch program! – Deity Pogchamp Oct 30 '18 at 00:43
  • `Process.Start(new ProcessStartInfo(path));` I discover that this would make the trick. In fact it opens the program from the taskbar if it is already running – Deity Pogchamp Oct 30 '18 at 00:51