Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console Application with selectable text

I want the user to be able to select the text (just like in Commandprompt) where you right click on the console application's surface and a menu will show, the user can then choose same functions as in commandprompt:

Mark
Copy        (Shortcut: Enter)
Paste
Select All
Scroll
Find

I have tried to Google after things like "C# Console Application select text" and other kind of things but can't seem to find a proper solution for this, since the user should be able to mark the text he/she wan't to copy or replace (with paste).

Do you have a solution for my question?

like image 679
fnky Avatar asked Dec 12 '22 10:12

fnky


2 Answers

There are no managed methods to do this, but quick edit mode can be enabled through P/Invoke. Quick edit mode allows console text to be selected with the mouse and copied, and for text to be pasted with the right-moue button. (See this article for a description of quick edit mode.)

// using System.Runtime.InteropServices;

[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);

[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int mode);

[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(int handle);

const int STD_INPUT_HANDLE = -10;
const int ENABLE_QUICK_EDIT_MODE = 0x40 | 0x80;

public static void EnableQuickEditMode()
{
    int mode;
    IntPtr handle = GetStdHandle(STD_INPUT_HANDLE);
    GetConsoleMode(handle, out mode);
    mode |= ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(handle, mode);
}
like image 157
drf Avatar answered Dec 26 '22 23:12

drf


maybe I didn't get you but when you execute your console application it will be hosted into a command-prompt window which allows you to copy end past text where ever you like.

like image 30
Massimiliano Peluso Avatar answered Dec 27 '22 00:12

Massimiliano Peluso