The code works fine when the target game is running on the version installed from the Windows App Store. The game is a port from an Android game and the Windows version is not receiving updates anymore so I want to run the Android version which is newer using BlueStacks on my PC.
This is the code that works with native Windows version of the game.
public GameWindow(int titleHeight)
{
GetGameWindow();
TitleHeight = titleHeight;
}
private void GetGameWindow()
{
Form1.Rect rectangle = default(Form1.Rect);
IntPtr intPtr = FindWindow("ApplicationFrameWindow", "Game Window");
SetForegroundWindow(intPtr);
GetWindowRect(intPtr, ref rectangle);
top = rectangle.Top;
left = rectangle.Left;
width = rectangle.Right - rectangle.Left;
height = rectangle.Bottom - rectangle.Top;
UIClass.ClickBar(this);
}
public void BringToFront()
{
IntPtr foregroundWindow = FindWindow("ApplicationFrameWindow", "Game Window");
SetForegroundWindow(foregroundWindow);
}
public Bitmap Capture()
{
Bitmap bitmap = new Bitmap(Width, Height);
Rectangle rectangle = new Rectangle(Left, Top, width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
}
return bitmap;
}
public void Capture(string path)
{
Bitmap bitmap = new Bitmap(Width, Height);
Rectangle rectangle = new Rectangle(Left, Top, width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
}
bitmap.Save(Path.Combine(path, "UT" + DateTime.Now.Ticks.ToString() + ".jpg"));
bitmap.Dispose();
}
}
}
This is what I tried to drill down to the child window
public GameWindow(int titleHeight)
{
GetGameWindow();
TitleHeight = titleHeight;
}
private void GetGameWindow()
{
Form1.Rect rectangle = default(Form1.Rect);
IntPtr intPtr = FindWindow("HwndWrapper[Bluestacks.exe;;310d8add-fc43-4c3e-a64b-2cd5f6d0618c]", "BlueStacks");
SecondBSWin = FindWindowEx(intPtr, IntPtr.Zero, "WindowsForms10.Window.8.app.0.34f5582_r6_ad1", "BlueStacks Android PluginAndroid");
ThirdBSWin = FindWindowEx(SecondBSWin, IntPtr.Zero, "WindowsForms10.EDIT.app.0.34f5582_r6_ad1", null);
ForthBSWin = FindWindowEx(ThirdBSWin, IntPtr.Zero, "BlueStacksApp", "_ctl.Window");
SetForegroundWindow(ForthBSWin);
GetWindowRect(ForthBSWin, ref rectangle);
top = rectangle.Top;
left = rectangle.Left;
width = rectangle.Right - rectangle.Left;
height = rectangle.Bottom - rectangle.Top;
UIClass.ClickBar(this);
}
public void BringToFront()
{
IntPtr foregroundWindow = FindWindow("HwndWrapper[Bluestacks.exe;;edc30241-e5fe-4f1b-a9ae-8fcca64ba7b9]", "BlueStacks");
SetForegroundWindow(foregroundWindow);
SecondBSWin = FindWindowEx(foregroundWindow, IntPtr.Zero, "WindowsForms10.Window.8.app.0.34f5582_r6_ad1", "BlueStacks Android PluginAndroid");
ThirdBSWin = FindWindowEx(SecondBSWin, IntPtr.Zero, "WindowsForms10.EDIT.app.0.34f5582_r6_ad1", null);
ForthBSWin = FindWindowEx(ThirdBSWin, IntPtr.Zero, "BlueStacksApp", "_ctl.Window");
}
public Bitmap Capture()
{
Bitmap bitmap = new Bitmap(Width, Height);
Rectangle rectangle = new Rectangle(Left, Top, width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
}
return bitmap;
}
public void Capture(string path)
{
Bitmap bitmap = new Bitmap(Width, Height);
Rectangle rectangle = new Rectangle(Left, Top, width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
}
bitmap.Save(Path.Combine(path, "UT" + DateTime.Now.Ticks.ToString() + ".jpg"));
bitmap.Dispose();
}
}
}
The entire project works fine on the native Windows version, all I have done so far is adjust the program to use the BlueStacks window which it does. I can set the BlueStacks emulator to use either OpenGL or DircetX as it's "Engine" it makes no difference at all. I have also tried to use the MeMu emulator but I get the same results.
When I check the pixel color at the x, y where I want to interact with the game it shows as black instead of the actual color green. I found many other questions that were getting black screens returned from similar operations, not sure if it's the same problem here.
I would appreciate any suggestions as I have searched a great deal and not found an answer yet.
Here is the window in Spy++
SpyPlus.JPG