我写了一个控制台小程序~需要获取这个控制台的坐标,怎么实现呢~?

解决方案 »

  1.   

    控制台的坐标?是说控制台上光标的位置吗?
    参考Console.CursorLeft   Console.CursorTop属性
      

  2.   

    试试这个,不太完善,每次都打出两遍坐标(如果开多个Cmd窗口则更多),不过打出来的第一个应该就是当前窗口坐标了,因为FindWindowEx是从最顶层窗口开始搜索…… C# code
    using System;
    using System.Runtime.InteropServices;namespace ConsoleApplication4
    {
        [StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }    unsafe class Program
        {
            static unsafe void Main(string[] args)
            {
                Console.Title = "MyProgram";
                string szClassName = "ConsoleWindowClass";
                IntPtr hwnd = IntPtr.Zero;
                RECT rect = new RECT();
                do
                {
                    hwnd = FindWindowEx(IntPtr.Zero, hwnd, szClassName, Console.Title);
                    if (hwnd != null)
                    {
                        GetWindowRect(hwnd, &rect);
                        Console.WriteLine(rect.left + " " + rect.top);
                    }
                }
                while (hwnd != IntPtr.Zero);
            }        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool GetWindowRect(IntPtr hWnd, RECT* lpRect);
        }
    }