fnky
fnky

Reputation: 705

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?

Upvotes: 4

Views: 6897

Answers (4)

drf
drf

Reputation: 8699

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);
}

Upvotes: 12

Erre Efe
Erre Efe

Reputation: 15557

If you build the command prompt app then you'll get the select/copy/paste behavior for free. If you want to implement a right click menu (context menu) I don't think you can.

Maybe to simple but you can implement a simple switch based menu:

Upvotes: 0

Ray
Ray

Reputation: 46565

You can't do context menu in console apps or in the command prompt.

Console Apps act exactly like the default cmd.exe. You need to go to the menu by clicking the icon on the top left, and the edit menu will give you the options you've listed.

You can also go to properties to turn quick edit on.

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26717

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.

Upvotes: 1

Related Questions