Click here to Skip to main content
15,912,329 members
Home / Discussions / C#
   

C#

 
GeneralRe: center text? Pin
Larsson16-Apr-05 10:11
Larsson16-Apr-05 10:11 
GeneralRe: center text? Pin
Heath Stewart16-Apr-05 10:24
protectorHeath Stewart16-Apr-05 10:24 
GeneralCookies and winforms Pin
Anonymous16-Apr-05 2:52
Anonymous16-Apr-05 2:52 
GeneralRe: Cookies and winforms Pin
Heath Stewart16-Apr-05 9:35
protectorHeath Stewart16-Apr-05 9:35 
GeneralRe: Cookies and winforms Pin
Anonymous16-Apr-05 23:55
Anonymous16-Apr-05 23:55 
GeneralRe: Cookies and winforms Pin
Heath Stewart17-Apr-05 16:21
protectorHeath Stewart17-Apr-05 16:21 
Generaltext color Pin
Larsson16-Apr-05 2:04
Larsson16-Apr-05 2:04 
GeneralRe: text color Pin
Heath Stewart16-Apr-05 9:09
protectorHeath Stewart16-Apr-05 9:09 
In .NET 2.0 the Console class will define new methods for setting text color, but for now you'd have to P/Invoke SetConsoleTextAttribute, as well as GetConsoleScreenBufferInfo.
using System;
using System.Runtime.InteropServices;
 
class ConsoleSample
{
  [DllImport("kernel32.dll")]
  static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput,
    TextAttributes wAttributes);
 
  [Flags]
  enum TextAttributes : short
  {
    None = 0,
    FOREGROUND_BLUE = 1,
    FOREGROUND_GREEN = 2,
    FOREGROUND_RED = 4,
    FOREGROUND_INTENSITY = 8,
    BACKGROUND_BLUE = 16,
    BACKGROUND_GREEN = 32,
    BACKGROUND_RED = 64,
    BACKGROUND_INTENSITY = 128
  }
 
  [DllImport("kernel32.dll")]
  static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput,
    out CONSOLE_SCREEN_BUFFER_INFO csbi);
 
  struct CONSOLE_SCREEN_BUFFER_INFO
  {
    public uint dwSize;
    public uint dwCursorPosition;
    public TextAttributes wAttributes;
    public ulong srWindow;
    public uint dwMaximumWindowSize;
  }
 
  [DllImport("kernel32.dll")]
  static extern IntPtr GetStdHandle(StdHandle nStdHandle);
 
  enum StdHandle : uint
  {
    STD_INPUT_HANDLE = unchecked((uint)-10),
    STD_OUTPUT_HANDLE = unchecked((uint)-11),
    STD_ERROR_HANDLE = unchecked((uint)-12)
  }
 
  static void Main()
  {
    // Get the handle to stderr.
    IntPtr hStdErr = GetStdHandle(StdHandle.STD_ERROR_HANDLE);
 
    // Get the current text attributes to restore them later.
    TextAttributes attrs = TextAttributes.None;
 
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(hStdErr, out csbi))
    {
      Console.Error.WriteLine("Error: could not get current colors.");
      Environment.Exit(1);
    }
    else
      attrs = csbi.wAttributes;
 
    // Now set the text color for stderr to bright red.
    if (!SetConsoleTextAttribute(hStdErr,
      TextAttributes.FOREGROUND_RED | TextAttributes.FOREGROUND_INTENSITY))
    {
      Console.Error.WriteLine("Error: could not set current colors.");
      Environment.Exit(1);
    }
 
    // Now print an example error message to stderr.
    Console.Error.WriteLine("Sample error: this is a test error in red.");
 
    // Restore the original colors.
    SetConsoleTextAttribute(hStdErr, attrs);
  }
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft

[My Articles] [My Blog]
GeneralRe: text color Pin
Larsson16-Apr-05 10:12
Larsson16-Apr-05 10:12 
GeneralRe: text color Pin
Heath Stewart16-Apr-05 10:23
protectorHeath Stewart16-Apr-05 10:23 
QuestionSDL.Net on WinForm possible? Pin
RATC16-Apr-05 1:56
RATC16-Apr-05 1:56 
AnswerRe: SDL.Net on WinForm possible? Pin
Heath Stewart16-Apr-05 8:37
protectorHeath Stewart16-Apr-05 8:37 
GeneralRe: SDL.Net on WinForm possible? Pin
RATC16-Apr-05 10:01
RATC16-Apr-05 10:01 
GeneralRe: SDL.Net on WinForm possible? Pin
Heath Stewart16-Apr-05 10:38
protectorHeath Stewart16-Apr-05 10:38 
GeneralRe: SDL.Net on WinForm possible? Pin
RATC16-Apr-05 10:53
RATC16-Apr-05 10:53 
GeneralRe: SDL.Net on WinForm possible? Pin
Heath Stewart16-Apr-05 11:12
protectorHeath Stewart16-Apr-05 11:12 
GeneralRe: SDL.Net on WinForm possible? Pin
RATC16-Apr-05 11:16
RATC16-Apr-05 11:16 
GeneralEncryption/decryption problem Pin
Kordzik16-Apr-05 0:38
Kordzik16-Apr-05 0:38 
Questionanyone help with this ??? Pin
Imran Adam16-Apr-05 0:33
Imran Adam16-Apr-05 0:33 
AnswerRe: anyone help with this ??? Pin
DavidNohejl16-Apr-05 1:13
DavidNohejl16-Apr-05 1:13 
AnswerRe: anyone help with this ??? Pin
Heath Stewart16-Apr-05 8:30
protectorHeath Stewart16-Apr-05 8:30 
QuestionHow to use CStringArray in C# using p/invoke Pin
shusong15-Apr-05 23:57
shusong15-Apr-05 23:57 
AnswerRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart16-Apr-05 8:23
protectorHeath Stewart16-Apr-05 8:23 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
shusong16-Apr-05 15:00
shusong16-Apr-05 15:00 
GeneralRe: How to use CStringArray in C# using p/invoke Pin
Heath Stewart16-Apr-05 22:53
protectorHeath Stewart16-Apr-05 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.