Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Visual CSharp 2005 Recipes (2006) [eng]

.pdf
Скачиваний:
48
Добавлен:
16.08.2013
Размер:
4.04 Mб
Скачать

x C O N T E N T S

CHAPTER 7 Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

7-1. Add a Control Programmatically. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 7-2. Link Data to a Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 7-3. Process All the Controls on a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

7-4. Track the Visible Forms in an Application. . . . . . . . . . . . . . . . . . . . . . . . . . 220

7-5. Find All MDI Child Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

7-6. Save Configuration Settings for a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 7-7. Force a List Box to Scroll to the Most Recently Added Item . . . . . . . . . . . 228 7-8. Restrict a Textbox to Accepting Only Specific Input . . . . . . . . . . . . . . . . . . 229

7-9. Use an Autocomplete Combo Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 7-10. Sort a List View by Any Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 7-11. Lay Out Controls Automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

7-12. Use Part of a Main Menu for a Context Menu . . . . . . . . . . . . . . . . . . . . . 239

7-13. Make a Multilingual Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

7-14. Create a Form That Cannot Be Moved . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

7-15. Make a Borderless Form Movable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

7-16. Create an Animated System Tray Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

7-17. Validate an Input Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 7-18. Use a Drag-and-Drop Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7-19. Use Context-Sensitive Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

7-20. Display a Web Page in a Windows-Based Application . . . . . . . . . . . . . . . 254

CHAPTER 8 Graphics, Multimedia, and Printing . . . . . . . . . . . . . . . . . . . . . . . .

257

8-1. Find All Installed Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 8-2. Perform Hit Testing with Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

8-3. Create an Irregularly Shaped Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

8-4. Create a Movable Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 8-5. Create a Scrollable Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 8-6. Perform a Screen Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

8-7. Use Double Buffering to Increase Redraw Speed . . . . . . . . . . . . . . . . . . . 271

8-8. Show a Thumbnail for an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

8-9. Play a Simple Beep or System Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

8-10. Play a WAV File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 8-11. Play a Sound File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

8-12. Show an Animation with DirectShow . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 8-13. Retrieve Information About the Installed Printers. . . . . . . . . . . . . . . . . . . 282

8-14. Print a Simple Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 8-15. Print a Multipage Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 8-16. Print Wrapped Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 8-17. Show a Dynamic Print Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 8-18. Manage Print Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

C O N T E N T S xi

CHAPTER 9 Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

9-1. Connect to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 9-2. Use Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304

9-3. Create a Database Connection String Programmatically. . . . . . . . . . . . . . 306 9-4. Store a Database Connection String Securely . . . . . . . . . . . . . . . . . . . . . . 308 9-5. Execute a SQL Command or Stored Procedure . . . . . . . . . . . . . . . . . . . . . 311 9-6. Use Parameters in a SQL Command or Stored Procedure. . . . . . . . . . . . . 315 9-7. Process the Results of a SQL Query Using a Data Reader. . . . . . . . . . . . . 318 9-8. Obtain an XML Document from a SQL Server Query . . . . . . . . . . . . . . . . . 321 9-9. Perform Asynchronous Database Operations Against SQL Server. . . . . . . 324 9-10. Write Database-Independent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 9-11. Discover All Instances of SQL Server on Your Network . . . . . . . . . . . . . . 331

CHAPTER 10 Networking and Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

10-1. Obtain Information About the Local Network Interface . . . . . . . . . . . . . . 336 10-2. Detect Changes in Network Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . 339

10-3. Download Data over HTTP or FTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

10-4. Download a File and Process It Using a Stream . . . . . . . . . . . . . . . . . . . 343 10-5. Respond to HTTP Requests from Your Application. . . . . . . . . . . . . . . . . . 346 10-6. Get an HTML Page from a Site That Requires Authentication . . . . . . . . . 349

10-7. Send E-mail Using SMTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

10-8. Resolve a Host Name to an IP Address . . . . . . . . . . . . . . . . . . . . . . . . . . 355

10-9. Ping an IP Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 10-10. Communicate Using TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

10-11. Create a Multithreaded TCP Server That Supports Asynchronous Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

10-12. Communicate Using UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

10-13. Avoid Hard-Coding the XML Web Service URL. . . . . . . . . . . . . . . . . . . . 374 10-14. Set Authentication Credentials for an XML Web Service . . . . . . . . . . . . 376 10-15. Call a Web Method Asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

10-16. Make an Object Remotable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

10-17. Register All the Remotable Classes in an Assembly . . . . . . . . . . . . . . . 385

10-18. Host a Remote Object in IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

10-19. Control the Lifetime of a Remote Object . . . . . . . . . . . . . . . . . . . . . . . . 388 10-20. Control Versioning for Remote Objects . . . . . . . . . . . . . . . . . . . . . . . . . 390

CHAPTER 11 Security and Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

393

11-1. Allow Partially Trusted Code to Use Your Strong-Named Assembly . . . . . 394

11-2. Disable Code Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

11-3. Disable Execution Permission Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 11-4. Ensure the Runtime Grants Specific Permissions to Your Assembly . . . . 400

xii C O N T E N T S

11-5. Limit the Permissions Granted to Your Assembly. . . . . . . . . . . . . . . . . . . 402 11-6. View the Permissions Required by an Assembly . . . . . . . . . . . . . . . . . . . 403 11-7. Determine at Runtime If Your Code Has a Specific Permission . . . . . . . . 406 11-8. Restrict Who Can Extend Your Classes and Override

Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 11-9. Inspect an Assembly’s Evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

11-10. Determine If the Current User Is a Member of a Specific

Windows Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

11-11. Restrict Which Users Can Execute Your Code . . . . . . . . . . . . . . . . . . . . 414

11-12. Impersonate a Windows User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418

11-13. Create a Cryptographically Random Number . . . . . . . . . . . . . . . . . . . . 421 11-14. Calculate the Hash Code of a Password . . . . . . . . . . . . . . . . . . . . . . . . 422 11-15. Calculate the Hash Code of a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

11-16. Verify a Hash Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

11-17. Ensure Data Integrity Using a Keyed Hash Code . . . . . . . . . . . . . . . . . . 429 11-18. Work with Security-Sensitive Strings in Memory. . . . . . . . . . . . . . . . . . 432 11-19. Encrypt and Decrypt Data Using the Data Protection API . . . . . . . . . . . 435

CHAPTER 12 Unmanaged Code Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . 439

12-1. Call a Function in an Unmanaged DLL . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 12-2. Get the Handle for a Control, Window, or File. . . . . . . . . . . . . . . . . . . . . . 442 12-3. Call an Unmanaged Function That Uses a Structure . . . . . . . . . . . . . . . . 444 12-4. Call an Unmanaged Function That Uses a Callback. . . . . . . . . . . . . . . . . 447 12-5. Retrieve Unmanaged Error Information . . . . . . . . . . . . . . . . . . . . . . . . . . 448 12-6. Use a COM Component in a .NET Client. . . . . . . . . . . . . . . . . . . . . . . . . . 450 12-7. Release a COM Component Quickly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452

12-8. Use Optional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

12-9. Use an ActiveX Control in a .NET Client . . . . . . . . . . . . . . . . . . . . . . . . . . 454 12-10. Expose a .NET Component Through COM . . . . . . . . . . . . . . . . . . . . . . . 455

CHAPTER 13 Commonly Used Interfaces and Patterns . . . . . . . . . . . . . . . . . . 457

13-1. Implement a Serializable Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 13-2. Implement a Cloneable Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 13-3. Implement a Comparable Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

13-4. Implement an Enumerable Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 13-5. Implement an Enumerable Type Using a Custom Iterator . . . . . . . . . . . . 475

13-6. Implement a Disposable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 13-7. Implement a Formattable Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

13-8. Implement a Custom Exception Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 13-9. Implement a Custom Event Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 13-10. Implement the Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 13-11. Implement the Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

C O N T E N T S xiii

CHAPTER 14 Windows Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

14-1. Access Runtime Environment Information . . . . . . . . . . . . . . . . . . . . . . . . 499 14-2. Retrieve the Value of an Environment Variable . . . . . . . . . . . . . . . . . . . . 503 14-3. Write an Event to the Windows Event Log . . . . . . . . . . . . . . . . . . . . . . . . 504 14-4. Read and Write to the Windows Registry . . . . . . . . . . . . . . . . . . . . . . . . . 505

14-5. Search the Windows Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 14-6. Create a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

14-7. Create a Windows Service Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516 14-8. Create a Shortcut on the Desktop or Start Menu. . . . . . . . . . . . . . . . . . . 518

APPENDIX Acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527

About the Authors

ALLEN JONES is a Director of Principal Objective Ltd., a UK-based consultancy that provides independent IT strategy and solutions architecture services. Allen has more than 15 years of commercial experience, covering almost every aspect of IT; however, his true passion has been and always will be software development. In his spare time, Allen works—writing books and training material—or studies in an effort to find some form of enlightenment that has so far eluded him.

MATTHEW MACDONALD is an author, educator, and MCSD developer. He is a regular contributor to programming journals and the author of more than a dozen books about .NET programming, including User Interfaces in C#: Windows Forms and Custom Controls (Apress), Pro ASP.NET 2.0 (Apress), and Microsoft .NET Distributed Applications (Microsoft Press). In a dimly remembered past life, he studied English literature and theoretical physics.

RAKESH RAJAN is a software engineer from India working with US Technology (http://www.ustri.com) at Technopark, Trivandrum in Kerala. He is a Microsoft MVP in C# and an MCSD in .NET. He has been working in

.NET for the past three years. You can find him posting at newsgroups, writing articles, working on his own projects, or speaking about .NET. Visit his site at http://www.rakeshrajan.com or drop him an e-mail at rakeshrajan@mvps.org.

xv

About the Technical Reviewer

CHRISTOPHE NASARRE is a Development Architect for Business Objects, a company that develops desktop and Web-based business intelligence solutions. During his spare time, Christophe writes articles for MSDN Magazine, MSDN/Longhorn, and ASPToday and has reviewed books on Win32, COM, MFC, and .NET since 1996.

xvii

Acknowledgments

I would like to thank everyone at Apress for working so hard to bring this book to print. In particular, I would like to thank Christophe, whose exceptional efforts as technical reviewer and many good suggestions made this book far better than it would have been without him. I would also like to thank Joss Whedon for giving us Firefly, a truly inspirational and entertaining science-fiction masterpiece.

Allen Jones

xix

Соседние файлы в предмете Программирование на C++