Lm th? no ? thay ?i mu s?c ti?n c?nh v mu n?n vn b?n trong m?t c?a s? giao di?n i?u khi?n b?ng cch s? d?ng Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 319883 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny m t? cch thay ?i ti?n c?nh v n?n mu s?c c?a vn b?n ?c vi?t vo c?a s? giao di?n i?u khi?n b?ng cch s? d?ng Visual C#.

Bi vi?t ny m t? lm th? no ? lu cc thi?t ?t ban ?u c?a c?a s? giao di?n i?u khi?n khi b?t ?u chng tr?nh, lm th? no ? s?a ?i cc thi?t ?t mu v lm th? no ? khi ph?c l?i cc mu s?c v?i gi tr? ban ?u c?a h? khi ra kh?i chng tr?nh.


Gi?i thi?u

? thay ?i mu s?c ti?n c?nh v n?n vn b?n hi?n th? c?a s? giao di?n i?u khi?n, s? d?ng cc SetConsoleTextAttribute Win32 ?ng d?ng l?p tr?nh ch?c nng giao di?n (API). Ch?c nng ny t?p h?p cc thu?c tnh c?a cc nhn v?t ang ghi vo b? ?m mn h?nh.

Khi b?n thay ?i cc thu?c tnh ny t?i th?i gian ch?y, nh?ng thay ?i l h?p l? cho mi?n l giao di?n i?u khi?n c?a s? ang m?. N?u b?n ng v m? l?i cc c?a s? giao di?n i?u khi?n, cc thu?c tnh ?c ?t l?i v? gi tr? m?c ?nh c?a h?. N?u b?n th?c hi?n chng tr?nh t? m?t d?ng l?nh trong m?t c?a s? giao di?n i?u khi?n ang ch?y, nh?ng thay ?i b?n th?c hi?n cho cc thu?c tnh vn b?n c hi?u l?c trong giao di?n i?u khi?n c?a s? cho mi?n l cc c?a s? ang m?, ngay c? sau khi thot kh?i chng tr?nh c?a b?n. V? v?y, m?t chng tr?nh s? khi ph?c cc thu?c tnh mu s?c ban ?u tr?c khi ra kh?i chng tr?nh.

B?n c th? c ?c cc thu?c tnh vn b?n c?a c?a s? giao di?n i?u khi?n b?ng cch s? d?ng cc GetConsoleScreenBufferInfo API ch?c nng. Ch?c nng ny l?p ?y m?t th? hi?n c?a cc CONSOLE_SCREEN_BUFFER_INFO c?u trc thng tin v? cc thi?t ?t hi?n th?i b? ?m ?u ra. Cc wAttribute tham s? c?u trc ny ch?a thng tin mu ?nh ngh?a pha sau v n?n mu s?c c?a vn b?n. Nh?ng mu s?c c th? l b?t k? s? k?t h?p mu s?c c th? ?c t?o ra b?ng cch k?t h?p mu ?, l?c v lam.
   OriginalColors = ConsoleInfo.wAttributes;
   SetConsoleTextAttribute(hConsoleHandle, color);
				
B?n c th? s? d?ng cc ResetColor phng php ? ?t l?i thu?c tnh b? ?m ?u ra kh?i c?a s? giao di?n i?u khi?n cc gi tr? ban ?u b? b?t khi chng tr?nh b?t ?u th?c hi?n n.
   SetConsoleTextAttribute(hConsoleHandle, OriginalColors);
				

V d?-by-step

  1. Trong ph?ng thu tr?c quan.NET ho?c Visual Studio 2005, t?o ra m?t d? n Visual C# giao di?n i?u khi?n ?ng d?ng m?i.
  2. Trong gi?i php Explorer, b?m chu?t ph?i vo d? n c?a b?n, h?y nh?p vo Thm, v ch?n Thm Class ? thm m?t l?p m?i vo chng tr?nh c?a b?n.
  3. Dn o?n m? m?u sau trong l?p ?c t?o ra. Ki?m ch?ng r?ng m?u m? thay th? t?t c? hi?n c m? trong l?p.
       using System;
       using System.Runtime.InteropServices;
    
       namespace ConsoleColor
       {
          /// Summary description for Class2.
          public class Class2
          {
             private int hConsoleHandle;
             private COORD ConsoleOutputLocation;
             private CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
             private int OriginalColors;
    
             private const int  STD_OUTPUT_HANDLE = -11;
    
             [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true,
                             CharSet=CharSet.Auto,
                             CallingConvention=CallingConvention.StdCall)]
    		private static extern int GetStdHandle(int nStdHandle);
    
             [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo",
                             SetLastError=true, CharSet=CharSet.Auto,
                             CallingConvention=CallingConvention.StdCall)]
    		private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput,
                             ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
    
             [DllImport("kernel32.dll", EntryPoint="SetConsoleTextAttribute",
                             SetLastError=true, CharSet=CharSet.Auto,
                             CallingConvention=CallingConvention.StdCall)]
    		private static extern int SetConsoleTextAttribute(int hConsoleOutput,
                                     int wAttributes);
     
             public enum Foreground
             {			
                Blue = 0x00000001,
                Green = 0x00000002,
                Red = 0x00000004,
                Intensity = 0x00000008
             }
    
             public enum Background
             {
                Blue = 0x00000010,
                Green = 0x00000020,
                Red = 0x00000040,
                Intensity = 0x00000080
             }
    
             [StructLayout(LayoutKind.Sequential)] private struct COORD
             {
                short X;
                short Y;
             }
    			
             [StructLayout(LayoutKind.Sequential)] private struct SMALL_RECT
             {
                short Left;
                short Top;
                short Right;
                short Bottom;
             }
    
             [StructLayout(LayoutKind.Sequential)] private struct CONSOLE_SCREEN_BUFFER_INFO
             {
                public COORD dwSize;
                public COORD dwCursorPosition;
                public int wAttributes;
                public SMALL_RECT srWindow;
                public COORD dwMaximumWindowSize;
             }
    
             // Constructor.
             public Class2()
             {
                ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
                ConsoleOutputLocation = new COORD();
                hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
                GetConsoleScreenBufferInfo(hConsoleHandle, ref ConsoleInfo);
                OriginalColors = ConsoleInfo.wAttributes;
             }		
    
             public void TextColor(int color)
             {
                SetConsoleTextAttribute(hConsoleHandle, color);
             }
    		
             public void ResetColor()
             {
                SetConsoleTextAttribute(hConsoleHandle, OriginalColors);
             }
          }
       }
    					
  4. Dn o?n m? m?u sau trong l?p t?p tin c ch?a cc Chnh ch?c nng. Ki?m ch?ng r?ng m?u m? thay th? t?t c? cc m? hi?n c trong t?p tin.
       using System;
    
       namespace ConsoleColor
       {
          class Class1
          {
             [STAThread]
             static void Main(string[] args)
             {
                Class2 TextChange = new Class2();
                Console.WriteLine("Original Colors");
                Console.WriteLine("Press Enter to Begin");
                Console.ReadLine();
                TextChange.TextColor((int)Class2.Foreground.Green +
                                     (int)Class2.Foreground.Intensity);
                Console.WriteLine("THIS TEXT IS GREEN");
                Console.WriteLine("Press Enter to change colors again");
                Console.ReadLine();
                TextChange.TextColor((int)Class2.Foreground.Red +
                                     (int)Class2.Foreground.Blue +
                                     (int)Class2.Foreground.Intensity);
                Console.WriteLine("NOW THE TEXT IS PURPLE");
                Console.WriteLine("Press Enter to change colors again");
                Console.ReadLine();
                TextChange.TextColor((int)Class2.Foreground.Blue +
                                     (int)Class2.Foreground.Intensity +
                                     (int)Class2.Background.Green +
                                     (int)Class2.Background.Intensity);
                Console.WriteLine("NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN");
                Console.WriteLine("Press Enter change everything back to normal");
                Console.ReadLine();
                TextChange.ResetColor();
                Console.WriteLine("Back to Original Colors");
                Console.WriteLine("Press Enter to Terminate");
                Console.ReadLine();
    	 }
          }
       }
    					
  5. Nh?n F5 ? bin d?ch v sau ch?y chng tr?nh.

Gi?i p th?c m?c

N?u b?n g?p v?n ?, h?y ki?m ch?ng r?ng b?n ang i qua m?t gi tr? h?p l? nh tham s? cho cc TextColor phng php. ? t?o m?t gi tr? h?p l?, k?t h?p cc gi tr? m b?n c ?c t? cc Ti?n c?nh v N?n enumerations ?c ?nh ngh?a trong l?p.

THAM KH?O

? bi?t thm chi ti?t, gh thm Web site sau c?a Microsoft:
Ch?c nng giao di?n i?u khi?n
http://msdn2.Microsoft.com/en-US/Library/ms682073.aspx
? bi?t thm chi ti?t, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
319257 Lm th? no ?: Xo giao di?n i?u khi?n c?a s? v?i Visual C#.NET

Thu?c tnh

ID c?a bi: 319883 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
T? kha:
kbhowtomaster kbmt KB319883 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:319883

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com