Chapter 11: Advanced Windows Forms

Overview

In this chapter we will examine some of the techniques you can use to develop Windows Forms applications, and - in accordance with the advanced philosophy of this book - I'm not talking about basic Windows Forms techniques. I'm referring to some of the ways that you can make your Windows Forms programs that little bit more sophisticated or look that bit more polished than those of your competitors.

There are broadly two areas that this chapter is going to cover: how to take full advantage of the message loop - possibly in combination with multiple threads - to perform asynchronous and background processing in a way that keeps your code simple without sacrificing application responsiveness; and how to give your application the edge in terms of graphics. So the chapter comprises two fairly independent halves, covering each of those areas.

In more detail, the topics we will cover are:

  • Message Loops - every Windows Forms application is underpinned by a Windows message loop. The details of the operation of the message loop are largely hidden from you by the System.Windows.Forms classes, but understanding how the loop works is important for writing and debugging any Windows Forms code that performs sophisticated background processing. We will also see how to access the message loop from your code: you might need to do this if you need to handle a situation for which no Windows Forms event is available.

  • Idle Processing - it's possible to take advantage of the message loop to slip in method calls to do background processing when the user is not doing anything. In this way you get some of the benefits of multithreaded applications without having to use multiple threads (with all the associated thread synchronization issues and potential bugs). We'll explore how to do this.

  • BeginInvoke - the Control.BeginInvoke() method provides a useful way of using the message loop to allow communication between threads without requiring use of the normal thread synchronization primitives - and hence without blocking threads. We'll see how to do this, and in the process develop an example that illustrates how to implement an Abort dialog that allows the user to cancel an asynchronous operation.

  • XP Themed Controls - we examine how to add support for XP themes to your controls.

  • Non-Rectangular Forms - we show how you can create forms and controls with more interesting shapes than the usual rectangles.

  • Owner-Draw Forms - it's also possible to make the appearance of certain controls - including menus, list boxes and buttons, much more attractive by taking charge yourself of some drawing operations that are usually handled by the Microsoft-supplied code in these controls. Used in conjunction with non-rectangular controls this can lead to astounding visual effects. We develop a example that illustrates this.

  • GDI - although in almost all cases you'll use the inbuilt GDI+ classes that come with the .NET Framework for your drawing operations, there are certain operations that GDI+ does not support, and for which you'll need to fall back on GDI. We discuss when you might wish to do this, and present an example that illustrates one such case: a form that can take screenshots. Note that we won't be extensively discussing DirectX. DirectX is another option for graphics, but is much more specialized and can be more complicated to code up - and is beyond the scope of this book (since this isn't a graphics book!)

We start off with a look under the hood at the Windows message loop architecture, and how the Windows Forms event architecture has been built on top of it.



Advanced  .NET Programming
Advanced .NET Programming
ISBN: 1861006292
EAN: 2147483647
Year: 2002
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net