Creating a Drawing Application by Using WPF
In this lesson, you will learn how to create a Windows Presentation Foundation (WPF) application that enables you to draw pictures. Creating a WPF application is like creating a Windows Forms application. You drag controls from the Toolbox to the design surface and then write code to handle the events of the controls.
For a video version of this topic, see Video How to: Creating a Drawing Application by Using WPF.
To create a WPF application
On the File menu, click New Project.
The New Project dialog box appears. This dialog box lists the default application types that you can create by using Visual Basic Express.
Select WPF Application as the project type.
Change the name of your application to Ink Pad and then click OK.
Visual Basic Express creates a new folder for your project. The folder has the same name as the project title. Visual Basic Express also displays your new WPF window, titled Window1, in Design view. You can go to the Code Editor at any time by right-clicking the design surface and clicking View Code. By default, the XAML editor is displayed under the designer, but you can see the XAML view in full screen by right-clicking the design surface and clicking View XAML.
To design the user interface
If you cannot see the Properties window, on the View menu, click Properties Window. This window lists the properties of the currently selected form or control. You can change the existing property values in this window.
Change the size of the WPF window by setting the Height property to 550 and the Width property to 370 in the Properties window.
Change the title property of the WPF window to Ink Pad.
Change the Background property of the WPF window to the color brown by clicking Brown in the drop-down box and then pressing ENTER.
You can alternatively modify the XAML markup directly by adding a Background attribute and setting its value to Brown: Background="Brown"
To open the Toolbox, on the View menu, click Toolbox.
Right-click the Toolbox, and then click Choose Items.
The Choose Toolbox Items dialog box opens.
On the WPF Components tab of the Choose Toolbox Items dialog box, scroll down to InkCanvas and select it so that a check appears in the check box.
Click OK to add the InkCanvas control to the Toolbox.
Drag an InkCanvas control from the Toolbox to the design surface.
Set the following properties of the InkCanvas control in the Properties window:
9, 9, 9, 68
Change the color of the InkCanvas control to yellow by setting its Background property to LightYellow.
The background color of the InkCanvas control will appear light yellow at run-time.
Drag two Button controls to the WPF window, positioning them under the InkCanvas control. Place button1 on the left and button2 on the right.
Select button1 and change the XAML markup in XAML view as shown in the following markup. This markup sets the Text property to Clear.
<Button Height="23" HorizontalAlignment="Left" Margin="85,0,0,24" Name="Button1" VerticalAlignment="Bottom" Width="75">Clear</Button>
Select button2 and change the XAML markup as shown in the following markup. This markup sets the Text property to Close.
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,72,24" Name="Button2" VerticalAlignment="Bottom" Width="75">Close</Button>
The WPF application should resemble the Ink Pad application in the following illustration.
To add code to the event handlers for the buttons
Double-click Clear, and then add the following code to the generated Click event handler:
Return to Design view by right-clicking the Code Editor and clicking Designer.
Double-click Close, and then add the following code to the generated Click event handler:
Press F5 to run the project.
When the application starts, draw a picture in the InkCanvas control. If you make a mistake, you can click Clear to start over.
Click Close to the exit the application.
In this lesson, you learned how to create a drawing application by using WPF.
In the next section, you will learn how to use the Visual Basic tools to find and fix errors (typically called bugs) in a program.