希望在 C# 的控制台应用程序中强制 Alt + Enter 全屏和强制缩放 (Ctrl + Scroll)
Looking to force Alt + Enter full screen and a forced zoom (Ctrl + Scroll) Inside of a console application in C#
基本上无法找到并回答这个问题,或者甚至可能。
我有一个正在为 class 创建的游戏,当强制全屏和将缩放设置为特定大小时,它看起来更好。我想知道我是否可以在玩家不需要自己更改它的情况下重新创建它。
ALT + ENTER 全屏
和
CTRL + 滚轮缩放
如需字面回答您关于如何执行以下操作的问题:
- 发送键进入全屏,
- 发送 Ctrl+鼠标滚轮
您需要 Win32 互操作的一些帮助,以将键盘和鼠标消息发送到您的控制台 window。
using System.Runtime.InteropServices;
public class Win32
{
public const int VK_F11 = 0x7A;
public const int SW_MAXIMIZE = 3;
public const uint WM_KEYDOWN = 0x100;
public const uint WM_MOUSEWHEEL = 0x20A;
public const uint WHEEL_DELTA = 120;
public const uint MK_CONTROL = 0x00008 << 16;
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
作为参考,幻数来自:
- Virtual Keys (
VK_*
)
- Window input messages (
WM_*
)
- Mousewheel params (
WHEEL_DELTA
& MK_*
)
- 和 ShowWindow params (
SW_*
)
然后您可以像这样简单地发送按键和鼠标滚轮:
using static Win32;
// Get window handle of the console
var hwnd = GetConsoleWindow();
// Go fullscreen by sending the F11 keydown message.
PostMessage(hwnd, WM_KEYDOWN, (IntPtr)VK_F11, IntPtr.Zero);
// Or maximize the window instead. Your users may not know how to get out of fullscreen...
/// ShowWindow(hwnd, SW_MAXIMIZE);
// Send mouse wheel message.
// MK_CONTROL: Holds the Ctrl key. WHEEL_DELTA: Positive=Up, Negative=Down.
PostMessage(hwnd, WM_MOUSEWHEEL, (IntPtr)(MK_CONTROL | WHEEL_DELTA), IntPtr.Zero);
或者,正如@JeremyLakerman 在对您的问题的评论中提到的,您可以 set the console font 增大尺寸;这比发送 Ctrl+MouseWheel 要好得多,但也有点复杂。
基本上无法找到并回答这个问题,或者甚至可能。 我有一个正在为 class 创建的游戏,当强制全屏和将缩放设置为特定大小时,它看起来更好。我想知道我是否可以在玩家不需要自己更改它的情况下重新创建它。
ALT + ENTER 全屏 和 CTRL + 滚轮缩放
如需字面回答您关于如何执行以下操作的问题:
- 发送键进入全屏,
- 发送 Ctrl+鼠标滚轮
您需要 Win32 互操作的一些帮助,以将键盘和鼠标消息发送到您的控制台 window。
using System.Runtime.InteropServices;
public class Win32
{
public const int VK_F11 = 0x7A;
public const int SW_MAXIMIZE = 3;
public const uint WM_KEYDOWN = 0x100;
public const uint WM_MOUSEWHEEL = 0x20A;
public const uint WHEEL_DELTA = 120;
public const uint MK_CONTROL = 0x00008 << 16;
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
作为参考,幻数来自:
- Virtual Keys (
VK_*
) - Window input messages (
WM_*
) - Mousewheel params (
WHEEL_DELTA
&MK_*
) - 和 ShowWindow params (
SW_*
)
然后您可以像这样简单地发送按键和鼠标滚轮:
using static Win32;
// Get window handle of the console
var hwnd = GetConsoleWindow();
// Go fullscreen by sending the F11 keydown message.
PostMessage(hwnd, WM_KEYDOWN, (IntPtr)VK_F11, IntPtr.Zero);
// Or maximize the window instead. Your users may not know how to get out of fullscreen...
/// ShowWindow(hwnd, SW_MAXIMIZE);
// Send mouse wheel message.
// MK_CONTROL: Holds the Ctrl key. WHEEL_DELTA: Positive=Up, Negative=Down.
PostMessage(hwnd, WM_MOUSEWHEEL, (IntPtr)(MK_CONTROL | WHEEL_DELTA), IntPtr.Zero);
或者,正如@JeremyLakerman 在对您的问题的评论中提到的,您可以 set the console font 增大尺寸;这比发送 Ctrl+MouseWheel 要好得多,但也有点复杂。