Tải bản đầy đủ - 0 (trang)
7-22. Display WPF Controls in Windows Forms

7-22. Display WPF Controls in Windows Forms

Tải bản đầy đủ - 0trang

CHAPTER 7 ■ WINDOWS FORMS



Figure 7-14. Using ElementHost in Visual Studio

If you do not want to use a user control, then you will need to populate the ElementHost control

programmatically by assigning the desired WPF element to the Child property of the ElementHost

control.



The Code

The following example demonstrates how to integrate WPF controls into a Windows Forms application.

The example (shown in Figure 7-15) uses a simple WPF user control consisting of a System.Windows.

Shapes.Ellipse that can change between red and blue color gradients. This EllipseControl is assigned

to one ElementHost using the Visual Studio form builder. Another ElementHost is populated

programmatically with a System.Windows.Controls.TextBox. A standard Windows Forms button triggers

the EllipseControl to change color, and then writes a log entry to the TextBox. Here is the XAML for the

WPF user control:


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Height="300" Width="300">






Center="0.5,0.5" GradientOrigin="0.05,0.5">










Center="0.5,0.5" GradientOrigin="0.05,0.5">













364



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS




Fill="{StaticResource RedBrush}">







Here is the code-behind for the EllipseControl, which is used to control and query its current color

gradient:

using System.Windows.Controls;

using System.Windows.Media;

namespace Apress.VisualCSharpRecipes.Chapter07

{

///

/// Interaction logic for EllipseControl.xaml

///


public partial class EllipseControl : UserControl

{

public EllipseControl()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-22.Designer.cs.

InitializeComponent();

}

// Gets the name of the current color.

public string Color

{

get

{

if (Ellipse1.Fill == (Brush)Grid1.Resources["RedBrush"])

{

return "Red";

}

else

{

return "Blue";

}

}

}

// Switch the fill to the red gradient.

public void ChangeColor()

{

// Check the current fill of the ellipse.

if (Ellipse1.Fill == (Brush)Grid1.Resources["RedBrush"])

{

// Ellipse is red, change to blue.

Ellipse1.Fill = (Brush)Grid1.Resources["BlueBrush"];

}



365



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



else

{

// Ellipse is blue, change to red.

Ellipse1.Fill = (Brush)Grid1.Resources["RedBrush"];

}

}

}

}

The following is the code-behind for the main Windows Forms form. The form constructor

demonstrates the programmatic creation and configuration of an ElementHost control to display a

standard WPF TextBox control. The button1_Click method is invoked when the user clicks the button,

and it changes the color of the ellipse and appends a message to the content of the TextBox. The rest of

the application code generated by Visual Studio is not shown here, but is provided in the sample code

(available on the book’s page on the Apress web site, www.apress.com).

using

using

using

using

using



System;

System.Windows;

System.Windows.Forms;

WPFControls=System.Windows.Controls;

System.Windows.Forms.Integration;



namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_22: Form

{

WPFControls.TextBox textBox;

public Recipe07_22 ()

{

InitializeComponent();

// Create a new WPF TextBox control.

textBox = new WPFControls.TextBox();

textBox.Text = "A WPF TextBox\n\r\n\r";

textBox.TextAlignment = TextAlignment.Center;

textBox.VerticalAlignment = VerticalAlignment.Center;

textBox.VerticalScrollBarVisibility =

WPFControls.ScrollBarVisibility.Auto;

textBox.IsReadOnly = true;

// Create a new ElementHost to host the WPF TextBox.

ElementHost elementHost2 = new ElementHost();

elementHost2.Name = "elementHost2";

elementHost2.Dock = DockStyle.Fill;

elementHost2.Child = textBox;

elementHost2.Size = new System.Drawing.Size(156, 253);

elementHost2.RightToLeft = RightToLeft.No;



366



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



// Place the new ElementHost in the bottom-left table cell.

tableLayoutPanel1.Controls.Add(elementHost2, 1, 0);

}

private void button1_Click(object sender, EventArgs e)

{

// Change the ellipse color.

ellipseControl1.ChangeColor();

// Get the current ellipse color and append to TextBox.

textBox.Text +=

String.Format("Ellipse color changed to {0}\n\r",

ellipseControl1.Color);

textBox.ScrollToEnd();

}

}

}



Figure 7-15. Using WPF controls in a Windows Forms form



367



www.it-ebooks.info



www.it-ebooks.info



CHAPTER 8

■■■



Graphics, Multimedia,

and Printing

Graphics, video, sound, and printing are the hallmarks of a traditional rich client on the Microsoft

Windows operating system. When it comes to multimedia, the Microsoft .NET Framework delivers a

compromise, providing support for some of these features while ignoring others. For example, you will

find a sophisticated set of tools for two-dimensional drawing and event-based printing with GDI+ and

the types in the System.Drawing namespaces. These classes wrap GDI32.dll and USER32.dll, which

provide the native graphics device interface (GDI) functions in the Windows application programming

interface (API), and they make it much easier to draw complex shapes, work with coordinates and

transforms, and process images. On the other hand, if you want to show a video file or get information

about the current print jobs, you will need to look beyond the .NET Framework.

This chapter presents recipes that show you how to use built-in .NET features and, where necessary,

native Win32 libraries via P/Invoke or COM Interop. The recipes in this chapter describe how to do the

following:





Find the fonts installed in your system (recipe 8-1)







Perform hit testing with shapes (recipe 8-2)







Create an irregularly shaped form or control (recipe 8-3)







Create a sprite that can be moved around (recipe 8-4)







Display an image that could be made to scroll (recipe 8-5), learn how to capture

the image of the desktop (recipe 8-6), and create a thumbnail for an existing image

(recipe 8-8)







Enable double buffering to increase performance while redrawing (recipe 8-7)







Play a beep or a system-defined sound (recipe 8-9), play a WAV file (recipe 8-10),

play a non-WAV file such as an MP3 file (recipe 8-11), and play an animation with

DirectShow (recipe 8-12)







Retrieve information about the printers installed on the machine (recipe 8-13),

print a simple document (recipe 8-14), print a document that has multiple pages

(recipe 8-15), print wrapped text (recipe 8-16), show a print preview (recipe 8-17),

and manage print jobs (recipe 8-18)



369



www.it-ebooks.info



CHAPTER 8 ■ GRAPHICS, MULTIMEDIA, AND PRINTING







Perform text-to-speech (TTS) (recipe 8-19)







Perform optical character recognition (OCR) to find words in an image (recipe 820).



8-1. Find All Installed Fonts

Problem

You need to retrieve a list of all the fonts installed on the current computer.



Solution

Create a new instance of the System.Drawing.Text.InstalledFontCollection class, which contains a

collection of FontFamily objects representing all the installed fonts.



How It Works

The InstalledFontCollection class allows you to retrieve information about currently installed fonts. It

derives from the FontCollection class, which allows you to get a list of font families as a collection in the

Families property.



The Code

The following code shows a form that iterates through the font collection when it is first created. Every

time it finds a font, it creates a new Label control that will display the font name in the given font face (at

a size of 14 points). The Label is added to a Panel control named pnlFonts with AutoScroll set to true,

allowing the user to scroll through the list of available fonts.

using

using

using

using



System;

System.Drawing;

System.Windows.Forms;

System.Drawing.Text;



namespace Apress.VisualCSharpRecipes.Chapter08

{

public partial class Recipe08_01: Form

{

public Recipe08_01()

{

InitializeComponent();

}

private void Recipe08_01_Load(object sender, EventArgs e)

{



370



www.it-ebooks.info



CHAPTER 8 ■ GRAPHICS, MULTIMEDIA, AND PRINTING



// Create the font collection.

using (InstalledFontCollection fontFamilies =

new InstalledFontCollection())

{

// Iterate through all font families.

int offset = 10;

foreach (FontFamily family in fontFamilies.Families)

{

try

{

// Create a label that will display text in this font.

Label fontLabel = new Label();

fontLabel.Text = family.Name;

fontLabel.Font = new Font(family, 14);

fontLabel.Left = 10;

fontLabel.Width = pnlFonts.Width;

fontLabel.Top = offset;

// Add the label to a scrollable Panel.

pnlFonts.Controls.Add(fontLabel);

offset += 30;

}

catch

{

//

//

//

//

}



An error will occur if the selected font does

not support normal style (the default used when

creating a Font object). This problem can be

harmlessly ignored.



}

}

}

}

}

Figure 8-1 shows this simple test application.



371



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-22. Display WPF Controls in Windows Forms

Tải bản đầy đủ ngay(0 tr)

×