如何获取c#控制台应用程序的位置
我是一名编码初学者,我想创建一个应用程序,说明窗口的宽度、高度和位置。 问题是我不知道如何获得窗口的位置。 我在网上搜索,但找不到我问题的答案 以下是我的代码:如何获取c#控制台应用程序的位置,c#,console-application,C#,Console Application,我是一名编码初学者,我想创建一个应用程序,说明窗口的宽度、高度和位置。 问题是我不知道如何获得窗口的位置。 我在网上搜索,但找不到我问题的答案 以下是我的代码: using System; namespace WindowSizeChecker { class Program { const bool alwaysTrue = true; static void Main(string[] args) {
using System;
namespace WindowSizeChecker
{
class Program
{
const bool alwaysTrue = true;
static void Main(string[] args)
{
while (alwaysTrue == true)
{
Console.Write("Set your console window to your prefered size and position. Then press Enter");
Console.ReadLine();
screenSizeAndPosition();
Console.WriteLine("\n\nPress enter to repeat\n\n");
Console.ReadLine();
}
}
public static void screenSizeAndPosition()
{
int consoleWidth = Console.WindowWidth;
int consoleHeight = Console.WindowHeight;
string consoleWidthString = consoleWidth.ToString();
string consoleHeightString = consoleHeight.ToString();
Console.WriteLine("\nThe width of the window is: {0}\nAnd the height of the window is: {1}", consoleWidthString, consoleHeightString);
int largestWindowWidth = Console.LargestWindowWidth;
int largestWindowHeight = Console.LargestWindowHeight;
string largestWindowWidthString = largestWindowWidth.ToString();
string largestWindowHeightString = largestWindowHeight.ToString();
Console.WriteLine("\nThe largest width of the window is: {0}\nAnd the largest height of the window is: {1}", largestWindowWidthString, largestWindowHeightString);
}
}
}
下面是正在运行的程序:
您应该通过PInovke调用Win32 API
DwmGetWindowAttribute
到当前窗口位置。请参阅,并了解如何
我是一名编码初学者,我想创建一个应用程序,说明窗口的宽度、高度和位置。问题是我不知道如何确定窗户的位置。我在网上搜索,但找不到我问题的答案
信息就在那里,但我承认,它不一定以最容易理解的方式呈现,特别是对于初学者
IMHO,您可能应该阅读两个最相关的堆栈溢出问题:它们并不是你问题的复制品,对于初学者来说,很难看出它们是如何回答的。但事实上,它们包含了你需要的几乎所有信息 除了“如何”之外,还有几件事你需要了解:
控制台
属性不是像素维度,而是字符列和行。也就是说,在一行中可以跨窗口容纳多少个字符,以及这些字符中有多少行可以垂直容纳控制台
类获得面向字符的维度。但要获得像素信息,需要使用.NET的本机互操作支持直接调用Windows API。以下是我根据可用文档和堆栈溢出帖子整理的一些帮助器类,用于实现您的场景:
[StructLayout(LayoutKind.Sequential)]
结构矩形
{
公共int左;
公共int Top;
公共权利;
公共int底部;
公共整数宽度=>右-左;
公共内部高度=>底部-顶部;
}
类NativeConsole
{
[DllImport(“内核32”)]
公共静态外部IntPtr GetConsoleWindow();
}
类Winuser
{
[DllImport(@“user32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
私有静态外部bool GetWindowRect(IntPtr hWnd,out Rect lpRect);
公共静态Rect GetWindowRect(IntPtr句柄)
{
如果(!GetWindowRect(句柄,out Rect))
{
抛出Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
}
返回矩形;
}
}
类DwmApi
{
私有常量int DWMWA_扩展_帧_界限=9;
[DllImport(@“dwmapi.dll”)]
私有静态外部intdwmgetwindowattribute(IntPtr hwnd、intdwattribute、out Rect pvAttribute、intcbattribute);
公共静态Rect GetExtendedFrameBounds(IntPtr hwnd)
{
int hresult=DwmGetWindowAttribute(hwnd,DWMWA_EXTENDED_FRAME_BOUNDS,out Rect Rect,Marshal.SizeOf(typeof(Rect));
如果(hresult!=0)
{
抛出Marshal.GetExceptionForHR(hresult);
}
返回矩形;
}
}
当然,你可以把上面所有的内容都集中在一节课上,但我更喜欢把事情安排得井井有条。上面将API的各个部分分组到本机Win32 API本身中使用的相同组织中
有了这些部件,现在我们可以组合一个类似于上面的程序,除了它将显示窗口位置(这是您想要的),以及宽度和高度(因为这些都是从本机API免费提供的)
看起来是这样的:
使用静态系统控制台;
班级计划
{
静态void Main(字符串[]参数)
{
清除();
string prompt=“将控制台窗口设置为首选大小和位置,按X键退出”;
while(true)
{
if(KeyAvailable&&ReadKey(intercept:true).Key==ConsoleKey.X)
{
打破
}
屏幕大小和位置(提示);
系统线程线程睡眠(时间跨度从秒(0.25));
}
}
静态无效屏幕大小和位置(字符串提示)
{
字符串格式=$“{0,{-WindowWidth}}”;
SetCursorPosition(0,0);
写入(格式、提示);
写入(格式,$“窗口为{WindowWidth}列宽,{WindowHeight}行高”);
写入(格式,$“屏幕上可容纳的最大窗口为{LargestWindowWidth}列宽和{LargestWindowHeight}行高”);
IntPtr consoleHwnd=NativeConsole.GetConsoleWindow();
Rect-winuserRect=Winuser.GetWindowRect(consoleHwnd),
dwmRect=DwmApi.GetExtendedFrameBounds(consoleHwnd);
写入(格式,$“DPI调整的屏幕值:位置为{{{winuserRect.Left},{winuserRect.Top}}},窗口为{winuserRect.Width}像素宽,{winuserRect.Height}像素高”);
写入(格式,$“桌面窗口管理器值:位置为{{{dwmRect.Left},{dwmRect.Top}}},窗口为{dwmRect.Width}像素宽,{dwmRect.Height}像素高”);
对于(int i=0;i
我杀了你