Не получается получить позицию окна консоли
Мне нужно получить координаты окна консоли на экране, мне выдаёт 0 0. В конcоли правильно пишет координаты мышки, но не окна. Program:
global using static System.Console;
using ConsoleUIFramework;
using System.Diagnostics;
Wrapper.ConsoleWindow = Process.GetCurrentProcess().MainWindowHandle;
while (true)
{
Wrapper.ConsoleWindow = Process.GetCurrentProcess().MainWindowHandle;
SetCursorPosition(0, 0);
Vec2 pos = Input.GetRelativeMousePosition();
Vec2 cPos = Input.GetConsolePos();
if (Input.MouseOnConsole())
Write(pos.X + ";" + pos.Y + " " + cPos.X + ";" + cPos.Y + " ");
}
Vec2 & Rect:
public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
public struct Vec2
{
public static Vec2 operator +(Vec2 a, Vec2 b) => new Vec2(a.X + b.X, a.Y + b.Y);
public static Vec2 operator -(Vec2 a, Vec2 b) => new Vec2(a.X - b.X, a.Y - b.Y);
public Vec2(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
public bool hasNegative() => X < 0 ? true : (Y < 0 ? true : false);
public static Vec2 RectToVec2(Rect rect) => new Vec2(rect.Left, rect.Top);
}
Input:
public static class Input
{
#region DllImports
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Vec2 point);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
#endregion
public static Vec2 GetMousePosition()
{
Vec2 pos;
GetCursorPos(out pos);
return pos;
}
public static Vec2 GetRelativeMousePosition() => GetMousePosition() - GetConsolePos();
public static Vec2 GetConsolePos()
{
Rect rect = new Rect();
GetWindowRect(Wrapper.ConsoleWindow, ref rect);
return Vec2.RectToVec2(rect);
}
public static bool MouseOnConsole() => !GetRelativeMousePosition().hasNegative();
}
Wrapper:
public class Wrapper
{
public static IntPtr ConsoleWindow;
}