visual c++/net. Классика программирования. Степаненко О.Е
Post on 19-Oct-2014
1.316 views
DESCRIPTION
http://leontyev.at.uaTRANSCRIPT
Visual C++.NET
. . .
2010
32.973-01 681.3.06
79
. . 79 Visual C++.NET. .: - : , .; -
, 2010. 768 .
ISBN 978-5-9315-0003-5
Visual C++.NET. , - , , . ++, - . ++ . , - , , Internet.
32.973-01
Visual C++.NET
25.01.2010. 70 00 1/16. . 500 . . . . 48. 2-1458
"" .
ISBN 978-5-9315-0003-5 (.) " ", 2010
1 0
VISUALSTUDIO.NET ! 1 0
.NET. Visual Studio.NET Visual Studio.NET /1
VISUAL STUDIO 5 / 6 VISUAL STUDIO.NET 11 12 1 2
I. MICROSOFT VISUAL C++.NET 13
1. VISUAL C++.NET 14
MICROSOFT VISUAL S T U D I O . N E T 14 Visual Studio.NET. 15
VISUAL S T U D I O . N E T 16 16
2. VISUAL STUDIO.NET 17
17 19
Solution Explorer Class View 23 23
: 2 4 2 4 2 4 2 7
II. ++ 28
3. ++ 29
C + + 2 9 + + 3 3
33 34 36 : 37 38 39 43 48 new delete 49
5 2
g
4. ++ 54
5 4 5 5 5 7 6 0
60 66 67
- 6 7 6 9
THIS 7 1
7 2
7 4
5. - ++ 76
- ( ) 7 6
78 79
81
Class View 82 8 3
86 87
8 9
6. , 90
9 0 - 92 95 96
9 9 100 102 107
108
7. ++ 109
109 112
112 114 116
.118
8. 119
119
Catch- 123
4 Visual ++.NET.
? 124 126
WIN32-HCKJ1I04EI1 128 1 3 2
. .... 134
9. 135
135 136
136 138 141
142 155
155 Ini tlnstance 157
160
10. 162
162 163 164 164 ScratchBook /72 ScratchBook /75 ScratchBook /76
187 My Scribe - 188 MyScribe 191
201
11. 203
2 0 3 2 0 7
Remove All 208 Undo 209
2 1 0 SCRATCHBOOK 211 2 2 4
12. - 226
- SCRATCHBOOK 2 2 6 File 226 227 ScratchBook 232
- MYSCRIBE 2 4 5
g
246 246 MyScribe 247 - 258
2 5 9
13. 260
2 6 0 260 262
2 6 6 2 6 7 SCRATCHBOOK 2 7 2 2 8 8
14. 290
SCRATCHBOOK 2 9 0 291 292 293 300
SCRATCHBOOK 3 0 1 SCRATCHBOOK 3 0 2 3 2 1
15. 322
3 2 2 FontView 323 Text Properties 323 329 331 MFC- 331 Text Properties 334 ! 339 FontView 344
3 6 0 361
Tab View 365 3 8 0 381
16. 382
DIALOGVIEW 3 8 2 DialogView 382 383 DialogView 387
FORMVIEW 3 9 3 FormView 394
6 Visual ++.NET.
FormView.... 399 4 1 0
17. 411
4 1 1 MYSCRIBE 4 1 2
412 4 1 5 MYSCRIBE 416 4 2 8
18. / 430
4 3 0 431 Font 435 445 Ini tlnstance 447
447 WM_KEYDOWN. 447 WM_CHAR 452
4 5 3 FONTINFO 4 5 5 4 7 3
19. 475
4 7 5 4 7 6 4 8 2 4 8 5
485 FractalView 490 501
SCRATCHBOOK 5 0 8 512 ScratchBook 526
5 5 6
20. 557
5 5 7 558 559 561
5 6 3 Pa t11 564 Bi tBl t 565 StretchBl t 568
5 6 9
g
CHESSBOARD 571
ChessBoard. 573 5 8 3
21. 585
5 8 5
5 8 8
559 590 OnDraw 594
SCRATCHBOOK 5 9 6
6 2 6
22. 628
6 2 8 630 630 MFC- 632
6 3 2
634 IMPFRACTAL 6 3 7
ImpFractal 641 6 5 2
23. 654
6 5 4 } 6 5 7 6 5 9 6 6 0 6 6 1
662 663 671 675
6 7 6
24. - VISUAL ++ 678
, 6 7 8 - EXPSERVER 6 8 0
- 681 - 685 ExpServer 690
- EXPCONTAINER 7 1 0
710 713 ExpContainer 713 ExpContainer 715
: 7 3 3
8 Visual ++.NET.
25. ACTIVEX VISUAL ++ 734
ACTIVEX- 7 3 4
735 ActiveX. 737 ActiveX. 740 ActiveX 740 ActiveX- 740 ActiveXControl 741
- ACTIVEX- 7 4 9 ActiveXContainer 750
7 5 5
756
g
, Visual ++ -
, -. ' , . Visual ++ .NET, Visual Studio.NET, - . ( - ), ActiveX Internet, , .
- Visual ++ - , . , . , - . - . - , Visual ++, , , , .
Visual Studio.NET
.NET Microsoft.NET - , , ,
, Internet, Web- Web- (Web-). Web- Web- ( Internet-), Web- - ( XML SOAP).
.NET- MSIL (Microsoft Intennediate Language) .NET Framework, CLR (Common Language Runtime) .NET Wramework Class Library. CLR pea- " " MSIL , , - , . .NET Framework , .NET-. Windows Form ( ), Web Forms (- Web- Web- ASP.NET), XML Iritemet- (FTP, HTTP, SMTP, SOAP), ADO.NET ( -) . .NET Framework , (assembly), /+ CLR COM Interop, - .NET .NET- . .NET Windows, .N- . , Global Assembly Cache (GAC).
.NET Framework , Microsoft (www. m i c r o s o f t . com/ n e t / d o w n l o a d s . asp).
/
10 Visual ++.NET.
Visual Studio.NET , Visual Studio.NET ,
. , ,
Web-, Visual C++.NET - ATL Server Library Managed Extension. , Web- . , Visual Studio.NET .
- , - Visual Studio 6 , Microsoft , . Visual Studio .NET 20 , , .
, - , .
. , , .
Internet, Visual Studio.NET - Microsoft, , - .
Visual Studio.NET Visual Studio.NET - Professional, Enterprise Developer Enterprise
Architect. Enterprise- , - .NET Enterprise Servers (Developer - , a Architect - ). Visual Studio.NET Enterprise Architect - - 5 CD.
Visual Studio 5/6 Visual Studio.NET , Visual Studio.NET
, .. Internet-, , , , Visual Studio - . - , Visual Studio.NET . , , - Visual Studio.NET Visual Studio 5/6 , , , . Visual Studio.NET , Microsoft , Visual Studio , . -, ( & & ). , ( , .), , , - , , . .NET, ActiveX. , , Visual Studio, Windows- Visual Studio.NET , : - , , , -, . , , .NET. , Web- .NET-, -. , Web-. Visual Studio.NET. Web-
11
. , Web-, , . .
Visual Studio.NET, - . , Web- ATL Server Library Managed Extensions.
. Visual Studio.NET , . Visual ++ 6 Visual ++.NET - , Managed Extensions. , Visual C++.NET "" , .NET Framework . Visual C++.NET - . Microsoft Visual C++.NET "" . .NET - . Managed Extensions , , .NET-, MSIL. , - , .NET-, , , .
(. 1 2) Visual ++. .
Visual ++. - Developer Studio. Developer Studio , , .
(. 3 8) ++, ++. - MFC (Microsoft Foundation Classes).
(. 9 25) - . - Microsoft Windows 2000, Windows NT 4.0 . , Developer Studio - , , Application Wizard, . . III - Windows, , , , (multiple document interface - MDI), , -, OLE ( ). ActiveX - , Microsoft OLE.
++
Windows. , - . ++ . , , : " " ("The Programming Language") (Kemighan) (Ritchie) " " (": A Reference Manual") (Harbison) (Steele).
12 Visual ++.NET.
1
Visual ++.NET Microsoft Visual Studio.NET Visual C++.NET
Visual C++.NET
Microsoft Visual ++.NET Microsoft Visual Studio.NET Visual ++, . Visual -t-.NET Visual Studio.NET .NET Frameworks , .
Microsoft Visual Studio.NET Visual ++.NET ,
Windows 2000, Windows NT 4.0, Windows XP ( Visual Sludio.NET).
: - 96 Windows 2000 Professional, 192 Windows 2000
Server ( 128 Professional, 256 Server); - Pentium II 400 ( Pentium III 650); ( ) - 500 , 2.5 ,
.
Visual Studio.NET 4- CD 1 DVD. 1. setup.exe -
. 2. -
. -Windows Component Update. Windows , Visual Studio.NET. -, .
3. , .
4. ( ) , . , , , ( , Automatically Log On).
5. - Visual Studio.NET. ,
14 I. Microsoft Visual ++.NET
, . () Continue. , /, Visual Studio.NET. - . Visual Studio.NET. - .
f ig Microsoft Visual Studio.NET 7.0 Professional Setup - Options Page
Microsoft* VisualStudio.NET
Select i tems to install
B0 Visual Studio.NET Professional Edition 1:1 H \ i j language Tools
0 *}Visual Baric.NET X Visual C++.NET
Vif-ual C#.NET Lil-'Htfej-NnT Frumrwork SDK i:i) 0 ! J Crystal Report* for Visual Sludio.NET Tools for Redistributing Apps 0 j Server Components H v ^ M S D N Documentation
. - 0 ^ Visual Basic documentation ; X Visual C# Documentation . V i s u a l ++ Documentation i 0>;^.NET Frsmei/Wi'l Documentation ! Platform 5DK Documentation ^ 0 ^ Additional M5DN Documentation ; V i s u a l Studio Tods Documentation
Visual Studio Documentation
Rf f t to i - De fau l t
Unleash the power of .NET
Feature properties
l ^ u a l S t i i d i c m e t i
Narrirt | Visual Studio.NET Pi of;.ion.i! Fdition
* || D:'iiUseis\mol)iavko\vs.nel\ | 5 Pequii xl I fill .97 MR
Feature description Installs sriterpr
out of tools that help you de si soluMons. d
zJ
Space allocation
Drive
S h o w / H i d e thr ives
Used Space (. . "stdafx.h" ! include "iostream.h"
-har name[16];
voici main() I
cout Name = "hello"; // PNode->Value = 1; PNode->Next = 0;
, 0. , .
PNode = new Node; if (PNode = 0)
// ... else
// PNode ... . 8. , new,
d e l e t e , . , , , :
delete PChar; delete Pint; delete PDouble; delete PNode;
, d e l e t e . 0 d e l e t e ( 0 ).
50 II. ++
, -, new. . , , . , new, delete. new delete , . malloc, . malloc, new - . . 4, - new , (.. , ). delete ( ). new delete , . . 6. new delete .
new : (.. );
( "[]"). : , , .
:
void Func (int Size) { char *String = new char [25]; int *ArrayInt = new int [Size]; double *ArrayDouble; ArrayDouble = new double [32];
I I . . . }
, new . , delete "[]", , , . , , , -:
delete [] String; delete [] Arraylnt; delete [] ArrayDouble;
new , - 0. new - (, c h a r ) :
char *PChar = new char ('a'); // char 'a' int *PInt = new int (3); // int 3
(, ) . NodeA , new .
// 25 // 'Size'
// 32 //
3. ++ 4 * 51
struct Node { char *String; int Value; Node *Next; } ;
void Func () { Node NodeA = {"hello", 1, 0);
Node *PNode = new Node (NodeA); }
new . , new. (. 4) ( ) , , new .
++.
, ++, :
++ ++ ( ), , ++, , , ++ . ++. , , , , ++. ++.
- ++ "//".
++ , . .
. , - , "::" .
Mine-. , i n l i n e , - ( ) .
. . , .
-. & , . , .
-. , c o n s t . c o n s t ,
52 II. ++
" ". , c o n s t , , . . , . , , . , , .
. , ++ new d e l e t e . , ++. .
3. ++ 53
4
++
-
this
++. , ++. , - , , (, , ). , (, ; , ; , ; , ). . - .
++ ,
, --, . ++ . -. , , , :
struct Rectangle { int Left; int Top; int Right; int Bottom;
} ;
. Line - - , , -, , . - in .
void DrawRectangle (struct Rectangle *Rect) { Line (Rect->Left, Rect.->Top, Rect->Right, Rect->Top) ;
54 II. ++
Line (Rect->Right, Rect->Top, Rect->Right, Rect->Bottom); Line (Rect->Right, Rect->Bottom, Rect->Left, Rect->Bottom); Line (Rect->Left, Rect->Bottom, Rect->Left, Rect->Top);
}
, Rectangle, DrawRectangle.
struct Rectangle Rect = (25, 25, 100, 100); DrawRectangle (ScRect) ;
, ++ , , . ++ - :
class CRectangle { int Left; int Top; int Right; int Bottom;
void Draw (void) { Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom); Line (Left, Bottom, Left, Top); }
} ;
, - ( ). , , - . - - Left, , Right Bottom, - - Draw. : - , , .
,
, .
struct Rectangle { int Left; int Top; int Right; int Bottom;
} ;
, ,
struct Rectangle Rect; , CRec t ang l e , , .
, . , -
4. ++ 55
CRectangle Rect; & CRectangle,
( - ). . Rect CRectangle . , , (, , - ). , , .
, , ++ new. , , .
CRectangle *PRect = new CRectangle; , d e l e t e
( . 3).
delete *PRect; c l a s s . ++
, , . .
- - . , . Rectangle , , - , (jmvate) ( , ). , - . , Draw - Top, Left, Right Bottom, Draw - - . * , main, - - Draw . , - public, ( ), ( , ). , Rectangle . , ( , ).
class CRectangle { public:
int Left; int Top; int Right; int Bottom;
void Draw (void) { Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom); Line (Left, Bottom, Left, Top); }
} ;
56 II. ++
CRectangle , ".",
CRectangle Rect; // CRectangle
Rect.Left = 5; Rect.Top = 10; Rect.Right = 100; Rect.Bottom = 150;
// - //
Rect.Draw (); // , new,
- . :
CRectangle *PRect = new CRectangle;
PRect->Left = 5; PRect->Top = 10; PRect->Right = 100; PRect->Bottom = 150;
PRect->Draw ();
, , ( ). Rectangle , -. CRectangle - , - ( Draw), - (Left, Top, Right Bottom), .
class CRectangle { private:
void Draw (void) { Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom); Line (Left, Bottom, Left, Top); }
- . private - , , . public, , private , ,
int Left; int Top; int Right; int Bottom;
public:
4. ++ 57
. , Left, Top, Right Bottom , Draw . : private , . private . ++ - protected. "" . 5.
, , Rectangle.
void main () { CRectangle Rect; Rect.Left = 5; Rect.Top = 10; Rect.Right = 100; Rect.Bottom = 150; Rect.Draw (); }
// CRectangle // : // // /'/ ( )
-, . - -, -. public Rectangle, .
int R, int ) void SetCoord (int L, int T, { L = min ( max (0,L) T = min ( max (0,T) R = min ( max (0,R) = min ( max (0,B) R = max (R,L); = max (, T) ;
80)
25) 80)
25)
Left = L; Top = T; Right = R; Bottom = B; }
: , , SetCoord , , - . max min ++. - Stdlib.h. CRectangle , :
void main () { I I . . . CRectangle Rect;
Rect.SetCoord (25,25,100,100);
Rec t.Draw ();
/ / . . . } ;
// // //
58 II. ++
-, . p u b l i c .
void GetCoord (int L, int T, int R, int B) { *L = Left; *T = Top; *R = Right; *B = Bottom; }
Rectangle, - SetCoord GetCoord. , - SetCoord GetCoord Rectangle ( ) , - .
#include class Rectangle { private:
int Left; int Top; int Right; int Bottom;
public: void Draw (void) {
Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom); Line (Left, Bottom, Left, Top); } void GetCoord (int *L, int *T, int *R, int *B) { *L = Left; *T = Top; *R = Right; *B = Bottom; }
void SetCoord (int L, int T, int R, int B) { L = min ( max (0,L), 80); T - min ( max (0,T), 25); R = min ( max (0,R), 80); = min ( max (0,), 25); R = max (R,L); = (,T); Left = L; Top = T; Right = R; Bottom = B; }
} ;
:
4. ++ 59
, -, .
, . , , ( , -). : CRectangle . - :
private: int Left; int Top; int Width; int Height;
, SetCoord GetCoord , - , Rectangle. (, , - .)
- :
;
.
CRectangle
- - SetCoord. , -. . - , . , . , void ( ). ( ). , CRectangle , -:
class CRectangle { private:
int Left; int Top; int Right; int Bottom;
public: // :
60 II. ++
CRectangle (int L, int T, int R, int B) { SetCoord (L, T, R, B); }
// - ... };
- public. ( , protected, . 5.) , . CRectangle .
CRectangle Rect (25,25,100,100); , R e c t a n g l e -
( , ""). void main () { CRectangle Rect (25,25,100,100); //
// Rect.Draw (); // }
, new. new , new , , m a l l o c .
CRectangle *PRect = new CRectangle (25,25,100,100);
.
-, , . , CRectangle , - 0. , , , (. . 3 " ").
class CRectangle { private:
int Left; int Top; int Right; int Bottom; ,
public: CRectangle () { Left = Top = Right = Bottom = 0; }
// - ... } ;
4. ++ 61
, - . , , - - . , - , . ( ), , - .
CRectangle Rect; ,
. , , .
CRectangle Rect (); // , // CRectangle
- , , R e c t . . 6 , ( " ").
- ,
( , . 3). , - - . . ) . , C R e c t a n g l e , - .
class CRectangle { private:
int Left; int Top; int Right; int Bottom;
public: // : CRectangle () { Left = Top = Right - Bottom = 0; }
// : CRectangle (int L, irt T, int R, int 8) { SetCoord (L, T, R, B); }
// - };
C R e c t a n g l e .
62 II. ++
void main () { // : CRectangle Recti; // : CRectangle Rect2 (25, 25, 100, 100); I I . . . }
, . , , , // . -, (. ).
- - const
(. . 3). , const , , . :
class CTest { public:
int A; int B; CTest (int AVal, int BVal); { A = AVal; = BVal; }
} ; , -.
.
const CTest Test (1, 2); - const Test. =3; // : -
// const
const. , CRectangle. const CRectangle Rect (5, 5, 25, 25); ,
- GetCoord CRectangle -, const. - -, - const.
int L, , R, ; Rect.GetCoord (&L, &T, &R, &B); //
, -, . GetCoord const,
4. ++ 63
const. const GetCoord , . -. , .
class CRectangle { I I . . . void GetCoord (int WL6 int *T6 int *R, int *B) const { *L = Left; *T = Top; *R = Right; *B = Bottom; }
I I . . . } ;
GetCoord const CRectangle. const CRectangle Rec: (5, 5, 25, 25); int L, T, R, B; Rect.GetCoord (&L, &T, &R, &B); // : GetCoord
// const - Draw const, -
. , const -, , const. , const , CRectangle: : SetCoord.
- , ,
-. -, -, .
class { private:
int N = 0; // const int CInt = 5; // int ScRInt = N; //
// ... } ;
, - . , , CRectangle -, . , , , . , ++ , , , . . , - . . ,
64 II. ++
, , , - .
class { private:
int N; const int CInt; int ScRInt ; //
public: (int Parm) : N (Parm), CInt (5), Rlnt (N) { // ... }
I I . . . } ;
, N C I n t 5, - R l n t - N :
CObject (0); , , . .
. - - . , , , -. , CContainer - CEmbedded, - CContainer. - , - , (: ). , .
class CEmbedded { I I . . .
public: CEmbedded (int Parml, int Parm2) {
// . . . }
... } ; class CContainer { private:
CEmbedded Embedded; public:
CContainer (int PI, int P2, int P3) : Embedded (PI, P2) { // ... }
. . . } ;
4. ++ 5 _ 2-1458 65
, , (. . 5).
++ -, -
. - ( vo id) . . , CMessage, :
-CMessage () { // ... }
, . , (CMessage) , .
#include
class CMessage { private:
char *Buffer; //
public: CMessage () { Buffer = new char ( '\0'); //
// }
-CMessage () // { delete [] Buffer; // }
void Display () { // Buffer ... }
void Set (char *String) // { delete [] Buffer; Buffer = new char [strlen (String) + 1] strcpy (Buffer, String); }
} ;
66 II. ++
, - .
-:
(. . ). main ( WinMain , Windows), - .
(.. ). , , - , (.. ).
static . -, , - .
new . , - delete. , . , (CRectangle -
, ).
CRectangle RectTable [10]; .
CRectangle *RectTable = new CRectangle [10];
. , " (, ). - , . - , - .
. ( ), , (. . 6 " ").
. new, , . , delete "[ ]". "[ ]" , .
delete [] RectTable;
- - , , -
. - , .
class CRectangle { private:
4. ++ 5 _ 2-1458 67
int Left; int Top; int Right; int Bottom;
public: CRectangle (); CRectangle (int L, int T, int R, int B); void Draw (void); void GetCoord (int VL, int *T, int *R, int *B) ; void SetCoord (int L, int T, int R, int 8);
} ; - -
"::", . #include
CRectangle::CRectangle () { Left - Top = Right = Bottom = 0; }
CRectangle::CRectangle (int L, int T, int R, int B) { SetCoord (L,T,R,); }
void CRectangle::Draw (void) { Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom) ; Line (Left, Bottcm, Left, Top); } ,
void CRectangle::GetCoord (int *L, int *T, int *R, int *B) { *L = Left; *T = Top; *R = Right; *B = Bottom; }
void CRectangle::SetCoord (int L, int T, int R, int B) { L = min ( max (0,L), 80) T = min ( max (0,T), 25) R = min ( max (0,R), 80) = min ( max (0,B), 25) R = max (R, L) ; = max (,T) ; Left = L; Top = T; Right = R; Bottom = B; }
, } , , . inline- (. - inline- . 3). ,
68 II. ++
, - . , CRrectangle GetCoord ( ) , Draw SetCoord (
) - . CRectangle - 4.1 4.2. , , , inline, . 3. , CRectangle: : GetCoord CRectangle, :
void inline GetCoord (int *L, int *T, int *R, int *B); :
void inline CRectangle::GetCoord (int *L, int *T, int *R, int *B) { *L = Left; *T = Top; *R = Right; *B = Bottom; }
-. ,
++, - ( .h). - , . , -. - . 3, .
-. -, -, , . (, . Visual ++). , " " (symbol redefinition).
4.1 4.2 CRectangle. CRectangle CRect.h ( ), -, , - CRect.cpp ( ). CRectangle , Line . CRect.h , CRectangle ( CRect.cpp!), CRect.cpp .
4.1 // CRect.h: CRectangle
class CRectangle { private:
int Left;
4. ++ 69
int Top; int Right; int Bottom;
public: CRectangle () {
Left = Top = Right = Bottom = 0; } CRectangle (int L, int T, int R, int B) {
SetCoord (L, T, R, B); } void Draw (void); void GetCoord (int *L, int *T, int *R, int *B)
{ *L = Left; *T = Top; *R = Right; *B = Bottom; }
void SetCoord (int L, irit: T, int R, int B) ; } ;
4.2 // CRect.cpp:
#include "crect.h" #include
void Line (int XI, int Yl, int X2, int Y2); void CRectangle::Draw (void) {
Line (Left, Top, Right, Top); Line (Right, Top, Right, Bottom); Line (Right, Bottom, Left, Bottom); Line (Left, Bottom, Lef:, Top); }
void CRectangle::SetCoord (int L, int T, int R, int B) { L = min ( max (0,L), 80); T = min ( max (0,T), 25); R = min ( max (0,R), 80); = min ( max (0,B), 25); R = max (R,L); , = max (,T) ; Left = L; Top = T; Right = R; Bottom = B; }
70 II. ++
this - , ,
. , - . , , CTest N, N Testl, - *PTest2, :
CTest Testl; CTest *PTest2 = new CTest; I I . . . cout , .
, , - -, "::"
int N = 0; // N
class CTest
4. ++ 71
{ public:
int N; //- N
int Demo () { cout Draw();
, CRectangle Draw
CRectangle *PRect; CRectangle Rect; I I . . .
PRect = &Rect; PRect->Draw ( );
Draw CBlock. CRectangle *PRect; CBlock Block; // . . . PRect = &Block; PRect->Draw( );
, ( ) . , , ( ) . . (, .) , , . - - , .
- . - . , , . -
PRect->Draw( ); , . , PRect .
, . , , , . , ,
5. - ++ 85
(CRectangle, CBlock CRoundBlock), . CBlock CRoundBlock CRectangle, CRectangle:
const int MAXFIGS = ICO; CRectangle *PFigure [MAXFIGS]; int Count = 0; I I . . . II : PFigure [Count++] = now CBlock (10, 15, 25, 30, 5); // ... // : PFigure [Count++] = new CRectangle(5, 8, 19, 23); . . . II : PFigure [Count++] = new CRoundBlock(27, 33, 43, 56, 10, 5);
. Draw , - , Draw. , CRectangle , , . , RECT, BLOCK ROUNDBLOCK .
// : class CRectangle {
// ... public:
int: Type; // ; // , : // RECT, BLOCK, ROUNDBLOCK
} ,
Draw. , case switch (, ).
// ; : for (int i = 0; i < Count; ++i) switch (PFigure [i]->Type) { case RECT: PFigure [i]->Drav(); break;
case BLOCK: ((CBlock *)PFigure [i])->Draw(); break;
case ROUNDBLOCK: ((CRoundBlock *)PFigure [i])->Draw(); break;
}
86 II. ++
, Draw , virtual CRectangle. - . . . , .
for (int i = 0; i < Count; ++i) PFigure [i]->Draw();
, , . , , , - , . ScratchBook, III , .
, . , , .
class CMessageBox { protected: char *Message; virtual void DrawBackground (int L, int T, int R, int B) ; //
public: CMessageBox() { Message = new char ( ' \0 ' ); }
-CMessageBox ( ) { delete [] Message; }
void Display ( ) { DrawBackground (0, 0, 35, 25); // ... }
void Set (char *Msg); } ;
- Set , Display . : Display , - DrawBackground . , . DrawBackground . , , . DrawBackground . , CMessageBox , DrawBackground, , -- CMessageBox. , CMessageBox
5. - ++ 87
CMyMessageBox. : DrawBackground , :
class CMyMessageBox: public CMessageBox { protected: virtual void DrawBackground (int L, int T, int R, int B) { // ... }
} ; .
CMyMessageBox MyMessageBox; MyMessageBox. Set ("hello") ; MyMessageBox.Display ();
DrawBackground , CMessageBox ( , , ) ( ). , MFC, , , MFC- .
, -
, . - , - ? : - this. , Display CMessageBox \*: :
class CMessageBox { // public: void Display ( ) { this->DrawBackground (0, 0, 35, 25); I I . . . }
// ... } ;
, DrawBackground , Display DrawBackground CMessageBox ( this CMessageBox). DrawBackground , DrawBackground . , Display CMyMessageBox, DrawBackground, CMyMessageBox.
88 II. ++
CMyMessageBox MyMessagBox; I I . . . MyMessageBox.Display ();
, ,
() . . . , ,
. , - . . - . (-) , .
. , .
. p r o t e c t e d , , .
( ) , . , .
. -. v i r t u a l , , , . - . . , ( ) . , , - .
5. - ++ 89
6
,
. (overloading) ++, . :
(copy constructor), ;
(conversion constructor), .
++ -
. , int , "+" double - . ++ . . , CCurrency, . CCurrency , .
class CCurrency { private:
long Dollars; int Cents;
public: CCurrency () {
Dollars = Cents = 0; } CCurrency (long Dol, int Cen) {
SetAmount (Dol, Cen); } void GetAmount (lone *PDol, int *PCen) {
*PDol = Dollars; *PCen = Cents; }
90 II. ++
void PrintAmount () { cout.fill('0'); cout.width (1); cout -> 0 [] new delete
, . , . , . , , . . , - .
// CCurrency: CCurrency operator- () // - ()
+ - * / *= /= %=
>= && II ++
6. , 95
{ return CCurrency (-Dollars, Cents); }
, , . , D o l l a r s . , -5,75$ -5 Dol l a r s +75 C e n t s .
// : CCurrency operator- (CCurrency &Curr) {
return CCurrency (-Zurr.Dollars, Curr.Cents); }
(,"++") . . .
MFC III . i o s t r e a m (, - ) .
, ++,
. - -. ,
CCurrency Money1 (12, 95); CCurrency Money2; Money2 =-- Moneyl;
Moneyl. Dollars Money2 . Dollars, a Moneyl. Cents - Money2 . Cents ( ). , .
CCurrency Money (85, 2 3); // Money.PrintAmount ; // Money = CCurrency (24, 65);//
// //
. . , CMessage:
# i n c l u d e < s t r i n g . h > # i n c l u d e < i o s t r e a m . h > class CMessage { private: char ^Buffer; //
public: CMessage()
96 II. ++
{ Buffer = new char('\0'); //
// Buffer }
-CMessage() { delete[]Buffer; // }
void Display() { cout ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
return TRUE; }
// CAboutDlg , App About class CAboutDlg : public CDialog { public:
CAboutDlg();
,
10. 13 2-1458 193
// enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
// protected:
DECLARE_MESSAGE_MAP() } ;
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_ME S SAGE_MAP() // void CMyScribeApp::OnAppAbout() {
CAboutDlg aboutDlg; aboutDlg.DoModal();
}
// CMyScribeApp
10.11 // MyScribeDoc.h : CMyScribeDoc / /
ttpragma once class CMyScribeDoc : public CDocument { protected: //
CMyScribeDoc() ; DECLARE_DYNCREATE(CMyScribeDoc)
// public: // public: //
public: virtual BOOL OnNewDocument(); virtual void Serialize (CArchiveSt ar) ;
194 III.
// public:
virtual -CMyScribeDoc() ; #ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump(CDumpContextb dc) const;
#endif
protected: // protected:
DECLARE_MES SAGE_MAP() } ;
10.12 // MyScribeDoc. : CMyScribeDoc / /
#include "stdafx.h" #include "MyScribe.h"
#include "MyScribeDoc.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ; #endif
// CMyScribeDoc
IMPLEMENT_DYNCREATE(CMyScribeDoc, CDocument)
BEGIN_MESSAGE_MAP(CMyScribeDoc, CDocument) END_MESSAGE_MAP()
// / CMyScribeDoc
CMyScribeDoc::CMyScribeDoc() { // TODO:
}
CMyScribeDoc::-CMyScribeDoc() { }
BOOL CMyScribeDoc::OnNewDocument() { if (!CDocument::OnNewDocument())
return FALSE;
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
10. 13 2-1458
195
// TODO: // (SDI- // )
return TRUE; }
// CMyScribeDoc
void CMyScribeDoc::Serialize(CArchiveb ar) { // CEditView , // ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
// CMyScribeDoc
#ifdef _DEBUG void CMyScribeDoc : :.AssertValid () const {
CDocument::AssertValid(); }
void CMyScribeDoc ::Dump (CDumpContextSc dc) const { CDocument::Dump(dc);
} #endif //_DEBUG
// CMyScribeDoc
10.13 // MyScribeView.h : CMyScribeView //
V ttpragma once
class CMyScribeView : public CEditView { protected: //
CMyScribeView(); DECLARE_DYNCREATE(CMyScribeView)
// public:
CMyScribeDoc* GetDocument() const;
// ' public:
// public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
196 III.
protected: virtual BOOL OnPreparePrinting(CPrintlnfo* plnfo); virtual void OnBeginPrinting(CDC* pDC, CPrintlnfo* plnfo) virtual void OnEndPrinting(CDC* pDC, CPrintlnfo* plnfo);
// public:
virtual -CMyScribeView(); ttifdef _DEBUG
virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// protected:
DEC LARE_ME S SAGE_MAP() } ;
#ifndef _DEBUG // MyScribeView. inline CMyScribeDoc* CMyScribeView::GetDocument() const
{ return (CMyScribeDoc*)m_pDocument; } #endif
10.14 // MyScribeView. : CMyScribeView / /
ttinclude "stdafx.h" #include "MyScribe.h"
ttinclude "MyScribeDoc.h" #include "MyScribeView.h"
ttifdef _DEBUG #define new DEBUG_NEW ttundef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
// CMyScribeView
IMPLEMENT_DYNCREATE(CMyScribeView, CEditView)
BEGIN_MESSAGE_MAP(CMyScribeView, CEditView) // ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint) ON_C OMMAND(ID_FILE_PRINT_PREVIEW,
CEditView::OnFilePrintPreview) END_MESSAGE_MAP()
// / CMyScribeView
10. 197
CMyScribeView::CMyScribeView() {
// TODO:
}
CMyScribeView::-CMyScribeView() { }
BOOL CMyScribeView::PreCreateWindow(CREATESTRUCT& cs) {
// TODO: , // cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs); cs . style c= - (ES_AUTOHSCROLL | WSJHSCROLL) ; //
return bPreCreated; }
// CMyScribeView
BOOL CMyScribeView::OnPreparePrinting(CPrintlnfo* plnfo) {
// CEditView return CEditView::OnPreparePrinting(plnfo);
}
void CMyScribeView::OnBeginPrinting(CDC* pDC, CPrintlnfo* plnfo) {
// CEditView CEditView::OnBeginPrinting(pDC, plnfo);
}
void CMyScribeView::OnEndPrinting(CDC* pDC, CPrintlnfo* plnfo) {
// CEditView CEditView::OnEndPrinting(pDC, plnfo);
}
// CMyScribeView
#ifdef _DEBUG void CMyScribeView::As^ertValid() const {
CEditView::AssertValid(); }
void CMyScribeView: : Dump (CDumpContextSc dc) const {
CEditView::Dump(dc); }
198 III.
CMyScribeDoc* CMyScribeView::GetDocument() const // ( ) {
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyScribeDoc))); return (CMyScribeDoc*)m_pDocument;
} #endif //JDEBUG // CMyScribeView
10.15 // MainFrm.h : CMainFrame //
#pragma once class CMainFrame : public CFrameWnd {
protected: // CMainFrame(); DECLARE_DYNCREATE(CMainFrame)
// public:
// public: // public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs); // public:
virtual -CMainFrame(); #ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump (CDumpContextSc dc) const;
ttendif protected: //
CStatusBar m_wndStatusBar; CToolBar m_wndToolBar;
// protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); DECLARE_MESSAGE_MAP()
} ;
10.16 // MainFrm.cpp : CMainFrame ttinclude "stdafx.h" ttinclude "MyScribe.h"
10. 199
ttinclude "MainFrm.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MES SAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE()
END_MESSAGE_MAP()
static UINT indicators[] = { ID_SEPARATOR, // ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
} ;
// / CMainFrame CMainFrame::CMainFrame() {
// TODO: }
CMainFrame::-CMainFrame() { }
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1; if ( !m_wndToolBar. CreateEx (this , TBSTYLE_FLAT, WS_CHILD | WSJVISl'feLE
| CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
TRACEO("Failed to create toolbar\n"); return -1; //
// }
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.Setlndicators(indicators, sizeof(indicators)/sizeof(UINT)))
{ TRACEO("Failed to create status bar\n"); return -1; //
}
200 III.
// TODO: , , // m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar);
return 0; }
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE; // TODO: , // cs
return TRUE; }
// CMainFrame
#ifdef _DEBUG void CMainFrame::AssertValid() const {
CFrameWnd::AssertValid(); }
void CMainFrame :: Dump (CDumpContextSc dc) const { CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame
MFC-.
ScratchBook CView. , , . MyScribe CEditView, , .
MFC- . - , .
. . , , , WM_LBUTTONDOWN. , - Properties, WM_LBUTTONDOWN , . - , .
10. 201
PreCreateWindow , , CView, . CREATESTRUCT, PreCreateWindow .
Windows. Application Wizard (, , ). , .
202 III.
11
ScratchBook
MFC ( ), : ; ; , .
, ( , ) , -. ScratchBook, , : , . , ,
. OnDraw ,
, . ScratchBook , .
, Edit Undo Remove . , . Undo , a Remove All - . - , , . 12.
Application Wizard . ScratchBook, . ScratchBook (. . 10), .
I
ScratchBook . . ScratchBookDoc.h CScratchBookDoc CLine. _1 m_Yl CLine , _2 m_Y2 - . CLine
11. 203
Draw . COb j ect ( -, ).
class CLine : public CObject { protected: int m_Xl, m_Yl, m_X2, m_Y2;
public: CLine (int XI, int Yl# int X2, int Y2) {
m _1 = XI; _Yl = Yl; _2 = 2;
m _Y2 = Y2; }
void Draw (CDC *PDC); >;
CScratchBookDoc , , . m_LineArray - MFC- CTypedPtrArray (. . 7.) CTypedPtrArray , , (CObArray CPtrArray). , . , m_LineArray , CI ine CObArray.
class CScratchBookDoc : public CDocument { protected: CTypedPtrArraycCOhArray, CLine*> m_LineArray;
public: void AddLine (int XI, int Yl, int X2, int Y2); CLine *GetLine (int Index); int GetNumLines ();
protected: // CScratchBookDoc(); DECLARE_.DYNCREATE (CScratchBookDoc)
CObArray ( MFC). CObArray COb j ect ( , COb j ect) , . COb j ect - MFC-, . - CObArray ScratchBook CTypedPtrArray, - CLine. , . CTypedPtrArray ( MFC-) MFC Afxtempl.h. StdAfx.h (
204 III.
), ScratchBook. StdAfx.h . , , , , , . .
#include // MFC- Internet // Explorer 4 Common Controls
# i fnde f _AFX_NO_AFXCMN_SU P PORT #include // MFC- Windows
// Common Controls #endif // _AFX_NO_AFXCMN_SU PPORT #include
CLine, , ScratchBook m_LineArray. AddLine, GetLine GetNumLines , m_LineArray. m_LineArray , .
CLine: :Draw ScratchBookDoc.cpp. Draw CDC - MoveTo LineTo (. . 10), , . , AddLine, GetLine GetNumLines CScratchBookDoc.
// CScratchBookDoc
void CLine::Draw (CDC *PDC) { PDC->MoveTo (m_Xl, m_Yl); PDC->LineTo (mX2, m_Y2);
} void CScratchBookDoc::AddLine (int XI, int Yl, int X2, int Y2) { CLine *PLine = new CLine (XI, Yl, X2, Y2); m_LineArray.Add (PLine);
}
CLine ^CScratchBookDoc::GetLine (int Index) { if (Index < 0 || Index > m_LineArray.GetUpperBound ())
return 0; return m_LineArray.GetAt (index);
}
int CScratchBookDoc::GetNumLines () { return m_LinaArray.GetSize ();
)
AddLine CLine Add , CLine, m_LineArray. , m_LineArray, . 0, - 1 .. GetLine , .
11. 205
. GetUpperBound CObArray , .. . GetLine GetAt CTypedPtrArray CLine.
- GetNumLines - CLine, m_LineArray. GetSize CObArray. AddLine, GetLine GetNumLines - (. ).
(. . 10), , OnLButtonUp , ScratchBookView.cpp, - . GetDocument AddLine, .
void CScratchBookView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: // if (m_Dragging) { m_Dragging = 0; ::ReleaseCapture (); ::ClipCursor (NULL); CClientDC ClientDC (this); ClientDC.SetROP2 (R2_NOT); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (m_Point01d); ClientDC.SetROP2 (R2_COPYPEN); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (point);
CScratchBookDoc* PDoc = GetDocument(); PDoc -> AddLine (m_PointOrigin.x,
m_PointOrigin.y, point.x, point.);
}
CView::OnLButtonUp(nFlags, point); }
: , . , , .
, OnDraw . , , , . . 13 , OnDraw . OnDraw, Application Wizard, - . , , ScratchBookView.cpp CScratchBookView: :OnDraw. - CScratchBookDoc: : GetNumLines, , .
206 III.
CScratchBookDoc: : GetLine, CLine, CLine: : Draw.
// CScratchBookView
void CScratchBookView::OnDraw(CDC* pDC) { CScratchBookDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);
/ / T O D O :
int Index = Doc->GetNumLines (); while (Index--) pDoc->GetLine (Index)->Draw(pDC);
}
. Edit, . Remove All Undo . Edit ScratchBook, Resource View . , , Menu IDR_MAINFRAME.
Properties. Caption &Edit. Edit. - . . - Edit , File View.
Edit ( ) , Properties . ID ID_EDIT_UNDO, Caption - &Undo\tCtrl+Z. Undo - Ctrl+Z, Application Wizard . , Undo Alt+Backspace, . Edit Undo. ^ Edit ( Undo) Properties Separator. Undo . - Edit, ID ID_EDIT_CLEAR_ALL, Caption - & Remove All. Remove All. Edit .
, Save All File Save All Standard. , (, ). .
11. 207
Remove All ,
Remove All: 1. Resource View, -
, Visual Studio Undo Remove All Edit ( ). 2. Class View. CScratchBookDoc
.
3. Properties. - Remove All COMMAND ( , - UPDATE_COMMAND_UI - ). , , , " " . 10.
4. . DeleteContents . UpdateAllViews CDocument -. (. . 13) UpdateAll Views .
void CScratchBookDoc::7633() { // TODO: DeleteContents (); UpdateAllViews (0) ;
}
5. UPDATE_COMMAND_UI, , Remove All ( Edit). , , . Remove All , , , . . 1 ...4 , UPDATE_COMMAND_UI. Visual Studio .
6. . CCmdUI. MFC-, . Enable CCmdUI. Remove All, . ( ) . , , .
void CScratchBookDoc::OnUpdate57633(CCmdUI *pCmdUI) { //-TODO: pCmdUI->Enable (m_LineArray.GetSize );
}
, . Visual Studio. , , Resource View .
208 III.
CCmdUI , :
Enable. , Enable TRUE. FALSE .
virtual void Enable (BOOL bOn = TRUE); SetCheck. SetCheck 1,
, 0, . , - - (, ), , ( ).
virtual void SetCheck (int nCheck = 1); SetRadio. ( -
) (), TRUE SetRadio. FALSE . virtual void SetRadio (BOOL bOn = TRUE);
SetText. , SetText, IpszText ( ). , , SetText Undo Undo Delete.
virtual void SetText (LPCTSTR IpszText); UDAT_OMMAND_UI
, (. . 14). CCmdUI. .
Undo , ,
Undo. Remove All. , Visual Studio Undo. Visual Studio . - . Undo , . GetUpperBound CObArray. CLine CtypedPtr-Array: : GetAt, delete. , , - UpdateAllViews, CScratchBookView: : OnDraw. OnDraw , - .
void CScratchBookDoc::On57643() { // TODO: int Index = mLinaArray.GetUpperBound (); if (Index > -1) {
delete m_LineArray.GetAt (Index); m_IiineArray. RemoveAt (Index) ;
11. 1 4 - 2 - 1 4 5 8
209
} U&dateAllViews (0);
}
Undo , Remove . . Undo .
void CScratchBookDoc::OnUpdate57643(CCmdUI *pCmdUI) { // TODO: pCmdUI->Enable (mJOineArray.GetSize ());
}
Undo Ctrl+Z. Ctrl+Z, Undo. OnUpdate , , , . OnUpdate , . , , , .
File New, OnFileNew CWinApp MFC
CDocument: :DeleteContents . ScratchBook . , - , -. MFC. DeleteContents, Class View. 1. View Class View. 2. Class View CScratchBookDoc
Properties. 3. Properties Events (). , ,
DeleteContents, ( ).
4. DeleteContents, Visual Studio ScratchBook-Doc.cpp, , . , - DeleteContents, CObArray GetSize, CLine, m_LineArray. CTypedPtrArray: : GetAt , delete CLine ( CLine new). , , m_LineArray, RemoveAll CObArray.
210 III.
void CScratchBookDoc::DeleteContents() { // TODO: // int Index = m_LineArray.GetSize (); while (Index)
delete m_LineArray.GetAt (Index); m_LineArray.RemoveAl1 (); CDocument::DeleteContents();
}
MFC DeleteContents ( ) OnDraw. OnDraw , . New .
ScratchBook (11.111.8)
ScratchBook, . , Application Wizard, , .
11.1 II ScratchBook.h : ScratchBook II ttpragma once
#ifndef AFXWIN_H terror include 'stdafx.h' before including this file for PCH
#endif
ttinclude "reso
urce.h" //
// CScratchBookApp: // ScratchBook. II
class CScratchBookApp : public CWinApp { public:
CScratchBookApp();
// public:
virtual BOOL Initlnstance();
// afx_msg void OnAppAbout(); DECLARE_MESSAGE_MAP()
} ;
11. 14 * 211
11.2 // ScratchBook. : / /
#include "stdafx.h" ttinclude "ScratchBook.h" ttinclude "MainFrm.h"
ttinclude "ScratchBookDoc.h" ttinclude "ScratchBookView.h"
ttifdef _DEBUG ttdefine new DEBUG_NEW ttundef THIS_fILE static char THIS_FILE[] = FILE ; ttendif
// CScratchBookApp
BEGIN_MESSAGE_MAP(CScratchBookApp, CWinApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()
// CScratchBookApp
CScratchBookApp::CScratchBookApp() {
// TODO: // // Initlnstance
}
// CScratchBookApp
CScratchBookApp theApp;
// CScratchBookApp
BOOL CScratchBookApp::Initlnstance() {
.CWinApp::Initlnstance();
// . // - // // , // , // . // ,
212 III.
// TODO: - , // , SetRegistryKey(_("Local AppWizard-Generated Applications")); LoadStdProfileSettings(4); //
// INI- ( MRU) // . // , // CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME, RUNTIME_CLASS(CScratchBookDoc), RUNTIME_CLASS(CMainFrame) , //
// SDI- RUNTIME_CLASS(CScratchBookView));
AddDocTemplate(pDocTemplate); // // , DDE, CCommandLinelnfo cmdlnfo; ParseCommandLine(cmdlnfo); // , . // FALSE, // /RegServer, /Register, /Unregserver /Unregister. if (!ProcessShellCommand(cmdlnfo))
return FALSE; // m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
return TRUE; }
// CAboutDlg , About
class CAboutDlg : public CDialog { public:
CAboutDlg();
// enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
// protected:
DECLARE_MESSAGE_MAP() } ;
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }
11. 213
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP()
// void CScratchBookApp::OnAppAbout() {
CAboutDlg aboutDlg; aboutDlg. DoModal () ;
}
// CScratchBookApp
11.3 // ScratchBookDoc.h : CScratchBookDoc / /
#pragma once
class CLine : public CObject { protected:
int m_Xl, m_Yl, m_X2, m_Y2;
public: CLine (int XI, int Yl, int X2, int Y2) {
m_Xl = XI; m_Yl = Yl; _2 = X2; m_Y2 = Y2; }
void Draw (CDC *PDC); } ;
class CScratchBookDoc : public CDocument { protected:
CTypedPtrArray m_LineArray;
public: void AddLine (int Xl, int Yl, int X2, int Y2); CLine *GetLine (int Index); int GetNumLines ();
protected: // CScratchBookDoc(); DECLARE_DYNCREATE(CScratchBookDoc)
//
214 III.
public:
// public:
// public: virtual BOOL OnNewDocument(); virtual void Serialize (CArchive&c ar) ;
// public:
virtual -CScratchBookDoc(); #ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;
#endif protected: // protected:
DECLARE_MESSAGE_MAP() public:
virtual void DeleteContents(); afx_msg void On57633(); afx_msg void OnUpdate57633(CCmdUI *pCmdUI); afx_msg void On57643(); afx_msg void OnUpdate57643(CCmdUI *pCmdUI);
} ;
11.4 // ScratchBookDoc.cpp : CScratchBookDoc / /
#include "stdafx.h" ttinclude "ScratchBook.h" #include "ScratchBookDoc.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ; ttendif // CScratchBookDoc IMPLEMENT_DYNCREATE(CScratchBookDoc, CDocument) BEGIN_MESSAGE_MAP(CScratchBookDoc, CDocument)
ON_COMMAND(57633, On57633) ON_UPDATE_COMMAND_UI(57633, OnUpdate57633) ON_COMMAND(57643, On57643) ON_UPDATE_COMMAND_UI(57643, OnUpdate57643)
END_MESSAGE_MAP()
11. 215
// / CScratchBookDoc
CScratchBookDoc::CScratchBookDoc() { // TODO:
}
CScratchBookDoc::-CScratchBookDoc() { }
BOOL CScratchBookDoc::OnNewDocument() { if (!CDocument::OnNewDocument())
return FALSE;
// TODO: // (SDI- // )
return TRUE; }
// CScratchBookDoc
void CScratchBookDoc:: Serialize (CArchiveSc ar) { if (ar.IsStoringO ) {
// TODO: } else {
// TODO: }
}
// CScratchBookDoc
#ifdef _DEBUG void CScratchBookDoc::AssertValid() const {
CDocument::AssertValid() ; }
void CScratchBookDoc ::Dump (CDumpContextSc dc) const { CDocument::Dump(dc);
}
#endif //_DEBUG
// CScratchBookDoc
void CLine::Praw (CDC *PDC)
216 III.
{ PDC->MoveTo (m_Xl, m_Yl); PDC->LineTo (m_X2, m_Y2);
}
void CScratchBookDoc::AddLine (int XI, int Yl, int X2, int Y2) { CLine *PLine = new CLine (XI, Yl, X2, Y2); m_LineArray.Add (PLine);
}
CLine *CScratchBookDoc::GetLine (int Index) { if (Index < 0 C| Index > m_LineArray.GetUpperBound ())
return 0; return m_LineArray.GetAt (Index);
}
int CScratchBookDoc::GetNumLines () { return m_LineArray.GetSize ();
}
void CScratchBookDoc::DeleteContents() { // TODO: //
int Index = m_LineArray.GetSize (); while (Index--)
delete m_LineArray.GetAt (Index); m_LineArray.RemoveAl1 () ;
CDocument::DeleteContents(); }
void CScratchBookDoc::On57633() { // TODO: DeleteContents (); UpdateAllViews (0);
}
void CScratchBookDoc::OnUpdate57633(CCmdUI *pCmdUI) { .
// TODO:
pCmdUI->Enable (m_LineArray.GetSize ()); } void CScratchBookDoc::On57643() {
// TODO: int Index = m_LineArray.GetUpperBound (); if (Index > -1) {
delete m_LineArray.GetAt (Index);
11. 217
m_LineArray.RemoveAt (Index); } UpdateAllViews (0) ;
}
void CScratchBookDoc::OnUpdate57643(CCmdUI *pCmdUI) { // TODC: pCmdUI->Enable (m_LineArray.GetSize );
}
11.5 // ScratchBookView.h : CScratchBookView / /
#pragma once
class CScratchBookView : public CView { protected:
CString m_ClassName; int m_Dragging; * HCURSOR m_HCross; CPoint m_PointOld; CPoint m_PointOrigin;
protected: // CScratchBookView(); DECLARE_DYNCREATE(CScratchBookView)
// public:
CScratchBookDoc* GetDocument() const;
// public:
// public: virtual void OnDraw(CDC* pDC); //
virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected:
// public:
virtual -CScratchBookView(); #ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump (CDumpContextSc dc) const;
#endif
protected:
218 III.
// protected:
DECLARE_MESSAGE_MAP() public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point);
} ;
ttifndef _DEBUG // ScratchBookView.cpp inline CScratchBookDoc* CScratchBookView::GetDocument() const
{ return (CScratchBookDoc*)m_pDocument; } ttendif
11.6 // ScratchBookView.cpp : CScratchBookView //
#include "stdafx.h" #include "ScratchBook.h"
#include "ScratchBookDoc.h" #include "ScratchBookView.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ;
#endif
// CScratchBookView
IMPLEMENT_DYNCREATE(CScratchBookView, CView) BEGIN_MESSAGE_MAP(CScratchBookView, CView)
ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOU S EMOVE()
END_MESSAGE_MAP()
// / CScratchBookView
CScratchBookView::CScratchBookView()
// TODO:
m_Dragging = 0; m__HCross = AfxGetApp ()->LoadStandardCursor (IDC_CROSS) ; }
CScratchBookView::-CScratchBookView() { }
11. 219
BOOL CScratchBookView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: , // cs
m_ClassName = AfxRegisterWndClass (CS_HREDRAW | CS_VREDRAW, // , // (HBRUSH) ::GetStockObj ect (WHITE_BRUSH),
// 0); //
cs.IpszClass = m_ClassName;
return CView::PreCreateWindow(cs); }
// CScratchBookView
void CScratchBookView::OnDraw(CDC* pDC) {
CScratchBookDoc* pDoc = GetDocument(); AS SERT_VALID(pDoc) ;
// TODO:
int Index = pDoc->GetNumLines (); while (Index--)
pDoc->GetLine (Index)->Draw(pDC); }
// CScratchBookView
#ifdef JDEBUG void CScratchBookView::AssertValid() const {
CView::AssertValid() ; }
void CScratchBookView: : Dump (CDumpContextSc dc) const { CView::Dump(dc) ;
}
CScratchBookDoc* CScratchBookView::GetDocument() const // {
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScratchBookDoc))); return (CScratchBookDoc*)m_pDocument;
} #endif //_DEBUG
// CScratchBookView
void CScratchBookView::OnLButtonDown(UINT nFlags, CPoint point)
220 III.
{ //- TODO: //
m_PointOrigin = point; m_PointOld = point; SetCapture (); m_Dragging = 1; RECT Rect; GetClientRect (&Rect); ClientToScreen (&Rect); ::ClipCursor (&Rect);
CView::OnLButtonDown(nFlags, point); }
void CScratchBookView: :OnLButtonUp (UINT nFlags, CPoint point). { // TODO: // if (m_Dragging) { m_Dragging = 0; ::ReleaseCapture (); ::ClipCursor (NULL); CClientDC ClientDC (this); ClientDC.SetROP2 (R2_NOT); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (m_PointOld); ClientDC.SetROP2 (R2_COPYPEN); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (point);
CScratchBookDoc* PDoc = GetDocument(); PDoc -> AddLine (m_PointOrigin.x,
m_PointOrigin.y, point.x, point.y);
}
CView::OnLButtonUp(nFlags, point); }
void CScratchBookView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: //
::SetCursor (m_HCross);
if (m_Dragging) { CClientDC ClientDC (this); ClientDC.SetROP2 (R2_NOT); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (m_PointOld); ClientDC.MoveTo (m_PointOrigin);
11. 221
ClientDC.LineTo (point); m_PointOld = point;
}
CView::OnMouseMove(nFlags, point);
11.7 // MainFrm.h : CMainFrame / /
ttpragma once class CMainFrame : public CFrameWnd {
protected: // CMainFrame () ; DECLARE_DYNCREATE(CMainFrame)
// public:
// public:
// public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
// public:
virtual -CMainFrame(); #ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;
#endif
protected: // CStatusBar m_wndStatusBar; CToolBar m_wndToolBar;
// protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); DECLARE_MESSAGE_MAP()
} ;
11.8 // MainFrm.cpp : CMainFrame / /
#include "stdafx.h" #include "ScratchBook.h"
222 III.
#include "MainFrm.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ; #endif
// CMainFrame
IMPLEMENT_DYNCREATE(CMa inFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE()
END_MESSAGE_MAP()
static UINT indicators[] = { ID_SEPARATOR, // ID_INDIATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
} ;
// / CMainFrame
CMainFrame::CMainFrame() { // TODO:
}
CMainFrame::-CMainFrame() { }
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_T00LJ4PS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
TRACEO("Failed to create toolbar\n"); return -1; //
}
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)))
{ TRACEO("Failed to create status bar\n"); return -1; //
}
11. 223
// TODC: , , // m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar);
return 0; }
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE; // TODO: , // cs
return TRUE; }
// CMainFrame
#ifdef _DEBUG void CMainFrame::AssertValid() const {
CFrameWnd::AssertValid(); }
void CMainFrame::Dump (CDumpContextSc dc) const { CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame
( ) -
, , - .
. , , , (, CLine ScratchBook). , MFC. , MFC- CObArray , , ( , CObj e c t ) . MFC- CTypedPtrArray, CObArray, - . ScratchBook m_LineArray. CLine.
224 III.
. . AddLine, GetLine GetNumLines. , . , OnDraw , .
. , , , Undo Remove All Edit, . , Class View Properties.
. : COMMAND_UPDATE_Ul, - , , COMMAND, . COMMAND_UPDATE_UI , - CCmdUI. , . (COMMAND) .
11. 1 5 - 2 - 1 4 5 8 225
12 - - ScratchBook
- MyScribe
-
. - ScratchBook MyScribe , File (New, Open..., Save Save As...). , drag-and-drop, , Windows Explorer .
- ScratchBook ScratchBook Open..., Save Save As..., ,
. , (. . 10 11), .
File Visual Studio ScratchBook, Resource View
. , , IDR_MAINFRAME. File. New File ( , ) - :
Open... ID_FILE_OPEN &0pen...\tCtrl+0. Save ID_FILE_SAVE &Save\tCtrl+S. Save As... ID_FILE_SAVE_AS Save &As... .
Recent File ID_FILE_MRU_FILE1 Recent File. , MFC Recent File . MFC File . Application Wizard 4. MFC (ScratchBook.ini) Windows, , - , .
"" , Application Wizard . . 10 , , ScratchBook . .
(string) , , Open Save As.
226 III.
Developer Studio, String Table ResourceView. IDR_MAINFRAME. Application Wizard , ScratchBook. , Properties. Caption . Enter. , .
"ScratchBook\n\nScratchBook\nScratchBook Files (*.drw) \n.drw\nScratchBook.Document\nScratchBook Documentn
"ScratchBook Files ( * . drw) " , Files of type ( Save as type) Open ( Save As) .
"(.drw)" . - ScratchBook , Open Save As . - Save As.
, , . Application Wizard - Document Template Strings, .
New, Open..., Save Save As... ,
MFC. . MFC File . New OnFileNew CWinApp (
ScratchBook). DeleteContents (. . 11) , .
Open... OnFileOpen CWinApp. Open. Open, OnFileOpen , Serialize (CScratchBookDoc: :Serialize). Serialize . OnFileOpen .
S e r i a l i z e , Open.
Save OnFileSave CDocument ( ScratchBook), OnFileSaveAs CDocument - Save As... , OnFileSaveAs OnFileSave Save As, - . , CScratchBookDoc : : Serialize . . , File name Save As , "Untitled" (.drw ScratchBook).
12. - 15 *
227
, Windows NT/2000/XP . , , Stdlib.h: __, _MAX_DRIVE, _MAX_DIR, _MAX_FNAME __. Windows 98, , 255 .
/ Application Wizard, ScratchBook, ScratchBookDoc.cpp
Serialize . . MFC Serialize - CArchive. CArchive, - / . , , MFC- CObject. (.. Save Save As...) IsStoring CArchive TRUE, (.. Open... File) IsStoring FALSE.
ScratchBook m_LineArray, CLine. m_LineArray - Serialize ( CObArray), CLine, . CScratchBookDoc: : Serialize , CObArray: : Serialize: .
// CScratchBookDoc
void CScratchBookDoc::Serialize(CArchiveb ar) { if (ar.IsStoring()) {
// TODO: m_LineArray. Serialize (ar);
} else {
// TODO: m_LineArray.Serialize(ar);
} }
CLine CObArray: : Serialize Serialize , -' . CObArray: :Serialize , (CLine) . Serialize .
CLine , (DECLARE_SERIAL IMPLEMENT_SERIAL) . CObArray: : Serialize ,
228 III.
. CObArray: : Serialize ( ) .
DECLARE_SERIAL CLine ScratchBookDoc.h, . DECLARERSERIAL .
class CLine : public CObject { protected: int m_Xl, m_Yl, m_X2, m_Y2; CLine () {} DECIARE_SERIAL (CLine)
public: ScratchBookDoc.cpp IMPLEMENT_SERIAL
CLine: : Draw. , IMPLEMENT_SERIAL, - , - . - , - ( -1 ( )). , , . . ScratchBook 1. .
// CScratchBookDoc
IMPLEMENT_SERIAL (CLine, CObject, 1) void CLine::Draw (CDC *PDC)
Cline CLine Serialize, CObArray: : Serialize . - Serialize ScratchBookDoc.h public CLine.
public: CLine (int XI", int Yl, int X2, int Y2) {
_ _1 = XI _ _Yl = Yl _ _2 = 2 _ .Y2 = Y2 }
void Draw (CDC *PDC); virtual void Serialize (CArchive bar);
};
ScratchBookDoc.cpp CLine: : Draw Serialize. , CLine: : Serialize, . Serialize CLine -. CArchive.
12. - 229
void CLine::Draw (CDC *PDC) { PDC->MoveTo (m_Xl, m_Yl); PDC->LineTo (m_X2, m_Y2);
}
void CLine::Serialize (CArchive bar) { if (ar.IsStoring())
ar m_Yl >> m_X2 >> m_Y2; }
, , . Serialize, . - , . , , , .
, , - Serialize .
, , Serialize CArchive .
drw- Windows ,
ScratchBook (.. .drw), Windows Explorer ( , ). EnableShellOpen RegisterShellFileTypes CWinApp Initlnstance ScratchBook.cpp. Windows ScratchBook (.drw) ( , ). , :
, drw-, ScratchBook;
drw- ScratchBook, , .
EnableShellOpen RegisterShellFileTypes AddDocTemplate, , - ( IDR_MAINFRAME) .
AddDocTemplate(pDocTemplate);
EnableShellOpen (); RegisterShellFileTypes (); // // , DDE, CCommandLinelnfo cmdlrillo;
230 III.
"drag-and-drop" , Windows
( Explorer , , ) . ScratchBook CWnd: :DragAcceptFiles . Initlnstance ScratchBook.cpp UpdateWindow. m_pMainWnd ( CWinThread, CWinApp), . Initlnstance DragAcceptFiles. P roces sShe l lCommand , m_pMainWnd.
m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
m_pMainWhd->DragAcceptFiles (); return TRUE;
, .. DragAcceptFiles ( ) MFC , CArchive Serial i ze..
, , CDocument
. MFC DeleteContents , . MFC DeleteContents , . TRUE ( ), . CDocument FALSE, . TRUE CDocument: : SetModif iedFlag. TRUE, ( SetModif iedFlag TRUE). FALSE, ( CDocument). AddLine Remove All Undo ScratchBookDoc.cpp SetModif iedFlag.
void CScratchBookDoc: .-AddLine (int Xl, int Yl, int X2, int Y2) { CLine *PLine = new CLine (XI, Yl, X2, Y2); m_LineArray.Add (PLine); SetModifiedFlag( );
} void CScratchBookDoc::On57633() { // TODO: DeleteContents (); UpdateAllViews (0); SetModifiedFlag ( );
}
12. - 231
void CScratchBookDoc::On57643() { // TODO: int Index = m_LineArray.GetUpperBound (); if (Index > -1) {
delete m_LineArray.GetAt (Index); m._LineArray .RemoveAt (Index);
} UpdateAllViews (0); SetModifiedFlag ();
}
ScratchBook , (12.1 - 12.8)
ScratchBook.
12.1 // ScratchBook.h : ScratchBook // ttpragma once
ttifndef AFXWIN_H terror include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" //
// CScratchBookApp: // ScratchBook. // class CScratchBookApp : public CWinApp { public: CScratchBookApp();
// public: virtual BOOL Initlnstance();
// afx__msg void OnAppAbout () ; DECLARE JMES SAGE_MAP()
} ;
12.2 // ScratchBook. : //
ttinclude "stdafx.h" ttinclude "ScratchBook.h"
232 III.
ttinclude "MainFrm.h"
ttinclude "ScratchBookDoc.h" ttinclude "ScratchBookView.h"
ttifdef _DEBUG ttdefine new DEBUG_NEW ttundef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
// CScratchBookApp
BEGIN_MESSAGE_MAP(CScratchBookApp, CWinApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_0PEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()
// CScratchBookApp
CScratchBookApp::CScratchBookApp(). { // TODO: // // Initlnstance
}
// CScratchBookApp
CScratchBookApp theApp;
// CScratchBookApp
BOOL CScratchBookApp::Initlnstance() { CWinApp::Initlnstance();
// . // - // // , // , // . // , //. . . // TODO: - , // , SetRegistryKey(_("Local AppWizard-Generated Applications")); LoadStdProfileSettings(4); //
// INI- ( MRU) // . // , // CSingleDocTemplate* pDocTemplate;
12. - 233
pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CScratchBookDoc), RUNTIME_CLASS(CMainFrame), //
// SDI- RUNTIME_CLASS(CScratchBookView));
AddDocTemplate(pDocTemplate) ;
EnableShe11Open (); RegisterShellFileTypes ();
// // , DDE, CCommandLinelnfo cmdlnfo; ParseCommandLine(cmdlnfo) ; // , . // FALSE, // /RegServer, /Register, /Unregserver /Unregister. if (!ProcessShellCommand(cmdlnfo))
return FALSE; // // m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow() ;
m_pMainWnd->DragAcceptFiles ();
return TRUE; }
// C7\boutDlg , App About
class CAboutDlg : public CDialog { public: CAboutDlg();
// enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
// protected: DECLARE_MESSAGE_MAP()
} ; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX);
}
234 III.
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP()
// void CScratchBookApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg. DoModal () ;
}
// CScratchBookApp
12.3 // ScratchBookDoc.h : CScratchBookDoc //
ttpragma once
class CLine : public CObject { protected: int m_Xl, m_Yl, m_X2, m_Y2; CLine () {} DECLARE_SERIAL (CLine)
public: CLine (int XI, int Yl, int X2, int Y2) {
m_ _X1 = XI m_ _Y1 = Yl m_ _X2 = X2 m_Y2 = Y2; }
void Draw (CDC *PDC); virtual void Serialize (CArchive &ar);
} ;
class CScratchBookDoc : public CDocument { protected: CTypedPtrArray m_LineArray;
public: void AddLine (int XI, int Yl, int X2, int Y2); CLine *GetLine (int Index); int GetNumLines ();
protected: // CScratchBookDoc(); DECLARE_DYNCREATE(CScratchBookDoc)
// public:
12. - 235
// public:
// public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar);
// public: virtual -CScratchBookDoc();
#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContextb dc) const;
#endif
protected:
// protected: DECLARE_MESSAGE_MAP()
public: virtual void DeleteContents(); afx_msg void On57633(); afx_msg void OnUpdate57633(CCmdUI *pCmdUI); afx_msg void On57643(); afx_msg void OnUpdate57643(CCmdUI *pCmdUI);
};
12.4 // ScratchBookDoc.cpp : CScratchBookDoc //
#include "stdafx.h" #include "ScratchBook.h"
#include "ScratchBookDoc.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ; #endif
// CScratchBookDoc
IMPLEMENT._DYNCREATE (CScratchBookDoc, CDocument)
BEGIN_MESSAGE_MAP(CScratchBookDoc, CDoc\^ent) ON_COMMAND(57633, On57633) ON_UPDATE_COMMAND_UI(57633, OnUpdate57633) ON_COMMAND(57643, On57643) ON_U P DATE_C OMMAND_UI(57643, OnUpdate57643)
END_MESSAGE_MAP()
236 III.
// / CScratchBookDoc
CScratchBookDoc::CScratchBookDoc() { // TODO:
}
CScratchBookDoc::-CScratchBookDoc() { }
BOOL CScratchBookDoc::OnNewDocument() { if (!CDocument::OnNewDocument())
return FALSE;
// TODO: // (SDI- // )
return TRUE; }
// CScratchBookDoc
void CScratchBookDoc:: Serialize (CArchiveSc ar) { if (ar.IsStoring()) {
// TODO: m_LineArray.Serialize(ar);
} else {
// TODO: m_LineArray.Serialize(ar);
} }
// CScratchBookDoc
#ifdef _DEBUG void CScratchBookDoc::AssertValid() const { CDocument::AssertValid() ;
}
void CScratchBookDoc ::Dump (CDumpContextSc dc) const { CDocument::Dump(dc) ;
} #endif //_DEBUG
12. - 237
// CScratchBookDoc
IMPLEMENT_SERIAL (CLine, CObject, 1)
void CLine::Draw (CDC *PDC) { PDC->MoveTo (m_Xl, m_Yl); PDC->LineTo (m_X2, m_Y2);
}
void CLine::Serialize (CArchive &ar) { if (ar.IsStoring())
ar m_Xl m_Yl m_X2 m_Y2 ; else
ar m_Xl m_Yl m_X2 m_Y2 ; }
void CScratchBookDoc:-.AddLine (int XI, int Yl, int X2, int Y2) { .CLine *PLine = new CLine (XI, Yl, X2, Y2); m_LineArray.Add (PLine); SetModi f iedFlag( );
}
CLine *CSc:ratchBookDoc::GetLine (int Index) { if (Index < 0 || Index > m_LineArray.GetUpperBound ())
return 0; return rn_LineArray. GetAt (Index);
}
int CScratchBookDoc::GetNumLines () { return m_LineArray.GetSize ();
} void CScratchBookDoc::DeleteContents() { // TODO: // int Index = m_LineArray.GetSize (); while (Index--)
delete m_LineArray.GetAt (Index); m_LineArray.RemoveAl1 ();
CDocument::DeleteContents(); }
void CScratchBookDoc::On57633() { // TODO: DeleteContents (); UpdateAllVi ews (0); SetModifiedFlag ( );
}
238 III.
void CScratchBookDoc::OnUpdate57633(CCmdUI *pCmdUI) { // TODO:
pCmdUI->Enable (m_LineArray.GetSize ()); }
void CScratchBookDoc::On57643() { / / T O D O :
int Index = m_LineArray.GetUpperBound (); if (Index > -1) {
delete m_LineArray.GetAt (Index); m_LineArray.RemoveAt (Index);
} UpdateAllViews (0) ; SetModifiedFlag ();
}
void CScratchBookDoc::OnUpdate57643(CCmdUI *pCmdUI) { // TODO: pCmdUI->Enable (m_LineArray.GetSize );
}
12.5 // ScratchBookView.h : CScratchBookView //
#pragma once
class CScratchBookView : public CView { protected: CString m_ClassName; int m_Dragging; HCURSOR m_HCross; CPoint m_PointOld; CPoint m_PointOrigin;
protected: // CScratchBookView(); DECLARE_DYNCREATE(CScratchBookView)
// public: CScratchBookDoc* GetDocument() const;
// public:
//
12. - 239
public: virtual void OnDraw(CDC* pDC) ; //
virtual BOOL PreCreateWindow (CREATESTRUCTSc cs); protected:
// public: virtual -CScratchBookView();
ttifdef DEBUG virtual void AssertValid() const; virtual void Dump (CDumpContextSc dc) const;
#endif
protected:
// protected: DECLARE_ME S SAGE_MAP()
public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point);
} ;
ttifndef _DEBUG // ScratchBookView.cpp inline CScratchBookDoc* CScratchBookView::GetDocument() const
{ return (CScratchBookDoc*)m_j?Document; } #endif
12.6 // ScratchBookView.cpp : CScratchBookView / /
#include "stdafx.h" #include "ScratchBook.h"
ttinclude "ScratchBookDoc.h" ttinclude "ScratchBookView.h"
ttifdef _DEBUG ttdefine new DEBUG_NEW ttundef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
// CScratchBookView
IMPLEMENT_DYNCREATE(CScratchBookView, CView)
BEGIN_MESSAGE_MAP (CScratchBookView, C\riew) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE()
END_MES SAGE_MAP()
240 III.
// / CScratchBookView
CScratchBookView::CScratchBookView() { // TODO:
m_Dragging = 0; m_HCross = AfxGetApp()->LoadStandardCursor (IDC_CROSS); ) CScratchBookView::-CScratchBookView() { }
BOOL CScratchBookView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: , // cs
m_ClassName = AfxRegisterWndClass (S_HREDRAW | CS_VREDRAW, // 0, // (HBRUSH) ::GetStockObj ect (WHITE_BRUSH),
// 0); //
cs.IpszClass = m_ClassName;
return CView::PreCreateWindow(cs); }
// CScratchBookView
void CScratchBookView::OnDraw(CDC* pDC) { CScratchBookDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);
// TODO:
int Index = pDoc->GetNumLines (); while (Index--)
pDoc->GetLine (Index)->Draw(pDC); }
// CScratchBookView
#ifdef _DEBUG void CScratchBookView::AssertValid() const { CView::AssertValid();
}
void CScratchBookView: :Dump (CDumpContextSc dc) const { CView: :Dump(dc) ;
}
12. - 1 6 - 2 - 1 4 5 8 241
CScratchBookDoc* CScratchBookView::GetDocument() const // { ASSERT(m_pDocument->
IsKindOf(RUNTIME_CLASS(CScratchBookDoc))); return (CScratchBookDoc *)m_pDocument;
} ttendif //_DEBUG
// CScratchBookView
void CScratchBookView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: //
m_Point:Origin = point; m_Point:Old = point; SetCapt.ure () ; m_Dragging = 1; RECT Rect; GetClientRect (&Rect); ClientToScreen (ScRect) ; ::ClipCursor (&Rect);
CView::OnLButtonDown(nFlags, point); }
void CScratchBookView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: // if (m_Dragging) { m_Dragging = 0; ::ReleaseCapture (); ::ClipCursor (NULL); CClientDC ClientDC (this); ClientDC.SetROP2 (R2_NOT); ClientDC.MoveTo (m_PointOrigin); lientDC.LineTo (m_PointOld); ClientDC.SetROP2 (R2_COPYPEN); ClientlDC.MoveTo (m_PointOrigin) ; CIientDC.LineTo (point);
CScratchBookDoc* PDoc = GetDocument(); PDoc -> AddLine (m_PointOrigin.x,
m_PointOrigin.y, point.x, point.y);
}
CView::OnLButtonUp(nFlags, point); }
void CScratchBookView::OnMouseMove(UINT nFlags, CPoint point) {
242 III.
// TODO: //
::SetCursor (m_HCross);
if (m_Dragging) { CClientDC ClientDC (this); ClientDC.SetR0P2 (R2_N0T); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (m_PointOld); ClientDC.MoveTo (m_PointOrigin); ClientDC.LineTo (point); m_PointOld = point;
}
CView::OnMouseMove(nFlags, point); }
12.7 // MainFrm.h : CMainFrame //
#pragma once class CMainFrame : public CFrameWnd {
protected: // CMainFrame(); DECLARE_DYNCREATE(CMainFrame)
// public:
// public:
// public: virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
// public: virtual -CMainFrame();
#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;
#endif
protected: // CStatusBar m_wndStatusBar; CToolBar m_wndToolBar;
// protected:
12. - 16* 243
afx_msg int OnCreate(LPCREATESTRUCT LpCreateStruct); DECLARE_ME S SAGE_MAP()
} ;
12.8 // MainFrm.cpp : CMainFrame //
ttinclude "stdafx.h" ttinclude "ScratchBook.h"
ttinclude "MainFrm.h"
ttifdef _DEBUG ttdefine new DEBUG_NEW ttundef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
/ / CMainFrame
IMPLEMENT_DYNCREATE (CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE()
END_MESSAGE_MAP()
static UINT indicators[] = { ID_SEPARATOR, // ID_INDIATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
} ;
// / CMainFrame ,
CMainFrame::CMainFrame() { // TODO:
}
CMainFrame::-CMainFrame(} { }
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndTo1Bar.CreateEx(this, STYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
244 III.
TRACEO("Failed to create toolbar\n"); return -1; ' //
// }
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.Setlndicators(indicators, sizeof'(indicators) /sizeof (UINT) ) )
{ TRACEO("Failed to create status bar\n"); r e t u r n - 1 ; / /
} // TODO: , , // m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar);
return 0; }
BOOL CMainFrame::PreCreateWindow(CREATESTRUCTb cs) { i f( !CFrameWnd::PreCreateWindow(cs) )
return FALSE; // TODO: , // cs
return TRUE; }
// CMainFrame
#ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd: : AssertValid () .;
}
void CMainFrame::Dump (CDumpContextSc dc) const { CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame
- MyScribe MyScribe New, Open..., Save, Save As..., ,
. , -(. . 10), .
12. - 245
Visual Studio MyScribe, Resource View
. , , IDR_MAINFRAME. File. File ( , ) :
New ID_FILE_NEW &New\ tctr 1+N. Open... ID_FILE_OPEN &0pen...\tCtrl+0. Save ID_FILE_SAVE &Save\tCtrl+S. Save As... ID_FILE_SAVE_AS Save &As...
:
Recent File ID__FILE_MRU_FILE1 Recent File. .txt,
IDR_MAINFRAME, , ScratchBook ( " File" ). Caption :
"MyScribe\n\nMyScribe\nMyScribe Files (*.txt)\n.txt XnMyScribe.DocumentNnMyScribe Document"
DeleteContents
CMyScribeDoc. MFC , . , Class View Properties (. " " . 11). DeleteContents, MyScribeDoc.cpp, , . CEditView EDIT. , , ( ). , . CEditView, - .
, DeleteContents GetFirstViewPosition GetNextView CDocument . . 13, -. . GetFirstViewPosition ( MyScribe). GetNextView Pos Pos , ( ). GetNextView Pos.
// CMyScribeDoc
void CMyScribeDoc::DeleteContents() { // TODO: //
246 III.
POSITION Pos = GetFirstViewPosition(); CEditView *PCEditView = (CEditView *)GetNextView (Pos); if (PCEditView)
PCEdi tView-> SetWindowText (""); CDocument::DeleteContents();
}
GetNextView , DeleteContents CWnd: : SetWindowText, , . GetNextView , DeleteContents . MFC , DeleteContents. GetNextView , , DeleteContents .
. 50 . CEditView . , .
"drag-and-drop", Initlnstance MyScribe.cpp DragAcceptFiles.
// // m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); m_pMainWnd->DragAcceptFiles(); return TRUE;
MyScribe ( ScratchBook) , CEditView. MyScribe Serialize , CEditView Application Wizard Serialize. SerializeRaw CEditView, , . .
MyScribe , (12.912.16)
MyScribe.
12.9 // MyScribe.h : MyScribe // #pragma once
ttifndef AFXWIN_H_ terror include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" //
12. - 247
// CMyScribeApp: // MyScribe. //
class CMyScribeApp : public CWinApp { public: CMyScribeApp();
// public: virtual BOOL Initlnstance();
// afx_msg void OnAppAbout(); DECLARE_MES SAGE_MAP()
} ;
12.10 // MyScribe. : . //
#include "stdafx.h" ttinclude "MyScribe.h" ttinclude "MainFrm.h"
ttinclude "MyScribeDoc.h" ttinclude "MyScribeView.h"
ttifdef _DEBUG ttdefine new DEBUG_NEW ttundef THIS_FILE static char THIS_FILE[] = FILE ; ttendif
/ CMyScribeApp
BEGIN_MESSAGE_MAP(CMyScribeApp, CWinApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_0PEN, CWinApp::OnFileOpen) // ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
// CMyScribeApp
CMyScribeApp::CMyScribeApp() { // TODO: // // Initlnstance
}
248 III.
// CMyScribeApp
CMyScribeApp theApp;
// CMyScribeApp
BOOL CMyScribeApp::Initlnstance() { CWinApp::Initlnstance();
// . // - // // , // , // . // , // . // TODO: - , // , SetRegistryKey(_("Local AppWizard-Generated Applications")); LoadStdProfileSettings(4); //
// INI- ( MRU) // . // , // CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME, RUNTIME_CLASS(CMyScribeDoc), RUNTIME_CLASS(CMainFrame), //
II SDI- RUNTIME_CLASS(CMyScribeView));
AddDocTemplate(pDocTemplate); // // , DDE, CCommandLinelnfo cmdlnfo; ParseCommandLine(cmdlnfo); // , . // FALSE, // /RegServer, /Register, /Unregserver /Unregister. if (!ProcessShellCommand(cmdlnfo))
return FALSE; // // m_pMa inWnd-> ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); m_pMainWnd->DragAcceptFiles(); return TRUE;
}
// CAboutDlg , App About
class CAboutDlg : public CDialog
12. - 249
{ public: CAboutDlg();
// enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
//