C 入门经典%28第四版%29 Word版本 -...

download C 入门经典%28第四版%29 Word版本 - d1.amobbs.comd1.amobbs.com/bbs_upload782111/files_47/ourdev_694970J3QNCU.pdf · 用C#语言编写的源程序,必须用C#语言编译器将C#源程序编译为中间语言

If you can't read please download the document

Transcript of C 入门经典%28第四版%29 Word版本 -...

  • 1

  • 2

  • 3

  • 4

  • 5

    C#

    C# C C#

    C# windowWeb

    C# C# C#

    (:www.codepub.com

    1.1 C#

    Microsoft.NET(.NET)WebC#

    .NETC# C++

    C++

    VBC# C++ JAVA

    C++ JAVA C#

    C# C# C#(MicroSoft

    Intermediate Language,MSIL) exe dll CPU

    (Common Language Runtime

    CLR)(JUST IN TimeJIT) CPU

    CPUCLRC#C#CLR JAVA

    l (Common Language SpecificationCLS).NETC#

    C++VBJ#

    CLR

    CLR

    l CLR

    C C++

    CLR

    l

    CLR

    l C#

  • 6

    CLR

    l

    .NET

    bin

    bin

    l C++

    C# C#

    C#

    1.2

    1.2.1 SDK

    :

    using System;////C#

    class Welcome//

    { /*C

    */

    static void Main()//

    { Console.WriteLine("");//

    Console.ReadLine();//

    Console.WriteLine("");

    }

    }

    welcome.csC#csCC#

    CC++

    #includeC#using

    using SystemSystemC#usingC++#include

    class Welcome

    { static void Main()

    { System.Console.WriteLine("");

    System.Console.ReadLine();

    System.Console.WriteLine("");

  • 7

    }

    }

    ConsoleSystem.ConsoleSystem

    C#CC++::->C#

    .System.Net

    class WelcomeWelcomeC#

    Main()CC++C#

    Main()Main()

    C#Main()Main

    Main()staticstatic void Main()Welcome

    ConsoleConsoleSystem

    ConsoleWriteLineReadLineReadLine

    WriteLine

    Visual Studio.Net

    Microsoft.Net

    Framework SDKC#Microsoft.Net Framework SDKC#

    csc.exewelcome.cs

    welcome.csd:\Charp

    d:cd Charp

    C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc welcome.cs

    welcome.cs

    Welcome.exe

    Welcome.exe

    C#

    CC++

    C#.obj

    .exe.dllC#

    1.2.1 Visual Studio.Net

    (1) Visual Studio.Net 1.2.2A

    (2) 1.2.2B(P) Visual C#

    (T)(N) e1(L)

    D:\csarp D:\csarp

    1.2.2C

    (3) class1.cs

    using System;

    namespace e1

    {

  • 8

    ///

    /// Class1

    ///

    class Class1

    {

    ///

    ///

    ///

    [STAThread]

    static void Main(string[] args)

    {

    //

    // TODO:

    //

    Console.WriteLine("");

    Console.ReadLine();

    Console.WriteLine("");

    }

    }

    }

    (4) CTRL+F5 1.2.1

  • 9

    1.2.2A

    1.2.2B

  • 10

    1.2.2C(:www.codepub.com

    1.3

    C#

    1.3.1

    C

    Person

    using System;

    class Person//classPerson

    { private string name="";//

    private int age=12;//private

    public void Display()//()

    { Console.WriteLine(":{0},{1}",name,age);

    }

    public void SetName(string PersonName)//()

  • 11

    { name=PersonName;

    }

    public void SetAge(int PersonAge)

    { age=PersonAge;

    }

    }

    Console.WriteLine(":{0},{1}",name,age)

    name{0} age{1}

    Person intchar

    Person

    class {}

    class newpublic

    protectedinternalprivateabstract sealed

    1.3.2

    private()public()

    name age

    SetName() SetAge()

    1.3.3

    Person Person C#

    Person OnePerson=new Person()

    Person Person OnePerson Person

    Person OnePersonOnePerson=new Person()OnePerson Person

    C

    ()Person CC++

    C#

    OnePerson.OnePerson.

    OnePerson.Display(),C# C++

    ->

  • 12

    1.3.4

    new

    Person

    public Person(string Name,int Age)//

    { name=Name;

    age=Age;

    }

    Person OnePerson=new Person(20) Person

    ~Person~ Person()C#

    1.3.5

    C#

    Person

    public Person()//

    { name="";

    age=12;

    }

    Person OnePerson=new Person("",30)

    Person OnePerson=new Person()

    1.3.6 Person

    Person(VisualStudio.Net)

    using System;

    namespace e1//

    { class Person

    { private String name="";//

    private int age=12;

    public void Display()//()

    { Console.WriteLine(":{0},{1}",name,age);

    }

  • 13

    public void SetName(string PersonName)//()

    { name=PersonName;

    }

    public void SetAge(int PersonAge)//()

    { age=PersonAge;

    }

    public Person(string Name,int Age)//,,

    { name=Name;

    age=Age;

    }

    public Person()//

    { name="";

    age=12;

    }

    }

    class Class1

    { static void Main(string[] args)

    { Person OnePerson=new Person("",30);//

    OnePerson.Display();

    //(Class1)Person

    //OnePerson.name="";

    //PersonSetNamePersonname

    OnePerson.SetName("");

    OnePerson.SetAge(40);

    OnePerson.Display();

    OnePerson=new Person();

    OnePerson.Display();

    }

    }

    }

    CTRL+F5

    : ,30

    : ,40

    : ,12

    1.4 C#

    C#

    1.4.1

    C#(

  • 14

    )(Stack)()

    (Managed Heap)

    (Stack)

    using System;

    class MyClass//

    { public int a=0;

    }

    class Test

    { static void Main()

    { f1();

    }

    static public void f1()

    { int v1=1;// v1 1(Stack)

    int v2=v1;// v1( 1) v2v2=1,v1

    v2=2;//v2=2,v1

    MyClass r1=new MyClass();//r1MyClass

    MyClass r2=r1;//r1r2MyClass

    r2.a=2;//r1.a=2

    }

    }

    v1v1

    f1 f1v1 C

    C++.NET

    CLR

    r1 r2 MyClass

    f1r1r2MyClass

    CLR

    1.4.2

    C#

    l (Simple types)

    (bool)

    (char)(decimal)

    l (Struct types)

    l (Enumeration types)

    C#

  • 15

    1.4.3

    protected virtual abstract

    point

    using System;

    struct point//

    { public int x,y;//

    }

    class Test

    { static void Main()

    { point P1;

    P1.x=166;

    P1.y=111;

    point P2;

    P2=P1;//P2.x=166,P2.y=111

    point P3=new point();//newP3P3

    }//newP3x=y==0

    }

    1.4.4

    int i=int.MaxValue;string s=i.ToString()C#

    string s=13.ToString()

    System

    sbyte System.Sbyte 1 -128~127

    byte System.Byte 1 0~255

    short System.Int16 2 -32768~32767

    ushort System.UInt16 2 0~65535

    int System.Int32 4 -2147483648~2147483647

    uint System.UInt32 4 0~4292967295

    long System.Int64 8 -9223372036854775808~9223372036854775808

    ulong System.UInt64 8 0~18446744073709551615

    char System.Char 2 0~65535

    float System.Single 4 3.4E-38~3.4E+38

    double System.Double 8 1.7E-308~1.7E+308

    bool System.Boolean (true,false)

  • 16

    decimal System.Decimal 16 1.01028 7.91028

    C#CC++

    l CC#

    l Unicode Unicode 16

    l (char)char c1=10

    char c1=(char)10char c='A'char c='\x0032';char c='\u0032'

    l false,true0falsetruebool x=1

    x=true x=false

    l (decimal)

    1.4.5

    C#CC++

    using System;

    class Class1

    { enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

    //Visual Studio.Net,enum[STAThread]

    static void Main(string[] args)

    { Days day=Days.Tue;

    int x=(int)Days.Tue;//x=2

    Console.WriteLine("day={0},x={1}",day,x);//:day=Tue,x=4

    }

    }

    DaysintSun=0Mon=1Tue=2

    enum Days{Sat=1,Sun,Mon,Tue,Wed,Thu,Fri,Sat};

    Sun=1Mon=2Tue=3Wed=4 CC++C#

    bytesbyteshortushortintuintlongulongchar

    enum Days:byte{Sun,Mon,Tue,Wed,Thu,Fri,Sat};//

    1.4.6

    sbytebyte

    shortushortintuintlongulong0char(char)0float

    0.0fdouble0.0ddecimal0.0mboolfalse0

    null

    int i=0

    new

    int j=new int() new int

    j new

  • 17

    1.4.7

    C#

    l C#(object)

    l

    l

    C#C#

    new

    (object)

    1.4.8 (object)

    C#()object(object)

    object

    C#(

    )object

    int x =25;

    object obj1;

    obj1=x;

    object obj2= 'A';

    objectSystemSystem.Object

    1.4.9

    C#System.Array

    int[] arr=new int[5];arr

    ()

    C#

    using System;

    class Test

    { static void Main()

    { int[] arr=new int[3];//new3

    for(int i=0;i

  • 18

    Cint arr[]

    arr[0] = 0

    arr[1] = 1

    arr[2] = 4

    string[] a1;//stringa1

    string[,] a2;//stringa2

    a2=new string[2,3];

    a2[1,2]="abc";

    string[,,] a3;//stringa3

    string[][] j2;//

    string[][][][] j3;

    int[] a1=new int[]{1,2,3};//3

    int[] a2=new int[3]{1,2,3};//

    int[] a3={1,2,3};//int[] a3=new int[]{1,2,3};

    int[,] a4=new int[,]{{1,2,3},{4,5,6}};//a4[1,1]=5

    int[][] j2=new int[3][];//j2,

    j2[0]=new int[]{1,2,3};//

    j2[1]=new int[]{1, 2, 3, 4, 5, 6};//

    j2[2]=new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};

    1.4.10 (string)

    C# string

    SystemSystem.Stringstring

    string

    l

    string s;//s

    s="Zhang";//s"Zhang"

    string FirstName="Ming";

    string LastName="Zhang";

    string Name=FirstName+" "+LastName;//+

    string SameName=Name;

    char[] s2={'','','','',''};

    string s3=new String(s2);

    l

    string s="ABC";

    int i=s.IndexOf("");

    ""0"A"0""3

    i=3i=-1C#ASCII2

    l

    string s1="abc";

    string s2="abc";

  • 19

    int n=string.Compare(s1,s2);//n=0

    n=0ns1s2

    string s1="abc";

    string s="abc";

    string s2="";

    if(s==s1)//!=String

    s2="";

    l

    string s="";

    string s1="";

    if(s.Length==0)

    s1="";

    l

    string s="";

    string sb=s.Substring(2,2);//22Sb="",s

    char sb1=s[0];//sb1=''

    Console.WriteLine(sb1);//

    l

    string s="";

    string sb=s.Remove(0,2);//02Sb="",s

    l

    string s="";

    string s1=s.Insert(3,"");//s1=""s

    l

    string s="";

    string s1=s.Replace("","");//s1=""s

    l String

    string S="";

    char[] s2=S.ToCharArray(0,S.Length);//Length

    l

    int i=9;

    string s8=i.ToString();//s8="9"

    float n=1.9f;

    string s9=n.ToString();//s8="1.9"

    l

    string s="AaBbCc";

    string s1=s.ToLower();//s

    string s2=s.ToUpper();//s

    l

    string s="A bc ";

    s.Trim();//

    string,Visual Studio.Net

  • 20

    stringstringF1

    1.4.11

    C#C#

    C#

    (boxing)(unboxing)

    int

    long

    int i=10;

    long l=i;

    long l=5000;

    int i=(int)l;//int

    (boxing)(unboxing)

    (boxing)(unboxing)C#

    object()object()

    object

    1

    object

    object

    int i=10;

    object obj=i;//objobject

    int i =10;

    object obj=object(i);//

    object

    using System

    class Test

    { public static void Main()

    { int n=200;

    object o=n;

    o=201;//n

    Console.WriteLine("{0},{1}",n,o);

    }

    }

    200201nobjecto

    2.

  • 21

    object

    int i=10;

    object obj=i;

    int j=(int)obj;//

    3.

    void Display(Object o)//oObject

    { int x=(int)o;//

    System.Console.WriteLine("{0},{1}",x,o);

    }

    int y=20;Display(y);Object o=y

    Object

    1.5

    C#C

    1.5.1

    CC#

    l -X++XX--

    l x+y

    l x? y:z

    C#

    (x) x.y f(x) a[x] x++ x-- new type of sizeof checked unchecked

    + - ! ~ ++x x (T)x

    * / %

    + -

    >

    < > = is as

    == !=

    &

    |

    &&

    ||

  • 22

    ?:

    = *= /= %= += -= = &= = |=

    1.5.2 is

    ise is Te

    TeT

    using System;

    class Test

    { public static void Main()

    { Console.WriteLine(1 is int);

    Console.WriteLine(1 is float);

    Console.WriteLine(1.0f is float);

    Console.WriteLine(1.0d is double);

    }

    }

    True

    False

    True

    True

    1.5.3 typeof

    typeofsystem

    using System;

    class Test

    { static void Main()

    { Console.WriteLine(typeof(int));

    Console.WriteLine(typeof(System.Int32));

    Console.WriteLine(typeof(string));

    Console.WriteLine(typeof(double[]));

    }

    }

    intSystem.int32

    System.Int32

    System.Int32

    System.String

    System.Double[]

  • 23

    1.5.4 checked unchecked

    (+-*/)

    checkedunchecked

    checked

    unchecked

    unchecked

    checkedunchecked

    using System;

    class Class1

    { static void Main(string[] args)

    { const int x=int.MaxValue;

    unchecked//

    { int z=x*2;//z=-2

    Console.WriteLine("z={0}",z);//-2

    }

    checked//

    { int z1=(x*2);//

    Console.WriteLine("z={0}",z1);

    }

    }

    }

    1.5.5 new

    new

    int x=new int();//newx

    Person C1=new Person ();//newPersonPerson C1

    int[] arr=new int[2];//arr

    int x=new int()intx

    0x

    1.5.6

    x+y*zx+(y*z)*+

    1.5.1

    x+y-z

    x+y-z(x+y)-z

    x=y=zx=(y=z)

  • 24

    1.6

    C#CC#ifswith

    whiledowhileforforeachbreakcontinuegoto

    returnforeachC#

    CC#

    1.6.1 C

    l Cifwhiledowhilefor

    0falsetrue

    l switchCC++switchcasebreak,

    C#,casebreakgoto

    casecaseswitch,

    sbytebyteshortushortuintlongulongchar

    stringcase

    caseswitch

    switchcase

    casecasedafault

    dafaultswitchswitch

    dafaultdafault

    using System;

    class class1

    { static void Main()

    { System.Console.WriteLine("");

    string s=System.Console.ReadLine();

    string s1="";

    switch(s)

    { case "1": case "3": case "5":

    case "7": case "8": case "10":

    case "12"://

    s1="31";break;

    case "2":

    s1="28";break;

    case "4": case "6": case "9":

    goto case "11";//goto

    case "11":

    s1="30";break;

    default:

    s1="";break;

  • 25

    }

    System.Console.WriteLine(s1);

    }

    }

    1.6.2 foreach

    foreachC#CC++Visual Basic

    foreach

    foreach( in )

    using System;

    class Test()

    { public static void Main()

    { int[] list={10,20,30,40};//

    foreach(int m in list)

    Console.WriteLine("{0}",m);

    }

    }

    foreach0

    breakcontinue

    foreach

    1.6.3

    C#

    C#trycatchfinallytry

    catch

    catchcatch

    System.Exception

    catchcatch

    catch

    catchcatch

    finallyfinally

    trycatch

    catch

    catchcatch

  • 26

    catchcatch

    finally

    l try catchcatch

    l try -finally

    l try -catch-finallycatch

    1 trycatch-finally

    using System

    using System.IO//

    public class Example

    { public static void Main()

    { StreamReader sr=null;//null,

    try

    { sr=File.OpenText("d:\\csarp\\test.txt");//

    string s;

    while(sr.Peek()!=-1)

    { s=sr.ReadLine();//

    Console.WriteLine(s);

    }

    }

    catch(DirectoryNotFoundException e)//

    { Console.WriteLine(e.Message);

    }

    catch(FileNotFoundException e)//

    { Console.WriteLine(""+e.FileName+"");

    }

    catch(Exception e)//

    { Console.WriteLine("{0}",e.Message);

    }

    finally

    { if(sr!=null)

    sr.Close();

    }

    }

    }

    2 try -finally

    catchfinally

    3 try -catch

    try-catchcatch

  • 27

    1.7

    1.3Person

    EmployeePerson

    PersonEmployeeEmployeePerson

    PersonEmployee

    EmployeePersonC#C#

    1.7.1

    class : {}

    Employee

    class Employee:Person//Person

    { private string department;//

    private decimal salary;//

    public Employee(string Name,int Age,string D,decimal S):base(Name,Age)

    {//base

    department=D;

    salary=S;

    }

    public new void Display()//Display(),new,override

    { base.Display();//base

    Console.WriteLine("{0} {1}",department,salary);

    }

    }

    class Class1

    { static void Main(string[] args)

    { Employee OneEmployee=new Employee("",30,"",2000);

    OneEmployee.Display();

    }

    }

    EmployeePersonSetName()SetAge()nameage

    PersonEmployee

    departmentsalaryDisplay()Employee

    PersonnameageEmployee

    nameageSetName()SetAge()

    Employeenameageprotected

  • 28

    1.7.2 base

    base

    l

    base

    Employeebase

    l Employee

    Display()base

    1.7.3

    Display()

    new

    base.Display()

    1.7.4 C#

    C#

    l C#

    l C#CBBACB

    A

    l

    l

    l

    base.

    l

    EmployeePerson

    C#

    1.8

    C#CC++

    :

    1.8.1

  • 29

    l forswitch

    l

    l

    l getSet

    l

    l

    l

    l

    1.8.2

    C# privateprotectedpublic

    internal4Private

    protected

    public

    internal

    (Application)(Library)

    1.9

    1.9.1

    static

    . static

    . const

    readonly

    public class Test

    { public const int intMax=int.MaxValue;//

    public int x=0;//

    public readonly int y=0;//

  • 30

    public static int cnt=0;//

    public Test(int x1,int y1)//

    { //intMax=0;//

    x=x1;//

    y=y1;//

    cnt++;//

    }

    public void Modify(int x1,int y1)

    { //intMax=0;//

    x=x1;

    cnt=y1;

    //y=10;//

    }

    }

    class Class1

    { static void Main(string[] args)

    { Test T1=new Test(100,200);

    T1.x=40;//.

    Test.cnt=0;//.

    int z=T1.y;//

    z=Test.intMax;//

    }

    }

    1.9.2

    C#

    C#

    getset

    Personnameage

    SetNameSetAge

    using System;

    public class Person

    { private string P_name="";//P_name

    private int P_age=12;//P_age

    public void Display()//

    { Console.WriteLine(":{0},{1}",P_name,P_age);

    }

    public string Name//Name

    { get

    { return P_name;}

  • 31

    set

    { P_name=value;}

    }

    public int Age//Age

    { get

    { return P_age;}

    set

    { P_age=value;}

    }

    }

    public class Test

    { public static void Main()

    { Person OnePerson= new Person();

    OnePerson.Name="";//value=""setP_Name

    string s=OnePerson.Name;//getP_Name

    OnePerson.Age=20;//

    int x=OnePerson.Age;//

    OnePerson.Display();

    }

    }

    set

    getsetget

    1.10

    1.10.1

    :

    (){}

    newpublicprotectedinternalprivatestaticvirtualsealedoverride

    abstractextern

    C#void( 1,

    2,...),C

    1.10.2

    C#()

  • 32

    l

    l ref

    l out

    l params

    1

    2

    C

    C#

    ref

    3

    ()

    ()

    using System;

    class g{public int a=0;}//

    class Class1

    { public static void F1(ref char i)//

    { i='b';}

    public static void F2(char i)//

    { i='d';}

    public static void F3(out char i)//

    { i='e';}

    public static void F4(string s)//

    { s="xyz";}

    public static void F5(g gg)//

    { gg.a=20;}

    public static void F6(ref string s)//

    { s="xyz";}

    static void Main(string[] args)

    { char a='c';

    string s1="abc";

    F2(a);//a

    Console.WriteLine(a);//ac

    F1(ref a);//,a

    Console.WriteLine(a);//abb

    Char j;

    F3(out j);//j

    Console.WriteLine(j);//e

  • 33

    F4(s1);//s1

    Console.WriteLine(s1);//abc,s1

    g g1=new g();

    F5(g1);//

    Console.WriteLine(g1.a.ToString());//:20

    F6(ref s1);//s1

    Console.WriteLine(s1);//xyzs1

    }

    }

    4

    params

    string[]string[][]

    refout

    using System;

    class Class1

    { static void F(params int[] args)//params

    { Console.Write("Array contains {0} elements:",args.Length);

    foreach (int i in args)

    Console.Write(" {0}",i);

    Console.WriteLine();

    }

    static void Main(string[] args)

    { int[] a = {1,2,3};

    F(a);//a

    F(10, 20, 30, 40);//F(new int[] {60,70,80,90});

    F(new int[] {60,70,80,90});//

    F();//F(new int[] {});

    F(new int[] {});//

    }

    }

    Array contains 3 elements: 1 2 3

    Array contains 4 elements: 10 20 30 40

    Array contains 4 elements: 60,70,80,90

    Array contains 0 elements:

    Array contains 0 elements:

    params

    using System;

    class Class1

    { static void F(int[,] args)//params

    { Console.Write("Array contains {0} elements:",args.Length);

    foreach (int i in args)

    Console.Write(" {0}",i);

    Console.WriteLine();

  • 34

    }

    static void Main(string[] args)

    { int[,] a = {{1,2,3},{4,5,6}};

    F(a);//a

    //F(10, 20, 30, 40);//

    F(new int[,] {{60,70},{80,90}});//

    //F();//

    //F(new int[,] {});//

    }

    }

    Array contains 3 elements: 1 2 3 4 5 6

    Array contains 4 elements: 60,70,80,90

    1.10.3

    static static

    .

    .

    this

    .

    using System;

    public class UseMethod

    { private static int x=0;//

    private int y=1;//

    public static void StaticMethod()//

    { x=10;//

    //y=20;//

    }

    public void NoStaticMethod()//

    { x=10;//

    y=20;//

    }

    }

    public class Class1

    { public static void Main()

    { UseMethod m=new UseMethod();

    UseMethod.StaticMethod();//.

    m.NoStaticMethod();//.

    }

    }

  • 35

    1.10.4

    C#

    Person

    C

    abc()labs()

    fabs() C#

    using System;

    public class UseAbs

    { public int abs(int x)//

    { return(x=

  • 36

    class Complex//

    { private double Real;//

    private double Imag;//

    public Complex(double x,double y)//

    { Real=x;

    Imag=y;

    }

    static public Complex operator - (Complex a)//,1

    { return (new Complex(-a.Real,-a.Imag));}

    static public Complex operator +(Complex a,Complex b)//

    { return (new Complex(a.Real+b.Real,a.Imag+b.Imag));}

    public void Display()

    { Console.WriteLine("{0}+({1})j",Real,Imag);}

    }

    class Class1

    { static void Main(string[] args)

    { Complex x=new Complex(1.0,2.0);

    Complex y=new Complex(3.0,4.0);

    Complex z=new Complex(5.0,7.0);

    x.Display();//:1+(2)j

    y.Display();//:3+(4)j

    z.Display();//:5+(7)j

    z=-x;//z=opeator-(x)

    z.Display();//-1+(-2)j

    z=x+y;//z=opeator+(x,y)

    z.Display();//4+(6)j

    }

    }

    1.10.6 this

    Person

    Person P1=new Person("",30);

    Person P2=new Person("",40);

    P1.Display()P2.Display()

    Display()C#

    this Display() Display()this

    () this

    this

    P1.Display()this P1

    P2.Display()this P2

  • 37

    1.11

    C#,

    C#C++

    C#

    virtualC#C++

    using System;

    class A

    { public void F()//

    { Console.Write(" A.F");}

    public virtual void G()//

    { Console.Write(" A.G");}

    }

    class B:A//AB

    { new public void F()//F()new

    { Console.Write(" B.F");}

    public override void G()//G()override

    { Console.Write(" B.G");}

    }

    class Test

    { static void F2(A aA)//A

    { aA.G();}

    static void Main()

    { B b=new B();

    A a1=new A();

    A a2=b;//a2Bb

    a1.F();//AF(),A.F

    a2.F();//F()AF(),A.F

    b.F();//F()BF(),B.F

    a1.G();//G()a1AAG()A.G

    a2.G();//G()a2BBG()B.G

    F2(b);//B,A aA=bBG(),B.G

    F2(a1);//A,AG(),A.G

    }

    }

    A.F A.F B.F A.G B.G B.G A.G

    F()G()a2

    bG()a2.G()BG()G.F

  • 38

    F()a2.F()AF()A.F

    F2(A aA)AF2(b)A aA=b

    aAbaA.G()BG(),B.GF2(a1)

    A,AG(),A.G

    PersonDisplay()

    EmployeeDisplay()

    using System;

    public class Person

    { private String name="";//

    private int age=12;

    protected virtual void Display()//

    { Console.WriteLine(":{0},{1}",name,age);

    }

    public Person(string Name,int Age)//,,

    { name=Name;

    age=Age;

    }

    static public void DisplayData(Person aPerson)//

    { aPerson.Display();//Display()

    }

    }

    public class Employee:Person//Person

    { private string department;

    private decimal salary;

    public Employee(string Name,int Age,string D,decimal S):base(Name,Age)

    { department=D;

    salary=S;

    }

    protected override void Display()//override

    { base.Display();//

    Console.WriteLine("{0} {1} ", department,salary);

    }

    }

    class Class1

    { static void Main(string[] args)

    { Person OnePerson=new Person("",30);

    Person.DisplayData(OnePerson);//

    Employee OneEmployee=new Employee("",40,"",2000);

    Person.DisplayData(OneEmployee); //

  • 39

    }

    }

    : ,30

    : ,40

    2000

    1.12

    abstract

    l

    l abstract

    l

    l

    abstract class Figure//

    { protected double x=0,y=0;

    public Figure(double a,double b)

    { x=a;

    y=b;

    }

    public abstract void Area();//

    }

    class Square:Figure///Square

    { public Square(double a,double b):base(a,b)

    {}

    public override void Area()//newoverride

    { Console.WriteLine("{0}",x*y);}

    }

    class Circle:Figure///Square

    { public Circle(double a):base(a,a)

    {}

    public override void Area()

    { Console.WriteLine("{0}",3.14*x*y);}

    }

    class Class1

    { static void Main(string[] args)

    { Square s=new Square(20,30);

    Circle c=new Circle(10);

    s.Area();

    c.Area();

  • 40

    }

    }

    600

    314

    FigureArea()SquareCircle

    FigureArea()

    Person

    C++C++

    C#

    1.13

    C#

    (sealed class),

    sealed

    C#

    C#(sealed method)sealed

    1.14

    C#

    1.14.1

    interface {}

    interface

    newpublicprotectedinternalprivate

    public interface IExample

    {//

    string this[int index] {get;set;}//

    event EventHandler E;//

    void F(int value);//

  • 41

    string P { get; set;}//

    }

    l

    l public

    1.14.2

    using System;

    interface IControl

    { void Paint();

    }

    interface ITextBox:IControl//IcontrolPaint()

    { void SetText(string text);

    }

    interface IListBox:IControl//IcontrolPaint()

    { void SetItems(string[] items);

    }

    interface IComboBox:ITextBox,IListBox

    {//

    }

    ITextBoxIListBoxIControl

    IControlPaintIComboBoxITextBoxIListBox

    ITextBoxSetTextIListBoxSetItemsIControlPaint

    1.14.3

    PersonPerson

    Person

    C#

    using System;

    public interface I_Salary//

    { decimal Salary//

  • 42

    { get;

    set;

    }

    }

    public class Person

    {//1.9.2Person

    }

    public class Employee:Person,I_Salary//Person,I_Salary

    {//

    private decimal salary;

    public new void Display()

    { base.Display();

    Console.WriteLine("{0} ",salary);

    }

    //Salary

    public decimal Salary

    { get

    { return salary;}

    set

    { salary=value;}

    }

    }

    public class Test

    { public static void Main()

    { Employee S=new Employee();

    S.Name="";//Name

    S.Age=20;//Age

    S.Salary=2000;//Salary

    S.Display();

    }

    }

    1.15

    C#----(delegate)

    CC#

    delegate ();

    newpublicprotectedinternalprivate

    intMyDelegate

    public delegate int MyDelegate();//int

    MyDelegateMyDelegate

  • 43

    int

    using System;

    delegate int MyDelegate();//

    public class MyClass

    { public int InstanceMethod()//int

    { Console.WriteLine("");

    return 0;

    }

    static public int StaticMethod()//int

    { Console.WriteLine("");

    return 0;

    }

    }

    public class Test

    { static public void Main ()

    { MyClass p = new MyClass();

    //newMyDelegatedInstanceMethod

    MyDelegate d=new MyDelegate(p.InstanceMethod);//

    d();//

    //newMyDelegatedStaticMethod

    d=new MyDelegate(MyClass.StaticMethod);//

    d();//

    }

    }

    1.16

    C#

    1.16.1

    WindowsC#

    Windows

    C#Windows

    Click

  • 44

    1.16.2

    C#

    ButtonC#

    ButtonButton

    public delegate void EventHandler(object sender,EventArgs e);//

    //EventHandler(object sender,EventArgs e)

    public class Button:Control//Button

    {//Button

    public event EventHandler Click;//Click

    protected void OnClick(EventArgs e)//ClickOnClick

    { if(Click!=null)//Click

    Click(this,e);

    }

    public void Reset()

    { Click=null;}

    }

    Click()OnClickClick

    ButtonEventHandlerClick

    ClickClick

    1.16.3

    Button

    OKCancel

    public class LoginDialog: Form//

    { Button OkButton;

    Button CancelButton;

    public LoginDialog()//

    { OkButton=new Button();//OkButton

    //ClickOkButtonClick+=

    OkButton.Click+=new EventHandler(OkButtonClick);

    CancelButton=new Button();//OkButton

    CancelButton.Click += new EventHandler(CancelButtonClick);

    }

    void OkButtonClick(object sender, EventArgs e)

    {//OkButton.Click

    }

    void CancelButtonClick(object sender, EventArgs e)

    {//CancelButton.Click

  • 45

    }

    }

    ButtonClick

    OkButton.Click+=new EventHandler(OkButtonClick)

    OkButton.ClickOkButtonClickClick

    OkButtonClickOkButtonClick

    OkButton.Click-=new EventHandler(OkButtonClick)OkButton.Click

    ClickOkButtonClick

    Visual Studio.Net

    1.17

    C#int[] arr=new int[5]

    arr()

    arr[]

    (indexer)

    using System

    class Team

    { string[] s_name = new string[2];//

    public string this[int nIndex]//thisTeam

    { get//[]get

    { return s_name[nIndex];

    }

    set//[]set

    { s_name[nIndex] =value;//value

    }

    }

    }

    class Test

    { public static void Main()

    { Team t1 = new Team();

    t1[0]="";

    t1[1]="";

    Console.WriteLine("{0}{1}",t1[0], t1[1]);

    }

    }

  • 46

    1.18

    C#

    Cinclude

    1.18.1

    namespaceusing

    using

    public

    using System;

    namespace N1//N1using

    { namespace N2//N1N2

    { class A//N2

    { void f1(){};}

    class B

    { void f2(){};}

    }

    }

    :

    namespace N1.N2//ABN1.N2

    { class A

    { void f1(){};}

    class B

    { void f2(){};}

    }

    namespace N1.N2//AN1.N2

    { class A

    { void f1(){};}

    }

    namespace N1.N2//BN1.N2

    { class B

    { void f2(){};}

    }

  • 47

    1.18.2

    using

    f1()f2(),

    using N1.N2;

    class WelcomeApp

    { A a=new A();

    a.f1();

    }

    using N1.N2A1.2.1

    1.19

    CC++

    intfloat

    Windows

    C#

    WindowsAPIC#

    (unsafe)

    unsafe void F1(int * p){}unsafe int* p2=p1;

    unsafe{ int* p2=p1;int* p3=p4;}

    csc C# /unsafe

    1. (string

    string s=Console.ReadLine())

    2.

    3.

    4.

    5.

    6. C

    7.

    8.

    9.

    10. 53

  • 48

    11. 13

    switch

    12. C#forfor(int i;i

  • 49

    Windows

    2.1

    Windows

    /

    C#

    2.2 Windows

    2.2.1

    Windows dos(),

    dosCPU

    dosCPU

    Windows

    CPU

    WindowsWindows

    ()

    Windows

    Windows

    2.2.2

    WindowsWindows

    Windows

    ()

    2.2.3

    Windows

  • 50

    Windows()

    2.3 Windows

    2.3.1 Windows(API)

    API(Application Programming Interface)Windows982000XP

    CWindows

    C APIWindows

    API

    2.3.2 MFC

    APIC

    APIWindowsVC++6.0API

    MFCMFCWindowsMFC

    VC++

    2.3.3

    Windows()

    Windows

    VB6.0VB.NetC#C++BuilderJavaDelphi

    (RAD)

    2.3.4 .NET

    .NET Windows Web Web .Net(.Net

    FrameWork)(Base Class Library)

    Windows

    Web

  • 51

    .Net.NetVC++.Net

    VB.NetC#

    ()I/O

    XMLWebWebWindows

    Windows982000 XP.NET C#

    .Net FrameWork

    2.4 Windows

    Windows Main()

    Main()Windows

    (Form)Windows

    2.4.1 Windows

    Windows

    using System;//

    using System.Windows.Forms;

    public class Form1:Form//

    { static void Main()//

    { Application.Run(new Form1());

    }

    }

    Form1FormForm.NetForm

    Windows

    FormForm

    System.ApplicationRun

    Form1Run

    RunWindows

    d:\Charpe1.cs

    d:cd Charp

    C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc /t:winexe

    /r:system.dll,System.Windows.Forms.dll e1.cs

    /t:winexe Windows /r

    g.bat

    g.bat

    FrameWork SDK 2000 e1.cs

    e1.exe

    e1.exeCRT

    Form1

    Form1

    Form1

    Form1 Form1 Form1

  • 52

    Form1

    (Button)

    using System;

    using System.Windows.Forms;

    public class Form1:Form

    { Button button1;//Button

    public Form1()//

    {//() Form1()

    Text="";//this.Text="";

    button1=new Button();//Button

    button1.Location=new Point(25,25);//button1location

    button1.Text="";//button1Text

    //button1_Click

    button1.Click+=new System.EventHandler(button1_Click);

    this.Controls.Add(button1);//

    }

    static void Main()

    { Application.Run(new Form1());

    }

    private void button1_Click(object sender, System.EventArgs e)

    {//

    this.button1.Text="";//

    }

    }

    button1 Form1

    new Button

    button1 button1

    2.4.2 Visual Studio.Net Windows

    Visual Studio.Net

    Visual Studio.NetWindows

    (1) Visual Studio.Net 1.2.2A

    (2) 1.2.2B(P) Visual C#

    (T) Windows (N) e2(L)

    D:\csarp

    2.4.2A(Form1)

  • 53

    2.4.2A

    (3) e28Form1.csForm1

    (C) Form1.csVisual Studio.Net

    Foem1.cs Visual Studio.NetWindows

    using System;//

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    namespace e2/////

    { //

    ///

    /// Form1

    ///

    public class Form1 : System.Windows.Forms.Form//Forme1

    { //

    ///

    ///

    ///

    private System.ComponentModel.Container components = null;

    public Form1()//

    {

    //

    // Windows

    //

  • 54

    InitializeComponent();//

    //

    // TODO: InitializeComponent

    //InitializeComponent()

    }

    ///

    ///

    ///

    protected override void Dispose( bool disposing )

    {

    if( disposing )

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    base.Dispose( disposing );

    }

    #region Windows Form Designer generated code

    ///

    /// -

    ///

    ///

    private void InitializeComponent()

    { //

    //

    // Form1

    //

    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

    this.ClientSize = new System.Drawing.Size(292, 273);

    this.Name = "Form1";//this Form1

    this.Text = "Form1";

    }

    #endregion

    ///

    ///

    ///

    [STAThread]

    static void Main()//

    {

    Application.Run(new Form1());//

    }//

    }

  • 55

    }

    (4) 2.4.2A

    Forms.cs[] Forms.cs[]

    /( 2.4.2B

    ) Windows Button,

    Forms.cs[]Form1Button

    ButtonForm1(

    2.4.2B)

    Button Text 4(

    2.4.2B)Button

    Click

    2.4.2B

    Foem1.cs

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    namespace e2

    {

    ///

    /// Form1

    ///

    public class Form1 : System.Windows.Forms.Form

    {

    private System.Windows.Forms.Button button1;//Button

    ///

    ///

    ///

    private System.ComponentModel.Container components = null;

  • 56

    public Form1()

    {

    //

    // Windows

    //

    InitializeComponent();

    //

    // TODO: InitializeComponent

    //

    }

    ///

    ///

    ///

    protected override void Dispose( bool disposing )

    {

    if( disposing )

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    base.Dispose( disposing );

    }

    #region Windows Form Designer generated code

    ///

    /// -

    ///

    ///

    private void InitializeComponent()

    {

    this.button1 = new System.Windows.Forms.Button();//

    this.SuspendLayout();

    //

    // button1

    //

    this.button1.Location = new System.Drawing.Point(96, 56);//

    this.button1.Name = "button1";

    this.button1.Size = new System.Drawing.Size(72, 32);

    this.button1.TabIndex = 0;

    this.button1.Text = "";

    this.button1.Click += new System.EventHandler(this.button1_Click);//

    //

    // Form1

  • 57

    //

    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

    this.ClientSize = new System.Drawing.Size(292, 273);

    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});

    this.Name = "Form1";

    this.Text = "Form1";

    }

    #endregion

    ///

    ///

    ///

    [STAThread]

    static void Main()

    {

    Application.Run(new Form1());

    }

    private void button1_Click(object sender, System.EventArgs e)

    {//

    }

    }

    }

    ButtonButtonbutton1Form1

    Form1

    button1Form1

    newButton

    button1button1button1_Click()

    this.button1.Click += new System.EventHandler(this.button1_Click)

    Button1Clickbutton1_Click()

    button1_Click()

    2.4.1

    2.4.3 (Solution)(Project)

    (Application)

    Visual Studio.Net

    ( 2.4.3)

    e2

  • 58

    e28

    l bin debug C#

    l obj

    l AssemblyInfo.cs

    DLL

    l Form1.cs

    l Form1.resx

    l e2.suo

    l e2.csproj

    l e2.sln

    Visual Studio.Net

    e2 e2

    /

    Visual

    Studio.Net

    ( csproj)/

    2.4.3

    (1)Windowsdos

    (2)

    (3)Windows.NETAPI

    (4)C#

    (5)

    (6)

    (7)

    (8)

  • 59

    Visual Studio.Net( VS.NET)() Windows VS.NET

    3.1

    LabelButtonTextBox ControlControl

    l Name Button

    Button button1=new Button() Name button1

    l Locationx

    y x yx

    y Location

    button1.Location=new Point(100,200) button1

    l Left Top Location X Y Left Top

    button1.Left=100 button1

    l SizeWidth Height

    button1.Size.Width=100 Button button1

    l BackColor

    l Enabled true false

    l Visible true false

    l Modifier privatepublicprotected private

    l Cursor Default

    3.2 Form

    Form.Net(WinForm)System.Windows.Forms

    FormWindows

    FormForm

    1. Form

    l AutoScroll

    l FormBorderStyle3D

    l Text

    l AcceptButton

    l CanceButton ESC

  • 60

    l MaxiMizeBox false

    l MiniMizeBox false

    MaxiMizeBoxMiniMizeBox false

    false

    2. Form

    l Close()

    l Hide() Show()

    l Show()

    3. Form

    l Load

    3.3 (Label)

    1. Label

    l Text

    l AutoSize false

    l ForeColorLabel

    l Font

    2. e3_3

    Visual Studio.Net

    C# Windows

    (1)(Form1) 2.4.2A( 2.4.2B

    ) Form1 Text

    (2)( 2.4.2B)Windows Label,Form1

    LabelLabel

    Label

    (3) Label textLabel1

    FontFont

    Label

    Font+ Font-

    Font ForeColor Label

    (4)

    (5)

    /

    (6) VS.NET/ sln

  • 61

    3.4 (Button)

    1. Button

    l Text

    l Click

    2. e3_4

    2

    (1) ButtonText

    (2)( 2.4.2B

    )

    Click(Click)

    private void button1_Click(object sender, System.EventArgs e)

    { label1.ForeColor=Color.Red;//

    }// label1(label Name)

    (3)(Click)

    private void button2_Click(object sender, System.EventArgs e)

    { label1.ForeColor=Color.Black;}

    (4)(Click)

    private void button3_Click(object sender, System.EventArgs e)

    { Close();}

    Close()(Form)

    label1.ForeColor=Color.Red;

    (5)

    3.5

    (object sender)

    Namesenderbutton1

    private void button1_Click(object sender,System.EventArgs e)

    { if(sender==button1)

    label1.ForeColor=Color.Red;

    else

    label1.ForeColor=Color.Black;

  • 62

    }

    (System.EventArgs e)

    e.X

    e.Yx ye.Button

    MouseButtons.Left

    button1_Click

    (2.4.2B)Click

    button1_Click

    3.6 (TextBox)

    TextBox

    1. TextBox

    l Text

    l MaxLength

    l ReadOnly true

    l PasswordChar

    l MultiLine true false

    l ScrollBarsMultiLine=true 4=0=1

    =2=3

    l SelLength

    l SelStart

    l SelText

    l AcceptsReturnMultiLine=true true

    false

    l TextChanged

    2. e3_6

    (1) LabelText

    *,=

    (2)textBoxName

    textBox1textBox2textBox3Text

    (3)ButtonText

    (4):

    private void button1_Click(object sender, System.EventArgs e)

    { float ss,ee;

    ss=Convert.ToSingle(textBox1.Text);

    ee=Convert.ToSingle(textBox2.Text);

    textBox3.Text=Convert.ToString(ss*ee);

    }

    (5):

  • 63

    private void button2_Click(object sender,System.EventArgs e)

    { textBox1.Text="";

    textBox2.Text="";

    textBox3.Text="";

    }

    (6):

    private void button3_Click(object sender, System.EventArgs e)

    { Close();}

    (7) textBox1textBox223

    textBox36

    3.7 Convert

    Convert

    Convert.ToSingle(textBox1.Text) textBox1.Text

    Convert.ToString(3.14) 3.14ToInt

    ToInt16

    3.8 (RadioButton) GroupBox

    RadioButton RadioButton

    RadioButtonGroupBox

    Text

    RadioButtonGroupBox RadioButton

    RadioButton GroupBox

    1. GroupBox

    GroupBox Text GroupBox

    2. RadioButton

    l Text

    l Checked true false

    l CheckedChanged

    l Click

    3. e3_8

    RadioButtonLabel

    (1)

    (2) Label,Text=

    (3) GroupBox,Text=

    (4) RadioButtonGroupBox,Text

    RadioButton

    Checked=true

    (5) RadioButton CheckedChanged

  • 64

    private void radioButton1_CheckedChanged(object sender, System.EventArgs e)

    { if(radioButton1.Checked)

    label1.Font=new Font("",label1.Font.Size);

    }//label1

    private void radioButton2_CheckedChanged(object sender, System.EventArgs e)

    { if(radioButton2.Checked)

    label1.Font=new Font("",label1.Font.Size);

    }

    private void radioButton3_CheckedChanged(object sender, System.EventArgs e)

    { if(radioButton3.Checked)

    label1.Font=new Font("_GB2312",label1.Font.Size);

    }

    (6) RadioGroup1RadioButton

    Click

    3.9 Font

    Fontnew Font(,)label1.Font=new

    Font("",9), e3_8new Font(,)

    enum FontStyle{

    Regular =0,//

    Bold =1,//

    Italic =2,//

    BoldItalic =3,//

    Underline =4,//5=6=7=

    Strikeout =8}//9=10=

    label1.Font=new Font("",9,label1.Font.Style|FontStyle.Italic);

    label1.Font=new Font("",9,label1.Font.Style|(FontStyle)2);

    label1.Font=new Font("",9,label1.Font.Style&~FontStyle.Italic);

    label1.Font=new Font("",9,label1.Font.Style&(FontStyle)(~2));

    e3_11

    3.10 (CheckBox)

    CheckBoxCheckBoxGroupBox

    CheckBox

    1. CheckBox

    l Text

    l Checked true false

  • 65

    l Click

    l CheckedChanged

    2. e3_10A

    2CheckBox

    CheckBoxLabel

    (1) LabelText=

    (2)GroupBoxText=CheckBoxGroupBox

    Text

    (3)CheckedChanged

    private void checkBox1_CheckedChanged(object sender, System.EventArgs e)

    { String text1="";

    if(checkBox1.Checked)

    text1=text1+checkBox1.Text;

    if(checkBox2.Checked)

    text1+=checkBox2.Text;

    label1.Text=text1;

    }

    (4)CheckedChanged

    CheckedChanged3.5

    (5)

    3. e3_10B

    (1)Form1String s=""

    (2) LabelGroupBoxCheckBox

    (4)CheckBox1CheckedChanged

    private void checkBox1_CheckedChanged(object sender,System.EventArgs e)

    { int n=s.IndexOf("");//s""?=-1

    if(n==-1)//=-1""

    s+="";

    else//""

    s=s.Remove(n,2);

    label1.Text=s;

    }

    (5)CheckBox2CheckedChanged

    private void checkBox2_CheckedChanged(object sender,System.EventArgs e)

    { int n=s.IndexOf("");//s""?=-1

    if(n==-1)//=-1""

    s+="";

    else//""

    s=s.Remove(n,2);

    label1.Text=s;

    }

    (6)

  • 66

    3.11 (ListBox)

    1.

    l Items ListBox ArrayList

    l SelectedIndex 0

    -1

    l SelectedIndices

    l SelectedItem

    l SelectedItems

    l SelectionMode

    none( ) one( )MultiSimple( )

    MultiExtended()

    l Sorted false

    l GetSelected() true

    l SelectedIndexChanged

    ()

    2. e3_11

    (1) Label

    ,Text=

    (2) ListBox,

    Name=listBox1 ListBox

    Items

    (3) ListBox1SelectionModeMultiExtended

    (4) SelectedIndexChenged

    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

    { int Style=0,k=1;//Style=01=2=3=3.9

    for(int i=0;i

  • 67

    (5) Ctrl

    3.12 (ComboBox)

    ComboBox

    1. ComboBox

    l DropDownStyle Simple

    DropDown

    DropDownList

    l Items ComboBox ArrayList

    l MaxDropDownItems(1100)

    l Sorted false

    l SelectedItem

    Text

    l SelectedIndex

    0-1

    l SelectedIndexChanged

    2. e3_12 Windows

    ComboBox

    ComboBoxWindows

    (1) Label,Text=

    (2) ComboBoxName=comboBox1DropDownStyle=DropDownList

    (3) Form1 Load

    private void Form1_Load(object sender, System.EventArgs e)

    {//Families FontFamily

    FontFamily[] families=FontFamily.Families;//

    foreach (FontFamily family in families)

    comboBox1.Items.Add(family.Name);// Add

    }

    (4) comboBox1 SelectedIndexChenged

    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)

    {label1.Font=new Font(comboBox1.Text,9);}

    (5)

    3.13 ToolTip

    WindowsWord

    ToolTip ToolTip

  • 68

    e3_13 Button

    (1) ButtonNameButton1

    (2) toolTip Name=ToolTip1

    (3) Form1

    toolTip1.SetToolTip(button1,"");

    (4) Button

    3.14 (LinkLable)

    LinkLable Label Label

    LinkLableLinkClicked LinkLable

    1.

    l LinkColor

    l VisitedLinkColor

    l LinkVisitedtruefalse

    l LinkArea: LinkArea.Start

    LinkArea.Length

    l LinkClicked LinkLable

    2. e3_14: LinkLabel

    (1) LinkLabelText=

    (2) LinkLabelLinkArea.Length=2LinkArea.Start=2

    linkLabel1.LinkArea=new LinkArea(2,2);

    (3) LinkLabelLinkClicked

    private void linkLabel1_LinkClicked(object sender,

    System.Windows.Forms.LinkLabelLinkClickedEventArgs e)

    { linkLabel1.LinkVisited=true;

    System.Diagnostics.Process.Start("http://www.micosoft.com.cn");

    }

    (4)

    (5) CLinkLabel

    LinkClicked

    linkLabel1.LinkVisited=true;

    System.Diagnostics.Process.Start("C:/");

    (6) LinkClicked

    linkLabel1.LinkVisited=true;

    System.Diagnostics.Process.Start("notepad");

    3.15 (Timer)

    (Timer)

  • 69

    3.

    l Interval

    l Enabledtrue, Start()

    l Start() Stop()Enabled=false

    l Tick Interval Tick

    4. e3_15

    (1) TimerNametimer1

    (2) LabelNamelabel1

    (3) Form1 Load

    private void Form1_Load(object sender, System.EventArgs e)

    { this.timer1.Interval=100;

    this.timer1.Enabled=true;

    label1.Text=DateTime.Now.ToString();

    }

    (4) Timer1Tick

    private void timer1_Tick(object sender, System.EventArgs e)

    { label1.Text=DateTime.Now.ToString();

    }

    (5)

    3.16 DateTime

    DateTime

    l

    String s=DateTime.Now.ToString();//DateTime.Today.ToString()

    l

    int y=DateTime.Now.Year;//

    int m=DateTime.Now.Month;//

    int d=DateTime.Now.Day;//

    String s=DateTime.Now.DayOfWeek.ToString();//

    l

    int h=DateTime.Now.Hour;//

    int m=DateTime.Now.Minute;//

    int s=DateTime.Now.Second;//

    l DateTime199911335732.11

    System.DateTime moment=new System.DateTime(1999,1,13,3,57,32,11);

    l ()

    System.DateTime dTime=new System.DateTime(1980,8,5);//198085

    //17421

    System.TimeSpan tSpan=new System.TimeSpan(17,4,2,1);

    System.DateTime result=dTime+tSpan;//19808224:2:1 AM.

  • 70

    3.17

    Windows

    MainMenuwindows

    1.

    MainMenu

    ALT

    &(&0)

    (0)

    Ctrl+OCtrlO

    ShortCut

    2.

    MainMenuVisual Studio.Net

    MainMenu mainMenu1=new MainMenu();

    This.Menu=mainMenu1;//mainMenu1

    MainMenu

    MenuItem myFile=mainMenu1.MenuItem.Add((&F));//

    myFile.MenuItem.Add((&O));//

    Visual Studio.Net

    3.

    l Checked=true

    l ShortCut

    l ShowShortCuttrue()false

    l Text-&

    (&c)Alt+c

    l Click

    4. e3_17

    (1)Label

    (2)Mainmenu

    (3),

  • 71

    Text-

    (4)(Click)

    private void menuItem2_Click(object sender,System.EventArgs e)

    {label1.ForeColor=Color.Red;}//

    (5)(Click)

    private void menuItem3_Click(object sender, System.EventArgs e)

    {label1.ForeColor=Color.Black;}//

    (6)(Click)

    private void menuItem4_Click(object sender, System.EventArgs e)

    { Close();}//

    (7)

    3.18

    Windows

    1.

    ToolBar

    ButtonWord(ComboBox)

    ImageListButtonClick

    2. ToolBar

    l BorderStyle=None()=FixedSingle

    =Fixed3D

    l Button ToolBar

  • 72

    ToolBarButton()

    l ImageList ImageList

    ToolBar

    l Wrappable=true() Form

    =false

    l ButtonClickToolBar ButtonClick

    l ShowToolTips=true

    l IndexOF() ToolBar Name

    3. ToolBar ToolBarButton

    ToolBar ToolBarButton

    ToolBar ToolBarButton

    l ImageIndexToolBar ImageList ImageList

    ToolBar ImageList

    l Style 4=PushButton=Separator

    =ToggleButton

    =DropDownButton

    l TextToolBar Text

    l ToolTipText

    4. e3_18

    (1) ImageList

    (2) ToolBarImageList=ImageList1

    (3) ImageListImages Image

    ImageList

  • 73

    C:\Program Files\Microsoft Office\Office\forms\2052

    C:\program files\Microsoft Visual Studio.Net\Common7\Graphics\Icon\Misc

    .ico

    ImageList

    (4) ToolBarButtonsToolBarButton()

    ImageIndex

    ToolTipText

    ToolTipText

    (5) ToolBarShowToolTipstrue

    (6) ToolBarButtonClick

    private void toolBar1_ButtonClick(object sender,

    System.Windows.Forms.ToolBarButtonClickEventArgs e)

    { int n=toolBar1.Buttons.IndexOf(e.Button);//n

    switch(n)

    { case 0://

    this.menuItem3_Click(sender,e);

    break;

    case 1://

    this.menuItem2_Click(sender,e);

    break;

    }

    }

    (7)

    3.19 (StatusBar)

    Windows

    1.

    l Panels

    StatusBarPanels

    l ShowPanel=true=false

    2. (StatusBar)

    Text

    statusBar1.Text= 2

    statusBar1.Panels[1].Text= 2

    l Alignment

    l Text

    l Width

    l BorderStyle =None(

    )=Raised=Sunken

    3. e3_19

    (1) StatusBarStatusBarPanels

    StatusBarPanels()201

  • 74

    (2) StatusBarShowPanel=true

    (3) TimerName=Timer1Interval=1000Enabled=true

    (4) Timer1Tick

    private void timer1_Tick(object sender, System.EventArgs e)

    { statusBar1.Panels[0].Text=DateTime.Now.ToString();

    }

    (5) Form1MouseMove

    private void Form1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)

    {statusBar1.Panels[1].Text="X:"+e.X.ToString()+",Y:"+e.Y.ToString();

    }

    (6) 1

    2

    3.20

    System.Windows.Forms.Control Click

    Form

    l MouseDown

    l MouseUp

    l MouseMove

    l MouseEnter

    l MouseLeave

    (object sender)

    Name Form1 MouseDown Form1

    senderForm1(System.Windows.Forms.MouseEventArgs e)

    e.Xxe.Yye.Button

  • 75

    MouseButtons.LeftRightMiddle

    e.Clicks2

    e3_20 Label

    (20,20) 200 200

    (1) Label Name=label1

    (2) Panel Panel Location.X=20

    Location.Y=20Width=200Height=200Name=p1

    (3) PanelMouseDown

    private void p1_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)

    { if(e.Button==MouseButtons.Left&&e.Clicks>1)//

    label1.Text="X:"+e.X.ToString()+",Y:"+e.Y.ToString();

    }

    (4)

    3.21 (ContextMenu)

    Word

    e3.21

    button1

    button2 2

    1

    (1) Label

    (2) 2 ButtonText

    (3)

    private void button1_Click(object sender, System.EventArgs e)

    { label1.ForeColor=Color.Red;}

    (4)

    private void button2_Click(object sender, System.EventArgs e)

    { label1.ForeColor=Color.Black;}

    (5) 2 ContextMenuName contextMenu1contextMenu2

    (6) contextMenu1

    (7) contextMenu2

    Close();

    (8) ContextMenu

    contextMenu1Form ContextMenu contextMenu2

    (9) , contextMenu1

    contextMenu1

  • 76

    contextMenu2

    3.22

    (1) Form MaxiMizeBox=false MiniMizeBox=false

    FormBorderStyle=FixedDialog

    (2) textBoxName=textBox1Text="0"ReadOnly=true

    (3) 10Button9NameButton1-Button9

    Button0Text1234567890

    (4) 7ButtonNamebtn_dotbtn_equbtn_addbtn_subbtn_mul

    btn_divbtn_CText.=+-*/C

    (5) Button0

    private void button0_Click(object sender, System.EventArgs e)

    { if(sender==button0) append_num(0);

    if(sender==button1) append_num(1);

    if(sender==button2) append_num(2);

    if(sender==button3) append_num(3);

    if(sender==button4) append_num(4);

    if(sender==button5) append_num(5);

    if(sender==button6) append_num(6);

    if(sender==button7) append_num(7);

    if(sender==button8) append_num(8);

    if(sender==button9) append_num(9);

    }

    (6) Form1

    public void append_num(int i)

    { if(textBox1.Text!="0")

    textBox1.Text+=Convert.ToString(i);

    else

    textBox1.Text=Convert.ToString(i);

    }

    (7) Button1-Button9Button0

    (8) .

    private void btn_dot_Click(object sender, System.EventArgs e)

    { int n=textBox1.Text.IndexOf(".");

    if(n==-1)//

    textBox1.Text=textBox1.Text+".";

    }

    (9) textBox1

    (10) sum0

    (11) (+-*\=)

    blnClear

  • 77

    false(+-*

    \=)blnClear=true

    blnCleartrue

    blnClear=false append_num

    public void append_num(int i)

    { if(blnClear)//textBox1

    { textBox1.Text="0";//

    blnClear=false;

    }

    if(textBox1.Text!="0")

    textBox1.Text+=Convert.ToString(i);

    else

    textBox1.Text=Convert.ToString(i);

    }

    (12) btn_dot_Click

    private void btn_dot_Click(object sender, System.EventArgs e)

    { if(blnClear) //textBox1

    { textBox1.Text="0";//

    blnClear=false;

    }

    int n=textBox1.Text.IndexOf(".");

    if(n==-1)//

    textBox1.Text=textBox1.Text+".";

    }

    (13) 1+2-311+,

    sum=sum+1(sum=0)sum()

    +22-,(

    +)sum=sum+2sum-

    strOper

    "+"sum=sum+sum=0

    sum=+

    private void btn_add_Click(object sender, System.EventArgs e)

    { double dbSecond=Convert.ToDouble(textBox1.Text);

    if(!blnClear)//

    switch(strOper)//

    { case "+":

    sum+=dbSecond;

    break;

    //-*\

    }

    if(sender==btn_add)

    strOper="+";

    //-*\=

  • 78

    textBox1.Text=Convert.ToString(sum);

    blnClear=true;

    }

    (14) =++

    private void btn_add_Click(object sender, System.EventArgs e)

    { double dbSecond=Convert.ToDouble(textBox1.Text);

    if(!blnClear)//

    switch(strOper)//

    { case "+":

    sum+=dbSecond;

    break;

    //-*\

    }

    if(sender==btn_add)

    strOper="+";

    if(sender==btn_equ)//=

    strOper="=";

    textBox1.Text=Convert.ToString(sum);

    blnClear=true;

    }

    btn_equ+

    (15) C

    private void btn_C_Click(object sender, System.EventArgs e)

    { textBox1.Text="0";

    sum=0;

    blnClear=false;

    strOper="+";

    }

    (16)

    (1) 12

    (Enabledfalse)

    (2) (3.9)

    (3)

    label1.Text=textBox1.Text

    (4) ( TextChanged

    label1.Text=textBox1.Text)

    (5)

    label1.Text=textBox1.SelText

    (6)

  • 79

    (7) RadioButtonLabel

    (8) e3_8Click

    (9) ComboBox(Item)

    (10) ListBox, Name=listBox1

    ListBoxListBox

    SelectedIndexChenged

    label1.Text="";

    for(int i=0;i

  • 80

    Word

    4.1 RichTextBox

    RichTextBox TextBox

    TextBox TextBox

    rtf TextBox

    TextBoxRichTextBox

    l Dock

    DockStyle NoneLeftRightTopBottom Fill

    DOCK 5

    l SelectedText RichTextBox

    l SelectionLength RichTextBox

    l SelectionStart RichTextBox

    l SelectionFont

    l SelectionColor

    l Lines RichTextBox

    l Modified true

    l SelectionChangeRichTextBox

    l TextChangedRichTextBox

    l Clear() RichTextBox Lines

    l Copy()Cut()Paste() RichTextBox

    l SelectAll() RichTextBox

    l Find()

    RichTextBoxFindsMatchCase(

    )Reverse() 12 3

    l SaveFile() 2

  • 81

    RichTextBoxStreamType.PlainTextRtf

    RichTextBoxStreamType.RichText Unicode

    RichTextBoxStreamType.UnicodePlainText

    l LoadFile() SaveFile()

    l Undo() RichTextBox

    l Redo() RichTextBox

    4.2

    CutCopy

    Paste

    (1) RichTextBox Name=richTextBox1Dock=FillText=

    (2) Mainmenu

    Name mainMenuEditmenuItemEditCut

    menuItemEditCopymenuItemEditPastemenuItemEditUndomenuItemEditRedo

    private void menuItemEditCut_Click(object sender, System.EventArgs e)

    { richTextBox1.Cut();} //

    private void menuItemEditCopy_Click(object sender, System.EventArgs e)

    { richTextBox1.Copy();} //

    private void menuItemEditPaste_Click(object sender, System.EventArgs e)

    { richTextBox1.Paste();} //

    private void menuItemEditUndo_Click(object sender, System.EventArgs e)

    { richTextBox1.Undo();} //

    private void menuItemEditRedo_Click(object sender, System.EventArgs e)

    { richTextBox1.Redo();} //

    (3)

    4.3

    4.3.1 OpenFileDialog SaveFileDialog

    OpenFileDialogSaveFileDialog

  • 82

    4.3.1A

    4.3.1B

    l FilterFilter

    |(T)

    ( 4.3.1A)

    Filter="(*.txt)|*.txt|(*.*)|*.*"

    (T)(*.txt)

    (*.*)"

    (*.txt)".txt"

  • 83

    (*.*)"

    l FilterIndex(T)

    FilterIndex

    FilteropenFileDialog1.Filter="

    (*.txt)|*.txt|(*.*)|*.*",openFileDialog1.FilterIndex=1

    l FileName

    l InitialDirectory

    l DefaultExt

    l ShowDialog()

    DialogResult.CancleDialogResult.OK

    4.3.2

    (4) OpenFileDialog SaveFileDialog Name openFileDialog1

    saveFileDialog1

    (5) .........

    NamemainMenuFilemenuItemFileNewmenuItemFileOpen

    menuItemFileSavemenuItemFileSaveAsmenuItemFileExit

    (6) Form1 stringstring s_FileName=""

    (7)

    private void menuItemFileNew_Click(object sender, System.EventArgs e)

    { richTextBox1.Text="";//richTextBox1.Clear();

    s_FileName="";//

    }

    (8)

    private void menuItemFileOpen_Click(object sender, System.EventArgs e)

    { if(openFileDialog1.ShowDialog()==DialogResult.OK)

    { s_FileName=openFileDialog1.FileName;

    richTextBox1.LoadFile(openFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    (9)

    private void menuItemFileSaveAs_Click(object sender, System.EventArgs e)

    { if(saveFileDialog1.ShowDialog()==DialogResult.OK)

    { s_FileName=saveFileDialog1.FileName;

    richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }//

    }

    (10)

  • 84

    private void menuItemSaveFile_Click(object sender, System.EventArgs e)

    { if(s_FileName.Length!=0)

    richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);

    else

    menuItemFileSaveAs_Click(sender,e);//

    }

    (11) SaveFileDialog

    SaveFileDialog

    SaveFileDialog

    ( SaveFileDialog)

    private void menuItemFileSaveAs_Click(object sender, System.EventArgs e)

    { SaveFileDialog saveFileDialog1=new SaveFileDialog();

    saveFileDialog1.Filter="(*.txt)|*.txt|(*.*)|*.*";

    saveFileDialog1.FilterIndex=1;

    if(saveFileDialog1.ShowDialog()==DialogResult.OK)

    { s_FileName=saveFileDialog1.FileName;

    richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }//

    }

    (12)

    private void menuItemExit_Click(object sender, System.EventArgs e)

    { Close();}

    (13)

    4.4

    FontDialog

    4.4.1 FontDialog

    FontDialogFontDialog OpenDialog

    FontFontDialog

    FontDialog4.3.1

    4.4.2

    (14) FontDialogName=fontDialog1

    Name mainMenuModel

  • 85

    menuItemModelFont

    private void menuItemModelFont_Click(object sender, System.EventArgs e)

    { if(fontDialog1.ShowDialog()==DialogResult.OK)

    richTextBox1.SelectionFont=fontDialog1.Font;

    }

    (15)

    4.3.1

    4.5 About

    SaveDialogOpenDialogFontDialog

    System.Windows.Forms.Form

    (16) /Windows(4.5)(T)

    Windows(N)formAbout.cs

    formAbout.csformAbout

    (17) formAboutStartPosition=CenterParent

    MaximizeBox=FalseMinimizeBox=False

    FormBorderStyle=FixedDialog

    Text=""Label

    LabelText=""

    Text=""

    private void button1_Click(object sender,System.EventArgs e)

    {Close();}

    (18) Form1

    NamemenuItemAbout

  • 86

    private void menuItemAbout_Click(object sender, System.EventArgs e)

    { formAbout AboutDialog=new formAbout();

    AboutDialog.ShowDialog(this);

    }//Show()

    (19) formAbout

    ()

    4.5

    4.6

    4.6.1

    SaveDialogOpenDialog

    System.Windows.Forms.Form

    ShowDialog()

    Show()

  • 87

    4.6.2

    (20) System.Windows.Forms.Form

    /Windows(4.5)Windows

    formFindReplace.cs

    Name=formFindReplace

    (21) formFindReplaceStartPosition=CenterParent

    MaximizeBox=FalseMinimizeBox=False

    FormBorderStyle=FixedDialog

    Text=""LabelText

    """"TextBoxText=""

    Text"""" TopMost=true

    (22) formFindReplaceForm1 MainForm1;

    (23) formFindReplace(

    )

    public formAbout(Form1 form1)//

    {

    //Windows

    InitializeComponent();

    //TODO:InitializeComponent

    MainForm1=form1;//,Form1Name

    }//Form1formFindReplace

    (24) Form1formFindReplace

    public void FindRichTextBoxString(string FindString)

    {} //

    (25) formFindReplace

    private void buttonFind_Click(object sender, System.EventArgs e)

    { if(textBox1.Text.Length!=0)//,

    MainForm1.FindRichTextBoxString(textBox1.Text);//

    else

    MessageBox.Show("","",MessageBoxButtons.OK);

    }//MessageBox4.7.1

    (26) Form1formFindReplace

    public void ReplaceRichTextBoxString(string ReplaceString)

    {} //

    (27)

    private void buttonReplace_Click(object sender, System.EventArgs e)

    { if(textBox2.Text.Length!=0)//,

    MainForm1.ReplaceRichTextBoxString(textBox1.Text,textBox2.Text);

    else//MainForm1.ReplaceRichTextBoxString(26)

    MessageBox.Show("","", MessageBoxButtons.OK);

    }

  • 88

    (28) Form1int FindPostion=0

    (29) Form1

    private void menuItemFindReplace_Click(object sender, System.EventArgs e)

    { FindPostion=0;

    formAbout FindReplaceDialog=new formAbout(this);//this

    FindReplaceDialog.Show();//Show()

    }

    (30) Form1 FindRichTextBoxString

    public void FindRichTextBoxString(string FindString)

    { if(FindPostion>=richTextBox1.Text.Length)//

    { MessageBox.Show(",",

    "",MessageBoxButtons.OK);

    FindPostion=0;

    return;

    }//-11

    //23

    FindPostion=richTextBox1.Find(FindString,

    FindPostion,RichTextBoxFinds.MatchCase);

    if(FindPostion==-1)//

    { MessageBox.Show(",",

    "", MessageBoxButtons.OK);

    FindPostion=0;//

    }

    else//

    { richTextBox1.Focus();//

    FindPostion+=FindString.Length;

    }//

    }

    (31) Form1 ReplaceRichTextBoxString

    public void ReplaceRichTextBoxString(string ReplaceString)

    { if(richTextBox1.SelectedText.Length!=0)//

    richTextBox1.SelectedText=ReplaceString;//

    }

    (32)

    /

  • 89

    4.7

    4.7.1 MessageBox

    MessageBox

    MessageBox.Show(this,"","",

    MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);

    YES,NO,CANCELAbortRetryIgnore(

    )OK()OKCancel()RetryCance()

    YesNo()

    AsteriskErrorExclamationHandStopWarningNone

    System.Windows.Forms.DialogResult

    System.Windows.Forms.DialogResult.YesYES

    System.Windows.Forms.DialogResult.CancelCancel

    System.Windows.Forms.DialogResult.NoNo

    MessageBox

    4.7.2

    (33) Form1 bool bSave=false RichTextBox

    bSave=false

    RichTextBox TextChanged

    bSave=true

    (34)

    true false

    public bool IfSaveOldFile()

    { bool ReturnValue=true;

    if(bSave)

    { System.Windows.Forms.DialogResult dr;

    dr=MessageBox.Show(this,"","",

  • 90

    MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);

    switch(dr)

    { case System.Windows.Forms.DialogResult.Yes://yes

    bSave=false;

    if(s_FileName.Length!=0)

    richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);

    else

    { SaveFileDialog saveFileDialog1=new SaveFileDialog();

    saveFileDialog1.Filter="(*.txt)|*.txt|(*.*)|*.*";

    saveFileDialog1.FilterIndex=1;

    if(saveFileDialog1.ShowDialog()==DialogResult.OK)

    { s_FileName=saveFileDialog1.FileName;

    richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    ReturnValue=true;

    break;

    case System.Windows.Forms.DialogResult.No://no

    bSave=false;

    ReturnValue=true;

    break;

    case System.Windows.Forms.DialogResult.Cancel://Cancel

    ReturnValue=false;

    break;

    }

    }

    return ReturnValue;

    }

    (35)

    if(!IfSaveOldFile())//

    return;

    (36)

    private void menuItemSaveFile_Click(object sender, System.EventArgs e)

    { if(s_FileName.Length!=0)

    { bSave=false;//

    richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);

    }

    else

    menuItemSaveAs_Click(sender,e);

    }

    (37)

    private void menuItemSaveAs_Click(object sender, System.EventArgs e)

    { SaveFileDialog saveFileDialog1=new SaveFileDialog();

  • 91

    saveFileDialog1.Filter="(*.txt)|*.txt|(*.*)|*.*";

    saveFileDialog1.FilterIndex=1;

    if(saveFileDialog1.ShowDialog()==DialogResult.OK)

    { s_FileName=saveFileDialog1.FileName;

    richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    bSave=false;//

    }

    }

    (38) RichTextBox TextChanged

    private void richTextBox1_TextChanged(object sender, System.EventArgs e)

    { bSave=true;}

    (39) Form1 Closing

    private void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)

    { if(!IfSaveOldFile())

    e.Cancel=true;//

    }

    (40)

    4.8

    4.8.1 PrintDocument

    PrintDocument

    l DocumentName

    l Print

    l BeginPrintPrint

    l PrintPage

    l EndPrint

    PrintDocument

    PageSetupDialog

    PrintDialog

    Document PrintDocument

    PrintDocumentPrintDocument.Print

    BeginPrintPrintPageEndPrint

    PrintPagePrintPage

  • 92

    PrintPage

    3BeginPrint

    PrintPageEndPrint

    2System.Drawing.Printing.PrintEventArgs e

    l e.Cancel true

    l e.Graphics

    l e.HasMorePagesPrintPage

    HasMorePages=true PrintPage

    PrintPage

    l e.MarginBoundsRectangleLeft

    TopWidthHeight1/100

    l e.MarginBoundsRectangle1/100

    l e.PageSettingsPageSettings PageSetupDialog

    PageSettings

    3

    (41) using

    using System.IO;

    using System.Drawing.Printing;

    (42) RichTextBoxStringReader streamToPrint=null

    StreamReader streamToPrint

    Font printFont

    (43) PrintDocumentnameprintDocument1

    (44) printDocument1BeginPrint

    private void printDocument1_BeginPrint(object sender,

    System.Drawing.Printing.PrintEventArgs e)

    { printFont=richTextBox1.Font;//

    streamToPrint=new StringReader(richTextBox1.Text);//richTextBox1.Text

    }//streamToPrint=new StreamReader("");

    (45) printDocument1 PrintPagestreamToPrint.ReadLine()

    private void printDocument1_PrintPage(object sender,

    System.Drawing.Printing.PrintPageEventArgs e)

    { float linesPerPage=0;//

    float yPos=0;//

    int count=0;//

    float leftMargin=e.MarginBounds.Left;//

    float topMargin=e.MarginBounds.Top;//

    string line=null;//RichTextBoxline

    //=/

    linesPerPage=e.MarginBounds.Height/printFont.GetHeight(e.Graphics);

    //null

    while(count

  • 93

    { //yPos

    yPos=topMargin+(count*printFont.GetHeight(e.Graphics));

    e.Graphics.DrawString(line,printFont,Brushes.Black,

    leftMargin,yPos,new StringFormat());//

    count++;//1

    }

    if(line!=null)//

    e.HasMorePages=true;//

    else

    e.HasMorePages=false;//

    }

    (46) printDocument1EndPrint

    private void printDocument1_EndPrint (object sender,

    System.Drawing.Printing.PrintEventArgs e)

    { if(streamToPrint!=null)

    streamToPrint.Close();//

    }

    4.8.2 PageSetupDialog

    WindowsPageSetupDialogWindows

    4.8.2

    4.8.2

    ()

    DocumentPrintDocument

  • 94

    PrintDocument

    (47)

    (48) PageSetupDialognamepageSetupDialog1

    (49)

    private void menuItem5_Click(object sender,System.EventArgs e)

    { pageSetupDialog1.Document=printDocument1;

    pageSetupDialog1.ShowDialog();

    }

    (50) pageSetupDialog1PageSetupDialog

    PrintDocument printDocument1

    PrintDocument.Print

    PrintPagee

    4.8.3

    PrintPreviewDialog PrintDocument

    (51)

    (52) PrintPreviewDialog name printPreviewDialog1

    (53)

    private void menuItemPrintView_Click(object sender,System.EventArgs e)

    { printPreviewDialog1.Document=printDocument1;

    printPreviewDialog1.ShowDialog();

    }

    (54) 4.8.3

    4.8.3

  • 95

    4.8.4 PrintDialog

    PrintDialog

    ()

    Document PrintDocument

    PrintDialogPrintDocumentPrintDialog

    4.8.4

    4.8.4

    (55) PrintDialog Name=printDialog1

    (56)

    (57)

    private void menuItemPrint_Click(object sender, System.EventArgs e)

    { printDialog1.Document=printDocument1;

    if(printDialog1.ShowDialog(this)==DialogResult.OK)

    printDocument1.Print();

    }

    (58)

    4.9

    Microsoft Word

    Visualstudio.Net

  • 96

    4.9.1 Microsoft Word

    Microsoft Word

    (MDI)()()

    (1) IsMdiContainer=true

    (2) MainmenuName=menuItemFile

    Name

    menuItemNewmenuItemOpenmenuItemSaveAsmenuItemCloseChildmenuItemExit

    Name=menuItemWindowMdiList=true,

    NamemenuItemTileHmenuItemCascademenuItemTileV

    (3) /Windows(4.5)Windows

    FormChild.cs

    FormChild

    (4) RichTextBox1Dock=FillText="",Modifiers=public,

    RichTextBox1,RichTextBox1

    (5)

    private void menuItemNew_Click(object sender,System.EventArgs e)

    { FormChild formChild=new FormChild();

    formChild.MdiParent=this;

    formChild.Show();

    }

    (6) OpenFileDialog

    private void menuItemOpen_Click(object sender, System.EventArgs e)

    { if(openFileDialog1.ShowDialog(this)==DialogResult.OK)

    { FormChild ChildForm=new FormChild();

    ChildForm.MdiParent=this;

    ChildForm.richTextBox1.LoadFile(openFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    ChildForm.Show();

    }

    }

    (7) SaveFileDialog

    private void menuItemChildSaveAs_Click(object sender, System.EventArgs e)

    { if(saveFileDialog1.ShowDialog(this)==DialogResult.OK)

    { FormChild ChildForm=(FormChild)this.ActiveMdiChild;

    ChildForm.richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    (8)

  • 97

    private void menuItemCloseChild_Click(object sender, System.EventArgs e)

    { this.ActiveMdiChild.Close();}

    (9)

    private void menuItemExit_Click(object sender, System.EventArgs e)

    { Close();}

    (10)

    private void menuItemTileH_Click(object sender, System.EventArgs e)

    { this.LayoutMdi(MdiLayout.TileHorizontal);}

    (11)

    private void menuItemCascade_Click_1(object sender, System.EventArgs e)

    { this.LayoutMdi(MdiLayout.Cascade);}

    (12)

    private void menuItemTileV_Click(object sender, System.EventArgs e)

    { this.LayoutMdi(MdiLayout. TileVertical);}

    (13)

    4.9.2

    (1) IsMdiContainertrue

    (2) MainmenuMergeType=MergeItems

    MergeOrder

    MergeOrder=0

    MergeTypeMergeItemsMergeOrder0

    Name=menuItemFile

    , Name menuItemNewmenuItemOpenmenuItemExit MergeType

    AddMergeOrder126

    MergeType=Add MergeOrder=7 Name=menuItemHelp

  • 98

    ,Name=menuItemAboutMergeOrder

    07

    MergeOrder=3

    (3) /WindowsWindows

    formChild.cs

    formChild

    (4) formChildForm1 MainForm1;

    (5) formChild()

    public formChild(Form1 form1)//

    {

    //Windows

    InitializeComponent();

    //TODO:InitializeComponent

    MainForm1=form1;//,Form1Name

    }//Form1formChild

    (6) MainmenuMergeType=MergeItems

    MergeOrder=0

    MergeType=Add MergeOrder=3

    MergeType=Add

    MergeOrder=4

    (7) MergeType=Add MergeOrder=3

    MergeOrder=3

    (8) MergeType=Add MergeOrder=6

    Name=menuItemWindowMdiList=true,

    NamemenuItemCascade

    (9) RichTextBox1 Dock=FillText=

    Modifiers=public,RichTextBox1,RichTextBox1

    (10)

    private void menuItemNew_Click(object sender, System.EventArgs e)

    { formChild ChildForm=new formChild(this);

    ChildForm.MdiParent=this;

    ChildForm.Show();

    }

    (11) OpenFileDialog

    private void menuItemOpen_Click(object sender, System.EventArgs e)

    { if(openFileDialog1.ShowDialog(this)==DialogResult.OK)

    { formChild ChildForm=new formChild(this);

    ChildForm.MdiParent=this;

    ChildForm.richTextBox1.LoadFile(openFileDialog1.FileName);

    ChildForm.Show();

    }

    }

  • 99

    (12)

    private void menuItemExit_Click(object sender, System.EventArgs e)

    { Close();}

    (13)

    private void menuItemCascade_Click_1(object sender, System.EventArgs e)

    { MainForm1.LayoutMdi(MdiLayout.Cascade);}

    (14) SaveFileDialog

    private void menuItemChildSaveAs_Click(object sender, System.EventArgs e)

    { if(saveFileDialog1.ShowDialog(this)==DialogResult.OK)

    { richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    (15)

    private void menuItemCloseChild_Click(object sender, System.EventArgs e)

    { Close();}

    (16)

    richTextBox1.Cut();richTextBox1.Copy();richTextBox1.Paste();

    (17)

    4.9.3 Visualstudio.Net

    Visualstudio.Net

    Visualstudio.Net

    (1) TabControlDock=Fill

    (2) TabControlTabPagesTabPage

    1Text4.9.3

    (3) Label Text=

    Label

    Text= RichTextBox

  • 100

    (4)

    4.9.3

    (1) TabControl Dock=Fill

    (2) Mainmenu 4

    Name menuItemFile

    menuItemFileNewmenuItemFileClosemenuItemFileOpenmenuItemFileSaveAs

    (3) MakeNewTbpage()

    private object MakeNewTbpage()

    { //TabPage

    TabPage tabPage1=new TabPage();

    tabControl1.Controls.Add(tabPage1);//tabPage1tabControl1

    tabPage1.Location=new Point(4, 21);

    tabPage1.Size=new Size(284, 248);

    tabPage1.Text=""+tabPage1.TabIndex.ToString()+"";

    //RichTextBox

    RichTextBox richTextBox1=new RichTextBox();

    richTextBox1.Dock=DockStyle.Fill;

    richTextBox1.Size=new Size(284, 248);

    richTextBox1.Text="";

    tabPage1.Controls.Add(richTextBox1);//richTextBox1tabPage1

    return (object)richTextBox1;

    }

    (4)

    private void menuItemFileNew_Click(object sender, System.EventArgs e)

    { MakeNewTbpage();}

  • 101

    (5)

    private void menuItemFileClose_Click(object sender, System.EventArgs e)

    { TabPage tabPage1=tabControl1.SelectedTab;//

    tabControl1.Controls.Remove(tabPage1);//tabControl1

    //0RichTextBox

    RichTextBox richTextBox1=(RichTextBox)tabPage1.Controls[0];

    if(richTextBox1!=null)

    richTextBox1.Dispose();//RichTextBox

    if(tabPage1!=null)

    tabPage1.Dispose();//

    }

    (6) OpenFileDialog

    private void menuItemFileOpen_Click(object sender, System.EventArgs e)

    { if(openFileDialog1.ShowDialog()==DialogResult.OK)

    { RichTextBox richTextBox1=(RichTextBox)MakeNewTbpage();

    richTextBox1.LoadFile(openFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    (7) SaveFileDialog

    private void menuItemFileSaveAs_Click(object sender, System.EventArgs e)

    { if(saveFileDialog1.ShowDialog()==DialogResult.OK)

    { TabPage tabPage1=tabControl1.SelectedTab;

    RichTextBox richTextBox1=(RichTextBox)tabPage1.Controls[0];

    richTextBox1.SaveFile(saveFileDialog1.FileName,

    RichTextBoxStreamType.PlainText);

    }

    }

    (8)

    (1) RichTextBox Lines

    Lines (

    string[] s=new string [richTextBox1.Lines.Length];s= richTextBox1.Lines)

    (2)

    (3) 2

    (4) 3

    (5) 1 ColorDialog

    (6)

    (7) RichTextBoxModified 4.7

    ModifiedRichTextBox

    (8)

  • 102

    (9) InitialDirectory DefaultExt

    (10) RichTextBox SelectionAlignment

    (HorizontalAlignment.Left)

    (HorizontalAlignment.Right)(HorizontalAlignment.Center)(3

    3 Style

    ToolBarButtonStyle.Separator)

    (11) RichTextBoxSelectionCharOffset

    0

    (12)

    (13)

    (14)

    :

  • 103

    Windows

    Graphics()

    Bitmap

    5.1 (GDI)

    Windows()Windows

    (GDI32.DLL) CRT

    (GDI) CRT

    GDI

    CRT CRT

    GDI CRT GDI

    CRT

    Word

    .NET(.Net FrameWork) Windows

    (GDI32.DLL)GDI+GDI+

    GDI+ System.Drawing

    System.Drawing.Priniting System.Drawing.ImagingSystem.Drawing.Drawing2D

    System.Drawing.DesignSystem.Drawing.Text

    5.2 Graphics

    System.Drawing.Graphics GDI+Graphics

    Graphics

    GraphicsGraphics

    CRT

    CRT

    GDI

  • 104

    5.2.1 Graphics

    GDI+GraphicsGraphics

    Graphics

    Graphics

    private void button1_Click(object sender,System.EventArgs e)

    { Graphics g=this.CreateGraphics();//Graphics

    Pen pen1=new Pen(Color.Red);//

    SolidBrush brush1=new SolidBrush(Color.Blue);//

    g.DrawEllipse(pen1,10,10,100,100);//

    g.FillEllipse(brush1,10,10,100,100);//

    }

    5.2.2 Paint

    Form

    Windows

    Windows Paint

    Paint Windows Paint

    Form Paint

    Paint Form Paint

    private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs e)

    { Graphics g=e.Graphics;//Graphics

    Pen pen1=new Pen(Color.Red);

    SolidBrush brush1=new SolidBrush(Color.Blue);

    g.DrawEllipse(pen1,10,10,100,100);

    g.FillEllipse(brush1,10,10,100,100);

    }

    5.3 GDI+

    GDI+ Graphics.TransformPoints()

    l

    l (Page) x y

  • 105

    l (Page)

    (Page)

    5.4 GDI+

    GDI+PointPointFSizeSizeFRectangleRectangleF

    ColorSystem.Drawing

    5.4.1 Point PointF

    XY x y

    Point p1=new Point(int X,int Y);//X,Y

    PointF p2=new PointF(float X,floa Y);//X,Y

    5.4.2 Size SizeF

    Size SizeFWidth Height

    public Size(int width,int height);

    public SizeF(float width,float height);

    5.4.3 Rectangle RectangleF

    Rectangle RectangleF

    l TopRectangley

    l LeftRectanglex

    l BottomRectangley

    l RightRectanglex

    l WidthRectangle

    l HeightRectangle

    l SizeRectangle

    l X:Rectanglex

    l Y:Rectangley

    //locationSizesize

    Rectangle(Point location,Size size);//PointFSizeF

    //xy,,

    Rectangle(int X,int Y,int width,int height);//XYfloat

    5.4.4 Color

    Color 32

  • 106

    (al)(bb)(gg)(rr)0xalrrbbggal,bb,gg,rr

    0255

    l public static Color FromArgb(int alpha,int rr,int gg,int bb);

    Color8

    256alpha=0=255

    l public static Color FromArgb(int rr,int gg,int bb);

    8 Color 255

    8256(255,0,0)(0,255,0)

    (0,0,255)

    l public static Color FromArgb(int alpha,Color color);

    ColorColoralphaalpha

    8

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

    { Graphics g=e.Graphics;

    SolidBrush RedBrush=new SolidBrush(Color.FromArgb(128,255,0,0));//

    SolidBrush GreenBrush=new SolidBrush(Color.FromArgb(128,0,255,0));

    SolidBrush BlueBrush=new SolidBrush(Color.FromArgb(128,0,0,255));

    g.FillRectangle(RedBrush,0,0,80,80);

    g.FillRectangle(GreenBrush,40,0,80,80);

    g.FillRectangle(BlueBrush,20,20,80,80);

    }

    alpha255

    C#

    Color.BlackColor.Red

    5.5

    PenPen4

    l public Pen(Color color);//color1

    l public Pen(Color colorfloat width);//colorwidth

    l public Pen(Brush brush);//

    l public Pen(Brush,float width);//width

    PenColorWidthDashStyleEndCap

    StartCapDashStyleEndCapStartCap

    ()Paint

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

    { Graphics g=e.Graphics;

    Pen pen1=new Pen(Color.Red,6);//

    g.DrawLine(pen1,10,10,100,10);//1

    pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;//

    g.DrawLine(pen1,10,20,100,20);//2

    pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.DashDot;//

    g.DrawLine(pen1,10,30,100,30);//3

    //

  • 107

    pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.DashDotDot;

    g.DrawLine(pen1,10,40,100,40);//4

    pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.Dot;//

    g.DrawLine(pen1,10,50,100,50);//5

    pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.Solid;//

    pen1.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;//

    g.DrawLine(pen1,150,10,250,10);//1

    pen1.StartCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;//

    g.DrawLine(pen1,150,22,250,22);//2

    pen1.EndCap=System.Drawing.Drawing2D.LineCap.RoundAnchor;

    g.DrawLine(pen1,150,34,250,34);//3

    pen1.EndCap=System.Drawing.Drawing2D.LineCap.SquareAnchor;

    g.DrawLine(pen1,150,46,250,46);//4

    pen1.EndCap=System.Drawing.Drawing2D.LineCap.Triangle;

    g.DrawLine(pen1,150,58,250,58);//5

    pen1.EndCap=System.Drawing.Drawing2D.LineCap.DiamondAnchor;

    //6

    g.DrawLine(pen1,150,70,250,70);

    }

    5.6

    GDI+

    l SolidBrushSystem.Drawing

    l HatchBrushSystem.Drawing.Drawing2D

    l TextureBrush()

    l LinearGradientBrush

    l PathGradientBrush

    5.6.1 SolidBrush

    1

    SolidBrush brush1=new SolidBrush(Color color);//

    Colorbrush1.Color=Color.Green;

    5.6.2 HatchBrush

    ()

  • 108

    //

    HatchBrush brush1=new HatchBrush(HatchStyle h,Color c);

    //

    HatchBrush brush1=new HatchBrush(HatchStyle h,Color c1Color c2);

    3

    l backgroundColor

    l foreColor

    l HatchStyle

    5.6.2HatchStyleForm1.cs

    using System.Drawing.Drawing2DPaint

    private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs e)

    { Graphics g=e.Graphics;//Graphics

    HatchBrush b1=new

    HatchBrush(HatchStyle.BackwardDiagonal,Color.Blue,Color.LightGray);

    g.FillRectangle(b1,10,10,50,50);//1

    HatchBrush b2=new HatchBrush(HatchStyle.Cross,Color.Blue,Color.LightGray);

    g.FillRectangle(b2,70,10,50,50);//2

    HatchBrush b3=new

    HatchBrush(HatchStyle.ForwardDiagonal,Color.Blue,Color.LightGray);

    g.FillRectangle(b3,130,10,50,50);//3

    HatchBrush b4=new

    HatchBrush(HatchStyle.DiagonalCross,Color.Blue,Color.LightGray);

    g.FillRectangle(b4,190,10,50,50);//4

    HatchBrush b5=new

    HatchBrush(HatchStyle.Vertical,Color.Blue,Color.LightGray);

    g.FillRectangle(b5,250,10,50,50);//5

    HatchBrush b6=new

    HatchBrush(HatchStyle.Horizontal,Color.Blue,Color.LightGray);

    g.FillRectangle(b6,310,10,50,50);