Windows Forms support several advanced techniques for creating form-based effects by modifying properties, as well as more advanced techniques supported through GDI+.
By setting a form's Opacity property to 100%, you get a normal-looking form. As you decrease Opacity toward 0%, the form becomes transparent. In addition, the Transparency property allows you to specify a transparent color. For example, if White is your Transparent color , all regions of the form that are White will be transparent. (These are simple property settings, so no example is provided here. Chapter 15, "Using Windows Forms," contains an example of using the Opacity method in the section titled "Region Class.")
Creating shaped forms is a simplified by-product of GDI+. Refer to Chapter 17, "Programming with GDI+," and the section titled "Shaped Forms" for an example of implementing a shaped form using GDI+.
In addition to shaped and transparent forms, there are some additional capabilities inherent in forms that resolve problems related to scrolling and scaling.
The Form.AutoScale property solves the problem that occurs when a user changes the size of fonts. Historically, when the font size was changed by the user, controls were clipped inappropriately, resulting in a choppy-looking GUI. By default Form.AutoScale is True.
Autoscrolling the Device Context
The Form.AutoScroll property is False by default. If you place controls on a form that has AutoScroll = False and the control's bounds are outside of the form's bounds, the control is clipped. If AutoScroll is True, a scrollbar is added to the form automatically when a control is placed beyond the visible bounds of the form. Autoscrolling allows you to expand the virtual boundary of the control. The visible real estate used remains the same as it would without scrolling, but the virtual real estate is much larger. Figure 16.4 shows a TextBox clipped when AutoScroll is False, and Figure 16.5 shows the improved behavior when AutoScroll is True.
Figure 16.4. When AutoScroll is False, controls are clipped to the visible region of the parent.
Figure 16.5. When AutoScroll is True, controls are not clipped and a scrollbar is used to extend the virtual real estate held by the parent control.
The Form.AutoScrollMargin property is a Size structure; it contains Width and Height properties that describe how much space to add between controls falling outside the bounding region when AutoScroll is True.
By default the value of Form.AutoScrollMargin is 0, 0. This means that if you place a control outside of the visible bounding rectangle, the scrollbar added by AutoScroll (as you saw in Figure 16.5) will allow you to scroll to the very edge of the clipped control. If AutoScrollMargin is non-zero , you will be able to scroll to the edge of the control plus the additional margin amount.
Using Custom CreateParams
Classes in the CLR hide the murky world of Windows API programming, but Windows hasn't gone anywhere . Nowhere is this more apparent than when you use the CreateParams class and override the ReadOnly property CreateParams.
CreateParams effectively adds values to the CREATESTRUCT API record that tell the Windows operating system what kind of window to create. The reason you might want to override CreateParams is to add something extra to a window control (controls that have an HWND, or window handle, field).
Although customizing a basic Windows Forms control by modifying the CreateParams values is not something you will do every day, it is something you might want to do at some point. Unfortunately, when working with CreateParams, you will be poking around in the world of Or'd integers with cryptic numbers that define window styles. You will need a good book on Windows API programming, such as Dan Appleman's Visual Basic Programmer's Guide to the Win32 API, by Dan Appleman, or a lot of experimentation and patience.
Believe it or not, the control shown in Figure 16.6 is a customized Windows Forms Button control that has simply overridden its CreateParams property. Listing 16.5 demonstrates the technical aspects of overriding CreateParams.
Figure 16.6. A custom Button with a caption and dialog-style border implemented by overriding CreateParams.
Listing 16.5 Subclass the Button control and override the CreateParams property for custom Windows styles
1: Imports System.Windows.Forms 2: 3: Public Class ExButton 4: 5: Inherits System.Windows.Forms.Button 6: 7: Protected Overrides ReadOnly Property CreateParams() _ 8: As CreateParams 9: Get 10: Dim Params As CreateParams = MyBase.CreateParams() 11: Params.Style = Params.Style Or &HC00000 12: Params.ExStyle = Params.ExStyle Or &H100 13: 14: Return Params 15: End Get 16: End Property 17: 18: End Class
Line 11 uses the value of the WS_CAPTION style to add the caption to the button, and the value of the WS_EX_WINDOWEDGE constant to create the raised-edge border, both shown in Figure 16.6.