调整控制台的大小 [英] Resize Event for Console

查看:219
本文介绍了调整控制台的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我认为窗口调整大小事件将通过winproc,我可能会错误,希望获得通知控制台调整大小事件。

So I thought the window resize event would come through winproc, I might be mistaken, looking to get notified for a Console resize event.

我想最大化控制台缓冲区调整大小,一旦它完成基本收缩回到窗口大小,从而防止由于缓冲区小于窗口的溢出错误。

I want to maximize the console buffer on resize, and once it's finished essentially shrink it back down to the window size, thus preventing an overflow error due to the buffer being smaller than the window.

推荐答案

不幸的是,答案是你不能挂接控制台的WndProc,因为它在一个单独的,控制过程。 Spy ++可以钩住其他进程的WndProcs来读取窗口消息,Spy ++甚至不能读取控制台窗口消息。即使你可以解决安全问题,C#也不能用来挂钩另一个进程。

Unfortunately, the answer is you can't hook the console's WndProc because it's in a separate, security-controlled process. Spy++ can hook other processes' WndProcs to read window messages, and Spy++ can't even read console window messages. Even if you could work around the security issue, C# cannot be used to hook another process.

我在调整大小时有完全相同的竞争条件。它在Windows 10更糟糕,因为调整窗口大小会重排文本,并更改​​缓冲区宽度以及窗口宽度。 Console.BufferWidth Console.BufferHeight 和家族是非原子的,如果使用

I'm having the exact same race condition on resize. It's worse on Windows 10 because resizing the window reflows the text and changes the buffer width as well as the window width. Console.BufferWidth, Console.BufferHeight and family are non-atomic and will throw both managed and unmanaged errors if you use them while the window is being resized.

您可以通过事后的大小。 msdn.microsoft.com/en-us/library/windows/desktop/ms685035.aspxrel =nofollow>阅读输入缓冲区事件,但这不会解决您的问题。你仍然会有并发问题。因为这不是一个钩子,你不能使调整大小等待你完成控制台类的非原子缓冲区/窗口大小操作。

You can definitely find out about resizes after the fact by Reading Input Buffer Events but that won't solve your problem. You'll still have concurrency issues. Since that's not a hook, you can't make the resize wait for you to finish the Console class's non-atomic buffer/window size operations.

我认为处理比赛条件的唯一选项是重试循环,这是我将使用的。

I think the only option to deal with the race condition is a retry loop, and that is what I will use.

while (true) try
{
    // Read and write the buffer size/location and window size/location and cursor position,
    // but be aware it will be rudely interrupted if the console is resized by the user.
}
catch (IOException) { }
catch (ArgumentOutOfRangeException) { }

这篇关于调整控制台的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆