Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Console user interface

[es] :: .NET :: Console user interface

[ Pregleda: 2170 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

artriba
Split

Član broj: 158342
Poruke: 109
*.adsl.net.t-com.hr.



Profil

icon Console user interface15.02.2008. u 14:18 - pre 197 meseci
Koje se klase u .netu bave tekstualnim sučeljem programa? Znam samo za system.console a šta sam malo gledao po msdn vidio sam da samo da se može manipulirati bojama prozora, veličinom, i kursorom, a siguran sam da to nije sve. Kako bi recimo napravio konzolni drop-down menu koji regira na mouse-click? Gledam malo po netu i ne nalazim nikakve tutoriale ni o .netu ni općenito o CUI.Ako netko zna ime nekog open source CUI programa rađenog u .netu ili neki link na tutorial naka posta.
 
Odgovor na temu

Predrag Glumac
Luxembourg

Član broj: 167588
Poruke: 127
*.eunet.yu.



Profil

icon Re: Console user interface15.02.2008. u 14:50 - pre 197 meseci
Citat:
Kako bi recimo napravio konzolni drop-down menu koji regira na mouse-click?

Tesko ako ne i nikako, jer konzola ne podrzava misa. Kada kazu kurzor misle na onaj underscore gde unosis tekst, nesto slicno karetu pod Windowsovim GUI. Davnih dana pod DOS-om, morao bi prvo da ucitas drajver za misa, a onda preko interapta 10h (moguce da gresim) pozoves funkciju da ocitavas njegovu poziciju, ili da korists biblioteku koja to radi umesto tebe (npr. mouse.h u C-u).
 
Odgovor na temu

artriba
Split

Član broj: 158342
Poruke: 109
*.adsl.net.t-com.hr.



Profil

icon Re: Console user interface15.02.2008. u 17:06 - pre 197 meseci
Izgleda da se u .netu bez direktnih api poziva ne mogu dodati funkcionalnosti miša ali evo naletio sam na dva zgodna konzolna programa, možda nekome zatreba
Code:
using System;

class ConsoleListBox
{

    static void Main ()
    {
        Console.TreatControlCAsInput = false;
        Console.CancelKeyPress += new ConsoleCancelEventHandler ( BreakHandler );
        Console.Clear ();
        Console.CursorVisible = false;

        string [] levels = new string [ 10 ];

        for ( int i = 0; i < 10; i++ )
        {
            levels [ i ] = "Level " + ( i + 1 ).ToString ();
        }

        WriteColorString ( "Choose Level using down and up arrow keys and press enter", 12, 20, ConsoleColor.Black, ConsoleColor.White );

        int choice = ChooseListBoxItem ( levels, 34, 5, ConsoleColor.Blue, ConsoleColor.White );
        // do something with choice
        WriteColorString ( "You chose " + levels [ choice - 1 ] + ". Press any key to exit", 21, 22, ConsoleColor.Black, ConsoleColor.White );
        Console.ReadKey ();
        CleanUp ();
    }

    public static int ChooseListBoxItem ( string [] items, int ucol, int urow, ConsoleColor back, ConsoleColor fore )
    {
        int numItems = items.Length;
        int maxLength = items [ 0 ].Length;
        for ( int i = 1; i < numItems; i++ )
        {
            if ( items [ i ].Length > maxLength )
            {
                maxLength = items [ i ].Length;
            }
        }
        int [] rightSpaces = new int [ numItems ];
        for ( int i = 0; i < numItems; i++ )
        {
            rightSpaces [ i ] = maxLength - items [ i ].Length + 1;
        }
        int lcol = ucol + maxLength + 3;
        int lrow = urow + numItems + 1;
        DrawBox ( ucol, urow, lcol, lrow, back, fore, true );
        WriteColorString ( " " + items [ 0 ] + new string ( ' ', rightSpaces [ 0 ] ), ucol + 1, urow + 1, fore, back );
        for ( int i = 2; i <= numItems; i++ )
        {
            WriteColorString ( items [ i - 1 ], ucol + 2, urow + i, back, fore );
        }

        ConsoleKeyInfo cki;
        char key;
        int choice = 1;

        while ( true )
        {
            cki = Console.ReadKey ( true );
            key = cki.KeyChar;
            if ( key == '\r' ) // enter
            {
                return choice;
            }
            else if ( cki.Key == ConsoleKey.DownArrow )
            {
                WriteColorString ( " " + items [ choice - 1 ] + new string ( ' ', rightSpaces [ choice - 1 ] ), ucol + 1, urow + choice, back, fore );
                if ( choice < numItems )
                {
                    choice++;
                }
                else
                {
                    choice = 1;
                }
                WriteColorString ( " " + items [ choice - 1 ] + new string ( ' ', rightSpaces [ choice - 1 ] ), ucol + 1, urow + choice, fore, back );

            }
            else if ( cki.Key == ConsoleKey.UpArrow )
            {
                WriteColorString ( " " + items [ choice - 1 ] + new string ( ' ', rightSpaces [ choice - 1 ] ), ucol + 1, urow + choice, back, fore );
                if ( choice > 1 )
                {
                    choice--;
                }
                else
                {
                    choice = numItems;
                }
                WriteColorString ( " " + items [ choice - 1 ] + new string ( ' ', rightSpaces [ choice - 1 ] ), ucol + 1, urow + choice, fore, back );
            }
        }
    }


    public static void DrawBox ( int ucol, int urow, int lcol, int lrow, ConsoleColor back, ConsoleColor fore, bool fill )
    {
        const char Horizontal = '\u2500';
        const char Vertical = '\u2502';
        const char UpperLeftCorner = '\u250c';
        const char UpperRightCorner = '\u2510';
        const char LowerLeftCorner = '\u2514';
        const char LowerRightCorner = '\u2518';
        string fillLine = fill ? new string ( ' ', lcol - ucol - 1 ) : "";
        SetColors ( back, fore );
        // draw top edge
        Console.SetCursorPosition ( ucol, urow );
        Console.Write ( UpperLeftCorner );
        for ( int i = ucol + 1; i < lcol; i++ )
        {
            Console.Write ( Horizontal );
        }
        Console.Write ( UpperRightCorner );

        // draw sides
        for ( int i = urow + 1; i < lrow; i++ )
        {
            Console.SetCursorPosition ( ucol, i );
            Console.Write ( Vertical );
            if ( fill ) Console.Write ( fillLine );
            Console.SetCursorPosition ( lcol, i );
            Console.Write ( Vertical );
        }
        // draw bottom edge
        Console.SetCursorPosition ( ucol, lrow );
        Console.Write ( LowerLeftCorner );
        for ( int i = ucol + 1; i < lcol; i++ )
        {
            Console.Write ( Horizontal );
        }
        Console.Write ( LowerRightCorner );
    }

    public static void WriteColorString ( string s, int col, int row, ConsoleColor back, ConsoleColor fore )
    {
        SetColors ( back, fore );
        // write string
        Console.SetCursorPosition ( col, row );
        Console.Write ( s );
    }

    public static void SetColors ( ConsoleColor back, ConsoleColor fore )
    {
        Console.BackgroundColor = back;
        Console.ForegroundColor = fore;
    }

    public static void CleanUp ()
    {
        Console.ResetColor ();
        Console.CursorVisible = true;
        Console.Clear ();
    }

    private static void BreakHandler ( object sender, ConsoleCancelEventArgs args )
    {
        // exit gracefully if Control-C or Control-Break pressed
        CleanUp ();
    }


}


Code:
using System;
using System.Text;

class LoginBox
{
    static ConsoleColor oldBack;
    static ConsoleColor oldFore;

    static void Main ()
    {
        Console.Clear ();
        DrawBox ( 20, 10, 60, 21, ConsoleColor.Green, ConsoleColor.Black, false );
        WriteColorString ( "Log In", 37, 10, ConsoleColor.Red, ConsoleColor.White );
        WriteColorString ( "User Name : ", 23, 14, ConsoleColor.Black, ConsoleColor.Cyan );
        WriteColorString ( "Password : ", 23, 17, ConsoleColor.Black, ConsoleColor.Cyan );
        // get name
        Console.SetCursorPosition ( 35, 14 );
        string name = GetName ( 20 );
        // get password
        Console.SetCursorPosition ( 35, 17 );
        string password = GetPassword ( 10 );
        // validate name and password and proceed to main program
        Console.SetCursorPosition ( 0, 40 );
        Console.WriteLine ( "(Password entered was '{0}', press any key to end program)", password );
        Console.ReadKey ();
        Console.Clear ();
    }

    public static void DrawBox ( int ucol, int urow, int lcol, int lrow, ConsoleColor back, ConsoleColor fore, bool fill )
    {
        const char Horizontal = '\u2500';
        const char Vertical = '\u2502';
        const char UpperLeftCorner = '\u250c';
        const char UpperRightCorner = '\u2510';
        const char LowerLeftCorner = '\u2514';
        const char LowerRightCorner = '\u2518';
        string fillLine = fill ? new string ( ' ', lcol - ucol - 1 ) : "";
        SetColors ( back, fore );
        // draw top edge
        Console.SetCursorPosition ( ucol, urow );
        Console.Write ( UpperLeftCorner );
        for ( int i = ucol + 1; i < lcol; i++ )
        {
            Console.Write ( Horizontal );
        }
        Console.Write ( UpperRightCorner );
        // draw sides
        for ( int i = urow + 1; i < lrow; i++ )
        {
            Console.SetCursorPosition ( ucol, i );
            Console.Write ( Vertical );
            if ( fill ) Console.Write ( fillLine );
            Console.SetCursorPosition ( lcol, i );
            Console.Write ( Vertical );
        }
        // draw bottom edge
        Console.SetCursorPosition ( ucol, lrow );
        Console.Write ( LowerLeftCorner );
        for ( int i = ucol + 1; i < lcol; i++ )
        {
            Console.Write ( Horizontal );
        }
        Console.Write ( LowerRightCorner );
        RestoreColors ();
    }

    // overload to use current background and foreground colors
    public static void DrawBox ( int ucol, int urow, int lcol, int lrow, bool fill )
    {
        DrawBox ( ucol, urow, lcol, lrow, Console.BackgroundColor, Console.ForegroundColor, fill );
    }

    public static void WriteColorString ( string s, int col, int row, ConsoleColor back, ConsoleColor fore )
    {
        SetColors ( back, fore );
        // write string
        Console.SetCursorPosition ( col, row );
        Console.Write ( s );
        RestoreColors ();
    }

    public static void SetColors ( ConsoleColor back, ConsoleColor fore )
    {
        // save current background and foreground colors
        oldBack = Console.BackgroundColor;
        oldFore = Console.ForegroundColor;
        // set them to new colors
        Console.BackgroundColor = back;
        Console.ForegroundColor = fore;
    }

    public static void RestoreColors ()
    {
        // restore original colors
        Console.BackgroundColor = oldBack;
        Console.ForegroundColor = oldFore;
    }

    public static string GetName ( int maxLength )
    {
        SetColors ( ConsoleColor.White, ConsoleColor.Black );
        string blank = new string ( ' ', maxLength + 2 );
        int startCol = Console.CursorLeft;
        Console.Write ( blank );
        Console.CursorLeft = startCol + 1;
        StringBuilder sb = new StringBuilder ();
        char key;
        // only permit letters, spaces, hyphens and apostrophes
        //continue until return pressed
        while ( ( key = Console.ReadKey ( true ).KeyChar ) != '\r' )
        {
            if ( key == '\b' && sb.Length > 0 ) // backspace
            {
                Console.Write ( key + " " + key );
                sb = sb.Remove ( sb.Length - 1, 1 );
            }
            else if ( sb.Length == maxLength )
            {
                Console.Beep ();
            }
            else if ( Char.IsLetter ( key ) )
            {
                Console.Write ( key );
                sb = sb.Append ( key );
            }
            else if ( ( key == ' ' || key == '-' || key == '\'' ) && sb.Length > 0 )
            {
                Console.Write ( key );
                sb = sb.Append ( key );
            }
        }
        RestoreColors ();
        SetColors ( ConsoleColor.Black, ConsoleColor.White );
        Console.CursorLeft = startCol;
        Console.Write ( blank );
        Console.CursorLeft = startCol + 1;
        string name = sb.ToString ();
        Console.WriteLine ( name );
        return name;
    }

    public static string GetPassword ( int maxLength )
    {
        SetColors ( ConsoleColor.White, ConsoleColor.Black );
        string blank = new string ( ' ', maxLength + 2 );
        int startCol = Console.CursorLeft;
        Console.Write ( blank );
        Console.CursorLeft = startCol + 1;
        StringBuilder sb = new StringBuilder ();
        char key;
        // only permit letters or digits
        //continue until return pressed
        while ( ( key = Console.ReadKey ( true ).KeyChar ) != '\r' )
        {
            if ( key == '\b' && sb.Length > 0 ) // backspace
            {
                Console.Write ( key + " " + key );
                sb = sb.Remove ( sb.Length - 1, 1 );
            }
            else if ( sb.Length == maxLength )
            {
                Console.Beep ();
            }
            else if ( Char.IsLetterOrDigit ( key ) )
            {
                Console.Write ( "x" );
                sb = sb.Append ( key );
            }
        }
        RestoreColors ();
        SetColors ( ConsoleColor.Black, ConsoleColor.White );
        Console.CursorLeft = startCol;
        Console.Write ( blank );
        Console.CursorLeft = startCol + 1;
        Console.WriteLine ( new string ( 'x', sb.Length ) );
        return sb.ToString ();
    }

 
Odgovor na temu

[es] :: .NET :: Console user interface

[ Pregleda: 2170 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.