[1] The standard controls that come with WinForms are listed in Appendix D : Standard WinForms Components and Controls.. Note that all the drawing techniques discussed in this chapter an
Trang 1If Chapters 4, 5, and 6 haven't convinced you of NET's very rich support for drawing, then Chapter 7, on drawing to the printer, should do the trick
Trang 2As handy as forms are, especially when laden with controls, sometimes the built-in controls[1] aren't sufficient to render the state of your application In that case, you need to draw the state yourself The drawing may be to the screen, to a file, or to
a printer, but wherever you're drawing to, you'll be dealing with the same primitivescolors, brushes, pens, and fontsand the
same kinds of things to draw: shapes, images, and strings This chapter starts by examining the basics of drawing to the screen and the basic building blocks of drawing
[1] The standard controls that come with WinForms are listed in Appendix D : Standard WinForms Components and Controls.
Note that all the drawing techniques discussed in this chapter and in the next two chapters relate equally as well to controls
as they do to forms For information about building custom
controls, see Chapter 8: Controls
One more thing worth noting before we begin is that the
System.Drawing namespace is implemented on top of GDI+ (Graphics Device Interface+), the successor to GDI The
original GDI has been a mainstay in Windows since there was a Windows, providing an abstraction over screens and printers to make writing GUI-style applications easy.[2] GDI+ is a Win32 DLL (gdiplus.dll) that ships with Windows XP and is available for older versions of Windows GDI+ is also an unmanaged C++ class library that wraps gdiplus.dll Because the
System.Drawing classes share many of the same names with the GDI+ C++ classes, you may very well stumble onto the unmanaged classes when looking for the NET classes in the online documentation The concepts are the same, but the
coding details are very different between unmanaged C++ and managed anything else, so keep an eye out
Trang 3[2] GDI programming certainly isn't easy when compared with System.Drawing programming, but it's orders of magnitude easier than supporting printers and video display adapters by hand, which was what DOS programmers had to do to put bread on the table.
Trang 4Probably the most useful thing to draw in any application is
text Sometimes you'll draw text yourself, and sometimes it will
be drawn for you by the controls you're using No matter who does the drawing, you often can specify the font used to draw the text, and that's what the first part of this chapter is about The second part deals with drawing text yourself into a Graphics object or a GraphicsPath
Trang 5Chapters 4 and 5 cover the basics of drawing, including colors, pens, brushes, shapes, paths, images, fonts, and string
drawing This chapter takes a look at advanced topics such as page units, world transforms, regions, and optimization
techniques And as if that weren't enough, Chapter 7 wraps up the tour of the System.Drawing namespace with a look at
printing
Trang 6Usually, drawing to the screen is pretty easy because screen settings are generally constant during the run of the
application Drawing to a printer, on the other hand, is more complicated because users may change the printer or the
printer settings many times, even for a single document
Similarly, paper costs money and can't be sent through the printer twice (unless you don't care what's on the back), so before users print their documents they want to see what they will look like The actual drawing is largely the same for a
printer as it is for the screen, but the printer settings are the interesting part, and the settings are covered in this chapter
Trang 7Components and Controls
Winforms provides several classes meant to be composed to build applications How (and whether) instances of these
classes need to interact with the user determines whether they are components or controls The technical distinction isn't
important unless you're building one (as covered in Chapter 8: Controls and Chapter 9: Design-Time Integration) What is
important is knowing what's available out of the box for your use, as listed in Table D.1 This appendix briefly covers all these except for the print-related components, which are discussed in
Chapter 7: Printing
Table D.1 Standard WinForms Components and Controls
PageSetupDialog ( Chapter 7 : Printing) HScrollBox (page 236)
Trang 8PrintDialog ( Chapter 7 : Printing) Label (page 236)
PrintDocument ( Chapter 7 : Printing) LinkLabel (page 236)
PrintPreviewDialog ( Chapter 7 : Printing) ListBox (page 237)
Panel (page 242)
PictureBox (page 236)
PrintPreviewControl (page 236)
ProgressBar (page 236)
RadioButton (page 236)
RichTextBox (page 236)
Splitter (page 236)
TextBox (page 236)
StatusBar (page 236)
TabControl (page 242)
ToolBar (page 234)
TrackBar (page 236)
TreeView (page 237)
Trang 9
The WinForms documentation does a really good job of
providing the details of each standard component and control This appendix is a quick look at each of them to give you an idea of what you have to choose from
Trang 10The basic unit of the ui in WinForms is the control Everything that interacts directly with the user in a region defined by a
container is a control This includes controls that do everything themselves, as well as standard controls such as the TextBox, user controls (controls that contain other controls), and even the Form class itself
This chapter covers the broad categories of the standard
controls provided by WinForms It explains how to build custom and user controls and how to provide support for drag and drop, the most popular kind of intercontrol communication If you'd like a survey of the standard controls, refer to Appendix D:
Standard WinForms Components and Controls
Trang 11A component is a nonvisual class designed specifically to
integrate with a design-time environment such as Visual Studio NET (VS.NET) WinForms provides several standard
components, and NET lets you build your own, gaining a great deal of design-time integration with very little work
On the other hand, with a bit more effort, you can integrate nonvisual components and controls very tightly into the design-time environment, providing a rich development experience for the programmer using your custom components and controls