Programming Microsoft Windows Forms

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Programming Microsoft Windows Forms"

Transcription

1 Chapter 1

2

3 Chapter Microsoft.NET Microsoft Windows..NET Windows Forms. Windows Forms Windows, Windows. (.EXE). (.DLL) (, ). Windows Forms C n a t iv e Windows 32 (Win32 API) MFC(Microsoft Foundation Classes) C+ + Windows. Windows Forms.NET Framework DLL..NET Framework Windows Forms Windows Forms,..NET Framework Microsoft (http :/ / msdn. microsoft.com/ netframework/ downloads/ updates) NET Framework 2.0. Windows Forms Windows, Win32 MFC. Windows Forms. MSIL(Microsoft Intermediate Languages ; Microsoft ). ( MSIL ). CLR(Common Language Runtime ; ).NET Framework.NET.

4 2 4 PROGRAMMING MICROSOFT WINDOWS FORMS.NET..,.NET Windows Forms. Windows Forms. m a n a ge d cod e. s t r on g..net CLR g a r b a g e colle ct ion., CLR.. Windows Forms Win32 API..NET Windows Forms. fie ld m e t h od p r op e r t y ev e n t ( ).. (,, ).. 0,,., Windows Forms.NET CLS(Common Language Specification ; )., C+ + Microsoft Managed Extensions. C+ +.NET..NET C# Microsoft Visual Basic.NET. C#( ) C C+ +, CLS..NET

5 Chapter C#. C C+ + C#. Programming in the Key of C#(Microsoft Press, 2003),. L a n gu a ge C# C# C# S p e cifica t ion. Microsoft C# (http :/ / msdn.micro soft.com/ library/ en-us/ cscon/ html/ vcoricstartpage.asp) Anders Hejlsberg, Scott Wiltamuth, Peter Golde The C# Programming Language 1) (Addison-Wesley, 2003). C# Programmer's Reference( C# ). C#,.NET..NET., C# Visual Basic.NET. Microsoft CLS J # J Script. (Microsoft ) J 2). Windows Forms Microsoft Visual Studio.. Visual Studio 2005.NET 2.0 Visual Studio 1) : C# : The C# Programming Language 2) : Java.

6 2 6 PROGRAMMING MICROSOFT WINDOWS FORMS. Microsoft Visual C# Visual Basic.NET Express. Visual Studio. Visual Studio,.NET. Visual Studio, Visual Studio Windows Forms.,. (Windows ) Windows Forms, MS-DOS.,.NET Framework SDK(Software Development Kit). SDK csc(c# ) vbc(visual Basic.NET ). ( ). Microsoft C#.NET, Visual Studio C#. (http :/ / com/ keycs) Key of C# IDE. Key of C#.NET Framework,.NET Framework SDK..NET Framework.NET Framework SDK Visual Studio.NET (Key of C#, ). Visual Studio., Microsoft IntelliSense,..

7 Chapter Visual Studio.NET Framework SDK,.NET Framework. MSDN(Microsoft Development Network) (http :/ / msdn.microsoft.com/ library)..net.. Contents.NET Framework Class Library., namespace. Microsoft, n a m e s p a ce System..NET Framework..,.,.NET Framework Timer,. t h ir d- p a r t y DLL Timer..NET,.NET System. s t a ck, q u e u e, h a s h t a b le System.Collections System.Collections.Generic. System.IO. System.Data System.Xml XML. Windows Forms System.Windows.Forms, System.Drawing.

8 2 8 PROGRAMMING MICROSOFT WINDOWS FORMS.NET,,. e n t it y. C#. class.,. r e fe r e n ce struct..net t y p e v a lu e t y p e... interface...,.. delegate. d e le g a t e p r ot ot yp e.. enum. e n u m e r a t ion. ( ),.,,...NET Windows Forms.?

9 Chapter con s ole.net..net. Console Write WriteLine, System Console (WriteLine ca r r ia ge r e t u r n Write ). Console Read ReadLine, ReadKey (.NET 2.0 ). s t a t ic. Console ( Console). Hello, world. FirstConsole Program.cs / / / / Fi r st Consol e Progr am. c s ( c) by Cha r l e s Pet zol d / / cl a s s Fi r st Consol e Pr og r am publ i c st at i c voi d Mai n ( ) Sy st em. Consol e.wr i t e Li ne ( " He l l o. NET Consol e " ) ;, FirstConsoleProgram.cs. (.cs) C Sharp. FirstConsoleProgram. C#, ( ). C# ( ), C# 2.0 partial.

10 30 PROGRAMMING MICROSOFT WINDOWS FORMS C# Main. C C+ +. C#, Main M. Main. Main. publ i c st at i c i nt Mai n ( st r i ng [] a r gs ) Main static., Main FirstConsoleProgram. Main. Fi r st Consol e Pr og r am. Ma i n () Main System.Console.WriteLine., fu ll q u a lifie d. System.Console.WriteLine System, using. SecondConsole Program.cs / / / / Se condconsol e Progr am. c s (c ) by Cha r l e s Pet zol d / / us i ng Syst em; cl a s s SecondConsol e Pr og r am publ i c st at i c voi d Ma i n () Consol e. Wr i t e Li ne ( "Hel l o agai n,. NET Consol e " ) ;

11 Chapter using. (, System.Threading System.Thread using, Timer ),. C# csc.exe. Key of C#. Visual Studio, Visual Studio. Vis ua l St ud io Visual Studio. Windows Forms,... C# Visual Basic.NET. Visual Studio.csproj (C# ).vbproj (Visual Basic.NET ) XML. Visual Studio ( ).., Visual Studio., C# DLL Visual Basic.NET C+ +.NET ( Visual Studio Visual C#, C# ).

12 32 PROGRAMMING MICROSOFT WINDOWS FORMS DLL. Visual Studio.sln. Visual Studio.. Visual Studio, Visual C# Windows (Visual C# ). Visual Studio ( ),.. Visual Studio. Visual Studio (.sln)., ch e ck b ox., Visual Studio. (Visual C# ).... C#.. Visual Studio, C#., Console Console. ( ) WriteLine. Visual Studio IntelliSense. Microsoft (, C#, IntelliSense IntelliSense ).

13 Chapter 1 3 3, F6. ( ). (F5) (Ctrl + F5)., Visual Studio. ( ), Visual Studio... Visual Studio, Visual Studio ConsoleApplication 1. Program.cs us i ng Sy st em; us i ng Sy st em. Col l e ct i ons. Gene r i c ; us i ng Sy st em.text ; name spac e Consol eappl i cat i on1 c l a s s Pr og r am st at i c voi d Mai n ( st r i ng [] a r gs) Visual Studio using.., (DLL ).

14 34 PROGRAMMING MICROSOFT WINDOWS FORMS Visual Studio, ( ).. Program.cs.,.NET C#. DLL. System Console, Console ReadLine? C# Console..NET DLL DLL m e t a d a t a (.NET System.Reflection ). Console MSDN a s s e m b ly. Console mscorlib (mscorlib.dll). Mscorlib Microsoft Common Object Runtime Library, Multilanguage Standard Common Object Runtime Library. Windows Forms DLL., System.Windows.Forms System.

15 Chapter 1 35 Windows.Forms. System. Windows.Forms.dll. C# DLL. DLL r e fe r e n ce s. Visual Studio. Visual Studio C#. Visual Studio DLL. Visual Studio IntelliSense. IntelliSense DLL. W ind ow s Windows Forms. FirstWinFormsProgram...NET. Windows Forms System System.Drawing, System.Windows.Forms. ( ), System.Data System.Xml. Ctrl., IntelliSense., IntelliSense using. FirstWinFormsProgram FirstWinFormsProgram.cs.

16 36 PROGRAMMING MICROSOFT WINDOWS FORMS FirstWinForms Program.cs / / / / Fi r st Wi nfo rms Progr am. c s (c ) by Cha r l e s Pet zol d / / us i ng Syst em. Wi ndows. Fo rms ; cl a s s Fi r st Wi nfo r ms Pr og r am publ i c st at i c voi d Ma i n () Appl i c at i on. Run ( new Fo r m( ) ) ; using. Application Form, System.Windows.Forms. using,.. using. using,. System.Windows.Forms.Application System.Windows.Forms.Form.. 3), System.Drawing.dll Windows Forms System.Drawing System.Drawing.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing., Ctrl + F5.. 3) :.

17 Chapter 1 3 7,,,, (. ). new Fo rm() Form. Form Windows Forms. Form Application Run. Windows. Windows, Windows. Application.Run Form., Application.Run, Main. Application.Run Application.Run Console.WriteLine (Console System using ). Windows..

18 38 PROGRAMMING MICROSOFT WINDOWS FORMS Windows Forms, Windows Forms, Console.WriteLine. Windows Forms... Ctrl+ C,. Windows Forms.., Visual Studio ( t arget ).. Windows. Windows Forms Windows,.. 4) Windows ( )-Windows Application.Run. FirstWinFormsProgram. Application.Run Form.,. 4) :.

19 Chapter 1 39 Form,. Application.Run. FormProperties.cs / / / / Fo r mprope rt i e s. c s (c ) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Fo rmpr ope r t i e s publ i c st at i c voi d Mai n ( ) Fo rm f rm = new Fo rm() ; f r m. Text = "My Wi nfo r ms Progr am" ; f r m. Wi dt h *= 2 ; Appl i c at i on. Run ( f r m) ; Text Width Form...,. Text Width. 5)? Text Width,.NET Framework.., s m a r t. Text. Width. Form Width.. 5) : public.

20 4 0 PROGRAMMING MICROSOFT WINDOWS FORMS, Form get set (, get_width set_width).. f r m. set _wi dt h ( 2 * f rm. get _wi dt h () ) ;. J #.. Visual Studio. ( ). Form.NET Framework Text Width. Form Control. Form Form..,.NET..,

21 Chapter callback. ev e n t h a n d le r...,,,.. Control Form., Control ( ) Click (Form ). Control Click. publ i c eve nt Event Handl e r Cl i c k ; event C#. Click EventHandler. EventHandler (C# delegate), System. publ i c del egat e voi d Event Handl e r (obj ect se nde r, Eve nt Ar gs e ) ;, Click.,.. voi d MyCl i c ke r (obj ect obj Sr c, Eve nt Ar gs a r gs)... 1), 2), 3) 4)..

22 4 2 PROGRAMMING MICROSOFT WINDOWS FORMS f r m. Cl i c k += new Event Handl e r (MyCl i c ke r ) ; (compound assignment operator). - = ( ). C# 2.0,. f r m. Cl i c k += MyCl i c ke r ; MyClicker EventHandler., Form Click, MyClicker. MyClicker obj Src. Form. Form. Fo rm f r m = ( Fo r m) obj Sr c ; Fo rm f r m = obj s r c a s Fo r m; Form. EventArgs. Click (MouseDown MouseHover ). Paint. ( ). Paint. Paint,. Paint Paint.

23 Chapter Paint Control. publ i c eve nt Pa i nt Eve nt Ha ndl e r Pa i nt ; PaintEventHandler System.Windows.Forms. publ i c del egat e voi d Pai nt Event Handl e r (obj ect sende r, Pai nt Event Args e) ; Paint EventArgs PaintEventArgs. voi d MyPai nt e r (obj ect obj Sr c, Pai nt Event Ar gs a r gs )... PaintEventHandler. f r m. Pa i nt += new Pa i nt Eve nt Ha ndl e r (My Pa i nt e r ) ; f r m. Pa i nt += My Pa i nt e r ; PaintEventArgs System.Windows.Forms. EventArgs, Graphics ClipRetangle. Graphics Graphics. ClipRectangle Graphics. Click Paint. Click, Paint.

24 4 4 PROGRAMMING MICROSOFT WINDOWS FORMS FormEvents.cs / / / / Fo rmevent s. c s (c ) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; cl a s s Fo r meve nt s publ i c st at i c voi d Ma i n () Fo r m f r m = new Fo r m( ) ; f rm.text = "My Eve nt s Pr og r am" ; f rm.wi dt h *= 2 ; f rm. Cl i c k += MyCl i c ke r ; f rm. Pai nt += MyPai nt e r ; Appl i cat i on. Run (f rm) ; st at i c voi d MyCl i c ke r (obj e ct obj Sr c, Event Ar gs a r gs ) Me s sa ge Box. Show( "The f o rm ha s be en c l i c ked!", "Cl i c k" ) ; st at i c voi d My Pa i nt e r (obj e ct obj Sr c, Pa i nt Eve nt Ar gs a r gs) Fo r m f r m = ( Fo rm) obj Sr c ; Gr a phi c s gr f x = a r gs. Gr a phi c s ; g r f x. Dr awst r i ng ( " He l l o, Wi ndows Fo rms ", f r m. Font, Syst embr ushe s. Cont r ol Text, 0, 0) ; st atic. FormEvent s,. FormEvent, public.. MyPainter Graphics DrawString., ( ), x y. x y.

25 Chapter MyPainter, Form Font., MyPainter obj Src Form, Font. Form Main. Form Form Form. Form,. Form. cl as s MyFo rm: Form... Main Application.Run. Main. cl a s s My Pr og r am publ i c st at i c voi d Mai n ( ) Appl i c at i on. Run ( new My Fo rm() ) ;

26 4 6 PROGRAMMING MICROSOFT WINDOWS FORMS MyForm Form, Form public protected,,.,. cl as s MyForm: Form publ i c MyFo r m( ) Text = "My I nhe r i t e d Fo r m" ; Wi dt h *= 2 ; C#.. MyForm.. Main MyForm., this C#. t hi s.text = My I nhe r i t e d Fo r m; Visual Studio this (.) IntelliSense.. MyForm. cl as s MyForm: Form publ i c MyFo r m( ) Text = "My I nhe r i t e d Fo r m" ; Wi dt h *= 2 ; Cl i c k += MyCl i c ke r ; Pa i nt += My Pa i nt e r ; voi d MyCl i c ke r (obj ect obj Sr c, Eve nt Ar gs a r gs)

27 Chapter Me s s agebox. Show( "The but t on ha s bee n cl i c ked!", "Cl i c k") ; voi d MyPai nt e r (obj ect obj Sr c, Pa i nt Eve nt Ar gs a r gs ) Gr aphi c s g r f x = a r gs. Gr aphi c s ; gr f x. Dr awst r i ng ( "Hel l o, Wi ndows Fo r ms ", Font, Syst embr ushe s. Cont r ol Text, 0, 0) ; Form. Control ( Form ),. On., OnPaint (Paint ) OnClick (Click )., ( this ). virtual. Control Form override. Control v ir t u a l protected ov e r r id e protected. cl as s MyFo rm: Form publ i c MyFo r m( ) Text = " " ; Wi dt h *= 2 ; p rot e ct ed ove r r i de voi d OnCl i c k( Event Ar gs a r gs) Me s s agebox. Show( "The but t on ha s bee n cl i c ke d!", "Cl i c k") ; p rot e ct ed ove r r i de voi d OnPa i nt ( Pa i nt Eve nt Ar gs a r gs ) Gr aphi c s g r f x = a r gs. Gr aphi c s ; gr f x. Dr awst r i ng ( "Hel l o, Wi ndows Fo r ms ", Font, Syst embr ushe s. Cont r ol Text, 0, 0) ; MyProgram, MyForm Main. Form Main

28 4 8 PROGRAMMING MICROSOFT WINDOWS FORMS Main. cl as s MyForm: Form publ i c st at i c voi d Ma i n () Appl i c at i on. Run ( new MyFo r m( ) ) ; Main MyForm MyForm. Main static MyForm.. InheritFromForm.cs / / / / I nhe r i t FromFo r m. c s ( c) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; cl a s s I nhe r i t Fr omfo rm: Fo rm publ i c st at i c voi d Ma i n () Appl i c at i on. Run ( new I nhe r i t Fr omfo rm() ) ; publ i c I nhe r i t Fr omfo rm() Text = "I nhe r i t f rom Fo r m" ; Wi dt h *= 2 ; pr ot ect e d ove r r i de voi d OnCl i c k ( Eve nt Ar gs a r gs) Me s s agebox. Show( "The f o rm ha s bee n c l i c ked!", "Cl i c k") ; pr ot ect e d ove r r i de voi d OnPai nt ( Pai nt Event Ar gs a r gs ) Gr aphi c s gr f x = a r gs. Gr aphi c s ; gr f x. Dr awst r i ng ( "Hel l o, Wi ndows Fo r ms ", Font, Syst embr ushe s. Cont r ol Text, 0, 0) ;

29 Chapter : Control OnClick OnPaint Click Paint. Click Paint, InheritFromForm, InheritFromForm Click Paint. InheritFrom Form( InheritFromForm ) Click Paint., OnClick OnPaint. ba se. OnPai nt (a r gs) ; Vis ua l St ud io Windows Visual Studio. Visual Studio.. Visual Studio Program.cs ( WindowsApplication) Program Main. Program.cs us i ng Sy st em; us i ng Sy st em. Col l e ct i ons. Gene r i c ; us i ng Sy st em.wi ndows. Fo r ms ; name spac e Wi ndowsappl i cat i on1 st at i c c l a s s Progr am / / / <summa r y> / / /.

30 5 0 PROGRAMMING MICROSOFT WINDOWS FORMS / / / </ s umma ry> [STAThr ea d] st at i c voi d Ma i n () Appl i c at i on. Enabl evi sual St yl e s () ; Appl i c at i on. Set Compat i bl etext Rende r i ngde f a ul t ( f a l s e) ; Appl i c at i on. Run ( new Fo rm1( ) ) ; Main XML... XML. Main STAThread a t t r ib u t e..net STA(Single-Threaded Apartment : - ). STA COM (Common Object Module) ( System STAThreadAttribute )., Windows Forms,. STAThread. Application EnableVisualStyles ( ). FlatStyle.,. Form1 Form1.cs. Form1.cs us i ng Syst em; us i ng Syst em. Col l ect i ons. Ge ne r i c ; us i ng Syst em. Component Model ;

31 Chapter us i ng Sy st em. Dat a ; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; name spac e Wi ndowsappl i cat i on1 pa r t i al cl a s s Fo rm1 : Fo r m publ i c Fo r m1 () I ni t i al i z ecomponent () ; Form1 Form. partial.. InitializeComponent Form1, Form1.Designer.cs. Form1.Des igner.cs name spac e Wi ndowsappl i cat i on1 pa r t i al cl a s s Fo rm1 / / / <s umma ry> / / / Requi r ed de si gne r va r i a bl e. / / / </ summa r y> pr i vat e Sy st em. Component Mode l. I Cont a i ne r component s = nul l ; / / / <s umma ry> / / / Cl ea n up any r e sour ce s bei ng use d. / / / </ summa r y> / / / <pa r am name=" di spos i ng">t r ue i f managed r e sour ce s shoul d be di s po se d ; / / / ot he rwi s e, f a l s e. </ pa r am> pr ot ect e d ove r r i de voi d Di s po se (bool di spos i ng) i f (di s po si ng && ( component s!= nul l ) ) compone nt s. Di spos e ( ) ; ba s e. Di spos e ( di spos i ng) ; # r egi on Wi ndows Fo r m De si gne r gene r at ed code / / / <s umma ry> / / / Requi r ed met hod f o r De s i gne r s uppo rt - do not modi f y

32 5 2 PROGRAMMING MICROSOFT WINDOWS FORMS / / / t he cont e nt s of t hi s met hod wi t h t he code e di t o r. / / / </ s umma ry> pr i vat e voi d I ni t i a l i zecomponent ( ) t hi s. component s = new Sy st em. Compone nt Mode l. Cont ai ne r ( ) ; t hi s. Aut osc al emode = Syst em. Wi ndows. Fo rms. Aut osc al emode. Font ; t hi s. Text = " Fo r m1" ; #e nd r e gi on component s Visual Studio boilerplate. Container Component. Windows Windows Forms Control Component. Component. Container. Visual Studio,., ( )., Width Height Size.. Visual Studio Form1.Designer.cs. t hi s. Cl i ent Si z e = new Syst em. Dr awi ng. Si ze ( 565, 266) ;. Windows Forms. ( ).

33 Chapter Windows Forms b u t t on, e d it fie ld, lis t b ox, s cr oll b a r, (.NET 2.0 Windows Forms ). p a n e l s p lit t e r. 3. Control,,. Parent Control.. ( Form ).,. Visible false. Enabled. false d u ll- color. Button p u s h. Button, Click., Visual Studio, C#. FormWithButton.cs / / / / Fo r mwi t hbut t on. c s (c ) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Fo rmwi t hbut t on : Fo r m

34 5 4 PROGRAMMING MICROSOFT WINDOWS FORMS [STATh r e ad] publ i c st at i c voi d Ma i n () Appl i c at i on. Enabl evi sual St yl e s () ; Appl i c at i on. Run ( new Fo r mwi t hbut t on ( ) ) ; publ i c Fo r mwi t hbut t on ( ) Text = " Fo r m wi t h But t on" ; But t on bt n = new But t on ( ) ; bt n. Pa r e nt = t hi s ; bt n. Text = "Cl i c k me!" ; bt n. Locat i on = new Poi nt ( 50, 2 5) ; bt n. Aut osi ze = t r ue ; bt n. Cl i c k += But t ononcl i c k ; voi d But t ononcl i c k (obj ect obj Sr c, Eve nt Ar gs a r gs) Me s s agebox. Show( "The but t on wa s c l i c ked!", " But t on") ; Button, Parent this (Form ). Text. Location 50, 25. AutoSize true,., ButtonOnClick Click. Control Button. AutoSize.

35 Chapter Button, Button. But t on bt n = (But t on) obj Sr c ; But t on bt n = obj Sr c a s But t on ; Click.. Text,.. Name. Name. Tag. Tag. Tag, Tag.., CheckBox Click. t ogg le., Boolean Checked.

36 5 6 PROGRAMMING MICROSOFT WINDOWS FORMS,.,. Control.,. But t on bt ncance l ;. bt ncance l = new But t on ( ) ;.... But t on bt ncance l = new But t on ( ) ; C#.,.., Visual Studio. Visual Studio. Visual Studio Windows,. Visual Studio Button private Form1. pr i vat e Syst em. Wi ndows. Fo rms. But t on but t on1 ; Button ( ). t hi s. but t on1 = new Sy st em.wi ndows. Fo r ms. But t on ( ) ; t hi s. but t on1. Loc at i on = new Syst em. Dr awi ng. Poi nt ( 92, 55) ;

37 Chapter t hi s. but t on1. Name = " but t on1" ; t hi s. but t on1. Si z e = new Syst em. Dr awi ng. Si ze ( 104, 47) ; t hi s. but t on1.tabi ndex = 0 ; t hi s. but t on1.text = " but t on1" ; t hi s. Cont r ol s. Add (t hi s. but t on1) ;. Visual Studio ( ). Visual Studio Parent Form.. t hi s. Cont r ol s. Add (t hi s. but t on1) ; Controls Form ( Form Control ). Controls Control.ControlCollection., ControlCollection Control. public Control. ControlCollection ICollection IEnumerable, IList.. Control Controls. Form Controls Parent Form. Parent null. ( ) Controls. Name Control Control.ControlCollection Find..? Z z-order ( 3. Z x y ). Z Controls., Parent.

38 5 8 PROGRAMMING MICROSOFT WINDOWS FORMS, Z. BringToTop SendToBack Z. Visual Studio, Visual Studio TabIndex 0. TabIndex. Visual Studio Visual Studio. TabIndex 0 ( ), Z,. Visual Studio,.,. Visual Studio SuspendLayout ResumeLayout., (, Layout ). Windows Forms Form.. Tag.. MessageButton.. Text

39 Chapter public. cl a s s Me s s agebut t on : But t on publ i c st r i ng Me s s ageboxtext = " " ; OnClick, public MessageButton. Me s s agebut t on mbbt n = new Me s sa ge But t on () ; mbbt n. Me s s ageboxtext = "Text i n t he me s s age box. " ; MessageBoxText public,.net Framework. const.. private public.., Control Text Color,.. MessageBoxText. MessageBoxText,. MessageBoxText Enabled. MessageButton private enabled false, MessageBoxText.

40 6 0 PROGRAMMING MICROSOFT WINDOWS FORMS MessageButton.cs / / / / Me s s agebut t on. c s ( c) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; cl a s s Me s sa ge But t on : But t on st r i ng st r Me s s ageboxtext ; publ i c Me s sa ge But t on () Ena bl ed = f a l s e ; publ i c st r i ng Me s s ageboxtext s et st rme s sa ge BoxText = val ue ; Enabl e d = val ue!= nul l && val ue. Lengt h > 0 ; get r et u r n st rme s sa ge BoxText ; pr ot ect e d ove r r i de voi d OnCl i c k ( Eve nt Ar gs a r gs) Me s sa ge Box. Show(Me s sa ge BoxText, Text ) ; MessageBox. MessageBoxText set get. set value. MessageBoxText string, value string Length. Visual Studio MessageButtonDemo, MessageButton.cs C#. MessageButton, MessageButtonDemo.cs C#..

41 Chapter MessageButtonDemo.cs / / / / Me s sa ge But t ondemo. c s ( c) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Me s s agebut t ondemo : Fo rm [STAThr ea d] publ i c st at i c voi d Mai n ( ) Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Me s sa ge But t ondemo () ) ; publ i c Me s sa ge But t ondemo () Text = "Me s sa ge But t on Demo " ; Me s sa ge But t on msgbt n = new Me s sa ge But t on () ; msgbt n. Pa r e nt = t hi s ; msgbt n. Text = "Ca l c ul at e 10, 000, 000 di gi t s of PI " ; msgbt n. Me s s ageboxtext = "Thi s but t on i s not yet i mpl eme nt ed!" ; msgbt n. Locat i on = new Poi nt ( 50, 50) ; msgbt n. Aut osi ze = t r ue ; MessageBoxText null. MessageButtonDemo.cs Visual Studio, IntelliSense msgbtn.net. MessageButton., IntelliSense MessageButton ( IntelliSense). t op - d ow n ( b ot t om - u p ) ( Main ). Visual Studio. IntelliSense?.

42 6 2 PROGRAMMING MICROSOFT WINDOWS FORMS MessageButton,. MessageButton.cs. Visual Studio ( ).. Control Location Size (3 ). Location Point X Y... Size Width Height Size (Point Size System. Drawing ). Windows. 6). 300, 600, 1200 dpi(dot per inch ; ),. 96 dpi, Windows. 19 dpi 480 dpi ( ). 96 dpi 120 dpi. 120 dpi 6) : Windows Windows.

43 Chapter ,. 96 dpi 120 dpi. Windows Forms Graphics DpiX DpiY. Windows Forms 8 p oin t. Font SizeInPoints ( 1/ 72. d e s ce n d e r s ( ) d ia cr it ics ). Font Height. Height 13 Height 16. 1/ 2.,. 1 3/ 4. 2 (4 ), 1/ 2.. Device IndependentButton.cs / / / / Devi c ei ndepende nt But t on. c s ( c) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Devi ce I nde pe ndent But t on : Fo rm [STAThr ea d] publ i c st at i c voi d Mai n ( ) Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Devi c ei ndepende nt But t on () ) ; publ i c Devi c ei ndepende nt But t on ()

44 6 4 PROGRAMMING MICROSOFT WINDOWS FORMS Text = " Devi ce - I ndepende nt But t on" ; i nt f nt ht = Font. Hei ght ; Cl i ent Si z e = new Si z e ( f nt ht * 30, f nt ht * 10) ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = " Looki n ' good!" ; bt n. Si z e = new Si z e ( 17 * f nt ht / 2, 7 * f nt ht / 4) ; bt n. Loc at i on = new Poi nt ( (Cl i e nt Si ze.wi dt h - bt n. Wi dt h) / 2, (Cl i ent Si z e. He i ght - bt n. He i ght ) / 2) ; Size 13 4, 1/ / dpi. 7) 120 dpi. 7) :, ( ),. (Some confusion things in your source http :/ / smile. egloos. com/ ).

45 Chapter 1 6 5,. 8) Visual Studio AutoScaleDimensions AutoScaleMode (.NET 1.x AutoScaleBaseSize ). Cont ainercontrol. Control Form. AutoScaleMode AutoScaleMode. AutoScaleMode.Dpi AutoScaleMode.Font, AutoScaleMode.Inherit. AutoScaleMode AutoScaleMode.Font, Form Font AutoScaleDimrensions. Visual Studio, Visual Studio. AutoScaleDimrensions AutoScaleMode. Windows,. t hi s.aut osca l e Di me ns i ons = new Sy st em. Dr awi ng. Si z e F(6F, 13F) ; t hi s.aut osca l emode = Sy st em.wi ndows. Fo r ms.aut osca l emode. Font ; SizeF Size Windows Forms. Windows, Form Form / 6 16/ 13. 8) :,.

46 6 6 PROGRAMMING MICROSOFT WINDOWS FORMS Windows, Visual Studio. t hi s.aut osca l e Di me ns i ons = new Sy st em. Dr awi ng. Si z e F(8F, 16F) ; t hi s.aut osca l emode = Sy st em.wi ndows. Fo r ms.aut osca l emode. Font ;, Visual Studio. AutoScaleMode AutoScaleMode.Dpi. (96 dpi ), Visual Studio. t hi s.aut osca l e Di me ns i ons = new Sy st em. Dr awi ng. Si z e F(96, 96) ; t hi s.aut osca l emode = Sy st em.wi ndows. Fo r ms.aut osca l emode. Dpi ;. t hi s.aut osca l e Di me ns i ons = new Sy st em. Dr awi ng. Si z e F( 120, 120) ; t hi s.aut osca l emode = Sy st em.wi ndows. Fo r ms.aut osca l emode. Dpi ;,., Windows r e s ou r ce s cr ip t. 1/ 8 1/ 4. 1/ 2,.. Aut osc al edi mensi ons = new Si z e ( 4, 8) ; Aut osc al emode = Aut osca l emode. Font ; 1/ 10,.

47 Chapter Aut osc al edi mensi ons = new Si z e ( 10, 10) ; Aut osc al emode = Aut osca l emode. Dpi ; AutoScaleDimensions AutoScaleMode.,,.. DeviceDependentButton. AutoScaleButton.cs / / / / Aut osca l e But t on. c s ( c) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Aut osc al ebut t on : Fo rm [STAThr ea d] publ i c st at i c voi d Mai n ( ) Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Aut osca l e But t on () ) ; publ i c Aut osca l e But t on () Text = "Aut o- Sc al e But t on" ; Cl i ent Si z e = new Si z e ( 240, 80) ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = " Loo ki n ' good!" ; bt n. Si z e = new Si z e ( 17 * 4, 14) ; bt n. Loc at i on = new Poi nt ( (Cl i e nt Si ze.wi dt h - bt n.wi dt h) / 2, (Cl i ent Si z e. He i ght - bt n. He i ght ) / 2) ; Aut osca l e Di me ns i ons = new Si ze (4, 8) ; Aut osca l emode = Aut osca l emode. Font ;

48 6 8 PROGRAMMING MICROSOFT WINDOWS FORMS Visual Studio Windows AssemblyInfo.cs. EXE DLL. Windows., Windows Forms.. Key of C#. [a s s embl y : As sembl yti t l e ( "Key of C# ") ] [a s s embl y : As sembl yde sc r i pt i on ( "Sma l l C# I DE f o r. NET" ) ] [a s s embl y : As sembl ycompany ( "www. c ha r l e s pet zol d. com" ) ] [a s s embl y : As sembl y Pr oduct ( "Key of C# ") ] [a s s embl y : As sembl ycopy r i ght ( " ( c) Cha r l e s Pet zol d, " + Ve r si on. Copy r i ght ) ] [a s s embl y : As sembl yve r s i on (Ve r s i on. Ma j o r + ". " + Ve r si on. Mi no r + ". *" ) ] Key of C#, About Version., Version. (:) assembly., (.EXE.DLL ). System.Reflection using. Reflection., AssemblyTitle AssemblyTitleAttribute. AssemblyVersion... < >.< >.< >.< > (*), C# , 1/ 2.

49 Chapter AssemblyVersion, AssemblyFileVersion AssemblyVersion.,.. (...). 9) m od a l m od e le s s 10)..,.,.. (, )..,. Windows Forms Form. 9) :. 10) : Modeless. KLDP, MSDN. MSDN,.

50 70 PROGRAMMING MICROSOFT WINDOWS FORMS cl a s s MyDi a l ogbox : Fo rm ShowDialog. MyDi al ogbox dl g = new MyDi al ogbox ( ) ; dl g. ShowDi al og () ; ShowDialog.. ( ). ( ).,. ( ).,. DialogResult DialogResult (DialogResult.OK DialogResult.Cancel). ShowDialog, DialogResult. Click. Click DialogResult. Click. voi d OkBut t ononcl i c k(obj e ct obj Sr c, Eve nt Ar gs a r gs ) Di al ogre sul t = Di a l ogre s ul t. OK;

51 Chapter i f ( dl g. ShowDi al og () == Di a l ogre s ul t. OK) Windows Forms Button DialogResult ( Form DialogResult,. Form Button DialogResult., Control ). DialogResult DialogResult, DialogResult ( ).... ModalDialogBox.cs / / / / Modal Di al ogbox. c s (c ) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Moda l Di a l ogbox : Fo r m Che c kbox c bgr aysha de s ; publ i c Modal Di al ogbox ( ) Text = "Cha nge Col o r " ; Fo r mbo r de r St yl e = Fo r mbo r de r St yl e. Fi xeddi al og ; Cont r ol Box = f a l s e ; Mi ni mi z ebox = f al se ; Maxi mi z ebox = f al se ; ShowI nta s kba r = f al se ; St a rt Po si t i on = Fo r mst a rt Po si t i on. Manua l ; Loc at i on = Act i ve Fo rm. Loc at i on + Syst emi nf o r mat i on. Capt i onbut t onsi ze + Syst emi nf o r mat i on. Fr ame Bo r de r Si ze ;

52 72 PROGRAMMING MICROSOFT WINDOWS FORMS Cl i ent Si z e = new Si z e ( 144, 56) ; c bgr aysha de s = new Che c kbox ( ) ; c bgr aysha de s. Pa r e nt = t hi s ; c bgr aysha de s. Text = "Gr ay Shade s Onl y" ; c bgr aysha de s. Locat i on = new Poi nt ( 16, 8) ; c bgr aysha de s. Si ze = new Si ze (80, 12) ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = "OK" ; bt n. Loc at i on = new Poi nt ( 16, 32) ; bt n. Si z e = new Si z e ( 48, 14) ; bt n. Di a l ogre s ul t = Di a l ogre s ul t. OK; Acc ept But t on = bt n ; bt n = new But t on () ; bt n. Pa r ent = t hi s ; bt n.text = "Ca nc el " ; bt n. Loc at i on = new Poi nt (80, 32) ; bt n. Si z e = new Si z e ( 48, 14) ; bt n. Di a l ogre s ul t = Di a l ogre s ul t. Ca nc el ; Cance l But t on = bt n ; Aut oscal e Di me ns i ons = new Si ze (4, 8) ; Aut oscal emode = Aut osc al emode. Font ; publ i c bool Gr ayshade s s et cbgr ayshade s. Che c ked = va l ue ; get ret urn cbgr ayshades. Checked ;.. ( ),. ShowInTaskBar false.

53 Chapter 1 7 3, Windows.., ActiveForm ( St artposition Location ). Gray Shades Only. DialogResult DialogResult.OK DialogResult.Cancel. AcceptButton CancelButton OK Cancel.. AcceptButton.. ( ). CancelButton Escape. CheckBox public. GrayShades b oole a n.... ModalDialogDemo. ModalDialogDemo ModalDialogBox.cs ModalDialogDemo.cs.

54 74 PROGRAMMING MICROSOFT WINDOWS FORMS ModalDialogDemo.cs / / / / Moda l Di a l ogdemo. c s ( c) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; cl a s s Modal Di al ogdemo : Fo rm [STATh r e ad] publ i c st at i c voi d Ma i n () Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Modal Di al ogdemo () ) ; publ i c Modal Di al ogdemo () Text = "Moda l Di al og Demo" ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = "Change Col o r " ; bt n. Loc at i on = new Poi nt ( 16, 16) ; bt n.aut osi z e = t r ue ; bt n. Cl i c k += But t ononcl i c k ; voi d But t ononcl i c k (obj ect obj Sr c, Eve nt Ar gs a r gs) Modal Di al ogbox dl g = new Modal Di al ogbox ( ) ; i f (dl g. ShowDi a l og ( ) == Di al ogre sul t. OK) Ra ndom r nd = new Random( ) ; i nt i Shade = r nd. Next (2 55) ; i f (dl g. Gr aysha de s) Ba c kcol o r = Col o r. Fr omar gb (i Shade, i Sha de, i Sha de ) ; el se Ba c kcol o r = Col o r. Fr omar gb (i Shade, r nd. Next (2 55), r nd. Next ( 255) ) ; Click.,. Click ModalDialogBox,

55 Chapter 1 75 ShowDialog., ShowDialog. ShowDialog DialogResult.OK,. Gray Shades Only, ( ). dlg.grayshades,.,. dlg,. ShowDialog... ( )...,. FormBorderStyle FixedDialog MinimizeBox MaximizeBox false, ControlBox true. ControlBox,,.

56 76 PROGRAMMING MICROSOFT WINDOWS FORMS Owner.. dl g. Owne r = t hi s ; ShowDialog Show., ShowDialog. Show. Show,...,. Change, Change EventHandler.. publ i c event Event Handl e r Cha nge ;., Change.. i f (Cha nge!= nul l ) Change (t hi s, new Eve nt Ar gs () ) ; Change null, Change..

57 Chapter ModelessDialogBox ModalDialogBox, Change. Modeless DialogBox.cs / / / / Model e s sdi a l ogbox. c s ( c) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; cl a s s Mode l e s s Di al ogbox : Fo rm Che c kbox c bgr aysha de s ; publ i c eve nt Eve nt Ha ndl e r Change ; publ i c Model e s sdi a l ogbox () Text = "Cha nge Col o r " ; Fo r mbo r de r St yl e = Fo r mbo r de r St yl e. Fi xeddi al og ; Mi ni mi z ebox = f al se ; Maxi mi z ebox = f al se ; ShowI nta s kba r = f al se ; St a rt Po si t i on = Fo r mst a rt Po si t i on. Manua l ; Loc at i on = Act i ve Fo rm. Loc at i on + Syst emi nf o r mat i on. Capt i onbut t onsi ze + Syst emi nf o r mat i on. Fr ame Bo r de r Si ze ; Cl i ent Si z e = new Si z e ( 144, 56) ; c bgr aysha de s = new Che c kbox ( ) ; c bgr aysha de s. Pa r e nt = t hi s ; c bgr aysha de s. Text = "Gr ay Shade s Onl y" ; c bgr aysha de s. Locat i on = new Poi nt ( 32, 8) ; c bgr aysha de s. Si ze = new Si ze (80, 12) ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = "Cha nge " ; bt n. Loc at i on = new Poi nt (48, 32) ; bt n. Si z e = new Si z e ( 48, 14) ; bt n. Cl i c k += But t ononcl i c k ; Acc ept But t on = bt n ; Aut oscal e Di me ns i ons = new Si ze (4, 8) ; Aut oscal emode = Aut oscal emode. Font ; publ i c bool Gr ayshade s s et

58 78 PROGRAMMING MICROSOFT WINDOWS FORMS cbgr ayshade s. Che c ked = va l ue ; get ret urn cbgr ayshades. Checked ; voi d But t ononcl i c k (obj ect obj Sr c, Eve nt Ar gs a r gs) i f (Cha nge!= nul l ) Change (t hi s, new Event Ar gs ( ) ) ; Change Change. ModelessDialogDemo. ModelessDialogDemo. Modeless DialogDemo.cs / / / / Mode l e s s Di al ogdemo. c s (c ) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; cl a s s Model e s sdi a l ogdemo : Fo r m [STATh r e ad] publ i c st at i c voi d Ma i n () Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Model e s sdi a l ogdemo ( ) ) ; publ i c Model e s sdi a l ogdemo ( ) Text = "Mode l e s s Di a l og Demo " ; But t on bt n = new But t on ( ) ; bt n. Pa r ent = t hi s ; bt n.text = "Change Col o r " ; bt n. Loc at i on = new Poi nt ( 16, 16) ; bt n.aut osi z e = t r ue ; bt n. Cl i c k += But t ononcl i c k ; voi d But t ononcl i c k (obj ect obj Sr c, Eve nt Ar gs a r gs)

59 Chapter 1 79 Model e s sdi a l ogbox dl g = new Mode l e s s Di al ogbox ( ) ; dl g. Owne r = t hi s ; dl g. Cha nge += Di a l ogoncha nge ; dl g. Show( ) ; voi d Di a l ogoncha nge (obj e ct obj Sr c, Eve nt Ar gs a r gs ) Model e s sdi a l ogbox dl g = (Model e s sdi a l ogbox) obj Sr c ; Random r nd = new Random( ) ; i nt i Shade = r nd. Next ( 255) ; i f (dl g. Gr aysha de s) Ba c kcol o r = Col o r. FromAr gb ( i Sha de, i Sha de, i Sha de ) ; e l s e Ba c kcol o r = Col o r. FromAr gb ( i Sha de, r nd. Next (2 55), r nd. Next ( 255) ) ; ButtonOnClick,. Change Show. Show. Change, Change. DialogOnChange. ModelessDialogBox. DialogBoxDemo, Change Color. ( ).. dlg dlg null. ButtonOnClick Change Color.

60 8 0 PROGRAMMING MICROSOFT WINDOWS FORMS ( ( But t on) obj Sr c). Ena bl ed = f al se ; Change Color. Close. dl g. Cl os ed += Di a l ogoncl o se d ;. voi d Di al ogoncl os ed (obj ect obj Sr c, Event Ar gs a r gs) Cont rol s [0]. Enabl e d = t r ue ;. Controls..NET Framework 2.0 CommonDialog,. ColorDialog, FolderBrowserDialog, FontDialog, OpenFileDialog SaveFileDialog(FileDialog ), PageSetupDialog, PrintDialog. DLL DLL.. DLL Main. DLL public. namespace. ( DLL ) DLL DLL..

61 Chapter MessageButton DLL. MessageButtonLib.cs / / / / Me s sa ge But t onli b. c s (c ) by Cha r l e s Pet zol d / / us i ng Sy st em; us i ng Sy st em. Dr awi ng ; us i ng Sy st em.wi ndows. Fo r ms ; name spac e Pet zol d. Pr og r ammi ngwi ndows Fo rms publ i c c l a s s Me s sa ge But t on : But t on st r i ng st rme s sa ge BoxText ; publ i c Me s s agebut t on ( ) Enabl e d = f al se ; publ i c st r i ng Me s sa ge BoxText set st r Me s s ageboxtext = va l ue ; Ena bl ed = va l ue!= nul l && va l ue. Le ngt h > 0 ; get r et ur n st r Me s s ageboxtext ; p rot e ct ed ove r r i de voi d OnCl i c k( Event Ar gs a r gs ) ba se. OnCl i c k (a r gs) ; Me s s agebox. Show(Me s s ageboxtext, Text ) ; namespace public MessageButton.cs. OnClick (Button )., MessageButton Click.

62 8 2 PROGRAMMING MICROSOFT WINDOWS FORMS MessageButtonLib.cs / target :library. MessageButtonLib.dll. Visual Studio. MessageButtonLibraryDemo Visual Studio MessageButtonLib. ProgramUsingLibrary. MessageButtonDemo. ProgramUs inglibrary.cs / / / / Pr og r amusi ngli br a r y. c s ( c) by Cha r l e s Pet zol d / / us i ng Syst em; us i ng Syst em. Dr awi ng ; us i ng Syst em. Wi ndows. Fo rms ; us i ng Pet zol d. Progr ammi ngwi ndows Fo r ms ; cl a s s Progr amus i ngli b r a ry : Fo rm [STATh r e ad] publ i c st at i c voi d Ma i n () Appl i cat i on. Ena bl evi s ua l St yl e s ( ) ; Appl i cat i on. Run (new Progr amus i ngli b r a ry () ) ; publ i c Progr amus i ngli b r a ry () Text = " Pr og r am Us i ng Li br a r y" ; Me s sa ge But t on msgbt n = new Me s sa ge But t on () ; msgbt n. Pa r e nt = t hi s ; msgbt n. Text = "Cal cul at e 10, 000, 000 di gi t s of PI " ; msgbt n. Me s s ageboxtext = "Thi s but t on i s not yet i mpl eme nt ed!" ; msgbt n. Locat i on = new Poi nt ( 50, 50) ; msgbt n. Aut osi ze = t r ue ; DLL using. using using

63 Chapter using, Petzold.ProgrammingWindows Forms.MessageButton.. Windows Forms System System.Drawing, System.Windows.Forms ( DLL ). MessageButtonLib.dll. / reference MessageButtonLib.dll., MessageButtonLib., DLL DLL. DLL, DLL. ( ).,,.. ProgramUsingLibrary.. DLL. ProgramUsingLibrary.exe MessageButtonLib.dll, DLL EXE.

64 8 4 PROGRAMMING MICROSOFT WINDOWS FORMS DLL Windows DLL.