Читаем Программирование КПК и смартфонов на .NET Compact Framework полностью

  new Rectangle(0, 0, imgCat.Width, imgCat.Height), GraphicsUnit.Pixel);


 ImageAttributes attr = new ImageAttributes();

 // Устанавливаем красный цвет как прозрачный

 attr.SetColorKey(Color.Red, Color.Red);

 // Выводим вторую картинку с установленными атрибутами

 Rectangle dstRect = new Rectangle(50, 120, imgName.Width, imgName.Height);

 g.DrawImage(imgName, dstRect, 0, 0,

  imgName.Width, imgName.Height, GraphicsUnit.Pixel.attr);

 g.Dispose();

}

ВНИМАНИЕ

He забудьте импортировать пространство имен System.Drawing.Imaging при работе с этим примером.

Если просто наложить одну картинку на другую, то результат будет, мягко говоря, не очень красивым (рис. 6.5).

Рис. 6.5. Неудачный вариант наложения двух картинок

Если же воспользоваться методом SetColorKey для установки прозрачного цвета, то результат наложения двух изображений будет выглядеть достойно (рис. 6.6).

Рис. 6.6. Наложение картинки с использованием прозрачности

Округленные прямоугольники

Так как .NET Compact Framework не позволяет создавать округленные прямоугольники встроенными средствами, то необходимо самостоятельно реализовать эту задачу. В этом разделе будет рассматриваться решение, предложенное Алексом Яхниным (Alex Yakhnin) в его блоге blog.opennetcf.org/ayakhnin/. Для достижения заданного эффекта надо нарисовать серию линий, которые соединяют эллипсы, и закрасить внутреннюю область сплошным цветом (рис. 6.7).

Рис. 6.7. Создание прямоугольника со скругленным углами

Соответствующий код приведен в листинге 6.19.

Листинг 6.19

public static void DrawRoundedRectangle(Graphics g, Pen p, Color backColor,

 Rectangle rc, Size size) {

 Point[] points = new Point[8];

 // подготовим точки для фигуры

 points[0].X = rc.Left + size.Width / 2;

 points[0].Y = rc.Top + 1;

 points[1].X = rc.Right - size.Width / 2;

 points[1].Y = rc.Top + 1;

 points[2].X = rc.Right;

 points[2].Y = rc.Top + size.Height / 2;

 points[3].X = rc.Right;

 points[3].Y = rc.Bottom - size.Height / 2;

 points[4].X = rc.Right - size.Width / 2;

 points[4].Y = rc.Bottom;

 points[5].X = rc.Left + size.Width / 2;

 points[5].Y = rc.Bottom;

 points[6].X = rc.Left + 1;

 points[6].Y = rc.Bottom - size.Height / 2;

 points[7].X = rc.Left + 1;

 points[7].Y = rc.Top + size.Height / 2;


 // приготовим кисть для фона

 Brush fillBrush = new SolidBrush(backColor);

 // рисуем отрезки и круги для округленного прямоугольника

 g.DrawLine(p, rc.Left + size.Width / 2, rc.Top,

  rc.Right - size.Width / 2, rc.Top);

 g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Top,

  size.Width, size.Height);

 g.DrawEllipse(p, rc.Right - size.Width, rc.Top, size.Width, size.Height);

 g.DrawLine(p, rc.Right, rc.Top + size.Height / 2, rc.Right,

  rc.Bottom - size.Height /2);

 g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Bottom - size.Height,

  size.Width, size.Height);

 g.DrawEllipse(p, rc.Right - size.Width, rc.Bottom - size.Height,

  size.Width, size.Height);


 g.DrawLine(p, rc.Right - size.Width / 2, rc.Bottom,

  rc.Left + size.Width / 2, rc.Bottom);

 g.FillEllipse(fillBrush, rc.Left, rc.Bottom - size.Height,

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных