重新定位控制台窗口相对于屏幕
我正在研究C#控制台应用程序,并使用Console.WindowHeight增加了窗口的高度,但现在窗口的底部趋于在应用程序第一次打开时熄灭屏幕。重新定位控制台窗口相对于屏幕
在控制台应用程序中,有没有办法设置控制台窗口相对于屏幕的位置?我查看了Console.SetWindowPosition,但这只会影响控制台窗口相对于“屏幕缓冲区”的位置,这似乎并不是我所追求的。
感谢您的帮助!
这里,它使用的窗口句柄和进口SetWindowPos()
本地函数来实现你正在寻找一个解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleWindowPos
{
static class Imports
{
public static IntPtr HWND_BOTTOM = (IntPtr)1;
// public static IntPtr HWND_NOTOPMOST = (IntPtr)-2;
public static IntPtr HWND_TOP = (IntPtr)0;
// public static IntPtr HWND_TOPMOST = (IntPtr)-1;
public static uint SWP_NOSIZE = 1;
public static uint SWP_NOZORDER = 4;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint wFlags);
}
class Program
{
static void Main(string[] args)
{
var consoleWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
Imports.SetWindowPos(consoleWnd, 0, 0, 0, 0, 0, Imports.SWP_NOSIZE | Imports.SWP_NOZORDER);
System.Console.ReadLine();
}
}
}
代码控制台窗口移动到屏幕的左上角,不改变ž也不改变窗口的宽度/高度。
非常感谢您的回复!由于某种原因,这还不适合我;即窗口仍然在屏幕的中间区域开放。 – fyodorfranz
是的,但它应该“跳”到左上角。这就是它为我做的。你有没有在SetWindowPos()之前设置一个断点? – BitTickler
我这样做顺便说一句。在Windows 7 x64上使用VS2015社区版,Debug |任何CPU构建配置。 – BitTickler
在本地控制台应用程序中,序列为:''HWND consoleWnd = GetConsoleWindow(); SetWindowPos(consoleWnd,....);''现在你所要做的就是看你是否在.NET环境中找到具有相似名称的函数。最坏的情况下,你仍然可以调用本地的win32函数。 – BitTickler
http://*.com/questions/1277563/how-do-i-get-the-handle-of-a-console-applications-window给出关于“如何获得窗口句柄”的指针。 – BitTickler
在Windows 10中运行的解决方案如下:http://*.com/questions/2888824/console-setwindowposition-centered-each-and-every-time – scar80