-1

I am new to StackOverflow, something may not be correct in this post.

I have the following sample code to describe my question:



using System;

sealed public class Program
{
    public static void Main(string[] args)
    {
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Yellow;
        
        Console.Write("F");
        
        Console.ResetColor();
        Console.BackgroundColor = ConsoleColor.Gray;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write("ile - ");
        
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Yellow;
        
        Console.Write("E");
        
        Console.ResetColor();
        Console.BackgroundColor = ConsoleColor.Gray;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write("dit - ");
        
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Yellow;
        
        Console.Write("C");
        
        Console.ResetColor();
        Console.BackgroundColor = ConsoleColor.Gray;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.WriteLine("lose");
        Console.ResetColor();
        
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - -");
        
        Console.WriteLine("This is just some sample code.");
        Console.WriteLine("Now I will generate a little box that requires a hover effect.");
        Console.WriteLine("Basically I want its color to be changed to red upon hovering on it, or better - triggering some function.");
        Console.WriteLine("I don't think this is possible with console application, but just asking in case if it is still possible.");
        
        Console.BackgroundColor = ConsoleColor.Gray;
        Console.ForegroundColor = ConsoleColor.Gray;
        
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
        Console.WriteLine("###################################");
    }
}

This little sample-code will generate some text and a gray box. Now, when a user puts their mouse pointer over a gray box, I'd like the gray box to change its color to red, or anything similar - such as detecting it and doing specific operation, mainly triggering a function. I don't think this is possible with C# Console Application, but just asking in case if it is still possible.

Thanks in advance. :)

I tried adding some kind of function, while loop, or adding DLL reference (using .NET Framework csc.exe feature). I quickly got confused and got no idea how to do this.

winscripter
  • 109
  • 1
  • 11
  • 2
    A hover effect would be a typical GUI thing and rather atypical for a TUI. I could imagine this would probably be possible - maybe with something like ncurses but not as naturally and easy as in wpf, WinForms or any other Graphical UI framework. – Fildor Feb 12 '23 at 18:39
  • Let's say your program is called app. Run it in the console as follows: `app.exe > out.txt`. That is, redirect the output to a file. How do you imagine hover effect now? – Alexander Petrov Feb 12 '23 at 19:31

2 Answers2

2

.NET and C# does not provide a built-in way for reading such mouse events. Although it might be possible to achieve using Windows API, I highly doubt that it's worth it. I suggest trying out any GUI framework such as Windows Forms, WPF or Avalonia UI. It will much easier to achieve and will save you a lot of time.

semptra
  • 740
  • 1
  • 6
  • 10
1

Ok, this is no Hover effect, but on a (Windows) Console you can draw users attention by using Blinking text.

@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
echo %ESC%[5mBlink%ESC%[0m

Save above as blink.bat, and run it on a CMD prompt. It will show the text "Blink", which is blinking.

More info, also on the use of colors: C# - Make text blinking in console using ANSI codes

Blinking text in C#:

char esc = (char)27;
string Hello = $"{esc}[5mHello World!{esc}[0m";
Console.WriteLine($"{Hello}");

Finally: Sorry to say this but flashing text is probably not a sensible thing to be doing with a console application.

enter image description here

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • It didn't seem like the C# code worked, I even tried to put it in the while loop and add System.Threading.Thread.Sleep(1000), but it seems like the text is just either showing once, or repeatedly when I add the while loop. I believe it's just my misunderstanding on how the code apparently must work. Also, the code in 'Sorry to say this but flashing text is probably not a sensible thing to be doing with a console application.' also seems to not work properly. – winscripter Feb 13 '23 at 07:25
  • @winscripterL I added a screenshot. – Luuk Feb 13 '23 at 19:01
  • I tried to use VS for now instead of online compiler. It works, that's cool. – winscripter Feb 14 '23 at 06:40