
bat腳本中獲取控制光標(biāo)位置各位大佬好,樓主也是一位計(jì)算從業(yè)人員,寫(xiě)代碼、用腳本也好多年了。腳本也只是簡(jiǎn)單應(yīng)用,腳本問(wèn)題還是會(huì)經(jīng)常來(lái)這兒搜索學(xué)習(xí)。關(guān)于控制光標(biāo)位置的方法(tab和backspace方法)其實(shí)一早就用過(guò),但是win10上不好使,一直沒(méi)有去研究。上周工作中又用到了,想著這次一定好好研究研究,然后翻遍了互聯(lián)網(wǎng),發(fā)現(xiàn)也沒(méi)有好的方法,不過(guò)倒是學(xué)習(xí)到了很多其他技巧,然后自己應(yīng)用到光標(biāo)的獲取和控制上,效果還行。發(fā)出來(lái)分享給大家,順便問(wèn)問(wèn)還有沒(méi)有更好法。另外,這個(gè)腳本啟動(dòng)時(shí)會(huì)打印// 2>nul這一行,大家有沒(méi)有好的辦法優(yōu)化一下?
nclick="copycode($('code0'));">復(fù)制代碼
- // 2>nul & @goto :batch_start
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace SetConsoleCursorPosition
- {
- class Program
- {
- [Dllimport(kernel32.dll, SetLastError = true)]
- static extern IntPtr GetStdHandle(int nStdHandle);
- [Dllimport(kernel32.dll, SetLastError = true)]
- internal static extern bool SetConsoleCursorPosition(
- IntPtr hConsoleOutput,
- COORD dwCursorPosition);
- [Dllimport(kernel32.dll, SetLastError = true)]
- static extern bool GetConsoleScreenBufferInfo(
- IntPtr hConsoleOutput,
- ref CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo);
- [Dllimport(kernel32.dll, SetLastError = true)]
- static extern uint GetLastError();
- public struct COORD
- {
- public short X;
- public short Y;
- };
- public struct SMALL_RECT
- {
- public ushort Left;
- public ushort Top;
- public ushort Right;
- public ushort Bottom;
- };
- public struct CONSOLE_SCREEN_BUFFER_INFO
- {
- public COORD dwSize;
- public COORD dwCursorPosition;
- public ushort wAttributes;
- public SMALL_RECT srWindow;
- public COORD dwMaximumWindowSize;
- };
- const int STD_INPUT_HANDLE = -10;
- const int STD_OUTPUT_HANDLE = -11;
- const int STD_ERROR_HANDLE = -12;
- static void WriteFile(string filepath, int value)
- {
- FileStream fileStream = File.Create(filepath);
- byte[] bytes = new UTF8Encoding(true).GetBytes(Convert.ToString(value));
- fileStream.Write(bytes, 0, bytes.Length);
- fileStream.Close();
- }
- static int Main(string[] args)
- {
- try
- {
- if (0 == args.Length)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
- IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- if (args[0].ToLower() == getcursorposition)
- {
- CONSOLE_SCREEN_BUFFER_INFO oCONSOLE_SCREEN_BUFFER_INFO = new CONSOLE_SCREEN_BUFFER_INFO();
- bool bRes = GetConsoleScreenBufferInfo(iStdOut, ref oCONSOLE_SCREEN_BUFFER_INFO);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
- string strAppPath = Process.GetCurrentProcess().MainModule.FileName;
- WriteFile(strAppPath + .CurrentX, oCONSOLE_SCREEN_BUFFER_INFO.dwCursorPosition.X);
- WriteFile(strAppPath + .CurrentY, oCONSOLE_SCREEN_BUFFER_INFO.dwCursorPosition.Y);
- WriteFile(strAppPath + .MaxX, oCONSOLE_SCREEN_BUFFER_INFO.dwMaximumWindowSize.X);
- WriteFile(strAppPath + .MaxY, oCONSOLE_SCREEN_BUFFER_INFO.dwMaximumWindowSize.Y);
- return 0;
- }
- else if (args[0].ToLower() == setcursorposition)
- {
- if (args.Length < 3)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
- COORD oCursorPosition;
- oCursorPosition.X = short.Parse(args[1]);
- oCursorPosition.Y = short.Parse(args[2]);
- bool bRes = SetConsoleCursorPosition(iStdOut, oCursorPosition);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
- return 0;
- }
- return 87; // ERROR_INVALID_PARAMETER
- }
- catch (Exception ex)
- {
- Console.WriteLine(exception: + ex.ToString());
- return 87; // ERROR_INVALID_PARAMETER
- }
- }
- }
- }

