How to programmatic disable C# Console Application's Quick Edit mode?

前端 未结 5 1025
庸人自扰
庸人自扰 2020-11-27 14:34

I,ve tried several solutions found, like the one ->

http://www.pcreview.co.uk/forums/console-writeline-hangs-if-user-click-into-console-window-t1412701.html

5条回答
  •  旧时难觅i
    2020-11-27 15:31

    I just happened to stumble over the same problem with quick edit mode enabled in my console application, which is written in C, and has been working under windows 7 32 bit for ages. After porting (well not really porting, but adapting some code lines) it to windows 10 64 Bit (still being a 32 bit application), I observed the same behaviour. So I searched for a solution.

    But for a reason unknown to me, the code works the other way round, i.e setting the bit ENABLE_QUICK_EDIT_MODE in the mode parameter actually disables quick edit mode. And resetting the bit enables the quick edit mode...???

    Here is my code:

        /// 
    /// 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.
    /// 
    const int QuickEditMode = 64;
    
    // ExtendedFlags must be combined with
    // InsertMode and QuickEditMode when setting
    /// 
    /// ExtendedFlags must be enabled in order to enable InsertMode or QuickEditMode.
    /// 
    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);
    }
    

    Greetings Wolfgang

提交回复
热议问题