New Windows Capabilities

New Windows Capabilities

Now let s look at what each of the subforms in the sample application does.

Accessing the Registry

Accessing the registry is awkward in Visual Basic 6. You have to use APIs like RegCreateKey and RegDeleteKey, which are cumbersome. The .NET Framework provides a much easier mechanism for accessing the registry. The registry editing form in the RegistryFeatures project (included on the companion CD) shows how to use the .NET Framework classes to create and delete keys and values. Figure 18-4 shows the registry editing form.

Clicking the Create button in the MyRegistryKey Key group box creates the registry key HKEY_CURRENT_USER\Software\MyRegistryKey. Clicking the Delete button deletes this key. Clicking the Create button in the MyRegistryKey\MyValue Value box adds a new string value to the key called MyValue. Clicking the Delete button deletes the value. The contents of the HKEY_CURRENT_USER\Software key are shown in the tree view on the right of the form.

Figure 18-4

Registry editing form.

The following code shows how the form adds the registry key. First it tries to open the registry key, and if the registry key doesn t already exist, the code creates it.

Dim rk As Microsoft.Win32.RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\MyRegistryKey", True) If rk Is Nothing Then    Registry.CurrentUser.CreateSubKey("Software\MyRegistryKey") Else    rk.Close() End If

To create a registry key, we first create a Microsoft.Win32.RegistryKey variable called rk. We then use rk to attempt to open a registry key within Current User. The CurrentUser property maps to the HKEY_CURRENT_USER registry hive. If the registry key doesn t exist, it is created using the CreateSubKey method.

Deleting a key is even simpler. It involves only one line of code.

Registry.CurrentUser.DeleteSubKeyTree("Software\MyRegistryKey")

This line deletes the MyRegistryKey registry key and any registry subkeys and values it may have.

To add a value, we open the registry key and use the SetValue method. In the following example, we add a string value MyValue with the contents of the newValue.Text TextBox.

Dim rk As Microsoft.Win32.RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\MyRegistryKey", True) rk.SetValue("MyValue", CStr(Me.newValue.Text)) rk.Close()

If the value already exists, it is overwritten with this new value. To delete the value, use the DeleteValue method, as in the following example:

Dim rk As Microsoft.Win32.RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\MyRegistryKey", True) rk.DeleteValue("MyValue") rk.Close()

Notice in all these examples that each time we open a registry key, we also close it using the Close method. The registry editing form acts upon the HKEY_CURRENT_USER hive. In addition, you can access the HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, HKEY_USERS, and HKEY_CURRENT_CONFIG registry hives using the Registry object s LocalMachine, ClassesRoot, Users, and CurrentConfig properties. This is certainly much easier than working with the Visual Basic 6 registry APIs.

Control Anchoring

The registry editing form also demonstrates automatic resizing using control anchoring. When you resize the form, the tree view also resizes without your writing a single line of resize code! Using the Anchor property of the TreeView control, you can bind the edges of the control to the edges of the form. The TreeView control is anchored to each side of the form so that when the form resizes, the control also resizes, keeping the top, bottom, left, and right edges the same distance from the edges of the form. This method is much easier than in Visual Basic 6, where you have to write lines and lines of code in the Form_Resize event to adjust the size of the control.

Graphics Features

If you enjoy adding some sharpness to your applications with graphics, you ll be pleased to know that the .NET Framework has a set of dedicated graphics classes called GDI+, making it easier to add graphics than in any previous version of Visual Basic. We discussed creating gradient backgrounds and drawing shapes in Chapter 15; now we ll look at three other features: opacity, regions, and alpha blending. These features are all demonstrated in the Graphics Effects form in the Graphics Features project on the companion CD. This form has three check boxes and a button. Each one applies a different graphics effect.

The first check box is Form Opacity. Setting this option makes the form semitransparent, as shown in Figure 18-5.

Figure 18-5

Semitransparent form.

The opacity property is a measure of opaqueness. It can take any value between 0 and 1. Setting the property to 1 makes the form solid, setting it to 0 makes the form invisible, and setting it to 0.5 makes the form semitransparent. A fun use of this property is to fade a form from invisible to fully solid. Click the Fade In button on the Graphics Effects Form to see a demonstration of this. Here is the code that fades the form from invisible to solid:

Private Sub FadeInButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles FadeInButton.Click    Dim d As Double    For d = 0 To 1 Step 0.01       Me.Opacity = d       Me.Refresh()       System.Threading.Thread.CurrentThread.Sleep(5)    Next    Me.HasOpacity.Checked = False End Sub

The For Next loop increments the variable d from 0 to 1. The code then sets the opacity to the value of d: 0.00, 0.01, 0.02, 0.03, and so on. The Refresh method forces the form to completely repaint, and the System.Threading.Thread.CurrentThread.Sleep method waits 5 milliseconds before continuing to fade the form in. While opacity may not be instrumental in solving any real-world business problems, it s a fun effect to add.

The second graphics feature this form demonstrates is regions. Setting the Donut Shape option changes the form from rectangular to a doughnut shape, as shown in Figure 18-6.

Figure 18-6

Form displayed using the Donut Shape option.

To change the region of a form, create a region from a set of graphics paths and apply this to the form s Region property. The following code shows how to do this:

Dim myPath As New Drawing2D.GraphicsPath() Dim bigDiameter As Integer = Me.ClientSize.Height Dim donutWidth As Integer = 80 myPath.AddEllipse(0, 0, bigDiameter, bigDiameter) myPath.AddEllipse(donutWidth, donutWidth, _    bigDiameter - (donutWidth * 2), _    bigDiameter - (donutWidth * 2)) Me.Region = New Region(myPath)

This code creates a GraphicsPath variable myPath. This variable is used to store one or more vector shapes. The sample above adds two ellipses to the variable. The first ellipsis is the outer ring of the doughnut. The second is the inner ring, the hole of the doughnut. Finally, the Region property of the form is set to the newly created path. This shape is a true doughnut if you click in the middle of the hole, the mouse clicks are sent to the window behind the doughnut form. This technique is very flexible. The number of different graphics paths and different shapes you can create are limited only by your imagination. For example, the following code changes the form s shape to the string VB.NET Rocks.

Dim myPath As New Drawing2D.GraphicsPath() myPath.AddString("VB.NET Rocks", Me.Font.FontFamily, Font.Bold, _ 80, New Point(0, 0), StringFormat.GenericDefault) Me.Region = New Region(myPath) 

The third graphics feature this form demonstrates is alpha blending. This technique allows you to paint text or pictures semitransparently. Figure 18-7 shows the effect of setting the Alpha Blending option on the Graphics Effects form.

Figure 18-7

Alpha blending.

Setting the check box enables a timer on the form. In the Timer.Tick event, two strings are painted onto the form: a blue VB.NET is painted first, and a red Rocks!! is painted over the top. Each is painted with a different alpha value that sets the transparency. The alpha value cycles from 0 to 256 and back to 0 each time the Timer.Tick event fires. The net effect is that the text on the form fades from VB.NET to Rocks!! to VB.NET again. Here is the code from the Timer.Tick event that does the alpha blending:

Static alpha As Integer = 0, upDown As Integer = 2 Dim g As Graphics Dim f As New Font("Arial Black", 60, FontStyle.Regular, _  GraphicsUnit.Pixel, 0) Dim tempBitmap As New Bitmap(300, 100) alpha += upDown If alpha <= 0 Then    upDown = -upDown    alpha = 0 ElseIf alpha >= 255 Then    upDown = -upDown    alpha = 255 End If g = Graphics.FromImage(tempBitmap) g.Clear(Me.BackColor) g.DrawString("VB.NET", f, New SolidBrush(Color.FromArgb(255 - alpha, _    Color.DarkBlue)), New RectangleF(0, 0, 300, 100)) g.DrawString("Rocks!!", f, New SolidBrush(Color.FromArgb(alpha, _    Color.Red)), 0, 0) g.Dispose() g = Me.CreateGraphics g.DrawImage(tempBitmap, 200, 10) g.Dispose() tempBitmap.Dispose() f.Dispose()

The alpha and upDown variables are static they retain their old value each time the event is called. Each time the event is called, alpha increases by 2 until it reaches 256, and then it decreases by 2 until it reaches 0 and starts all over again. A temporary bitmap called tempBitmap is created for drawing the string. The reason for drawing to a temporary bitmap first is to avoid screen flicker. The two strings are drawn with the opposite alpha values when VB.NET is drawn as solid, Rocks!! is drawn as transparent. Valid alpha values range from 0 (transparent) to 255 (opaque). Finally, the bitmap is drawn onto the form.

Notice that to draw onto the bitmap or form, we first have to create a graphics object:

g = Me.CreateGraphics g.DrawImage(tempBitmap, 200, 10)

This graphics object is the GDI+ graphics drawing surface that supports all of the advanced graphics methods. A new graphics object has to be created for each bitmap or form you want to draw onto. When you finish using the graphics object, it is important to destroy it using the Dispose method:

g.Dispose()

Windows XP Style Controls

The Windows XP user interface has a new look. The Windows common controls, such as buttons, group boxes, and tabbed dialog boxes, are softer in appearance they have curved edges and are highlighted with a pleasant orange outline when the mouse moves over them.

Although you can t add or edit Windows XP style controls using the Visual Basic .NET Windows Forms designer, you can configure your application to use Windows XP controls when it runs. In effect, these are normal controls that simply display differently at run time. The XPTheme sample on the companion CD shows how to use Windows XP themed controls, and the following steps describe how to add the Windows XP theme to a new application:

  1. Create a new Windows application. Add some controls.

  2. For every control that has a FlatStyle property, set the FlatStyle property to System, and Windows XP will use the default system style for the control.

  3. Compile your application as usual.

  4. Create a manifest file in the same directory as the application s executable (usually the bin directory). A manifest file is an XML file that contains run-time information about the application. Using Notepad, create a text file with the following constants:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1"  manifestVersion="1.0">    <dependency>       <dependentAssembly>          <assemblyIdentity             type="win32"             name="Microsoft.Windows.Common-Controls"             version="6.0.0.0"             processorArchitecture="X86"             publicKeyToken="6595b64144ccf1df"             language="*"          />       </dependentAssembly>    </dependency> </assembly>

  5. Save the file with the name <exename>.manifest. For example, if your application name is MyApp.exe, this file should be called MyApp.exe.manifest. The manifest works on an application-by-application basis rather than a file-by-file basis. Thus, you need to create only one manifest for the main EXE of the application, even if it contains many DLLs. When your application runs, Windows XP looks at the application manifest and uses the information it finds there to alter the application s execution. In this case, the manifest instructs Windows XP to use the latest version of the common controls library the version with the Windows XP style controls in it.

Figure 18-8 shows a form with standard controls on the left and the same forms with Windows XP controls on the right.

Figure 18-8

Normal controls (above) and Windows XP controls (below).



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

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