我已经尝试了几个找到的解决方案,比如一个 - >
但是,我观察到GetConsoleMode(IntPtr hConsoleHandle,out int mode)中的模式对于不同的控制台应用程序会有所不同。它不是恒定的。
我是否可以在控制台应用程序上禁用鼠标单击(右/左按钮)以实现相同的方案。我发现它可以用IMessageFilter完成,但仅适用于Window Form Application而不适用于控制台应用程序。
请指导。
答案 0 :(得分:30)
对于像我这样喜欢 no-brainer 代码进行复制/粘贴的人,这里的代码灵感来自接受的答案:
using System;
using System.Runtime.InteropServices;
static class DisableConsoleQuickEdit {
const uint ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
internal static bool Go() {
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
// get current console mode
uint consoleMode;
if (!GetConsoleMode(consoleHandle, out consoleMode)) {
// ERROR: Unable to get console mode.
return false;
}
// Clear the quick edit bit in the mode flags
consoleMode &= ~ENABLE_QUICK_EDIT;
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode)) {
// ERROR: Unable to set console mode
return false;
}
return true;
}
}
答案 1 :(得分:10)
如果要禁用快速编辑模式,则需要调用GetConsoleMode以获取当前模式。然后清除启用快速编辑的位,并调用SetConsoleMode。假设你有非托管函数的托管原型,你会写:
const int ENABLE_QUICK_EDIT = 0x0040;
IntPtr consoleHandle = GetConsoleWindow();
UInt32 consoleMode;
// get current console mode
if (!GetConsoleMode(consoleHandle, out consoleMode))
{
// Error: Unable to get console mode.
return;
}
// Clear the quick edit bit in the mode flags
mode &= ~ENABLE_QUICK_EDIT;
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode))
{
// ERROR: Unable to set console mode
}
如果要禁用鼠标输入,则需要清除鼠标输入位。
const int ENABLE_MOUSE_INPUT = 0x0010;
mode &= ~ENABLE_MOUSE_INPUT;
答案 2 :(得分:2)
已阅读以上答案,无法使用GetConsoleWindow()。而是必须使用GetStdHandle()。
因此,这是一个用于启用/禁用QuickEditMode的复制和粘贴类。调用 ConsoleWindow.QuickEditMode(false); 禁用控制台窗口的快速编辑模式。
using System;
using System.Runtime.InteropServices;
public static class ConsoleWindow
{
private static class NativeFunctions
{
public enum StdHandle : int
{
STD_INPUT_HANDLE = -10,
STD_OUTPUT_HANDLE = -11,
STD_ERROR_HANDLE = -12,
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle); //returns Handle
public enum ConsoleMode : uint
{
ENABLE_ECHO_INPUT = 0x0004,
ENABLE_EXTENDED_FLAGS = 0x0080,
ENABLE_INSERT_MODE = 0x0020,
ENABLE_LINE_INPUT = 0x0002,
ENABLE_MOUSE_INPUT = 0x0010,
ENABLE_PROCESSED_INPUT = 0x0001,
ENABLE_QUICK_EDIT_MODE = 0x0040,
ENABLE_WINDOW_INPUT = 0x0008,
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,
//screen buffer handle
ENABLE_PROCESSED_OUTPUT = 0x0001,
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
ENABLE_LVB_GRID_WORLDWIDE = 0x0010
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
public static void QuickEditMode(bool Enable)
{
//QuickEdit lets the user select text in the console window with the mouse, to copy to the windows clipboard.
//But selecting text stops the console process (e.g. unzipping). This may not be always wanted.
IntPtr consoleHandle = NativeFunctions.GetStdHandle((int)NativeFunctions.StdHandle.STD_INPUT_HANDLE);
UInt32 consoleMode;
NativeFunctions.GetConsoleMode(consoleHandle, out consoleMode);
if (Enable)
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
else
consoleMode &= ~((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_EXTENDED_FLAGS);
NativeFunctions.SetConsoleMode(consoleHandle, consoleMode);
}
}
答案 3 :(得分:0)
通过下面使用的代码组合,我可以启用或禁用快速编辑模式。
const int ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
要启用,请执行简单的currentConsoleMode &= ENABLE_QUICK_EDIT;
要禁用,请执行
currentConsoleMode &= ~ENABLE_QUICK_EDIT
然后调用 SetConsoleMode 。
答案 4 :(得分:0)
我偶然发现了同一个问题,该问题在我的控制台应用程序中启用了快速编辑模式,该模式使用C语言编写,并且已经在Windows 7 32位环境下工作了很长时间。在将其移植到Windows 10 64位(仍然是32位应用程序)之后(不是真正地移植,而是修改了一些代码行),我观察到了相同的行为。 所以我寻找解决方案。
但是出于我未知的原因,该代码以相反的方式工作,即在mode参数中设置位ENABLE_QUICK_EDIT_MODE
实际上禁用了快速编辑模式。重置该位将启用快速编辑模式... ???
这是我的代码:
/// <summary>
/// This flag enables the user to use the mouse to select and edit text. To enable
/// this option, you must also set the ExtendedFlags flag.
/// </summary>
const int QuickEditMode = 64;
// ExtendedFlags must be combined with
// InsertMode and QuickEditMode when setting
/// <summary>
/// ExtendedFlags must be enabled in order to enable InsertMode or QuickEditMode.
/// </summary>
const int ExtendedFlags = 128;
BOOLEAN EnableQuickEdit()
{
HWND conHandle = GetStdHandle(STD_INPUT_HANDLE);
int mode;
DWORD dwLastError = GetLastError();
if (!GetConsoleMode(conHandle, &mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
return (dwLastError == 0);
}
else
dwLastError = 0;
mode = mode & ~QuickEditMode;
if (!SetConsoleMode(conHandle, mode | ExtendedFlags))
{
// error setting console mode.
dwLastError = GetLastError();
}
else
dwLastError = 0;
return (dwLastError == 0);
}
BOOLEAN DisableQuickEdit()
{
HWND conHandle = GetStdHandle(STD_INPUT_HANDLE);
int mode;
DWORD dwLastError = GetLastError();
if (!GetConsoleMode(conHandle, &mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
return (dwLastError == 0);
}
else
dwLastError = 0;
mode = mode | QuickEditMode;
if (!SetConsoleMode(conHandle, mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
}
else
dwLastError = 0;
return (dwLastError == 0);
}
问候沃尔夫冈
答案 5 :(得分:0)
对于那些使用 vb.net 的人
df<-data.frame(zip=c(1,22,333,4444,55555))
df$zip <- stringr::str_pad(df$zip, width=5, pad = "0")