logo
Учебник_ПОА

Compiling the Code

You can compile the example at a command prompt or paste the code into a console application by using the IDE. In the latter case, you must reference the System.Drawing.dll file.

Replace "c:\test.bmp", "c:\test.jpg", "c:\test.gif" and c:\test.png with the actual file name.

Преобразование изображений из одного формата в другой

В этом примере показана загрузка изображения и его сохранение в нескольких различных графических форматах.

Пример28

-----

Компиляция кода

Пример можно скомпилировать в командной строке либо вставить код в консольное приложение с помощью IDE. В последнем случае необходимо добавить ссылку на файл System.Drawing.dll.

Замените "c:\test.bmp", "c:\test.jpg", "c:\test.gif" and c:\test.png фактическим именем файла.

Customizing, Displaying, and Printing Windows Forms

This topic provides links to topics that show you how to perform tasks specific to Windows Forms, such as customizing the shape and color of a form, and displaying and printing forms.

How to: Change the Background Color of a Form

This example changes the background color of a Windows Form programmatically.

Example

private void Form1_Click(object sender, EventArgs e)

{

this.BackColor = System.Drawing.Color.DarkBlue;

}

Compiling the Code

This example requires:

How to: Create a Shaped Form

The following example gives a form an elliptical shape.

Example

System.Drawing.Drawing2D.GraphicsPath shape =

new System.Drawing.Drawing2D.GraphicsPath();

shape.AddEllipse(0, 0, this.Width, this.Height);

this.Region = new System.Drawing.Region(shape);

Compiling the Code

To use this code, copy it to the Form1_Load event handler.

The Region property of the Form class is an advanced member.

Настройка, отображение и печать Windows Forms

В данном разделе приводятся ссылки на разделы, содержащие сведения о выполнении определенных задач в Windows Forms, таких как настройка вида и цвета формы, отображение и печать форм.

Изменение цвета фона формы

В этом примере программно изменяется цвет фона формы Windows Forms.

Пример

private void Form1_Click(object sender, EventArgs e)

{

this.BackColor = System.Drawing.Color.DarkBlue;

}

Компиляция кода

Для этого примера необходимы следующие компоненты.

Создание сложной формы

В следующем примере создается форма в виде эллипса.

Пример

System.Drawing.Drawing2D.GraphicsPath shape =

new System.Drawing.Drawing2D.GraphicsPath();

shape.AddEllipse(0, 0, this.Width, this.Height);

this.Region = new System.Drawing.Region(shape);

Компиляция кода

Чтобы использовать этот код, скопируйте его в обработчик событий Form1_Load.

Свойство Region класса Form является дополнительным элементом.

How to: Get a Value from Another Form

This example retrieves a value from a text box on a Windows Form and displays it in a text box on another form.

Example

// In Form1.cs.

private Form2 otherForm = new Form2();

private void GetOtherFormTextBox()

{

textBox1.Text = otherForm.TextBox1.Text;

}

private void button1_Click(object sender, EventArgs e)

GetOtherFormTextBox();

}

Compiling the Code

This example requires:

Получение значения из другой формы

В этом примере извлекается значение из текстового поля в одной форме Windows Forms и отображается в текстовом поле в другой форме.

Пример29

// In Form1.cs.

private Form2 otherForm = new Form2();

private void GetOtherFormTextBox()

{

textBox1.Text = otherForm.TextBox1.Text;

}

private void button1_Click(object sender, EventArgs e)

{

GetOtherFormTextBox();

}

Компиляция кода

Для этого примера необходимы следующие компоненты.

How to: Display One Form from Another

This example displays a second form from a Windows Form.

This example requires two Windows Forms named Form1 and Form2.

Procedure

To create Form1

  1. Create a Windows Forms Application, and name it Form1.

  2. Add a Button control to the form, and name it button1.

  3. Add the Form2 class to the namespace, and set the Click event handler of button1 as shown in the following code.

When you click the button, Form2 will be displayed.

Example

private void button1_Click(object sender, System.EventArgs e)

{

Form2 frm = new Form2();

frm.Show();

}

// Create Form2.

public class Form2: Form

{

public Form2()

{

Text = "Form2";

}

}

Отображение одной формы из другой

Этот пример отображает вторую форму из формы Windows Forms.

Для данного примера требуются две формы Windows Forms с именами Form1 и Form2.

Процедура

Создание формы Form1

  1. Создайте приложение Windows Forms и назовите его Form1.

  2. Добавьте в форму элемент управления Button и присвойте ему имя button1.

  3. В пространство имен добавьте класс Form2 и задайте обработчик событий Click для button1, как показано в следующем коде.

При нажатии кнопки будет отображена Form2.

Пример30

---------

Creating WPF Applications

This following topics show you how to perform tasks specific to Windows Presentation Foundation (WPF) applications, such as designing a user interface with WPF controls and writing event handlers.

Designing a User Interface for a WPF Application

You can design a user interface for a Windows Presentation Foundation (WPF) application just as you can for a Windows Form application. You drag controls from the Toolbox to the design surface. The integrated development environment (IDE) is different for WPF applications. In addition to having a Properties window and Toolbox, the WPF IDE has a XAML editor. XAML is an extensible application markup language that can be used to create a user interface. The following illustration shows the location of the XAML editor.