Vous êtes sur la page 1sur 133

,

“ ”

6.050103
“' ( ”
( )

2011
2

-
“. ( ” -
6.050103 “' ( ”( -
) / 0. .. . , 1. . 2 , .. .. . . – (( :
56 , 2011. – 66 .

0 : 0 . . , . . .,
1 2 ,
. . . , . . ., . .

7 : 8... 8 99 , . . .,

:
: 0.:. ' , . . .,

' ;6
“2” 9 2010 .
3

...........................................................................................................6
1 ! -# $%& ' !( )*1 +, ' )
- ./,! '0 MS Office ( - # 8 Visual Basic for
Application ..................................................................................................7
1.1 9 ..........................................................................................7
1.2 . ..............................................................7
1.2.1 2 VBA ...............................................................7
1.2.2 8 .......................................................10
1.2.3 6 ( ...........................................11
1.2.4 A .................................................................................13
1.2.5 0 VBA ............................................16
1.2.6 VBA MS Word...................................................19
1.3 9 .............................................................................20
1.4 ............................................................................................28
1.5 J ........................................................................28
2 ! -# $%& ' !( )*2 F ' ! #
'( #.........................................................................................29
2.1 9 ........................................................................................29
2.2 . ............................................................29
2.2.1 8 K ............................................................30
2.2.2 8 MDI- ................................................................33
2.2.3 ' MdiLayout ..............................................................36
2.2.4 : , K ..............38
2.2.5 J K .............................................................................39
2.2.6 A .................................................................................42
2.2.7 9 ( .........................................48
2.2.8 OpenFileDialog SaveFileDialog SDI- .......................49
2.2.9 FontDialog.........................................................................................51
2.2.10 ColorDialog .....................................................................................52
2.2.11 StatusBar..........................................................................................53
2.2.12 .........................................................................56
2.2.13 CheckBox ........................................................................................57
2.2.14 : TabIndex ...............................60
4

2.3 9 .............................................................................64
2.4 ............................................................................................64
2.5 J ........................................................................64
3 ! -# $%& ' !( ) *3 F -' 8
F F !, ) %! # ...........................................................................66
3.1 9 ........................................................................................66
3.2 . ............................................................66
3.2.1 K ......................................................................66
3.2.1.1 J K ..........................................................................66
3.2.1.2 : K ................................................................67
3.2.1.3 ' K ..................................................................67
3.2.1.4 J d ............................................................................67
3.2.1.5 e d ...............................................................................68
3.2.1.6 0 K .....................................68
3.2.1.7 : 9 ( K.........................................68
3.2.1.8 8 K ........................................................69
3.2.2 .9 9 .......................................................71
3.2.2.1 ' 9 9 ........................................................72
3.2.2.2 e ........................................................................76
3.2.2.3 ' .......................................................77
3.2.2.4 5 ...............................................................78
3.2.2.5 e .........................................................79
3.2.2.6 J .............................................80
3.3 ...............................................................................................83
3.4 ............................................................................................83
3.5 J ............................................................................84
4 ! -# $%& ' !( )*4 F ( -
! ' ) '/'-' %! ' ! ) #..................................................85
4.1 9 ........................................................................................85
4.2 . ............................................................85
4.2.1 A ..................................................................................................85
4.2.1.1 J PrintDocument............................................................85
4.2.1.2 : ................................................................................88
4.2.1.3 PrintPreviewControl ............92
4.2.1.4 5 d .................................................94
4.2.1.5 ' ................................97
5

4.2.2 8 K. NET
Framework................................................................................................105
4.2.2.1 GDI + ....................................................................105
4.2.2.2 8 ................................................116
4.2.2.3 7 ' , ' 9 K
...............................................................................125
4.3 9 ...........................................................................130
4.4 ..........................................................................................131
4.5 J ..........................................................................131
L% ' ..............................................................................................133
6

MN

A n
( .
A - -
K K, ( -
( o Internet.
A ( - -
n , ( n
’K 9 .
n :
– d( K ,
, , , 9 , 9 ,
o n , );
– 9 ;
– 9 , 9
9 , (
’ , .
K 9 A4 (210 x 297 ).
6 o K n .'
9 – 20 .0 d KK K -
9 o K . 9 -
K .
' 9 - -
9 , -
, ( 9 ,
nK
.8 -
.
- K n
K , -
, n d
- ,
9 .
7

1 OM -P Q R * 1
S R TL U MS OFFICE
N P Z VISUAL BASIC FOR APPLICATIO`

1.1 ' F ,

1.1.1 : ( Visual Basic for Application


o .
1.1.2 5 K K Visual
Basic for Application.

1.2 ! % ' ' ,& % !%( - %

.9 ( , K -
9 MS Office 9 Kn 9
VBA.

1.2.1 Q -, , ! & VBA

Visual Basic for Application n K -


9 K
( , . ). s ( . 1.1)
n 9'n .6 , 9 - 9'n , K ,
( .s ( -
9 d, .
s ( ( K ,
. Userform) — ( 9 ), o n
K .
7 9 K K n -
:
• 9 9 ;
• Visual Basic;
• ;
• ;
• ;
• 9 9 .
8

7 1.1 – '

' 9 ( n
“' ”. “' ” -
, ( .u o -
' ,
( , K View
Toolbox 9 ( d .
“' ” ( 9 “' ”) -
9'n
VBA. 5 ( .
Label ( )
– 9 ,o , , -
K, ( n
.
Textbox ( ) – ,
n .
Commandbutton ( ) – , K
( .'
K .
Listbox ( ) – n 9 -
. ( 9 9 ,
K .
Combobox ( ) – n 9
.v n n ( Listbox
Textbox.
9

Scrolbar ( ) – , -
n , , 9 d .
K
9 . , ,9 , K
( 9 9 9 9 (
9 d 9 9 d .
Spinbutton ( ) – n 9
.
Optionbutton ( ) – n 9
n K 9 . ' 9 -
( K , 9 K ( 9 .
Checkbox – n ( 9 .' -
n : .
Togglebutton ( ) – , d n
, n
.
Frame ( ) – n 9'n -
- 9 , K ,o -
' ( 9 K.
Image ( ) – n
. K ( 9 -
( : bmp; cur; gif; ico; jpg; wmf.
Multipage ( 9 ) –
( 9 ( 9 .
: ( n ,o n ,o -
( K K 9 ), -
( 9 .
Tabstrip ( 9 ) – ,
9 . : n
' .
Refedit – ( ,
n , n ,o -
n 9 9 - 9 d.
5 9 d n:
• Label ( );
• Textbox ( );
• Commandbutton ( );
10

• Image ( K );
• Optionbutton ( );
• Checkbox.

1.2.2 ! ' )$ -, , ! &

A 9
:
1) : 9 9 - , (
K .
2) : K8 7 Visual
Basic. : n Visual Basic.
3. J Insert Userform
Visual Basic 9 Insert Userform (: -
s ). ' ( . 1.2).

7 1.2 – 8
11

8 n -
Userform1. 8 n o K -
, 9 ( .
' 9 -
9'n , o 9 K .
A 9 ( 9 9 View
Properties Window (: : ) 9 -
.

1.2.3 ' ! ) 0 !%(/ # (.' ) # -

: ( 9 9 ( n
9 .u 9 , 9-
( 9 9 9 -
9 K: ( K 9 9
d .u , 9 n, 9 d
9 n , K ( ' 9 -
,o K . : VBA (n 9 ,
K 9 K 9 , 9 -
.
5 ( 9 9 – ,
' .

9 .5 ( n -
, o .5 , -
, 9 .
! – , -
K .' K ( 9
, d 9 ,o 9 K
K . 5 , ,
9 n 9 , 9 9 -
.
5 ( 9 K , -
d . -
. . 9 d -
n ( d . 9
9 ( VBA.
12

8 o 9 VBA n -
9 ( .
' ,o ( d .5 -
9 - –
.
" #$%& – (
9 .
– -
K .
u o 9 9-
, 9 9 d , 9 -
9 , ,o
n .
( ( :
1. ' ! ! ( )(*. : K , o VBA ( -
, , -
9 9 -
.A K -
, d — .A
, VBA n
, .
2. '
! ! &! .: K , 9-
d . ' K ( 9
9 ( , , -
). ' , ,
K VBA. ' ,
9 .' ( -
: ( 9 9 ,
( d , K d .
3. + (, ( ! ! ( d .y -
9 .
. n K ,o n9 ( -
.' K d -
K 9 ( VBA.
- ( )( – , -
K K, d .u o n -
, n , K
9 K ( n -
13

.A ( 9 -
( “Continue”
VBA. 09 , ( d
“Reset” , , .
' n -
K . , d -
K K VBA,
.7 Visual Basic n -
.
: K ,
( K 9 - 9 .
7 o K -
K :
• , -
;
• ( d , 9 d -
;
• ( d , K , ,o
' 9 ;
• d, .
' d d 9 (
K “Run
Run Sub/Userform” (“: : / -
”) 9 ( d F5. 2 9 -
9 . J “ ” ( 9 n
) n
Visual Basic.
7 9 K K n .'
9 o K -
, 9 9 , K -
9'n n K 9'n ( 9 K.

1.2.4 % / # !% !%

. n:
– ;
– .
14

2 !% !%( -/' b n :
Msgbox (' [,0 9 ] [, ]).
' &( – , 9 ( ,n 9 -
' .v 9
.6 ( 1024 .:
( n ,o Msgbox n nK, o -
n .
.$ ! %$! K 9 , 9
, 9 ( .0 .$ ! %$! n -
: K
, o , K, 9 ( -
.
v n 9 ( -
( 9 :
.$ ! %$! = ' $ 1+' $ 2.
' $ 1 Kn
( 9 . 1.1).

6 9 1.1 – ( 1 Msgbox

N -' 1 F%
0 : 9 ( n “OK”
1 : 9 ( K “OK” “Cancel” (: -
)
2 : 9 ( K “Abort” (8 ), “Retry” (' -
) “Ignore” (' )
3 : 9 ( K “Yes” (6 ), “No” (5 )
“Cancel” (: )
4 : 9 ( K “Yes” (6 ), “No” (5 )
5 : 9 ( K “Retry” (' ) “Ignore”
(' )
u o .$ ! %$! , VBA n,
o n “.J”.
' $ 2 n ,
( 9 . 1.2).
15

6 9 1.2 – ( 2 Msgbox

N -' 2 ,( !%( -/' ) N% # -


16 : 9 ( n -

32 : 9 ( n
( ( )
48 : 9 ( n
( ( )
64 : 9 ( n -

3 & – .
Msgbox .8 -
Msgbox 9 n . -
, o n , ( , -
.
9 . 1.3 ,o K nK
Msgbox.

6 9 1.3 – 8 , K nK
Msgbox

&' ) ,
1 “OK”
2 “Cancel” (: )
3 “Abort” (8 )
4 “Retry” (' )
5 “Ignore” (' )
6 Yes” (6 )
7 “No” (5 )

% !!'(' ), , ( 9 (
9 nK.
2 Inputbox n 9 .
v 9 ( n , o
,o Kn; :
16

Inputbox(' [, ] [, ]).

' &( –n 9 ' ; ( -


K , K 9 -
.
3 & – .
5 &, – , 9 9 (
, n .u o
, 9 ( n ( .
, o n K nK, n , o
. ,o n , ( -
, 9 d , -
9 . .
5 Msgbox .$ ! %$! , ' -
n 5 &, : n , -
9 9 . '
“.J” Kn ,
“Cancel” n ( .
: ,o Inputbox n ,
Msgbox n .

1.2.5 /# , -%& % c%d - !, VBA

J VBA d n . n
. J ( , o , n 9 -
K- nK, 9 K- K.
' ) % –9 - VBA, n -
.u , n ,o -
K - 9 9 ,o 9 KK .J ( -
n . ~
n nK - 9 (6. ,
o 7 (6.
! ' ) d c'( , n -
:
• 9 9 ;
• .
17

A 9 - K 9 -
.A ( , 9 ' .
5 , ( 9.
1. : 9 ( Excel) 9
Word.
2. : n ' !% '( Visual
Basic. : n Visual Basic.
3. : Vbaproject (J 1). K 9 -
Insert Module (: ). d 9 -
1.
4. : n Insert Procedure (: ' -
). : n Add Procedure (A ).
5. ' . 6 (6 ) -
Sub (' ). 5 “.J”. 5
9 .
' ( !, ,. A
n d :
1. : 9 K Run Sub Userform (: -
) K Run (: ) 9
Run Sub Userform .
2. 5 d F5.
3. 8 9 - d Excel '
.
' VBA n 9 K . A
o 9 9 K , K
. VBA K 9 -
: ('), ( 9 -
, Rem .'
n .
' With/End with – n -
9'n .
8 :
With '7 $
$ 1
$ 2
..............
$ 9
18

End with
' 8&% , VBA K
.€ 9
. . 9 9
9 9 ' - d , (
9 ,
. K -
.
c%) If . . . Then
J If ... Then n , 9
9 ( .8 n
n 9 -
:
1-0 %F:
If % & Then &! :
2-0 %F:
If % & Then
&! :
End If
n , ( 9
9 - 9 K .u o ,
K ( , o d K K
Then.
' c, / For ($% ! ' &, / ! ' b)
8 :
For : ( = M1 To M2 [Step M3]
$ !
ext
6 M1, M2, M3 – .. Kn
, ( ) Kn
1 2 3. u o
, n 1.
19

1.2.6 VBA ! ) ( MS Word

' Word 9 VBA nd (


9 9 . ( 9-
9 - 9 9 , . .
A 9 d -
K 9'n , Sections ( ), Paragraphs ( 9 ), Sentences
( ), Words ( ) .
' ! :
Words(6) – n ( d
.
Sentences(11) – .
' ( -
9'n Font, o n K d .v
9'n , o 9 K K “2
• ”.
.9'n Range n 9 .‚ ( -
9 ( ' -
K 9 K), ( ( . Range -
9 9'n , Words ( ) 9 Sentences ( );
9'n Range 9 ,
Sections ( ) 9 Paragraphs ( 9 ) 9 9 .
.9'n Range n , K
( , “J K ”, “: ”, “: -
”, K “2 ”).
A 9 9 ,o
KK .
A
, n (
, n n .
A 9 :
1. : 9 K “8 | |' ” ( . 1.3).
20

7 1.3 –

2. “ ” “„ ' ” ' -
.
3. : 9 “ ”d 9 9 -
, 9 9 .
4. “. ”.
5. “' ” “' ” (
9 K);
“J d ” ( 9 d);
o 9 , “OK”. u o
9 n - 9 , -
9 9 K d.
6. : , 9 K .
8 ' ,o 9 -
, , 9 K d, K .
7. 5 ( “ ”.

1.3 !( )( F ,

1.3.1 .
K 9 , K , ( -
.
21

1.3.2 K VBA -
« ( », « », « », « » ,
o .
1.3.2.1 MS Word.
1.3.2.2 ( VBA (8 7 Visual
Basic).
1.3.2.3 ' ,o 9 ' -
.
1.3.2.4 : : 9 Insert
Userform, “.9 ” “Toolbox”.
1.3.2.5 8 5
“Commandbutton” (‡, +, -, :, :ˆ ), “Label” (' -
,: ,. ), ( “TextBox” ( .
1.4). 9'n . 1.4.

7 1.4 – s 1.3.2
22

1.3.2.6 “‡”, o 9 -
. ' n Private Sub -
n nK End Sub ( ). ( -
d :
Label3.Caption = Str(CInt(TextBox1.Text) * CInt(TextBox2.Text))
n ( ,o
n TextBox1 TextBox2.
A ( (
, o ( Text 9’n
TextBox1 TextBox2) n
K CInt.
. ( n , Caption
9’n Label3 n 9 , n -
K Str.
1.3.2.7 0 “+”, “–”, “:” “: ”
9 :
Label3.Caption = Str(CInt(TextBox1.Text) + CInt(TextBox2.Text))
Label3.Caption = Str(CInt(TextBox1.Text) - CInt(TextBox2.Text))
Label3.Caption = Str(CInt(TextBox1.Text) / CInt(TextBox2.Text))
UserForm1.Hide.
1.3.2.8 ' K
“Run Run Sub/Userform” (“: |: /
”). 6 ( ( K
K 9 ( d d F5.
1.3.2.9 : Word “.9-
”, A 1 “.9 ”.
1.3.2.10 : “' s ” K
K .
1.3.2.11 : d , d
“: ” .
1.3.2.12 : “.9 ”,
“: ” .
1.3.2.13 d : Userform1.Show.
1.3.2.14 9 ( K 9 .
1.3.3 ' 9 ,
( 0e.y.:.J), -
23

d Arial 14 -
9 ( . 1.5).

7 1.5 – : 1.3.3

1.3.3.1 5 9 MS Word.
1.3.3.2 8 9 .
1.3.3.3 ' “J 9 ”. A
“8 ”. '
“Properties”. : 9 “Caption” -
9 .
1.3.3.4 A 9 , o 9
( 9 o d .A 9'n -
- ( K 9 “2 9Œ-
”. ' “2 • ”. 5
“' ( ” 9 “' ”.
24

1.3.3.5 : 9 - ( K 9'n -
“Ž ˆ ” 9 .: n -
( .
1.3.3.6 ,o , :
a) k – 9 ;
b) Text – -
9 ;
c) ZAGOLOVOK – ,o n 9
.
d) REZULTAT – 9 ( 9 ,
9 9 .
1.3.3.7 : 9 K Count
9'n Paragraphs.
1.3.3.8 8 .
1.3.3.9 : 9 9 -
.
1.3.3.10 : - 9 .
1.3.3.11 8 9
“ 0e.y.:.J”.
1.3.3.12 : “ 0e.y.:.J”.
1.3.3.13 d .
1.3.3.14 : 9 ,o 9 -
9 .
6 9 9 . 8.4.

6 9 1.4 – 6 1.3.3

)( # -, N /# , -
Public Sub CommandButton1_Click() '
Dim k As Integer 1.3.3.6 a
Dim Text As String 1.3.3.6 b
Dim ZAGOLOVOK As Range 1.3.3.6 c
Dim REZULTAT As Range 1.3.3.6 d
k = ActiveDocument.Paragraphs.Count 1.3.3.7
25

' ( 9 . 1.4

)( # -, N /# , -
Text = "J 9 -
1.3.3.8
- " & k & "."
ActiveDocument.Paragraphs(k).Range.Inser
1.3.3.9
tParagraphAfter
ActiveDocument.Paragraphs(k +
1.3.3.10
1).Range.InsertBefore Text
Set REZULTAT = ActiveDocu-
ment.Paragraphs(k + 1).Range
9
With REZULTAT
:
.Font.Name = "Arial"
–d – Arial
.Font.Size = 14
– d – 14 ;
.Font.ColorIndex = wdDarkRed
– –
End With
Set ZAGOLOVOK =
1.3.3.11
ActiveDocument.Range(Start:=0, End:=0)
With ZAGOLOVOK ' With
.InsertBefore " 0e.y.:.J" 1.3.3.12
.Font.Name = "Arial" 1.3.3.13
.Font.Size = 24 1.3.3.13
.InsertParagraphAfter 1.3.3.14
End With J With
End Sub J

1.3.4 K VBA ,
.1.3.4.1–1.3.4.10, d .
1.3.4.1 8 ,
Label ( ), CommandButton ( ). ' -
“ ” .
v 9 ( Label ( ). 6 (
9 ,
. J
, .' K “: -
26

” 9 .A
MS Excel 9 MS Word 9 -
“2 VBA”.
1.3.4.2 : K MsgBox InputBox -
, o , o
. A MS Word 9 MS Excel
n .
1.3.4.3 : K -
( , / ) -
, 9 o , o -
.A MS Word 9
MS Excel 9 .
1.3.4.4 8 MS Word 9 MS Excel
, K ( , ,
( ( . .) , K .
7 ( . . 8.6).

7 1.6 – : 1.3.4.4
27

1.3.4.5 5 y 1 MS Excel 01–020


(–50; 50).
5 VBA (
, ,
.
A ( MS Excel.
: ( K 9'n Application:
Application.2 9 (0 ).
1.3.4.6 5 ,o 9
MS Word 9 Kn 9 « » -
( 9 9 , 9 ) -
9 .
1.3.4.7 5 ,o MS Word 9 Kn
-
9 .
1.3.4.8 5 ,o MS Word
Kn
d : d – Arial, – 20 ,( , –
.
6 ( 9 K
9 ,o 9 d .
1.3.4.9 5 ,o MS Word
9 9 d K K , Kn d
9 -
9
.
A ( 9 9 Kn ( -
),
9 n 9 9 d K K .
1.3.4.10 5 ,o MS Word
- n ( -
( ,o d
, : •0y0•, 50e05, 0A0 .).
1.3.5 . 9 .
1.3.6 : .
28

1.4 -% !%

1.4.1 6 9 .
1.4.2 9 .
1.4.3 J .
1.4.4 6 9 9 .
1.4.5 7 9 .
1.4.6 : , o
(5 d . 9 ), ( 9 ( K -
9 .

1.5 /b % , )

1.5.1 ” Visual Basic for Application?


1.5.2 ' Visual Basic for Application.
1.5.3 u Visual Basic Ms Word?
1.5.4 u d ?
1.5.5 u : n ?
1.5.6 . d .
1.5.7 u « K»?
1.5.8 u 9 ,o 9 -
?
1.5.9 u ?
1.5.10 u ?
1.5.11 u K VBA?
1.5.12 u K K VBA?
1.5.13 ~ K 9 VBA
Word?
1.5.14 u VBA 9 Word?
1.5.15 u ?
1.5.16 u K VBA?
1.5.17 u 9 ?
1.5.18 u d d K -
9 VBA?
1.5.19 ' K VBA. '
K d .
1.5.20 VBA?
29

2 OM -P Q R * 2
o P M M #

2.1 ' F ,

9 9 -
K 8#.

2.2 ! % ' ' ,& % !%( - %

s – ,o 9 K n -
K ( K. 8 Visual Studio.NET n
, ( -
.
e K. € d K -
: 9 n -
, d – d . 0 9 -
9 K, o 9
( d . s MainMenu,
ContextMenu K 9 K -
K. ' 9 -
9 .
A .: K – -
, 9 ( , , ,– n -
. 7 9 . NET
:
OpenFileDialog, SaveFile Dialog, ColorDialog, PrintDialog (
.
s Button, LinkLabel, ToolBar K -
d K 9 - K. 5 9 d
d .
€ d K (
, K , K
.s TextBox, RichTextBox K ,
Label, StatusBar .A 9 9 -
, , 9
9'n .
30

e . A (
9 ; n
K ( 9 .v 9 d -
, ComboBox, ListBox, ListView,
TreeView, NumericUpDown 9 d .
e . n 5 -
' K , K -
d , -
. u , n , d ,
( K , , –
K . s Panel, GroupBox, TabControl,
d , K ( ,
9 K 9 .
e . 5
Windows - , , 9 9 ( .A
o 9 ( K
9 K – Image List, Picture Box.

2.2.1 ! ' )# / ! # -' 8

€ d Windows- o K,
n 9 K n -
.' , ( K -
, K K.
A K n
MainMenu, d ToolBox. 8
NotepadCSharp. : -
. ' K
9 . 8.1.
6 9 2.1 – 8 K

Form1, $ - , &' )
!/ ,!% b
Name frmmain
Icon Code\Glava2\NotepadCSharp\Icon\README.ICO
Text Notepad C#
WindowState Maximized
31

' n . 5 (
K ( .2.1):

7 2.1 – ' K Notepad C #

J ( K n n ,
, 9 d , K
Name Text (
. 2.2). Text New
&– , n, o N 9
9 K 9
Windows. J n d Alt
N, K New.

7 2.2 – : K New
32

Windows n o 9 d
d , 9 . ' n d K
Shortcut. 8
n d. e
n , 9
9 ; Text K
.A K ,
( K ( Name), 9 ( ,
o K 9 n 9 . :
K Notepad C # 9 . 2.2.

6 9 2.2. ' K Notepad C #

`ame Text Shortcut


mnuFile &File
mnuNew &New CtrlN
mnuOpen &Open CtrlO
mnuSave &Save CtrlS
menuItem5 -
mnuExit &Exit AltF4
mnuEdit &Edit
mnuCut Cu&t CtrlX
mnuCopy &Copy CtrlC
mnuPaste &Paste CtrlV
mnuDelete &Delete Del
mnuSelectAll &SelectAll CtrlA
mnuFormat F&ormat
mnuFont Font…
mnuColor Color…
mnuWindow &Window
mnuArrangeIcons Arrange Icons
mnuCascade Cascade
mnuTileHorizontal Tile Horizontal
mnuTileVertical Tile Vertical
mnuHelp ?
mnuAbout About Programm...
33

: ( 9 d,
9 2.2, K.

2.2.2 ! ' ) MDI-( ( %!

6 , 9 Microsoft Paint, SDI


(Single – Document Interface) , K
.„ d, Microsoft Word 9 Adobe Photoshop,
K 9 K
MDI (Multiple – Document Interface) ( . 2.3 2.4).

7 2.3 – SDI Microsoft Paint

MDI- 9
, ( n 9
. ' ( 9
Notepad C #. Solution Explorer n K K
K 9 n Add / Add Windows
Form .... n – blank.cs. d
' - 9 K. (
34

RichTextBox:
textBox,
9 ( n 64 J9; , RichTextBox n
, 9 ( . : Dock
Kn Fill ( . 2.5):

7 2.4 – MDI- Adobe 2 d

7 2.5 – : Dock RichTextBox


35

' ( Frmmain Kn
IsMdiContainer true. J n
- .5 9 '
K New ( n d Ctrl + N),
n :

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


{
blank frm = new blank();
frm.MdiParent = this;
frm.Show();
}

n .6 d Ctrl + N 9
9 K New ' n , d
. . – blank. '
, , Microsoft Word,
K A N, N– .' n
blank, blank d n DocName:

public string DocName = "";

' n frmmain frmmain


d n openDocuments:

private int openDocuments = 0;

5 n DocName d 9 ,
K ,
n Text K frm:

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


{
blank frm = new blank();
frm.DocName = "Untitled " + ++openDocuments;
frm.Text = frm.DocName;
frm.MdiParent = this;
frm.Show();
}

n .6 -
( . 2.6).
36

7 2.6 – 5 K

2.2.3 N' ' ! ) MdiLayout

' 9 MDI-
. ( , ,
, 9 K K
n . K Window n
K .

8 Kn 9 9 :

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


{
this.LayoutMdi(MdiLayout.ArrangeIcons);
}

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


{
this.LayoutMdi(MdiLayout.Cascade);
}
37

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


{
this.LayoutMdi(MdiLayout.TileHorizontal);
}

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


{
this.LayoutMdi(MdiLayout.TileVertical);
}

LayoutMdi MdiLayout, o
. ArrangeIcons n 9 ,
MdiList K ArrangeIcons Kn (
true. '
d K ( . 2.6), ( d
– TileHorizontal 9 –
TileVertical, d – Cascade
( . 2.7):

7 2.7 – 7 d TileHorizontal K
ArrangeIcons
38

2.2.4 , % ), %8! )%! ! ' !, $ #-' %!

K 9 d , o
9 K 9
. A WindowState blank
Maximized. 6 9 9
, K . s
RichTextBox n SelectedText,
. 5 n 9
9 . blank d n
BufferText, 9 9 9
:

private string BufferText = "";

A Kn :

public void Cut()


{
this.BufferText = richTextBox1.SelectedText;
richTextBox1.SelectedText = "";
}

public void Copy()


{
this.BufferText = richTextBox1.SelectedText;
}

public void Paste()


{
richTextBox1.SelectedText = this.BufferText;
}

public void SelectAll()


{
richTextBox1.SelectAll();
}

public void Delete()


{
richTextBox1.SelectedText = "";
this.BufferText = "";
}
39

' n ( Kn 9 9
K:

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.Cut();
}

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.Copy();
}

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.Paste();
}

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.Delete();
}

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.SelectAll();
}

: ActiveMdiChild n ,
o , n ,
. n .6 (
.

2.2.5 ' ' -' 8

J K, 9 Kn K, -
d d 9 9 K,
. s TextBox 9
K, 9 Kn K Edit. A o 9
40

,
( . 2.8):

7 2.8 – J K TextBox

d Notepad C #
n RichTextBox. A
contextMenu ToolBox blank. A n
K , 9
K( . 2.9):

7 2.9 – ' K

: Text Shortcut K d n
.u o ShowShortcut
false, d9 K ,
K 9 ( 9 – . 2.8. : Name 9
: Cut – cmnuCut, 8opy -
cmnuCopy . .
9 9 n :
41

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


{
Cut();
}

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


{
Copy();
}

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


{
Paste();
}

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


{
Delete();
}

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


{
SelectAll();
}

. n, o d 9 ,– , 9
' K. s RichTextBox, -
frmmain blank, n ContextMenu, n
contextMenu1, 9 9 ( K -
. n – 9 - -
K ( . 2.10).

7 2.10 – J K
42

2.2.6 % / # !% !%

8 Visual Studio. NET , o


K 9 9 d
9 ( ( . 2.11).

7 2.11 – A

OpenFileDialog. A frmmain
OpenFileDialog ToolBox. ' 9
MainMenu, 9 d
( . 2.12):
: FileName n , 9
"„ ' :" .5 . . 2.12
– "6 ", . . 2.11 9
.: Filter n 9 ( , ( 9
9 – 9
d . ~ K (
d , 9 ( K
43

"6 ". 6 Text Files (*. txt) | *. txt | All Files


(*.*)|*.* o n 9 , 9 .:
InitialDirectory n K, 9
.u o , nK 9
9 .

7 2.12 – A OpenFileDialog

A 9 blank K-
n System.IO:
using System.IO;

Open n RichTextBox:
public void Open(string OpenFileName)
{
if (OpenFileName == "")
{
return;
}
else
{
StreamReader sr = new StreamReader(OpenFileName);
richTextBox1.Text = sr.ReadToEnd();
44

sr.Close();
DocName = OpenFileName;
}

' 2.1.

A 9 9 K Open frmmain:

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


{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
blank frm = new blank();
frm.Open(openFileDialog1.FileName);
frm.MdiParent = this;
frm.DocName = openFileDialog1.FileName;
frm.Text = frm.DocName;
frm.Show();
}
' 2.2.

n n , 9 -
( 9 ( . 2.13).

7 2.13 – n 9 K '

A 9 9
9 ( Unicode. 5 ( , 9
OpenFileDialog Visual Studio. NET , o
n 9 9
9 ( , , , 9 .
45

SaveFileDialog. A 9 ( n
frmmain saveFileDialog1. :
(, OpenFileDialog ( . 2.12). '
blank:
public void Save(string SaveFileName)
{

if (SaveFileName == "")
{
return;
}
else
{
StreamWriter sw = new StreamWriter(SaveFileName);
sw.WriteLine(richTextBox1.Text);
sw.Close();
DocName = SaveFileName;
}
}

' 2.3.

A n 9 9 K Save frmmain:

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


{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
blank frm = (blank)this.ActiveMdiChild;
frm.Save(saveFileDialog1.FileName);
frm.MdiParent = this;
frm.DocName = saveFileDialog1.FileName;
frm.Text = frm.DocName;

}
n . 6 ( ,
9 . . , 9 ( (
9 ( ' n
SaveFileDialog. d , o 9 ( 9
9 . frmmain
InitializeComponent K K Save:
46

mnuSave.Enabled = false;
' n ( frmmain n
K Save As Save. : Kn
: Name – mnuSaveAs, Shortcut – CtrlShiftS,
Text Save & As. 9 9 Save As n 9 9
Save n K Save:

mnuSave.Enabled = true;

9 9 o 9 ( ,
, d .
6 Open K K Save:

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


{
mnuSave.Enabled = true;
}

9 9 Save –
Save blank:

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.Save(frm.DocName);
}

n .6 , o Kn 9 (
, Save ( . 2.14), 9 (
n ( . 2.15) , , Kn n d Ctrl +
S. ( 9 K , d
K Save As ( . 2.16).
47

7 2.14 – 5

7 2.15 – 9 (

7 2.16 – 9 (

, ( d 9 d –
n d 9 9 ( . 0
K ( 9 (
, , K
K.
48

2.2.7 F' '.' ) $ 0/ , , %$ -,

J ( , n Microsoft Word,
, ' n ( , o n
9 . A K d .
blank: System.Windows.Forms.Form blank Kn
, 9 9 ( :

public bool IsSaved = false;

9 9 Save Save As frmmain n -


n :

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


{

frm.IsSaved = true;
}

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


{

frm.IsSaved = true;
}

' ( blank
n , d 9 K.
Closing n :

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


{
if(IsSaved ==true)
if(MessageBox.Show("Do you want save changes in " + this.DocName + "?",
"Message", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)

{
this.Save(this.DocName);
}

' 2.4.
49

n . ' blank 9 frmmain


' n ( ( . 2.17):

7 2.17 – : (

2.2.8 OpenFileDialog % SaveFileDialog (/) SDI-( ( %!

' MDI-
9 ( , 9 Notepad C
#. SDI- 9 9 9 .
8 , n TextEditor. 5
o n TextBox Kn
.' TextBox 9 . 2.3.

6 9 2.3 – TextBox

TextBox,
Name txtBox
Dock Fill
Multiline true
Text A

A n MainMenu, 9
– File, Open Save ( . 9 2.2).
ToolBox OpenFileDialog
SaveFileDialog – (,
50

Notepad C #. ' .
' K n 9 :

using System.IO;

A n 9 9 K Open:

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


{
openFileDialog1.ShowDialog();
string fileName = openFileDialog1.FileName;
FileStream filestream= File.Open(fileName, FileMode.Open, FileAccess.Read);
if(filestream != null)
{
StreamReader streamreader = new StreamReader(filestream);
txtBox.Text = streamreader.ReadToEnd();
filestream.Close();
}
}

' 2.5.

A n 9 9 K Save:

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


{
saveFileDialog1.ShowDialog();
string fileName=saveFileDialog1.FileName;
FileStream filestream = File.Open(fileName, FileMode.Create, FileAc-
cess.Write);
if(filestream != null)
{
StreamWriter streamwriter = new StreamWriter(filestream);
streamwriter.Write(txtBox.Text);
streamwriter.Flush();
filestream.Close();
}
}

' 2.6.

n .u ( ,
TextBox n 9 K, n d
, 9 ( K
d K d .
51

5 , , TextEditor
(Code \ Glava2 \ TextEditor).

2.2.9 FontDialog

' ( 9 Notepad C #. A
( 9 d , . (
frmmain ToolBox
FontDialog. 5 KK
, 9 9 Font K:

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.MdiParent = this;
fontDialog1.ShowColor = true;
fontDialog1.Font = frm.richTextBox1.SelectionFont;
fontDialog1.Color = frm.richTextBox1.SelectionColor;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
frm.richTextBox1.SelectionFont = fontDialog1.Font;
frm.richTextBox1.SelectionColor = fontDialog1.Color;
}
frm.Show();
}

' 2.7.
n , Output ' n
( . 2.18).

7 2.18 – 8 – NotepadCSharp.blank.richTextBox1
-
52

( ? ' d blank
RichTextBox Visual Studio.NET
richTextBox1 System.Windows.Forms.RichTextBox
private, o
n :

private System.Windows.Forms.RichTextBox richTextBox1;

public .'
9 K Font ( K
.

2.2.10 ColorDialog

A FontDialog , (
9 , 9 ( .
€ d K n (
, ( 9 ( . 2.19):

7 2.19 – A ColorDialog
53

ToolBox n ColorDialog ,
KK , 9 9
Color K frmmain:

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


{
blank frm = (blank)this.ActiveMdiChild;
frm.MdiParent = this;
colorDialog1.Color = frm.richTextBox1.SelectionColor;

if (colorDialog1.ShowDialog() == DialogResult.OK)
{
frm.richTextBox1.SelectionColor = colorDialog1.Color;
}

frm.Show();
}

,o ColorDialog
( , Color FontDialog. v
: ( ' n (
9'n – RichTextBox.

2.2.11 StatusBar

s StatusBar n
– ( , d
. Microsoft Word, , 9 ( n
, , d
. A Notepad C # ,
Kn , o
.
A n blank StatusBar.
: n Text. Panels
n (...). : n StatusBarCollectionEditor,
Kn 9 ( . 8 ,
K Add,
( K ( d ) ( . 2.20
. 2.21):
54

7 2.20 – : sbAmount

7 2.21 – : sbTime
55

9 . 2.4.

6 9 2.4 – :

/ ,!% b &' )
Alignment : K Text
AutoSize
BorderStyle d – , 9
9
Icon A
Style 8
Text 6 ,o d n
ToolTipText 8 K – ' n

Width •
Name 5

: , K
StatusBarCollectionEditor, ( K –
9 .
' d 9 n .
: ShowPanels StatusBar Kn
True. 5 9 ( K .
: n RichTextBox,
n Kn 9 9 TextChanged:

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


{
sbAmount.Text = "0mount of symbols" + richTextBox1.Text.Length.ToString();
}

: Text ' sbAmount Kn : -


o 9 StatusBarCollec-
tionEditor, TextChanged ' .
K – n, 9
. blank n :
56

public blank()
{
InitializeComponent();
sbTime.Text = Convert.ToString(System.DateTime.Now.ToLongTimeString());
sbTime.ToolTipText = Convert.ToString(System.DateTime.Today.ToLongDateString());
}

n . '
d n ( . 2.22), 9
( (
K ( K .

7 2.22 – 7

2.2.12 ,! )$ -,

' Notepad # n
-" ", "7 " " ". € d
( K o " "
. ' 9 K
K Exit. 9 9 n n

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


{
this.Close();
}

Close n ( 9 d
– , .
57

2.2.13 CheckBox

~ 9 ,
9 n 9 - .
K Kn ,
d .A
Notepad C # d , 9
CheckBox. Solution Explorer
n K K d – NotepadCSharp –
K 9 n Add Windows Form. 5 n
FindForm.cs Kn .'
9 . 2.5.
6 9 2.5 – 5

FindForm, $ - , !/ ,!% b &' )


Name FindForm
FormBorderStyle FixedToolWindow
Size 328; 112
Text Find

n TextBox, ChekBox
, . 2.23:

7 2.23 – 2 FindForm (

: Kn
9 . 2.6 – 2.10 :
58

6 9 2.6 – 2 FindForm, TextBox

TextBox, !/ ,!% b &' )


Name txtFind
Size 192; 20
Text

6 9 2.7 – 2 FindForm, checkBox1

checkBox1, !/ ,!% b &' )


Name cbMatchCase
Text Match &Case

6 9 2.8 – 2 FindForm, checkBox2

checkBox2, !/ ,!% b &' )


Name cbMatchWhole
Text Match &whole word

6 9 2.9 – 2 FindForm, button1

button1, !/ ,!% b &' )


Name btnOK
DialogResult OK
Text &OK

6 9 2.10 – 2 FindForm, button2

button2, !/ ,!% b &' )


Name btnCancel
DialogResult Cancel
Text &Cancel
: 9 d 9 – cbMatchCase – 9 K
d , – cbMatchWhole – d
. DialogResult –
59

9 K , Kn 9 9
9 . .J n ,
K K, – – d ;
Cancel n. frmmain K
n K Find Kn
: Name – mnuFind, Shortcut – CtrlF, Text – & Find.
' 9 9 :

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


{
FindForm frm = new FindForm();
if(frm.ShowDialog(this) == DialogResult.Cancel) return;
blank form = (blank)this.ActiveMdiChild;
form.MdiParent = this;
int start = form.richTextBox1.SelectionStart;
form.richTextBox1.Find(frm.FindText, start, frm.FindCondition);
}

. K d 9 Find
RichTextBox. 5 d n d d ,
( ( . 2.24):

7 2.24 – ' Find. J K


Intel Sense, 9 n -

FindForm d 9 ,
o ( ( CheckBox:

public RichTextBoxFinds FindCondition


{
get
{
if (cbMatchCase.Checked && cbMatchWhole.Checked)
{
return RichTextBoxFinds.MatchCase| RichTextBoxFinds.WholeWord;
60

}
if (cbMatchCase.Checked )
{
return RichTextBoxFinds.MatchCase;
}
if (cbMatchWhole.Checked )
{
return RichTextBoxFinds.WholeWord;
}
return RichTextBoxFinds.None;
}
}

„ d , Kn FindText, n
d FindForm:

public string FindText


{
get{return txtFind.Text;}
set{txtFind.Text = value;}
}

n .: 9 K , ( -
d ( . 2.25):

7 2.25 – ' d
2.2.14 / ,!% b TabIndex '/'-' %! !/% )
61

€ ( K K o 9 d
K d , 9
( 9 .' 9 K Find 9 9 -
, , d d Tab Enter, -
d ,– 9 , d d Tab, K -
9 Match Case. A ( -
Find TabIndex
, K .' -
TabIndex 9 . 2.7.
6 9 2.7 – : TabIndex

/'-' !/% ) TabIndex


TxtFind 0
BtnOK 1
CbMatchCase 2
CbMatchWhole 3
BtnCancel 4

' 9 -
– txtFind btnCancel. : -
TabIndex n 9 -
, 9 K.
Label, LinkLabel PictureBox
' , , K "' -
", ,
, 9 d .8 -
9 , K – Label,
LinkLabel PictureBox.
. ( , d About.cs.
: :

6 9 2.8 – 5 About.cs
62

About, $ - , !/ ,!% b &' )


Name About
FormBorderStyle FixedSingle
MaximizeBox False
MinimizeBox False
Size 318; 214
Text About Notepad C#

A PictureBox – -
n 9 K , o n , (
9 K 9 ( . Image n
(...) 9 n 7 K Code \ Glava2 \
NotepadCSharp \ Icon \ logo.gif. . logo.gif n K -
, PictureBox n K K (,
( .
ToolBox , Label
LinkLabel. Text & OK. s
Label o ,
9 . Text
Notepad C # 2006 All rights reserved. s LinkLabel -
9 ( n web- -
n ( 9 .
Text –
www.notepadcsharp.com. J 9 , -
d , n
Click. Properties n Click n-
9 9 :
private void linkLabel1_Click(object sender, System.EventArgs e)
{
try
{

VisitLink();
}
catch (Exception ex )
{
MessageBox.Show(ex +"Unable to open link that was clicked.");
}
63

}
private void VisitLink()
{
linkLabel1.LinkVisited = true;
System.Diagnostics.Process.Start("http://www.notepadcsharp.com");
}
J OK 9 :

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


{
this.Close();
}

K About Programm ... frmmain -


n About:

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


{
About frm = new About();
frm.Show();
}

n ( . 2.26):

7 2.26 – : About Notepad C #


64

2.3 !( )( F ,

2.3.1 .
K 9 , K , ( -
.
2.3.2 : :
7 «Notepad C #». ' 9 ( -
, 9 ( , , .'
, -
9 -
.
2.3.4 . 9 .
2.3.5 : .

2.4 -% !%

2.4.1 6 9 .
2.4.2 9 .
2.4.3 J .
2.4.4 6 9 9 .
2.4.5 7 9 .
2.4.6 : ,o ,
( 9 ( K 9
.

2.5 /b % , )

2.5.1 ” C#?
2.5.2 ” ?
2.5.3 u n C#?
2.5.4 ” MDI?
2.5.5 ” ?
2.5.6 : ActiveMdiChild C#?
2.5.7 StatusBar?
2.5.8 LayoutMdi?
2.5.9 ” K?
2.5.10 ” ?
2.5.11 ” OpenFileDialog?
65

2.5.12 ” SaveFileDialog?
2.5.13 ” FontDialog?
2.5.14 : TabIndex ?
2.5.15 ' .NET Framework?
2.5.16 „ . NET Framework?
2.5.17 € 9 . NET Framework
2.5.18 u nc 9 99 .
2.5.19 ” .
2.5.20 ” ClassView?
2.5.21 ” ?
2.5.22 ” ?
2.5.23 J Console?
2.5.24 J MessageBox?
2.5.25 J Random?
2.5.26 6 ˆ ˆ C#
2.5.27 ” C #?
2.5.28 ” 9 C #?
2.5.29 . Form
2.5.30 ” ?
66

3 OM -P Q R *3
o Z o o R M #

3.1 ' F ,

3.1.1 5 K K.
3.1.2 5 9 9 .

3.2 ! % ' ' ,& % !%( - %

3.2.1 ' 8 0 # ,

K– ,o
n .7
K n ,o -
9 . -
, K 9
9 .

3.2.1.1 ' ' -' 8

J K 9 ,
( .J K -
KK 9 ContextMenu. A o 9 ’
9’n 9 9’n ,
K K9 ’ K —> -
ContextMenuStrip 9 9 K.
' KK K (
9 , :
Menultem item = (Menultem) sender; // item Kn
9’n sender d .. ,o
( K K, n -
Menultem.
string text = item.Text;// .9’n Menultem K -
Text, d n K.
67

3.2.1.2 %(-%&' % , -' 8

u o MS Windows —
«J » «: », 9 o n
K. A o 9 K -
9 9 Checked -
True, CheckState 9 n : Inderter-
minate – , Checked – , Uncheked – -
.
A o 9 9 K -
9 9 9 K—>
CheckOnClick true.

3.2.1.3 N (%/ '/'-' %! -' 8

' 9 K K
nK- .7 K K -
K, n 9 , 9 d
.
” 9 K , K -
— .

3.2.1.4 / !%+% (

5 K Alt 9 , d K -
, d n K. 8 d d K , -
( ( 9 K. 5 , 9
Alt+F n K File. : , 9 -
d , : 9 9 -
K.
A n d -
K, o d . 5 ,
n Alt+C K Close File
Copy Edit. . d -
K n , Alt C
Cut 8 K Edit. u o n nK, -
d 9 K,
68

, 9 - 9 d -
d Enter.

3.2.1.5 P )&% / !%+%

e d KK
Shortcut – 150 ,o -
K.
A K K d
9 ShortKeys 9 9
d.
Win32 – d ( -
d K. 0
Windows Forms d (
K. e d ,o ’ K, ( -
, ’ d K nK d K, K-
d n K d K Visi-
ble false. ' 9 , d d 9 -
K .

3.2.1.6 ,! c%) (' ,! c%) - ( -' 8

( Kn Enabled. : d
false, n K, 9 K -
, d d -
d.A K 9 - .

3.2.1.7 ! F .' b (/) %! -' 8

: 9 K 9 Image.
' ’ ( .3.1):
69

7 3.1 – 9 ( ( 9 (
K

: 9 n 9 :
Local resources – o 9 , 9 ( -
K 9
d . ' d ’K 9 -
9 K K 9 ( , 9
n .
Project resource file – o 9 , 9 (
9 K , d
’K K .
A n Import 9 n 9 .
' n .J. : 9 9 ( d n
d K.
A ( K ( 9 -
.

3.2.1.8 ! ' ) -' 8 # -

' K,
K “2 ”( “: ” “: ”) A (
“' ”) ’ n -
.
using System;
70

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
this.Text = "456789 :9;<";

// >6?6@;9 :9;<
MenuStrip menu = new MenuStrip();
this.Controls.Add(menu);

// A6BCD? File
ToolStripMenuItem itemFile = new ToolStripMenuItem();//E8@659;;F
B:D;;6G, FHI JKC9 @DCL6@DCI8M BI LK;H8 :9;< NIO?
itemFile.Text = "&NIO?"; // QIB@I LK;H8K
menu.Items.Add(itemFile);// R?9:9;8 @95S;T6U6 5D@;F

// A6BCD? Help
ToolStripMenuItem itemHelp = new ToolStripMenuItem();
itemHelp.Text = "&W6L6:6UI";
menu.Items.Add(itemHelp);

// XIL6@;9;;F 56BCD?K File


// Y6:I;CI Open...
ToolStripMenuItem itemOpen = new ToolStripMenuItem();
itemOpen.Text = "&[DCH5M8M...";
itemOpen.ShortcutKeys = Keys.Control | Keys.O; // Ctrl+O
itemOpen.ShowShortcutKeys = true;// [DC6J5I_9;;F @ H6:I;CD
itemOpen.Click += OpenOnClick;
itemFile.DropDownItems.Add(itemOpen);
// itemOpen.ShowShortcutKeys = false;// EH5M8M H6:JD;IbD<

// A6BCD?<@Ic
ToolStripSeparator itemSep = new ToolStripSeparator();
itemFile.DropDownItems.Add(itemSep);

// Y6:I;CI Exit
ToolStripMenuItem itemExit = new ToolStripMenuItem();
itemExit.Text = "&[MSDC";
71

itemExit.Click += ExitOnClick;
itemFile.DropDownItems.Add(itemExit);

// XIL6@;9;;F 56BCD?K Help


// Y6:I;CI About...
ToolStripMenuItem itemAbout = new ToolStripMenuItem();
itemAbout.Text = "&456 L56U5I:K...";
itemAbout.ShortcutKeys = Keys.Control | Keys.A; // Ctrl+A
itemAbout.ShortcutKeyDisplayString = "Ctrl^A";// E8@659;;F "UI5FcMS"
H?I@De C?F bT6U6 :9;<
itemAbout.ShowShortcutKeys = true;// [DC6J5I_K@I8M @ H6:I;CD
itemAbout.Click += AboutOnClick;
itemHelp.DropDownItems.Add(itemAbout);
}

// fJ56J;MH L6CDO
void OpenOnClick(object sender, EventArgs e)
{
MessageBox.Show("XIU68D@?F 6LbDG \"[DCH5M8M\"", this.Text);
}

void ExitOnClick(object sender, EventArgs e)


{
this.Close();
}

void AboutOnClick(object sender, EventArgs e)


{
MessageBox.Show("XIU68D@?F 6LbDG \"456 L56U5I:K...\"", this.Text);
}
}
}

3.2.2 F F #' ' c%) !, ) %!

5 ( K K -
( .7
, K .u o
9 n 9 , , -
, o 9
Kn, . Visual C # K 9 9-
, n K , 9 9 -
, , ( . J ,
( K 9'n

.
72

3.2.2.1 N , c, , F F , !, ) %!

: d , n
(exception). : 9 K
, o System.Exception. v K -
, 9 K 9 9 K, -
:
– Message —
K d ( ;
– StackTrace — 9 n ; -
9 n d , .
5 ,o K K, n -
K
System.ApplicationException. ' -
Kn K n
, , .
n , 9 , o
.u o n , 9 9 -
, 9 9 n , d n -
. .u o , 9 9
d , n 9 9 -
, .: 9 ( n -
9 - , 9'-
n , n , K 9
9 .
9 9 n
9 9 , , 9
.

! ' ) F F , %! !, ) %!
.9 9 K . ( -
9 9 9 , n
9 9 9 ,
n. .9 9
, K , K
. A 9 9 try
... catch ... finally.
73

try–9 Kn , o n
, ( 9
.
catch–9 Kn , 9 9 Kn ,
o 9 K 9 try.
finally–9 Kn ,o o n9 - -
n d , 9 9 try catch.
. 9 9 :
– , , 9 try;
– 9 9 catch, o 9 9 K -
, ( 9 try;
– 9 finally , 9 9 -
.v 9 ( ,
.
: :
try
{
// iK8 B;IS6CM8T7F B@McIO;MO H6C L56U5I:M
}
catch
{
// fJ56JHI L6:M?6H
}
Finally
{
// fcMj9;;F
}
. catch ( n 9'n
.v , o 9 9
n 9'n .J
, n K K
9 catch, 9 9 . 6 ,
, ( :
9 finally o K , 9 -
( , .v ( 9 , 9 n
, n 9 , n
.. 9 try. .. catch .. finally:
5 n 1/x 9
9 :
static void Main()
{
int x DE4A
74

int y =1 / x;
Console.WriteLine(y);
}
v n x>0. u o -
, n :
static void Main()
{
try
{
int x = int.Parse(Console.ReadLine());
int y =1 / x;
Console.WriteLine("y={0}", y);
Console.WriteLine("J?6H try @MH6;I;6 @CI?6 ");
}
catch
{
Console.WriteLine("@M;MH?I L6:M?HI");
}
Console.WriteLine("HD;9bT L56U5I:M");
}

A 9 9 9 . 3.1.

6 9 3.1 – A 9 9

5 .
ArithmeticException '

ArrayTypeMismatchException 8 9 9 (

DivideByZeroException 8 9
FormatException 8 9

IndexOutOfRangeException „ K

InvalidCastException '
OutOfMemoryException 5 ’
9’n
OverflowException ' -

StackOverflowException '
75

A o 9 9 9 9 :
– 9 try , , -
9 9 9 ;
– o 9 9 ,
9 9 catch ;
– , 9 ( -
9 finally.

/ Exception
7 9 Exception, o 9 ,
K ( 9 9 , -
n 9'n , o n .
. n:
– Message — ,o n . -
n Kn -
, Kn 9'n , o n ;
– HelpLink — (URL) , o
( -
9 ;
– InnerException — d .J 9-
9 n 9 9
, n d K
;
– Source — ' ,o K ;
– StackTrace — K( — ,o 9 K
;
– TargetSite — , .
GetBaseException. ' -
K( n —
d .
J n , ( .
. — 9 , — n ,
o n K Message, — no : -
, InnerException.
K Kn -
InnerExceptions. A (
76

, o n 9 9
:
static public void PrintProperties (Exception e)
{
Console.WriteLine ("[?I78M@678D @M;F8HK:");
Console.WriteLine ("TargetSite = {0}", e.TargetSite);
Console.WriteLine ("Source = {0}", e.Source);
Console.WriteLine ("Message = {0}", e.Message);
if (e.InnerException == null)
Console.WriteLine ("InnerException = null");
else Console.WriteLine ("InnerException = {0}",
e.InnerException.Message);
Console.WriteLine ("StackTrace = {0}", e.StackTrace);
Console.WriteLine ("GetBaseException = {0}",
e.GetBaseException ());
}
5 o ,o -
-
. 8 ' (
:
– 9 9 9 -
, -
,o ;
– o K n ,
9 ,o 9 9 ,
K .

3.2.2.2 P' ' c%) !, ) %!

A ( 9 .
( :
– , n
9 9 K. ' 9 9 -
,o 9 d 9 9 ;
– 9 ,
n .' 9 9 -
, d .NET 9 -
, .
.9 K .
77

3.2.2.3 N ! #' ' c%) !, ) %!

„ 9 9 n K 9 9
.5 , 9 , o n
9 9 , n
NullReferenceException. d 9 9
( , ( , n . : d
, 9 9 , o .u o
n 9 catch,
K K throw, :
try
{
// iK8 L6@M;9; JK8M H6C C6CI8HK,
}
catch (System.NullReferenceException e)
{
// q 7<CM L6:DjI<8T H6C, FHMO L959@D5Fr @M;F86H D L686c;MO H6;89H78,
// s6J @MB;IcM8M, cM :6_;I 6J56JM8M @M;F86H @ CI;6:K J?6bD catch,
// 4959CI;6 @Mj9 L6 789HK. tHj6 @6;6 ;9 :6_9 JK8M 6J56J?9;6 ?6HI?T;6.
// u6HI?T;6, ;I78KL;MO 5FC6H U9;95Kr @M;F86H L6@865;6 D L959CIr
// v6U6 CI?D L6 789HK.
throw e;
}

' 9 catch, n -
9'n , 9 ( -
K K throw, d
9 .
9
.A
d , -
n 9 d .' 9'n
n InnerException 9'n -
.v n
, d -
d Inner
Exception. 5 ( n -
, o n , 9 -
K n:
/ / w9 L6cI86H J?6HK Catch.
catch (NullReferenceException e)
78

{
throw new NullReferenceException ("FHMO7T 89H78", e);
}
u :
–o 9 , 9 Catch
(catch), K Throw (throw);
–o 9 ,o n
, 9'n , o n 9 K
, K K -
throw.

3.2.2.4 ' ( ,0 !, )

' 9 ( 9 -
.u o 9 n -
, n .A
( K , -
K K d -
.
' . , d -
.5 n (
n — .:
, ( -
9 d ( .
5 , ( K
System.ApplitalionException. v Kn K 9 -
, Message, StackTrace
InnerException, ( K , -
n 9 d .
5 ( ,
Application Exception. J
n 9'n Widget, n -
9'n , d :
public class WidgetException: System.ApplicationException
{
// wF B:D;;I BJ95DUIr 6J'rH8 Widget.
Widget mWidget;
public Widget ErrorWidget
{
get
79

{
return mWidget;
}
}
// w9O H6;785KH865 L5MO:Ir 6J'rH8 Widget D 5FC6H String, bD
// 4I5I:985M :6_;I @MH65M786@K@I8M C?F 6LM7K H6;89H78K L56U5I:M
// QI :6:9;8 L6:M?HM. W6B@6?Fr8T7F 8IH6_ 78@65<@I8M L959@I;8I_9;D
@957DG
// Y6;785KH865I, j6 LDC85M:K<8T 5DB;D ;IJ65M LI5I:985D@.
//
// QI78KL;MO 5FC6H @MH?MHIr H6;785KH865 B JIB6@6U6 H?I7K
// y @78I;6@?<r @?I78M@D78T Message,
// z7LICH6@I;9 @DC H?I7K ApplicationException.
public WidgetException (Widget W, string S): base (S)
{
// [78I;6@M8M @?I78M@D78T Widget.
mWidget = W;
}
}
. d , n -
, ,
:
Widget Alpha;
/ / iK8 L6@M;9; JK8M H6C, j6 KeH6C_Kr 6J'rH8 Widget B D:'F: Alpha.
throw new WidgetException (Alpha, "Widget Alpha is corrupt!");

u :
– .: 9 -
o System.ApplicationException ( 9 - -
, K n , o 9 9 K .v
K -
;
– d
K K throw,
d .

3.2.2.5 P' ' c%) !/ , !, ) %!

v :
” 9 9 -
throw, d , o K -
.' n9 9'n , (
System.Exception. v 9'n n -
9 9 .
80

static void Main ()


{
try
{
int x = int.Parse (Console.ReadLine ());
if (x <0) throw new Exception (); // 1
Console.WriteLine ("ok");
}
catch
{
Console.WriteLine ("[@9C9;6 ;9H659H8;9 B;Ic9;;F");
}
}
1 K new 9 9'n -
Exception. ' 9 (
9 - .
' ( , 9
" " 9 9 .5 :
static void Main ()
{
try
{
int x = int.Parse (Console.ReadLine ());
if (x <0) throw new Exception ("@@9C9;6 ;9L5MLK78M:9 B;Ic9;;F"); //
1
Console.WriteLine ("ok");
}
catch (Exception error)
{
Console.WriteLine (error.Message);
}
}

3.2.2.6 , % (, #' ' c%d !, ) %!

7 9 9
.
' 1. . try-9 ( d .: , -
d try-9 catch-
nK, ' try-9 , n d
try-9 . 5 ,
ArithmeticException Kn d try-9 , -
d .
static void Main ()
{
Console.WriteLine ("a =");
81

byte a = byte.Parse (Console.ReadLine ());


Console.WriteLine ("b =");
byte b = byte.Parse (Console.ReadLine ());
int f = 1;
try / / X6@;De;DO J?6H-try
{
for (byte i = a; i <= b; + + i)
{
try / / [;K85De;DO J?6H-try
{
f = checked ((int) (f * i));
Console.WriteLine ("y ({0}) = {1: f6}", i, 100 / (f - 1));
}
catch (DivideByZeroException)
{
Console.WriteLine ("y ({0}) = WD?9;;F ;I 0", i);
}
}
}
catch (ArithmeticException)
{
Console.WriteLine ("ERROR");
}
: try-9 9 9 ( 9 -
9 9 ..
K K. „ d —
9 d , (
.6 d try-9
( ,
K d try-9 9 9 d 9 .
' 2. : , n catch- nK,
( ,o 9 9 ( -
d ( d ) catch- nK. v n -
9 9 .
static void genException ()
{
Console.WriteLine ("a =");
double a = double.Parse (Console.ReadLine ());
Console.WriteLine ("b =");
double b = double.Parse (Console.ReadLine ());
int f = 1;
try / / X6@;De;DO J?6H-try
{
for (double i = a; i <= b; + + i)
{
try / / [;K85De;DO J?6H-try
{
f = checked ((int) (f * i));
82

Console.WriteLine ("y ({0}) = {1: f6}", i, 100 / (f - 1));


}
catch (DivideByZeroException)
{
Console.WriteLine ("y ({0}) = WD?9;;F ;I 0", i);
}
}
}
catch (ArithmeticException)
{
Console.WriteLine ("ERROR");
throw; / / L6@865;I U9;95IbDF @M;F8HK
}
}

static void Main ()


{
try
{
genException ();
}
catch
{
Console.WriteLine ("•I8I?T;I L6:M?HI !!!");
}
}
' 9 ' , o
9 K n ( catch- nK,
n ( d ) catch- .
' 3. u o , 9
, catch- .: d
9 . ( K , K-
catch- K 9 . J , try-9 (
' , catch- . catch-
K .u o
( ,o 9 , ( -
catch- K9 .
( .„ n 9 9 '
, o try/catch-
9 .5 , n 9 n -
d , d -
n .A 8 # n 9
finally, n 9 catch.
static void Main ()
{
for (int i = 0; i <5; i + +)
83

{
try
{
Console.WriteLine ("[@9CD8T C@I cM7?I");
int a = int.Parse (Console.ReadLine ());
int b = int.Parse (Console.ReadLine ());
Console.WriteLine (a +"/"+ b +"="+ a / b);
}
catch (FormatException)
{
Console.WriteLine ("4685DJ;6 @@978M cM7?6!");
}
catch (DivideByZeroException)
{
Console.WriteLine ("WD?M8M ;I ;K?T ;9 :6_;I!");
}
catch
{
Console.WriteLine ("tHI7T L6:M?HI");
}
finally
{
Console.WriteLine ("LD7?F try-J?6HK");
}
}
}

3.3 !( )

3.3.1 .
K 9 , K , ( -
.
3.3.2 8 ( ,
, K 9 9 .
3.3.3 . .
3.3.4 : .

3.4 -% !%

3.4.1 6 9 .
3.4.2 9 .
3.4.3 J .
3.4.4 J .
84

3.4.5 : ,o (5
d . 9 ), ( 9 ( K -
9 .

3.5 /b % , )

3.5.1 ” K ?
3.5.2 u K?
3.5.3 u K?
3.5.4 ” K?
3.5.5 u K?
3.5.6 u 9 K ?
3.5.7 u K« » d?
3.5.8 ” 9 & K? u 9
K?
3.5.9 u K?
3.5.10 u K 9 ( ?
3.5.11 u K K ?
3.5.12 ” ?
3.5.13 u 9 9 ? '
( 9 ?
3.5.14 u 9 9 ?
3.5.15 5 ’ 9 9 .
3.5.16 J Exception. . ?
3.5.17 5 o 9 ?
3.5.18 u 9 K ?
3.5.19 u ?
3.5.20 5 o try-9 ?
85

4 OM -P Q R * 4
o M R L
M R M #

4.1 ' F ,

4.1.1 5
4.1.2 5 K .

4.2 ! % ' ' ,& % !%( - %

4.2.1

9 - , ( ,
9 .A , .NET Framework
9 PrintDocument, o 9 n
n '
K .
€ , ( n nK ( d
.5 ( K ,o 9 d K 9 -
K , -
9 ( 9 K
.6 9 9 K
. .NET Framework n ,
( d K n
nK.

4.2.1.1 - ' PrintDocument

.NET Framework -
PrintDocument.
‡ PrintDocument n , -
Windows Forms
Toolbox, ( ,
. .9'n PrintDocument Kn K K,
9 . n :
PrinterSettings 9 n ( -
86

, Default PageSettings Kn -
, a PrintController Kn 9 9 .
v K n -
.

! ' ) F'x PrintDocument


.9'n PrintDocument ( 9 , -
K , 9 ,
. d 9'n PrintDocument
Toolbox 9 ' -
9'n ,
9 .
. Kn 9'n PrintDocument:
PrintDocument myPrintDocument = new PrintDocument();
. 9'n PrintDocument -
n 9 -
.

N%( ,- (
, .NET Framework, o K
9 , K .:
PrintDocument, Print Kn , K
PrintPage. u o n 9 9 n ,
n .” 9 , 9
9 9 PrintPage.
u o n ,
PrintPage n ( , 9 9 n -
n 9 . .9 9 K PrintPage Kn -
, 9 K 9 -
; 9 9 d
d .

N (%) PrintPage
' PrintPage n K .” 9 -
, 9 9 9
PrintPage , o n
. 9'n , 9 , K 9 9 -
9'n PrintPageEventArgs, 9 . 4.1.
87

6 9 4.1 – : 9'n PrintPageEventArgs

„ ' .
Cancel : n,
Graphics 9'n Graphics, o n

HasMorePages . n 9 Kn , n,

MarginBounds . n 9'n Rectangle, o n 9-


, 9 (
PageBounds . n 9'n Rectangle, o n -

PageSettings . n 9 Kn 9'n PageSettings

A -
n 9'n Graphics, PrintPageEventArgs.
n n ( ,o n
9 , 9 - d
K , 9'n Graphics. A -
K ( , o K
.. ( , -
n , 9 , 9 ( :
//s6J b9O :986C JK@ @MH?MHI;MO, @D; L6@M;9; 6J56J?F8M7F L6CDr<
//PrintPage.
public void PrintEllipse (object sender, Sys-
tem.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawEllipse (Pens.Black, e.MarginBounds);
}
: MarginBounds PageBounds K 9 -
, . ( 9 9 -
, 9 d 9'n
MarginBounds. ' ( -
— ,o 9 ( -
.A 9'n
PageBounds. u , -
, 9 KK .
88

: HasMorePages n , n -
9 . K
false. u o ( 9 9 9 -
, true, d
— false.
' . .9 9 PrintPage ( -
K , d
9 9 .8 ( , o -
HasMorePages false,
9 PrintPage .
J , 9 ( ,
K .A
Cancel true.
” 9 9 9 PrintPage, -
PrintDocument ( Kn 9 9 -
) 9 9 9 .

4.2.1.2 ,!%( (

” 9 , Print
Document, Print. n , ,
n , .

# $% ,
A d , ( K
.A 9 K 9'n Graphics,
9'n PrintPageEventArgs. A K , 9 d
, K 9'n
GraphicsPath. . K 9'n -
Graphics Path:

/ / w9O :986C L6@M;;I 6J56J?F8M L6CDF PrintPage.


public void PrintGraphics (object sender, Sys-
tem.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath myPath = new Sys-
tem.Drawing.Drawing2D.GraphicsPath ();
myPath.AddPolygon (new Point [] {new Point (1, 1), new
Point (12, 55), new Point (34, 8), new Point (52, 53),new Point (99,
5)});
89

myPath.AddRectangle (new Rectangle (33, 43, 20, 20));


e.Graphics.DrawPath (Pens.Black, myPath);
}
s6J ;IC5KHK@I8M JIUI867865D;H6@MO U5I•Dc;MO C6HK:9;8, 859JI @5Kc;K
56BJM8M O6U6 ;I 7865D;HM D ;ILM7I8M @DCL6@DC;MO H6C. QI78KL;MO :986C
56BDJ’r L5M C5KHK@I;;D 9?DL7, j6 ;9 @:DjIr8T7F ;I 6C;DO 7865D;bD, ;I
C@D 7865D;HM:
bool FirstPagePrinted = false;
/ / w9O :986C L6@M;;I 6J56JM8M L6CDF PrintPage
public void PrintBigEllipse (object sender,
System. Drawing. Print ing. PrintPageEventArgs e)
{
if (FirstPagePrinted == false)
{
FirstPagePrinted = true:
e. HasMorePages = true;
e.Graphics. DrawEllipse (Pens. Black, new Rectangle (0,0,
e.PageBounds. Width, e. PageBounds. Height * 2));
else
e. HasMorePages = false;
FirstPagePrinted = false;
e. Graphics, DrawEllipse (Pens. Black, new Rectangle (0,
- (E. PageBounds. Height), e. PageBounds. Width,
e. PageBounds. Height * 2));
-
K FirstPagePrinted. ( : d
.u o , n
( ( n false. 5 -
( n , KK
( ,o 9
.

'
: 9 n , ,—
K Graphics.DrawString. u ,
9 d , , 9'n Brush -
, :
Font myfont = new Font ("Batang", 36, FontStyle. Regu-
lar,GraphicsUnit. Pixel);
string Hello=”Hello World!”;
e.Graphics.DrawString(Hello,myfont,Brushes.Black,30,30);
' .' ,
( : 9 - ,
( , 9 .
90

F # )( ! # '
A 9 , 9
, 9 , 9 Kn (
.” 9 , -
n 9 ( Kn -
) d .' ( 9 -
( 9 , ( d
d .5 n
myStrings:
// wF B:D;;I @DC789_Kr L686c;MO 9?9:9;8 :I7M@K.
// s6J K;MH;K8M D;DbDI?DBIbDG bDrG B:D;;6G L5M C5KbD H6_;6G
// E865D;HM, 7?DC 6U6?6eK@I8M L6BI 6J56J;MHI L6CDG PrintPage.
int ArrayCounter = 0;
// w9 6J56J;MH L6CDG PrintDocument.PrintPage. [D; L5MLK7HIr,
// s6 5FCH6@MO :I7M@ B D:'F: myStrings () @_9 6U6?6e9;MO D BIL6@;9-
;MO,
// q 8IH6_ j6 D;DbDI?DB6@I;MO 6J'rH8 myFont, j6 L59C78I@?Fr
// fJ5I;MO C?F C5KHK e5M•8.
private void PrintStrings (object sender, PrintPageEventArgs e)
{
/ / fU6?67M8M B:D;;D, H95K<cM :D_7856H6@M:M
/ / y;895@I?I:M C6HK:9;8I ;I 7865D;HM.
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
float MyLines = 0;
float YPosition = 0;
int Counter = 0;
string CurrentLine;
// 4DC5ISK@I8M HD?THD78T 5FCHD@ ;I 7865D;bD.
MyLines = e.MarginBounds.Height /myFont.GetHeight (e.Graphics);
// A6BC5KHK@I8M @7D 5FCHM •IO?K, BKLM;F<cM7T @ HD;bD 7865D;6H.
while (Counter <MyLines && ArrayCounter <=mySt rings.GetUpperBound
())
{
CurrentLine = myStrings [ArrayCounter];
YPosition = TopMargin + Counter *myFont.GetHeight (e.Graphics);
e.Graphics.DrawString (CurrentLine, myFont, Brushes.Black,
LeftMargin, YPosition, new StringFormat();
Counter ++;
ArrayCounter ++;
// tHj6 ;IC5KH6@I;D j9 ;9 @7D 5FCHM, 86 C5KHK@I8M ;I78KL;K 7865D;HK,
if (!(ArrayCounter == myStrings.GetUpperBound (0)))
e.HasMorePages = true;
else
e.HasMorePages = false;
91

F /b -,
5 9 ,o n ,
n ,o 9 -9 -
.' 9 ,o -
, 9 , -
.6 , -9 -
.
” 9 , n
, PrinterSettings.SupportsColor. u o ,
DefaultPageSettings.Color true,
9 d .A -
9 Default PageSettings.Color false.
5 n K -
-9 K
PrinterSettings.SupportsColor.
// w9O L5MH?IC L5MLK7HIr ;IF@;D78T 6L95I865D@ Imports Sys-
tem. Drawing,
// System. Drawing. Drawing2D D System. Drawing. Printing
Brush BrushOne;
Brush BrushTwo;
if (PrintDocument1. PrinterSettings.SupportsColor == true)
{
// E8@65M8M L9;B?D C?F H6?T656@6U6 C5KHK.
BrushOne = Brushes. Red;
BrushTwo = Brushes. Blue;
}
else
{
// E8@65M8M 6J'rH8M HatchedBrush C?F c65;6-JD?6U6 C5KHK.
BrushOne - new HatchBrushfHatchStyle. DarkVertical, Color. Black);
BrushTwo = new HatchBrush (HatchStyle. DashedHorizontal,
Color. Black);
}
. :
– 9'n PrintDocument Toolbox -
— 9 '
, d
.8 PrintDocument ( -
;
– d 9 9 PrintDocument PrintPage;
– 9 9 PrintPage , Kn
n 9 .' 9 (
K 9'n PrintPageEventArgs.Graphics;
92

– o Kn 9 , -
d ,o Kn 9 .

4.2.1.3 ! ) '/'-' ' ! )


PrintPreviewControl

.NET Framework n PrintPreviewControl, o


9 n ( -
K . PrintPreviewControl
Windows Forms Toolbox, (
.
” 9 d , 9 ' -
PrintPreviewControl
PrintDocument, d PrintPreviewControl.Document
:
myPrintPreview. Document = myPrintDocument;
' PrintPreviewControl 9 -
9 ( , .: 9 9
PrintDocument.Print, o 9 9
9 PrintPreviewControl.
' Kn -
.
” 9 9 , o 9 ( n -
, InvalidatePreview:
myPrintPreview.InvalidatePreview();
Zoom — o (
PrintPreviewControl. : n d 9
.u o Kn 1, d 9 Kn 100%. ' -
1 9 ( d -
, ,o o K 1, 9 ( 9 -
d .

/'-' ' ! ) PrintPreviewDialog


s PrintPreviewControl d n -
,o K ,
( .. 9-
d -
.A .NET Framework 9 -
93

PrintPreviewDialog, n
d d ( -
. PrintPreviewDialog 9 ( n , . 4.1.

7 4.1 – : PrintPreviewDialog

A -
Document 9'n PrintPreviewDialog 9'n Print Document,
n 9 .s
PrintPreviewDialog n
, K d 9 . : -
PrintPreviewDialog n 9 -
d Kn K Show 9
ShowDialog.
94

4.2.1.4 / + ! ) -' %! (

„ .NET Framework n 9 K
.: PrintDocument PrinterSettings -
, ,
PrintDocument.DefaultPageSettings — -
( K , o 9 9 PrintPage
d ). K , n 9
.5 o , 9 d -
9 . J , 9'n
PrintDocument PrinterSettings o n 9'n -
, -
, DefaultPageSettings K -
.6 , -
( 9 .
u o -
, Visual
Studio .NET, o 9 n 9 d K d

PrintDialog PageSetupDialog.

PrintDialog
: PrintDialog n PrinterSettings
9'n PrintDocument .” 9
PrintDialog , Windows Forms -
Toolbox .” 9 9 PrintDialog
, ShowDialog :
PrintDialog1.ShowDialog ();
: PrintDialog 9 ' 9'n
PrintDocument. A PrintDialog.Document
9'n PrintDocument, o n 9 .
' 9 ( PrintDialog ( . 4.2) ' n
K PrinterSettings 9'n PrintDocument, -
K Document, K -
d .
95

7 4.2 – : PrintDialog

PageSetupDialog
PageSetupDialog PrintDialog -
9 .: PageSetupDialog ( . 4.3) n
d -
.J ( 9 n K ,
, ( d .
u PrintDialog, PageSetupDialog n
Document, 9'n PrintDocument, o n 9-
. : d Document, ( '
9'n PrintDocument PageSetupDialog. ' 9 - ,
9 d ,9 9 ( -
9'n PrintDocument.PrinterSettings DefaultPageSettings.
96

7 4.3 – : PageSetup Dialog

$%# ! ) PageSettings ! ' % ( !, )


„ 9 9
, d, -
9 n , d — ( . -
n PrintPageEventArgs. PageSettings,
n . , 9
K n , K , d
97

K ,
.5 n -
, KK 9 n K :
// w9O •5IU:9;8 6J56J;MHI L6CDO PrintPage.
// X:D;;I 9 L59C78I@?Fr 6J'rH8 PrintPageEventArgs.
9.PageSettings.Landscape = true;
J PageSettings :
– , o K
PrintDocument. v n PrinterSettings, o -
n , DefaultPageSettings, o n
;
– K d
PrintDocument.Print. : n PrintPage,
9 9 o K ;
– 9'n PrintPageEventArgs n -
, 9 -
.A K 9'n Graphics, o n
; 9'n Graphics n 9'n
PrintPageEventArgs;
– 9 9
,o n 9 .A -
PrintPage
PrintEventArgs.HasMorePages true;
– PrintPreviewControl 9 n
K , a PrintPreviewDialog
n 9 d 9 ( PrintPreview
;
– PrintDialog PageSetupDialog K (-
d -
.' 9 ( ( d

PrintPageEventArgs.PageSettings .

4.2.1.5 N , / ( ( !-% % , - ' %!

7 d RichTextBox MenuStrip
( 9 . 4.2).
98

6 9 4.2 – ' K

Name Text Shortcut


mnuFile &2
Name Text Shortcut
mnuOpen &: Ctrl+O
mnuSave & 9 Ctrl+S
menuItem1 -
mnuPageSetup ' &
mnuPrintPreview ' &
mnuPrint &A Ctrl+P

' ToolBox :
PrintDocument, PageSetupDialog, PrintPreviewDialog PrintDialog. ' -
9 d , 9 ( K -
o Visual Studio. NET. ' n
9 , n 9'n PrintDocument. s -
PageSetupDialog, PrintPreviewDialog PrintDialog
K 9 K ,
.5 d 9'n n -
d , d ,o ,
, , OpenFileDialog 9
SaveFileDialog, n K , :
// PrintDocument printDocument1 = new PrintDocument ();
// PageSetupDialog pageSetupDialog1 = new PageSetupDialog ();
// PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog
();
// PrintDialog printDialog1 = new PrintDialog ();
v K K -
. , ,9 ( .
: Document PageSetupDialog
PrintPreviewDialog Kn Name
PrintDocument – printDocument1. 6 ' n 9'n
printDocument1, o n , -
. : Kn
PrintDialog ( 9 . 4.3).
99

6 9 4.3 – : PrintDialog

printDialog1,
.
AllowSelection True A -

AllowSomePages True A
PrintDocument printDocu ' 9'-
ment1 n PrintDocument

5 Properties 9 -
AllowSelection 9 AllowSomePages (
— Enables and disables the Selection/Pages radio button (: K n 9
K n ": /8 -
"). 8 9 o K n
—o , -
9'n o :
InitializeComponent ();
// PageSetupDialog1.Document = printDocument1;
// PrintPreviewDialog1.Document = printDocument1;
// PrintDialog1.Document = printDocument1;
// PrintDialog1.AllowSomePages = true;
// PrintDialog1.AllowSelection = true;
' .A 9 99
.NET Framework n System.Drawing.Printing,
9 K 9 :
using System.Drawing.Printing;
d n :
// X:D;;I C?F BJ95DUI;;F 89H78K C?F C5KHK.
// z ;9G :M JKC9:6 56B:DjK@I8M 89H78 B RichTextBox
string stringPrintText;
// X:D;;I, FHI @MB;IcIr ;6:95 7865D;HM, B FH6G L685DJ;6 L6cI8M C5KH
int StartPage;
// X:D;;I, FHI @MB;IcIr HD?THD78T 7865D;6H C?F C5KHK:
int NumPages;
// X:D;;I, FHI @MB;IcIr ;6:95 L686c;6G 7865D;HM:
int PageNumber;
8 stringPrintText — 9
9'n ..
, n, o (
K — n , RichTextBox n
( ( , K d -
100

Microsoft Word d ). : 9
9 d ,o , , n
).
n :
public Form1 ()
{
InitializeComponent ();
...
/ / [MB;IcIr:6 ;6:95 7865D;HM, B FH6G 7?DC L6cI8M C5KH
printDialog1.PrinterSettings.FromPage = 1;
/ / [MB;IcIr:6 :IH7M:I?T;MO ;6:95 C5KH6@I;6G 7865D;HM.
printDialog1.PrinterSettings.ToPage = printDia-
log1.PrinterSettings.MaximumPage;
}
— 9999; ,
o 9 - -
.. K nK, 9 9 9 K -
, 9 PrintPage print-
Document1. ' n ( , n ,
K d Properties , n
n :
private void printDocument1_PrintPage (object sender, Sys-
tem.Drawing.Printing.PrintPageEventArgs e)
{

}
A 9 9 -
9 . J Graphics, o ( System.Drawing —
( d ,o K K
.'
System.Drawing K n d 9 .A -
Kn :
Graphics graph = e.Graphics;
// E8@65<r:6 6J'rH8 font, FH6:K @78I;6@?<r:6
// ‹5M•8 9?9:9;8I rtbText
Font font = rtbText.Font;
// f85M:Kr:6 B;Ic9;;F :D_5FCH6@6U6 D;895@I?K - @M768K e5M•8K
float HeightFont = font.GetHeight (graph);
// E8@65<r:6 9HB9:L?F5 stringformat
//H?I7K StringFormat C?F @MB;Ic9;;F
// W6CI8H6@MS LI5I:985D@ •65:I8K@I;;F 89H78K.
StringFormat stringformat = new StringFormat ();
8 n 9 K 9'n ,
. A n RectangleF
101

( F n K K K
float):
// E8@65<r:6 9HB9:?F5D@ rectanglefFull D rectfText H?I7K RectangleF
C?F
// [MB;Ic9;;F 6J?I789O C5KHK D 89H78K.
RectangleF rectanglefFull, rectfText;
// E8@65<r:6 B:D;;D C?F LDC5ISK;HK cM7?I 7M:@6?D@ D 5FCHD@.
int NumberSymbols, NumberLines;
' d 9 — , n ,
, A4 9 A3. „ K K 9 n -
PageBounds PrintPageEventArgs. ‚ n 9'n
Rectangle, X 0, Width Height
K , (
K .A 9 ' 9
—9 d K o , -
d . .9 n -
VisibleClipBounds Graphics, n 9'n
RectangleF. : ‡ 9'n K 0, -
Width Height -
9 . 9 ,
n . Microsoft Word, ,
Kn . , 9
n K ).
n 9'n Rectangle MarginBounds.
9 - , 9 K 9'n Rectangle
9 RectangleF, ( :
Rectangle (int x, int y, int width, int height);
RectangleF (float x, float y, float width, float height);
v K , -
K . J 9 , o n 9 K -
.
' d .u 9 Kn
9'n rectanglefFull:
if (graph.VisibleClipBounds.X <0) rectanglefFull = e.MarginBounds;
else
// [MB;IcIr:6 6J'rH8 rectanglefFull
rectanglefFull = new RectangleF (
// [78I;6@?<r:6 H665CM;I8K X
e.MarginBounds.Left - (e.PageBounds.Width -
graph.VisibleClipBounds.Width) / 2,
// [78I;6@?<r:6 H665CM;I8K Y
e.MarginBounds.Top - (e.PageBounds.Height -
graph.VisibleClipBounds.Height) / 2,
102

// [78I;6@?<r:6 eM5M;K 6J?I78D


e.MarginBounds.Width,
// [78I;6@?<r:6 @M768K 6J?I78D
e.MarginBounds.Height);
fJ?I78T 89H78K JKC9 L59C78I@?F8M 76J6< H6LD< 6J?I78D rectanglefFull c
K5ISK@I;;F: @M768M e5M•8K:
rectanglefText = RectangleF.Inflate (rectanglefFull, 0, -2 * Height-
Font);
// [MB;IcIr:6 HD?THD78T 5FCHD@
int NumDisplayLines = (int) Math.Floor (rectanglefText.Height /
HeightFont);
// [78I;6@?<r:6 @M768K 6J?I78D
rectanglefText.Height = NumDisplayLines * HeightFont;
s RichTextBox n WordWrap, -
True
.' 9 ( (
9 rectanglefText. ” 9 ,
stringformat StringFormat, K-
n Trimming:
if (rtbText.WordWrap)
{
stringformat.Trimming = StringTrimming.Word;
}
else
{
stringformat.Trimming = StringTrimming.EllipsisCharacter;
stringformat.FormatFlags | = StringFormatFlags.NoWrap;
}
A , ( :
// 45M C5KHK @MJ5I;MS 7865D;6H L959S6CM:6 C6 L95e6G 78I586@DO 7865D;-
bD
while ((PageNumber <StartPage)&&(stringPrintText.Length> 0))
{
if (rtbText.WordWrap)
// [M:D5<r:6 89H786@D B:D;;D,
// N65:Kr:6 C5KH, D L6@958Ir:6 cM7?6 7M:@6?D@ NumberSymbols
// y HD?THD78T 5FCHD@ NumberLines
graph.MeasureString (stringPrintText, font, rectanglefText.Size,
stringformat, out NumberSymbols, out NumberLines);
else
NumberSymbols = SymbolsInLines (stringPrintText, NumDisplayLines);
stringPrintText = stringPrintText.Substring (NumberSymbols);
// XJD?TeKr:6 HD?THD78T 7865D;6H
PageNumber + +;
}
// tHj6 C6@_M;I 5FCHI stringPrintText C65D@;<r ;K?< (;9:Ir 89H78K
//C?F C5KHK),
// XKLM;Fr:6 C5KH
if (stringPrintText.Length == 0)
{
103

e.Cancel = true;
return;
}
// [M@6CM:6 (:I?<r:6) 89H78 C?F C5KHK - stringPrintText,
//@MH65M786@Kr:6 C?F bT6U6 e5M•8 font,
// YM78T c65;6U6 H6?T65K - Brushes.Black, 6J?I78T C5KHK - rectan-
glefText,
// 4959CIr:6 5FC6H C6CI8H6@6U6 •65:I8K@I;;F stringformat
graph.DrawString (stringPrintText, font, Brushes.Black, rectan-
glefText, stringformat);
// f85M:Kr:6 89H78 C?F ;I78KL;6G 7865D;HM
if (rtbText.WordWrap)
graph.MeasureString (stringPrintText, font, rectanglefText.Size,
stringformat, out NumberSymbols, out NumberLines);
else
NumberSymbols = SymbolsInLines (stringPrintText, NumDisplayLines);
stringPrintText = stringPrintText.Substring (NumberSymbols);
// fcMjIr:6 6J'rH8 stringformat, @MH65M78I;MO C?F •65:K@I;;F L6?D@.
stringformat = new StringFormat ();
// W6CIr:6 @M7;6@6H ;I H6_;K 7865D;HK GG ;6:95I
stringformat.Alignment = StringAlignment.Far;
graph.DrawString ("E865D;HI" + PageNumber, font, Brushes.Black, rec-
tanglefFull, stringformat);
PageNumber + +;
// C;6@I L959@D5Fr:6, cM r 89H78 C?F C5KHK D ;6:95 7865D;HM, BICI;6G
C?F C5KHK
e.HasMorePages = (stringPrintText.Length> 0) & & (PageNumber <Start-
Page + NumPages);
// W?F C5KHK B @DH;I L6L959C;T6U6 L959U?FCK B;6@K D;DbDI?DBK@I?M
B:D;;D
if (! e.HasMorePages)
{
stringPrintText = rtbText.Text;
StartPage = 1;
NumPages = printDialog1.PrinterSettings.MaximumPage;
PageNumber = 1;
}
}
printDocument1_Print-Page. A
n ,o
RichTextBox (
WordWrap). MeasureString n , o -
.A 9 , o K 9
rectanglefText, n SymbolsInLines:
int SymbolsInLines (string stringPrintText, int NumLines)
{
int index = 0;
for (int i = 0; i <NumLines; i + +)
{
index = 1 + stringPrintText.IndexOf ('\ n', index);
104

if (index == 0)
return stringPrintText.Length;
}
return index;
}
8 Kn 9 9 mnuPageSetup
mnuPrintPreview K:
private void mnuPageSetup_Click (object sender, System.EventArgs e)
{
// 46HIBKr:6 CDI?6U
pageSetupDialog1.ShowDialog ();
}
private void mnuPrintPreview_Click (object sender, System.EventArgs
e)
{
// y;DbDI?DBD5K9: B:D;;D
printDocument1.DocumentName = Text;
stringPrintText = rtbText.Text;
StartPage = 1;
NumPages = printDialog1.PrinterSettings.MaximumPage;
PageNumber = 1;
// 46HIBKr:6 CDI?6U
printPreviewDialog1.ShowDialog ();
}
: 9 9 K mnuPrint n -
– , 9 9 :
private void mnuPrint_Click (object sender, System.EventArgs e)
{
printDialog1.AllowSelection = rtbText.SelectionLength> 0;
if (printDialog1.ShowDialog () == DialogResult.OK)
{
printDocument1.DocumentName = Text;
// [MB;IcIr:6 CDILIB6; 7865D;6H C?F C5KHK
switch (printDialog1.PrinterSettings.PrintRange)
{
// [MJ5I;D @7D 7865D;HM
case PrintRange.AllPages:
stringPrintText = rtbText.Text;
StartPage = 1;
NumPages = printDialog1.PrinterSettings.MaximumPage;
break;
// fJ5I;I @MCD?9;I 6J?I78T
case PrintRange.Selection:
stringPrintText = rtbText.SelectedText;
StartPage = 1;
NumPages = printDialog1.PrinterSettings.MaximumPage;
break;
// [MJ5I;6 5FC 7865D;6H
case PrintRange.SomePages:
stringPrintText = rtbText.Text;
StartPage = printDialog1.PrinterSettings.FromPage;
105

NumPages = printDialog1.PrinterSettings.ToPage - StartPage +1;


break;
}
PageNumber = 1;
// [MH?MHIr:6 @JKC6@I;MO :986C C?F L6cI8HK C5KHK
printDocument1.Print ();
}
}
n . , 9 "' -
", 9 K ,
( 9 .
( -
.' 9 9 9 -
.
: ,o 9 -
, 9 (
.. , , -
— .
9 9 , ( 9
( 9 .' d (
pdfFactoryPro — ' n -
,o n 9 pdf.

4.2.2 ! ' ) '/'-' %! ' ! ) ( - # 8. `ET


Framework

4.2.2.1 ! ) GDI +

Windows – .J -
KK , K
.” 9 9 -
( Windows,
. .NET
Framework n 9 n
.
' 9 9 ( -
, ( -
.” 9 K K Windows, 9
( (Graphic Device
106

Interface, GDI). .NET Framework K -


K GDI+.
6 GDI + .NET Framework K -
K GDI, n 9 (
'K . A n
d , GDI+.

N % %-' System.Drawing
' System.Drawing n d ( -
. A ( n
K , -
( 9 . 4.4).

6 9 4.4 – ' System.Drawing

„ ’ ”
System.Drawing . , 9 9 -
( .v K -

Sys- J , K
tem.Drawing.Design
( 9
Sy'stem.Drawing.2D J , o 9 ( K

Sys- J , o K (
tem.Drawing.Imaging
K K
Sys- J
tem.Drawing.Printing
System.Drawing.Text J ,o K (
d

' Graphics
J K 9 ( n
Graphics System.Drawing. .9'n Graphics n
9 K , d -
107

,— , 9 9'n Image. -
' 9'n Graphics, n K -
9 ( n , o .
. 9'n Graphics 9 ' -
, ( , d
9 . 9 9'n
Graphics . € - - o Control
( K K Form) n CreateGraphics. . n
9'n Graphics, ' -
. 5 n 9'n
Graphics myForm:
System.Drawing.Graphics myGraphics;
myGraphics = myForm.CreateGraphics();
Kn 9'n Graphics,
( 9 ( -
.
' 9
9 ( Graphics. FromImage -
n 9'n Graphics, ' 9'n Image. v
, 9'n Graphics
9 . .9'n Image ( n 9 - 9'n - o
Image, Bitmap. . 9'n Bitmap
' 9'n Graphics:
Bitmap myImage = new Bitmap ("C: \\ myImage.bmp");
System.Drawing.Graphics myGraphics;
myGraphics = Graphics. FromImage (myImage);
( ,o 9'n Image n
9'n Graphics K .
J . 2 9 ( 9 n 9 ,
( .6 n -
, ( K X Y. -
(0,0) ( -
9 n , K -
.' System. Drawing ( 9 4.5),
o K 9 9 .
108

6 9 4.5 – 8 ,o K -

„ ’ .
Point ' n X Y, -
Integer (Int).
PointF ' n ‡ Y, -
Single (float)
Size ' n 9 , -
K Height Width Integer
SizeF ' n 9 , -
K Height Width Single (float)
Rectangle ' n 9 , (
Top, Bottom, Left Right Integer
(Int)
RectangleF ' n 9 , (
Top, Bottom, Left Right Single
(float)

u 9 4.5, n 9 : ,o
K , , o K
K K K.
8 Point, Size Rectangle d n -
K .” 9
, (
K K K , :
Point myPoint;
PointF myPointF = new PointF (13.5F, 33.21F);
myPoint = new Point ((int) myPointF.X, (int) myPointF.Y);
( 9 Size Rectangle:
9 K n 9 , -
Size n , ( ,
Rectangle n n -
9 .A Rectangle 9 : Size
Point, o K ( :
Point myOrigin = new Point (10, 10);
Size mySize = new Size (20, 20);
109

// E8@65M8M L5F:6HK8;MH 56B:D56: 20 ;I 20 LDH79?D@, @95S;DO


// uD@MO HK8 FH6U6 56B8Ie6@I;MO K 86cbD B H665CM;I8I:M (10,10),
Rectangle myRectangle = new Rectangle (myOrigin, mySize);
K . .9'n Graphics Kn 9
K .„ n
.A d , -
n «Draw» ( 9 4.6). v K
K , .„ d , -
n «Fill» ( 9 4.7), ( K
- , 9 .
v K 9 ,o -
K ( 9 ( .J , -
9 9'n , o n 9 ( ,
7 n, 9 – Brush.

6 9 4.6 – K

„ ' .
DrawArc Kn ,o n
DrawBezier Kn € n
DrawBeziers Kn € n
DrawClosedCurve '
DrawCurve ' -

DrawEllipse Kn ,
DrawLine ' n n nK
DrawPath Kn 9'n GraphicsPath, -
n
DrawPie Kn ,
DrawPolygon Kn 9 K K -

DrawRectangle Kn
Draw Rectangles Kn
110

6 9 4.7 – K 9

„ ' .
FillCIosedCurve Kn , -
, 9 n
FillEllipse Kn 9
FillPath Kn 9 n 9'n Graphics Path,
n
FillPie Kn 9
FillPolygon Kn 9 , ,
9 n
FillRectangle Kn 9
FillRectangles Kn 9
FillRegion Kn 9 n 9'n Region,
n

' Color, Brush & Pen


.9'n Color, Brush 7 n K -
. .9'n Brush K 9 ( 9 -
,7 n– 9 ( , 9'n Color n
.
Color. 8 Color d n
System.Drawing n .J ( n -
: Alpha, Kn ,
Red, Green Blue 0-255. ” 9 , 9-
Color.FromArgb:
Color myColor;
myColor = Color.FromArgb (128, 255, 12, 43);
u o n , Alpha
( , d d Red, Green Blue:
Color myColor;
myColor=Color.FromArgb(255,12,43);
J ,9 n .NET Framework
:
Color myColor;
myColor = Color.Tomato;
Brush. .9'n Brush n ( K
9 .: n o 9
Brush K K 9 -
111

.6 , d 9'n -
, 4.8.

6 9 4.8 – 6

6 ' .
SolidBrush System.Drawing 8

TextureBrush System.Drawing J -
9 -
(
HatchBrush System.Drawing.Drawin J
g2D
LinearGradi- System.Drawing.Drawin J -
entBrush
g2D n
PathGradi- System.Drawing.Drawin J , o n
entBrush
g2D n -

A 9'n SolidBrush , -
:
SolidBrush myBrush = new SolidBrush (Color.PapayaWhip);
J d d K
.5 , TextureBrush 9-
9'n Image, LinearGradientBrush —
d , ( 9 .
7 n. .9'n 7 n K ' , -
K , (
.
„ n 7 n, n
(sealed). 8 9'n 7 n ,
:
Pen myPen = new Pen (Color.BlanchedAlmond);
: Kn ,
d Kn 1 K. • K
9'n 7en :
Pen myPen = new Pen (Color.Lime, 4);
112

v nd , .'
( , ' 9 -
( .v 9
d .5 n
myBrush:
Pen myPen = new Pen (myBrush);
' ( n
d .8 , ' .
' 9 -
( ,o -
.A .NET Framework n
SystemColors, 9 , o
K .6 , 9 -
(
,o 9 9 ( -
.. -
( :
Color myColor = SystemColors.HighlightText;
' SystemColors, .NET Framework ( n
SystemPens System Brushes, o K ' ,
K . v KK ,
9 - d, 7 n 9 SolidBrush.

/8! ) , $%#
J Graphics n ,o K K
.
€ - K 9
9'n 7 n, ,o Kn 9 ,– -
9'n Brush. J ,
9 - 9 9'n . 6 , , ( K
9 .
DrawRectangle:
/ / E8@65M8M 6J'rH8 Rectangle.
Rectangle myRectangle = new Rectangle(0, 0, 30, 20);
/ / E8@65M8M 6J'rH8 Graphics, L6@'FBI;MO B •65:6<,
Graphics g = this.CreateGraphics();
/ / QI:I?<@I8M L5F:6HK8;MH 7M789:;M: L956:.
g.DrawRectangle (SystemPens, ControlDark, myRectangle);
/ / X@D?T;M8M 597K57M, BIO;F8D 6J'rH86: Graphics.
g.Dispose();
113

d 9 9'n Graphics, 9 '


Dispose. : d d ( -
, o 9'n n9 .
0 9 - 9'n 7 n
Brush. 5 n, K 9
9'n Brush Graphics:
SolidBrush myBrush = new SolidBrush (Color.MintCrearn);
Graphics g = this.CreateGraphics();
// QI:I?<@I8M 9?DL7, @LM7I;MO @ L5F:6HK8;MH.
Rectangle inyRectangle = new Rectancle (0, 0, 30, 20):
g.FillEllipse (myBrush, myRectangle);
// X@D?T;M8M 597K57M 6J'rH8D@ Graphics D Brush.
g.Dispose();
myBrush.Dispose ();
u K :
– 9'n Graphics, o n 9 , -
K ;
–c 9 ( 9'n .A 9'-
n , o K , Point 9
Rectangle, ( 9'n 7en ( K )
Brush ( 9 );
– 9'n Graphics;
– , 9'n 7 n 9
Brush;
– 9'n Graphics.

%( F .' ) '
DrawString 9'n Graphics n 9 -
, K d -
. J 9'n Brush. v
9 9 ( -
( PointF). 5 ( -
,o 9 ( n K
Graphics. DrawString:
// z bT6:K L5MH?ICD @MH65M786@Kr8T7F 6C;I B
// EM789:;MS HM789O H?I7K SystemBrush.
Graphics g = this.CreateGraphics();
String myString = "Hello World";
Font myFont = new Font ("Times New Roman", 36, FontStyle, Regular);
//f78I;;D C@I LI5I:985M L59C78I@?F<8T H665CM;I8M 5FCHI.
g.DrawString (myString, myFont, SystemBrushes.Highlight, 0. 0);
114

// Q9 BIJKCT89 B@D?T;M8M 6J'rH8 Graphics!


g.Dispose();
: 9 ( :
– 9 9'n Font Brush, o K
9'n String;
– 9'n Graphics, ' 9 K,
9 9 ( ;
– Graphics.DrawString, d 9'n String,
Font, Brush 9 , 9
9 ( .;
– 9'n Graphics, d Graphics.Di-spose.

/8! ) / ( , $%#
~ K 9 d , (
, 9 . 9'n n 9 -
, d K , K K
n .J K -
n 9'n Graphics Path
System.Drawing. Drawing2D. : n
9 9 .6 , ( 9'n GraphicsPath,
, d 9'n , o -
n .

! ' ) F'x GraphicsPath


.9'n GraphicsPath KK nK
. 5 d 9'n GraphicsPath
n :
GraphicsPath myPath = new Drawing2D.GraphicsPath ();
J , 9'n
GraphicsPath -
Byte. 6 K -
, Byte – , 'n K .' -
d 9 n
System.Drawing.Drawing2D.PathPointType, ( 9 d
.. ,o Kn
( 9'n GraphicsPath:
// w9O L5MH?IC L5MLK7HIr ;IF@;D78T 6L95I865I using
//System. Drawing.Drawing2D
GraphicsPath myPath = new GraphicsPath (new Point [] {new Point (1,
1),
new Point (32, 54), new Point (33, 5)}, new byte [] {
115

(Byte) PathPointType.Start, (byte) PathPointType.Line,


(Byte) PathPointType.Bezier});
A 9'n GraphicsPath ( -
.2 — , ( 9 (
9 ) 9 ( -
).
GraphicsPath K -
( 9 4.9).

6 9 4.9 – 9'n GraphicsPath

„ ' ” n
AddClosedCurve ,
AddEllipse
AddPath 9'n GraphicsPath
AddPie (
AddPolygon 9 ,
AddRectangle '
AddRectangles
AddString -

A n 9 9'n -
GraphicsPath, , K , -
.” 9 ,
GraphicsPath.StartFigure. A , K
GraphicsPath, ( . d
K , GraphicsPath.CloseFigure, o 9
– 'n n
d K. . :
GraphicsPath myPath = new Drawing2D.GraphicsPath ();
myPath. StartFigure ();
6 9 , n .
myPath.CloseFigure();
u o StartFigure , -
d ( CloseFigure, , d -
StartFigure, d K.
116

' .' 9 ( 9 - -
n ,o 'n -
n d K n .
9 4.10 9'n GraphicsPath, o -
K .

6 9 4.10 —

„ ' ” n
AddArc A
AddBezier J € 'n
AddBeziers 5 9 € n
AddCurve J ,
AddLine '
AddLines 5 9 'n

u K :
– 9'n Graphics, ' 9 K
K , 9 K ;
– GraphicsPath;
– K
GraphicsPath;
– Graphics. DrawPath, o 9 K ,
9 Graphics.FillPath, o 9 K d , 9'-
n GraphicsPath;
– 9'n Graphics.

4.2.2.2 ! ' ) '/'-' %! ' ! )

: Windows Forms -
( .: KK
n . .NET
Framework n ( 9 -
.s -
.NET Framework — , ,o -
n .„ n ( -
,o K .' - d , -
117

Visual Studio .NET, 9 Microsoft. : 9 -


K d ( - K 9 n -
9 K K. ' - ,
9 , (
K ActiveX. s -
9 K ( ,
.NET Framework. 5 d , o , 9 -
d ,
.NET Framework, 9 , -
n K .

N , c, , ! ' ) '/'-' %! ' ! )


: .NET Framework n 9 -
o Control K 9 -
9 , 9 -
.5 , Control n 9 9 , -
K d, ( , n-
n .8. J , K
o 9 , , -
.. 9 n o
, , o n -
.
. :
– n K ;
– 9'n ;
– « ».

()*+ ,-*../ -&+ &0.,12 3 45464. &- 47,-*../


' d , d
K .8
9 n K 9 ,
n ( .
J , n
.5 , ,
Button, n .v
n ,
9 ( 9 .
118

€ d Windows Forms, o
K NotInheritable (sealed), ( 9 9 -
.5 , -
n TextBox 9
9 PictureBox K
9 ( K 9 ( .8 -
( n K , -
9 , d
.' — , n -
K K.
. d K -
( n d 9 n -
.v , o n 9 9-
d 9 ( K Windows
Forms , d d -
.A 9 , 9
, 9 d .
u o ( 9 d K
K, d K ;
K 9 , .

()*+ ,-*../ -&+ 5*0, UserControl



d .' , 9 -
, o n ' ( -
9 ( n ' , o
TextBox. ( , d
,o -
.v 9 , 9 ( 9
.s , 9'n -
Windows Forms, K -
9 .
8 d K
UserControl n 9 , ( d
d , , d
.A n -
o Windows Forms
, n ( .6 ,
119

( K , 9'n K
Windows Forms .
' 9
( , 9 , ,
d d d -
.
8 ( -
— (
.8 9, ,
, o n K
( .

()*+ ,-*../ -&+ 5*0, Control


u o 9 9 d 9 ( ,
n -
, K -
.2 n o Control — 9
.J Control n « o »
9'n , K -
, n d K ( -
, Click), ( d K 9 -
, , Font, ForeColor, BackColor, Visible . .
6 d Control n
( .: K , 9
d , -
9 .‡ Control n
, o K 9 , 9 -

.
8 9 n 9 -
d , n -
( ,
,o ( 9
.2 9 , -
o 9 9 , n(
d .

9:+*-*../ 254.&- +: 45464. &- 47,-*../


120

' , ,
n -
.~ n d -
9 - o ,o 9 9 -
, o .
: , 9-
, 9 ( K Properties, ( -
d 9 - .u o ,o 9
9 ( Properties, -
d 9 Browsable false.
. 9 :
<System. ComponentModel. Browsable (false)>
public int StockNumber
{
/ / A9I?DBIbDF @?I78M@678D 6LKj9;I.
}

! ' ) %( , '/'-' %! ' ! )


' K -
9 , ( ( .” 9 -
, 9 9
, Windows
Forms. 5 , , 9 (
d o . 5 ( -
, Button;
public class myButton: System.Windows.Forms.Button
{
/ / y;eMO H6C 6LKj9;MO.
}

;4*5&<*=&/ .:- 3 6:>5 -:0 4? - ):3&+. 3 45464. *3 47,-


-*../
u , K ,
9 ,
Windows Forms. A
K 9 K ,
9 . ' , o 9 9
, ( d .A
9 OnKeyPress :
121

public class NumberBox:System.Windows.Forms.TextBox


{
protected override void OnKeyPress (KeyPressEventArgs e)
{
if (char.IsNumber (e.KeyChar) = false)
e.Handled = true;
}
}

A:+ B& *=&/ <:-.&C.D:E: - E5/+, &0.,12 3 45464. &- 47,-


-*../
„ d , KK ,–
d .” 9
K , OnPaint, -
d .5 ,o 9
, Region
OnPaint. J Region, 9 GraphicsPath, n 9 -
, K GraphicsPath. 6 , (
GraphicsPath, o n d -
, 9'n Region
.v K n
:
public class WowButton: System.Windows.Forms.Button
{
protected override void OnPaintfPaintEventArgs pe)
{
System. Drawing. Drawing2D.GraphicsPath myPath = new Sys-
tem.Drawing.Drawing2D.GraphicsPath ():
// QI78KL;I H6:I;CI @78I;6@?<r @?I78M@678D FontFamily 8I FontStyle
// R?9:9;8K H95K@I;;F 8IH, j6J 6J'rH8 GraphicsPath L59C78I@?F@
// Y6;8K5 5FCHI 7M:@6?D@ 56B:D56: 72 LK;H8M.
// [5ISKO89: K E# ;96JSDC;6
// t@;6 L9598@65M8M B;Ic9;;F Font.Style @ bD?9 cM7?6,
myPath.AddString("Wow! ", Font.FontFamily, (int) Font.Style, 72,
new PointF(0, 0), StringFormat.GenericDefault):
// E8@65M8M ;I 67;6@D GraphicsPath ;6@MO 6J'rH8 Region.
Region myRegion = new Region (myPath);
// 46:D78M8M ;6@MO 6J'rH8 Region @ 6C;6O:9;;9 @?I78M@D78T
// R?9:9;8K H95K@I;;F. this.Region = myRegion;
}
}
( , WowButton —
,o 9 ( n , n 9 ,o
«Wow!». ” 9 , 9
9 , 9 ( 9 .' 9 KK
122

,o 9 -
.' .' -
, , TextBox, n
, K, d n .
6 n Paint, o -
n K o 9 9 n , -
9 .
8 :
– K
;
– 9 (
,o n .

( -:74../ 0 5*+:- 3 45464. &- 47,-*../


J , 9 , 9'n -
K ( K -
Windows Forms, K d K
.J n
9 , 9'n
.A UserControl
, (
.
7 9 ( 9 9 , -
K .' ,o -
9 , -
n .' 9 -
n
9 ( .' 9 n -
, d .nKeyPress -
:
// W?F L6CDG KeyPress 9?9:9;8I H95K@I;;F TextBox2
// 4685DJ;6 78@65M8M I;I?6UDc;MO 6J56J;MH.
protected override void OnKeyPress (object sender, KeyPressEventArgs
e)
{
// 4959@D5M8M, cM r ;I8M7;K8I H?I@DeI bM•56@6<.
if (char.IsNumber (e.KeyChar) == false)
e.Handled = true;
Label1.Text = (int.Parse (TextBox1.Text) + int.Parse (Text-
Box2.Text)). ToString ();
}
123

: ( K -
.6 ( 9 , 9 K d
, ( -
, , . ” 9
d 9 K -
, 9 9'n
.7 -
K K (Button1). ” 9
BackColor 9'n Button1, , ,
— 9 9'n , ( :
public color ButtonColor
{
get
{
return Buttonl. BackColor:
}
set
{
Buttonl. BackColor = value;
}
}
” 9 ,
Modifiers. v ( Properties
9 , n. : -
Modifiers n 9 - , -
n .
u :
– , UserControl;
– UserControl K
Windows Forms,
;
– 9 9
;
– ( , 9
.

( -:74../ B:76:-*. 3 45464. &- 47,-*../


2 K (
d K K nK K, -
9 n 9 d . . 9 Control n
o 9 , 9
124

,o n 9 -
.u o , n
9 .
' n 9
n .. d -
, n Paint, -
K 9 9 Paint. Control 9 9
Paint n OnPaint.
OnPaint n n —
PaintEventArgs, o K n 9
.: — Graphics
ClipRectangle.
Graphics — 9'n Graphics, o n n
9 .' 9 -
9 ( . ClipRectangle — -
, n 9 .' d
9 ( ClipRectangle 9 ( n
9 .u o n d ,
( 9 K .'
ClipRectangle n
9 , 9 .8 -
n 9'n
ClipRectangle n Size.
K
, d n -
(0,0), 9 KK .5 -
n OnPaint, o n
:
// w9O L5MH?IC L5MLK7HIr ;IF@;D78T 6L95I865I Imports System.Drawing
protected override void OnPaint (PaintEventArgs e)
{
Brush aBrush = new SolidBrush (Color.Red);
Rectangle clientRectangle = new Rectangle (new Point (0,0),
this.Size);
e.Graphics.FillEllipse (aBrush, clientRectangle);
}
' Kn
ClipRectangle,
9 ( .” 9 -
( K , -
125

Control.SetStyle ResizeRedraw true,


:
SetStyle (ControlStyles.ResizeRedraw, true);
s6J K JKCT-FHMO cI7 L959:I?<@I8M 9?9:9;8 H95K@I;;F @5Kc;K, C678I8;T6
@MH?MHI8M :986C Refresh:
Refresh();
tH 78@65M8M •65:6@I;MO 9?9:9;8 H95K@I;;F:
6U6?67D8T H?I7, L6SDC;MO @DC H?I7K Control;
C6CIO89 @ :986C OnPaint H6C, j6 @DBKI?DBKr 9?9:9;8 H95K@I;;F.
7 ( , 9 d -
.

4.2.2.3 !') ) , !, ( &, !') , F 8


'/'-' %! ' ! )

' 9 9 - K
, d , d d .5 -
d , ( K 9
.
( ! ) '/'-' %! ' ! ) '/b Toolbox
, ( K. ” 9
9 9 9 -
9 , Toolbox,
9 - ( .
v n d Customize Toolbox, -
K Toolbox. J K -
K Toolbox 9 - Customize Toolbox, o 9 -
Customize Toolbox. 9 -
( , 9
Toolbox.
A
Toolbox:
– K K d Toolbox 9 -
Customize Toolbox — n ;
– .NET Framework Components
Browse, o 9 File;
– DLL 9 s‡s- ,
9 .: 9
Open;
126

–' , o 9
Customize Toolbox .J —
' Toolbox.

N , &' ) & '/'-' ' ! )


Visual Studio .NET n 9 ( -
, Toolbox, ( -
n 9 ( .A -
, 9 . 9 ( Toolbox
K K ToolboxBitmapAttribute — -
9 ,o n .: -
n 9 (
16x16 9 Type (type). : -
Kn ,
9'n .
0 9 ToolbaxBitmapAttribute d
. Visual C# 9 K
( ( K d -
, d . u -
, d 9 ( : ( -
9 ( K 9 ToolboxBitmapAttribute:
[ToolboxBitmap (@ "C:\Pasta.bmp")]
public class PastaMaker : Control
{
// A9I?DBIbDF 6LKj9;I.
}
u :
d 9
ToolboxBitmapAttribute -
:
[ToolboxBitmap(typeof (Button))]
public class myButton: Button
{
/ / A9I?DBIbDF 6LKj9;I.
}

/ # (.' ) '/'-' %! ' ! )


9 9 - n
( , ..
n , ( 9 -
127

Windows Forms. ' -


9 K .
' , d
, ( d ( ,
.
u o d n K
, Windows Forms, -
, o -
99 9 ,
d .
5 ( Windows Forms:
– d , 9 d K Build -
Build Solution;
– 9 9 -
K ;
– -
9 . d K
Toolbox, o 9 9 d K
9 ;
– ,
d F5, — ( -
( : -
, d ( 9 ,
o K ;
– d , K ,
o 9 .A 9 -
K Build Rebuild Solution. 5 (
99 9 :
– d , 9 d K Build -
Build Solution;
– K File 9 Add Project \ New Project, o 9
d , — n
Add New Project;
– Add New Project 9 Windows
Application, ' .J;
– Solution Explorer K K d
References 9 K
Add Reference — n Add Reference;
128

– Projects. u o , o -
, ,
(¢, Browse
DLL- , ( n -
.: .J.
– -
9 . d
Toolbox, 9
9 ;
– , d F5 — ( -
( : -
, d ( 9 ,o
K .

' ! ) /%c' ! )- '/'-' %! ' ! )


: .NET Framework n 9 -
.y -
n K :
9 -
. , , n, o 9
9 9 License
Provider Attribute, o n 9'n License.
Provider — 9 -
. License Provider — 9 ,o n -
.
LicenseManager. Validate, o 9 -
K. u o -
, ( K. License
Manager. Validate n GetLicense 9'n License
Provider, n K n K
IsKeyValid 9'n LicenseProvider. u o IsKeyValid n true,
( n K, -
( n .
(
LicenseProvider. .NET — LicenseProvider n
LieFileLicenseProvider. u o LicenseProvider,
n . GetLicense
d n DLL-
129

' FullName.LIC, FullName — ' -


.' IsKeyValid n d
«my ClassName is a licensed component»,
myClassName — ' .7 9
, d .
( 9
Dispose, , ' -
K .
. n Widget, o -
n K LieFileLicenseProvider.
// w9O L5MH?IC L5MLK7HIr ;IF@;D78T 6L95I865I Imports Sys-
tem.ComponentModel
// w9 I85MJK8 LicenseProvider, FHMO @HIBKr 8ML @_M@I;6U6
//LicenseProvider.
[LicenseProvider (typeof (LieFileLicenseProvider))]
public class Widget: System.Windows.Forms.Control
{
private License myLicense;
public Widget ()
{
// 4959@D5M8M ?Db9;BD< 8I 685M:I8M L67M?I;;F ;I ;9G.
myLicense = LicenseManager.Validate (typeof (Widget), this);
// y;eMO H6C H6;785KH865I 6LKj9;MO.
}
// w9 :986C Dispose, j6 B@D?T;Fr 597K57M,
// 46@'FBI;D B L959@D5H6< ?Db9;BDG.
protected override void Dispose (bool Disposing)
{
if (myLicense != null)
{
myLicense.Dispose ();
myLicense = null;
}
}
}
7 :
– ( LicenseProvider, d d
9 LicenseProviderAttribute;
– License, 9 9
LicenseProvider;

LicenseManager. Validate ;
– , K , d
Dispose .
130

! .' ) '/'-' ' ! ) ! Internet Explorer


€ - Windows Forms ( ( -
Internet Explorer. v n 9 HTML- d -
( .
n , .
” 9 ( Internet Explorer, -
n ( d 9 9 (Global
Assembly Cache, GAC) 9 HTML-
K, d . A d -
Windows Forms HTML- ( <OBJECT>,
n 9'n . 6
<OBJECT> n ( 9'n
classid, ( ( Windows
Forms K .
: classid Windows Forms -
n :d -
, «#».
5 ( <OBJECT>, d n
' ControlLibrary1.myControl,
ControlLibrary1.dll, d -
HTML- K:
<OBJECT id = "myControl"
classid = "http:ControlLibrary1.dll#ControlLibrary1.myControl"
VIEWASTEXT>
</ OBJECT>
( Internet Explorer:
– d DLL-
HTML- , d , 9 GAC;
– HTML- <OBJECT> -
classid. : classid K nd
DLL- ' , «#».

4.3 !( )( F ,

4.3.1 .
K 9 , K , ( -
.
4.3.2 8 , -
9 ( , .
131

4.3.3 . 9 .
4.3.4 : .

4.4 -% !%

4.4.1 6 9 .
4.4.2 9 .
4.4.3 J .
4.4.4 J .
4.4.5 : , o
(5 d . 9 ), ( 9 ( K -
9 .

4.5 /b % , )

4.5.1 u , , K -
?
4.5.2 ' PrintPage, .
4.5.3 u 9 n ?
4.5.4 u 9 n ?
4.5.5 s PrintPreviewControl, .
4.5.6 s PrintPreviewDialog, .
4.5.7 s PageSettings, .
4.5.8 s PageSetupDialog PrintDialog,
.
4.5.9 ” GDI + ?
4.5.10 u 99 System.Drawing?
4.5.11 .9'n Graphics, K -
9 .
4.5.12 .9'n Color, Brush Pen , -
.
4.5.13 5 ’ , o 9 9'n
GraphicsPath.
4.5.14 u ?
4.5.15 u K ?
4.5.16 u 9 n UserControl?
4.5.17 u 9 n Control?
4.5.18 u Toolbox?
132

4.5.19 u ?
4.5.20 u ?
4.5.21 u ( Internet Explorer?
133

L M

1. 8 Ž. Ž ( 9 /
8 Ž. – . : Ž «: », 2002. – 623 .
2. Pfleeger S. L. Software Engineering. Theory and practice /
S. L. Pfleeger. – New Jersey : Printice Hall, 1998. – 576 p.
3. 7 K (
= Software Engineering 2004: Curriculum
Guidelines for Undergraduate Degree Programs in Software Engineering;
Computing Curricula 2001: Computer Science: . . – .:
Ž56 Ž6.7 «Ž - Ž ˆ 6 »,
2007. – 462 .
4. Guide to the Software Engineering Body of Knowledge
(SWEBOK). – New York : IEEE Publising House, 2004. – 129 p.
5. . 8. 0. 6 9 9 -
. 9 : / 8. 0. . . 8'9. : ' . – 2002. – 463 .
6. : 0. Microsoft Office 2007 / 0. : . – 8'9. : ' -
, 2007. – 160 .
7. J :. e. ' VBA 2002 /
:. e. J . – .: ... «€ - », 2003. – 880 .
8. y 9 :.:. 8 • 8 ( Windows /
:. :. y 9 . – :‡ , 2003. – 385 .
9. A( €. . ˆ Microsoft Visual Studio .Net 2003 /
€. A( .– :7 , 2003. – 463 .
10. € d A. C# ( / A. € d . – :
€Ž5. y 9 , 2005. – 467 .

Vous aimerez peut-être aussi