Although web programming has been the hot topic in recent years, Windows programming is undergoing something of a rejuvenation under .NET. This is partly because web services allow us to take advantage of the Intemet even in Windows applications, but it's also a belated recognition that Windows programs offer a much richer experience to the user than browser-based applications. This book provides a concise but in-depth guide to writing programs in the Windows environment using C#. The emphasis is firmly on GUI programming, but important related areas are also covered.
The book aims to take .NET programmers to a level where they have a good understanding not just of how Windows Forms work in .NET, but also of the undedying graphical APl of .NET, GDI+. While we show how VS.NET simplifies the creation of Windows applications, the emphasis is always on the actual cede, so readers should be very comfortable building sophisticated Windows applications without VS.NET by the end of the book.
What you need to know
This book is ideal for programmers who have learned the basics of .NET and C#, but need
more information about using C# in real applications. Prior experience of Windows
programming, probably using either Visual Basic or MFC, is assumed.
What you will learn from this book
Creating Windows applications in C# with and without Visual Studio .NET
Using the Windows Forms classes and controls
Advanced data binding
How to handle user input
Drawing with GDI+
Printing
Creating custom controls
Multi-threaded programming in .NET
Deploying Windows applications
Introducion
What does this book cover?
Who is this book for ?
What do you need to use this book?
Conventions
Customer support
Chapter1 .NET and windows
Chapter2 working with windows forms
Creating a form-based application
Adding menus to forms
Multiple document interface forms
Common dialog boxes
Toolbars
The status bar
Summary
Chapter3:windows common controls
The control base class
Common controls
Button control
Custom drawing
Consuming activeX controls from.NET
Summary
Chapter4 data binding
Data binding in.net
Simple data binding
Updating data
Complex binding
Binding to a custom control
Summary
Chapter5 user input
Introducing the keyboard
Internationalizing the keyboard
Losing and gaining focus
Responding to the mouse
Drawing with the mouse
Using tooltips
Summary
Chapter6 drawing in.NET
Overview of GDI+
Events and painting
Graphics objects
Colors,pens,and brushes
Shapes,paths,and regions
Drawing texts-fonts
Handing images
Summary
Chapter7 printing
System.drawing.printing
Printing in.NET
Example:printing a datagrid
Summary
Chapter8 custom controls
Extended controls
Composite controls
Custom drawn controls
Deeper into the propertygrid
Summary
Chapter9 optimizing windows applications
Basic threading issues
Application locking
Thread performance
Writing multithreaded applications
Different windows forms programming models
Using p/invoke
Event accessors
Summary
Chapter10 deploying windows applications
Using windows installer
Merge module projects
CAB files
Thrickle deployment
Code access security-protecting the client
Summary
Index
Although web programming has been the hot topic in recent years, Windows programming is undergoing something of a rejuvenation under .NET. This is partly becauseweb services allow us to take advantage of the Internet even in Windows applications, but it's also a belated recognition that Windows programs offer a much richer experience to the user than browser-based applications.
Perhaps the most exciting aspect of the .NET Framework is the fact that it combines the sort of power previously available only to C++ programs with the simplicity we associate with Visual Basic. In fact, with Visual Studio .NET, it's just as easy to create functional Windows applications in C4~ as it was in VB6. But the drag-and-drop simplicity of Visual Studio .NET shouldn't obscure the fact that .NET gives us much greater power than VB did. For example, it's now a relatively straightforward matter to customize the visual appearance of controls, so that we can still take advantage of their functionality while making them conform to our own look and feel. Even creating controls from scratch is now easy enough to be a worthwhile investment of time.
True, if we want to take advantage of these features, we need to do a little bit more work, but it's an effort that's amply repaid. In this book, we'll try to show you the capabilities of Windows Forms, and how to take advantage of them. The beauty of .NET is that it allows us to choose exactly the level of complexity that we need. Wherever we want to take control ourselves, that option exists (right down to the level of processing Windows messages ourselves); but where we just require the standard behavior,.NET obliges and does the hard work for Us.-Thisbook aims to give you an understanding~Sbth of what .NET does for us, and of what we can achieve by taking control ourselves.
What does this book cover?
This book aims to provide a concise but in-depth guide to writing programs in the Windows environment using C#. The emphasis is firmly on GUI programming, but important related areas are also covered in some depth. This is not a book about the aesthetics of UI design.
Professional Windows GUI Programraing using C# is aimed at programmers who have learnt the basics of .NET and C#, but need more information about using C# in real applications. The book aims to take .NET programmers to a level where they have a good understanding not just of how Windows Forms work in .NET, but also of the underlying graphical API of .NET, GDI+.
While we show how VS.NET simplifies the creation of Windows applications, the emphasis is always on the actual code, so readers should be very comfortable huilding sophisticated Windows applications without VS.NET by the end of the book.
Chapter 1: .NET and Windows
We start by taking a quick look at how Windows works, and how the operating system uses messages to communicate with individual windows. We also see how we can create Windows programs in .NET, both with and without VS.NET.
Chapter 2: Working with Windows Forms
In the next chapter, we take a more detailed look at the Form class, and how we can add standard features such as menus, toolbars, and dialogs to our Windows Forms, and how we can create MDI applications in .NET.
Chapter 3: Windows Common Controls
Now that we've covered the basics of constructing Windows applications in .NET, we move on to look at some of the most useful of the predefined controls that ship with the .NET Framework, concentrating on the more advanced features and controls, and those that are new to .NET. We also see how existing ActiveX controls can be incorporated into Windows Forms applications.
Chapter 4: Data Binding
The ability to bind controls to data sources is extremely useful, but has tended to be limited to simple client-server applications..NET changes all this, because it allows us to bind to many different types of objects, and not just datasets. This chapter examines how we can use this feature to take advantage of data binding even in multi-tier applications.
Chapter 5: User Input
Windows controls all provide standard handling for keyboard and mouse events, but there are many occasions when we need a greater level of control. This chapter examines how we can trap keyboardevents to discover what combination of keys have been pressed/released, how we can allow for international keyboards, and how we can use mouse events to track the position of the mouse, presses on the mouse buttons, and rotations of the mouse wheel.
Chapter 6: Drawing in .NET
.NET provides a set of classes specifically for drawing to an output device such as the user's screen.These classes act as wrappers for an unmanaged DLL called GDI+. In this chapter, we see how we can use the powerful graphics capabilities of GDI+ to draw text and images on the screen, including the use of different pens, brushes, and colors, performing image transformations such as rotations, and zooming and scrolling images.
Chapter 7: Printing
No self-respecting Windows program would be complete without offering the user the option of printingout the window's contents. In .NET, this is achieved using the same GDI+ library that's used for drawing to the screen. In this chapter, we see how we can provide full printing support, including accessing page and printer settings, providing a print preview, and of course printing out the content itself.
Chapter 8: Custom Controls
.NET provides three ways of creating custom controls - extending existing controls, combining several controls into a single composite control, and creating the control from scratch, including its user interface. This chapter looks at real-world examples of all three types. As well as examining how we create the controls, we also see how we can make them easier to use at design time by modifying the
property grid.
Chapter 9: Optimizing Windows Applications
In this chapter we look at some of the programmatic issues involved with Windows Forms applications, and particularly at the issue of threading. We see how we can create multi-threaded Windows applications in .NET, and look at some of the issues and pitfalls involved. We also examine how we can take advantage of other programming models in our Windows Forms applications, by accessing web
services, and by calling Windows API functions. Finally, we see how we can use event accessors to improve the efficiency of .NET's event handling model.
Chapter 10: Deploying Windows Applications
Now that we know how to create a fully-featured Windows application, it's time to deploy it to client machines. Despite .NET's much-vaunted xcopY deployment features, in reality end users will want a much friendlier setup program to install and maintain our application. Our last chapter discusses how we can create setup projects in Visual Studio .NET that will use Windows Installer to provide a user- friendly setup application. We also see how we can deploy applications over the Internet.
Who is this book for?
This book is aimed at developers who have some experience of the .NET Framework and C#, and who want to learn more about how they can use their new skills in practical Windows applications. In particular, this book is for those who want to deepen their knowledge of Windows Forms beyond the simple drag-and-drop techniques offered by Visual Studio .NET, and to go beyond the_ standard'interfaces available to the developer. It is assumed that you have prior experience programming Windows applications, probably using either MFC or Visual Basic.
What do you need to use this book?
A prerequisite for this book is to have a machine with the .NET Framework installed upon it. This means that you'll need to be running either Windows 2000 Professional (or better), or Windows XP. The screenshots used in this book reflect the fact that either OS is compatible with the examples that we explore.
The .NET Framework itself is available as a free download from http:/Imsdn.microsoft.com/netframework/downloadslhowtoget.asp. It comes in two versions:
The .NET Framework Redistributable - the full framework on its own. This includes everything you need to run any .NET application. Its approximate size is 20Mb.
the .NET Framework SDK (Software Development Kit) - the full framework, plus tools for creating .NET applications, documentation, samples, and tutorials that you can refer to in order to learn more about .NET. Approximate size: 130Mb
This book is designed to be 'editorneutral. All of the examples can be created, run, and understood using a simple text editor such as Notepad. You do not need Visual Studio .NET in order to use this book, although we do point out where it would be beneficial to use it. Chapter 10 discusses deploying Windows applications specifically using the tools shipped with Visual Studio .NET, although the latest
versions of other installation applications (such as InstallShield) can be used with .NET.
Conventions
We've used a number of different styles of text and layout in this book to help differentiate between different kinds of information. Here are examples of the styles we used and an explanation of what they mean.
Code has several fonts. If it's a word that we're talking about in the text - for example, when discussing a for (...) loop, it's in this font. If it's a block of code that can be typed as a program and run, then it's also in a gray box:
Graphics g = CreateGraDhics();
Sometimes we'll see code in a mixture of styles, like this:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g. DrawString ( "ProfessiOnal Windows GUI Programming" ,
new Font("Ariai",10f),
new SolidBrush (Color. Red),
20f, 20f);
}
In cases like this, the code with a white background is code we are already familiar with Advice, hints, and background information comes in this type of font.
Important pieces of information come in boxes like this
Bullets appear indented with each new bullet marked as follows
Important works are in a bold type font
Words that appear on the screen, or in menus like the Open or Close, are in a similar the one you would see on a Windows desktop.
Keys that you press on the keyboard
Customer Support
you liked, what you didn't like, and what you think we can do better next time. Your comments, either by returning the reply card in the back of the book, or by e-mail to
How to Download the Sample Code for the Book
When vou visit the Wrox site, http://www.wrox.com/, simply locate the title through our Search facility or by using one of the title lists. Click on this book, you are presented with a page with three options:
If you are already a member of the Wrox Developer Community (if you have already registered on ASPToday, C#'Today, or Wroxbase), you can log in with your usual username
If you are not already a member, you are asked if you would like to register for free code downloads. In addition you will also be able to download several free articles from Wrox Press.registering will allow us to keep you informed about updates and new editions of this book
The third option is to bypass registration completely and simply download the code
Registration for code
Registration for code download is not mandatory for this book,but should you wish to register for your code download your details will not be passed to any third party for more details,you may wish to view our terms and conditions which are linked from the download page
Once you reach the code download section, you will find that the files that are available for download from our site have been archived using WinZip. When you have saved the files to a folder on your hard drive, you will need to extract the files using a de-compression program such as WinZip or PKUnzip. When you extract the files, the code is usually extracted into chapter folders. When you start the extraction process, ensure your software (WinZip, PKUnzip, etc.) is set to use folder names.
Errata
We've made every effort to take sure that there are no errors in the text or in the code. However, no one is perfect and mistakes do occur. If you find an error in one of our books, like a spelling mistake or a faulty piece of code, we would be very grateful for feedback. By sending in errata you may save another reader hours of frustration, and of course, you will be helping us provide even higher quality
information. Simply e-mail the information to support @wrox.com; your information will be checked and if correct, posted to the errata page for that title, or used in subsequent editions of the book To find errata on the web site, go to http://www.wrox.com/, and simply locate the title through our Advanced Search or title list. Click on the Book Errata link, which is below the cover graphic on the book's detail page.
E-mail Support
If you wish to directly query a problem in the book with an expert who knows the book in detail then e-mail support@wrox.com, with the title of the book and the last four numbers of the ISBN in the subject field of the e-mail. A typical e-mail should include the following things:
The title of the book, last four digits of the ISBN (7663), and page number of the problem in the Subject field.
Your name, contact information, and the problem in the body of the message.
We won't send you junk mail. We need the details to save your time and ours. When you send an e-mailmessage, it will go through the following chain of support:
Customer Support - Your message is delivered to our customer support staff, who are the first people to read it. They have files on most frequently asked questions and will answer anything general about the book or the web site immediately.
Editorial - Deeper queries are forwarded to the technical editor responsible for that book.They have experience with the programming language or particular product, and are able to answer detailed technical questions on the subject.
The Authors - Finally, in the unlikely event that the editor cannot answer your problem, they will forward the request to the author. We do try to protect the author from any distractions to their writing; however, we are quite happy to forward specific requests to them. All Wrox authors help with the support on their books. They will e-mail the customer and the editor with their response, and again all readers should benefit.
The Wrox Support process can only offer support to issues that are directly pertinent to the content of our published title. Support for questions that fall outside the scope of normal book support is provided via the community lists of our http://p2p.wrox.com/forum.
p2p.wrox.com
For author and peer discussion join the P2P mailing lists. Our unique system provides programmer to programmerTM contact on mailing lists, forums, and newsgroups, all in addition to our one-to-one e-mail support system. If you post a query to P2P, you can be confident that it is being examined by the many Wrox authors and other industry experts who are present on our mailing lists. At p2p.wrox.com you
will find a number of different lists that will help you, not only while you read this book, but also as you develop your own applications.
To subscribe to a mailing list just follow these steps:
1. Go to http://p2p.wrox.com/.
2. Choose the appropriate category from the left menu bar.
3. Click on the mailing list you wish to join.
4. Follow the instructions to subscribe and fill in your e-mail address and password.
5. Reply to the confirmation e-mail you receive.
6. Use the subscription manager to join more lists and set your e-mail preferences.
Why this System Offers the Best Support
You can choose to join the mailing lists or you can receive them as a weekly digest. If you don't have the time, or facility, to receive the mailing list, then you can search our online archives. Junk and spam mails are deleted, and your own e-mail address is protected by the unique Lyris system. Queries about joining or leaving lists, and any other general queries about lists, should be sent to listsur)Dort @ D20.wrox.com.