Programming Microsoft Windows With C# - Petzold_ Charles.pdf

(7918 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming Microsoft Windows with C#
Programming Microsoft Windows with C#
by Charles Petzold
Microsoft Press © 2002
For the great people from [OR] Forum
Enjoy
878739333.002.png
Programming Microsoft Windows with C#
Charles Petzold
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2002 by Charles Petzold
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form
or by any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data
Petzold, Charles, 1953-
Programming Microsoft Windows with C# / Charles Petzold.
p. cm.
Includes index.
ISBN 0-7356-1370-2
1. C# (Computer program language) 2. Microsoft Windows (Computer file) I. Title.
QA76.73.C154 P48 2001
005.26'8--dc21
2001052178
1 2 3 4 5 6 7 8 9 QWT 7 6 5 4 3 2
Distributed in Canada by Penguin Books Canada Limited.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further
information about international editions, contact your local Microsoft Corporation office or contact
Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at
ClearType, Georgia, IntelliMouse, Microsoft, Microsoft Press, MS-DOS, MSDN, Natural, the .NET
logo, OpenType, Verdana, Visual Basic, Visual C#, Visual Studio, Webdings, Win32, Windows,
Windows NT, and Wingdings are either registered trademarks or trademarks of Microsoft
Corporation in the United States and/or other countries. Other product and company names
mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people,
places, and events depicted herein are fictitious. No association with any real company,
organization, product, domain name, e-mail address, logo, person, place, or event is intended or
should be inferred.
Acquisitions Editor: Danielle Bird
Project Editor: Sally Stickney
Technical Editor: Jean Ross
Body Part No. X08-22413
About The Author
Charles Petzold
Charles Petzold ( www.charlespetzold.co m ) is a full-time freelance writer who has been programming
for Microsoft Windows since 1985 and writing about Windows programming for nearly as long. He
wrote the very first magazine article about Windows programming for the December 1986 issue of
Microsoft Systems Journal . His book Programming Windows (first published by Microsoft Press in
1988 and currently in its fifth edition) taught a generation of programmers how to write applications
for Windows. In May 1994, Petzold was one of only seven people (and the only writer) to be given a
Windows Pioneer Award from Windows Magazine and Microsoft Corporation for his contribution to
the success of Microsoft Windows. He is also the author of a unique introduction to the inner
workings of computers entitled Code: The Hidden Language of Computer Hardware and Software .
Petzold is currently researching a book on the historical origins of software.
The manuscript for this book was prepared and galleyed using Microsoft Word version 2000. Pages
were composed by Microsoft Press using Adobe PageMaker 6.52 for Windows, with text in
Garamond and display type in Helvetica Condensed. Composed pages were delivered to the printer
as electronic prepress files.
Cover Designer: Methodologie, Inc.
Interior Graphic Designer: James D. Kramer
Principal Compositor: Paula Gorelick
Interior Artist: Joel Panchot
Principal Copy Editor: Holly M. Viola
Indexer: Shane-Armstrong Information Systems
PROGRAMMING MICROSOFT WINDOWS WITH C# ..........2
W INDOWS P ROGRAMMING : A N O VERVIEW ....................................................................14
U SER R EQUIREMENTS .........................................................................................................15
S YSTEM R EQUIREMENTS ....................................................................................................16
T HE O RGANIZATION OF T HIS B OOK .................................................................................16
T HE CD-ROM .......................................................................................................................17
S UPPORT ................................................................................................................................17
S PECIAL T HANKS ..................................................................................................................17
CHAPTER 1: CONSOLE THYSELF ......................................................19
O VERVIEW ..............................................................................................................................19
T HE C# V ERSION ..................................................................................................................19
A NATOMY OF A P ROGRAM ..................................................................................................21
C# N AMESPACES .................................................................................................................22
C ONSOLE I/O .........................................................................................................................23
C# D ATA T YPES ....................................................................................................................25
E XPRESSIONS AND O PERATORS ......................................................................................29
C ONDITIONS AND I TERATIONS ...........................................................................................31
T HE L EAP TO O BJECTS .......................................................................................................34
P ROGRAMMING IN THE K EY OF C# ...................................................................................38
S TATIC M ETHODS .................................................................................................................40
E XCEPTION H ANDLING ........................................................................................................41
T HROWING E XCEPTIONS ....................................................................................................43
G ETTING AND S ETTING P ROPERTIES ..............................................................................44
C ONSTRUCTORS ...................................................................................................................47
I NSTANCES AND I NHERITANCE ..........................................................................................51
A B IGGER P ICTURE ..............................................................................................................54
N AMING C ONVENTIONS .......................................................................................................55
B EYOND THE C ONSOLE .......................................................................................................56
CHAPTER 2: HELLO, WINDOWS FORMS ...................................57
O VERVIEW ..............................................................................................................................57
T HE M ESSAGE B OX ..............................................................................................................58
T HE F ORM ..............................................................................................................................63
S HOWING THE F ORM ...........................................................................................................64
I T ' S AN A PPLICATION AND W E W ANT TO R UN I T ...........................................................66
V ARIATIONS ON A T HEME ...................................................................................................68
F ORM P ROPERTIES ..............................................................................................................68
E VENT -D RIVEN I NPUT ..........................................................................................................70
H ANDLING THE P AINT E VENT ............................................................................................71
D ISPLAYING T EXT .................................................................................................................74
The Font ...............................................................................................................................74
The Brush ............................................................................................................................75
The Coordinate Points .......................................................................................................75
878739333.003.png
 
T HE P AINT E VENT I S S PECIAL ! .........................................................................................78
M ULTIPLE F ORMS , M ULTIPLE H ANDLERS ......................................................................78
I NHERITING F ORMS ..............................................................................................................80
T HE O N P AINT M ETHOD .......................................................................................................82
D OES M AIN B ELONG H ERE ? .............................................................................................83
E VENTS AND "O N " M ETHODS ............................................................................................85
CHAPTER 3: ESSENTIAL STRUCTURES ....................................89
O VERVIEW ..............................................................................................................................89
C LASSES AND S TRUCTURES ..............................................................................................89
T WO -D IMENSIONAL C OORDINATE P OINTS .....................................................................90
A RRAYS OF P OINTS ..............................................................................................................92
T HE S IZE S TRUCTURE .........................................................................................................92
T HE F LOAT V ERSIONS .........................................................................................................93
A R ECTANGLE I S A P OINT AND A S IZE ............................................................................95
R ECTANGLE P ROPERTIES AND M ETHODS .....................................................................96
A N ICE -S IZED F ORM ............................................................................................................98
T HE F ORM AND THE C LIENT ...............................................................................................99
P OINT C ONVERSIONS ........................................................................................................102
T HE C OLOR S TRUCTURE ..................................................................................................103
T HE 141 C OLOR N AMES ...................................................................................................104
P ENS AND B RUSHES ..........................................................................................................104
S YSTEM C OLORS ................................................................................................................106
T HE K NOWN C OLORS ........................................................................................................108
W HAT TO U SE ? ...................................................................................................................109
G ETTING A F EEL FOR R EPAINTS .....................................................................................109
C ENTERING H ELLO W ORLD .............................................................................................112
M EASURING THE S TRING ..................................................................................................115
T EXT IN A R ECTANGLE ......................................................................................................117
CHAPTER 4: AN EXERCISE IN TEXT OUTPUT ...................120
O VERVIEW ............................................................................................................................120
S YSTEM I NFORMATION ......................................................................................................120
S PACING L INES OF T EXT ..................................................................................................120
P ROPERTY V ALUES ............................................................................................................121
F ORMATTING INTO C OLUMNS ..........................................................................................123
E VERYTHING I S AN O BJECT .............................................................................................125
L ISTING THE S YSTEM I NFORMATION ..............................................................................130
W INDOWS F ORMS AND S CROLL B ARS ..........................................................................132
S CROLLING A P ANEL C ONTROL ......................................................................................132
T HE H ERITAGE OF S CROLLABLE C ONTROL ..................................................................136
S CROLLING W ITHOUT C ONTROLS ..................................................................................136
A CTUAL N UMBERS ..............................................................................................................138
D ON ' T B E A P IG ...................................................................................................................141
R EFLECTING ON THE F UTURE .........................................................................................142
CHAPTER 5: LINES, CURVES, AND AREA FILLS ...........149
878739333.004.png 878739333.001.png
 
Zgłoś jeśli naruszono regulamin