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

  size.Width, size.Height);

 g.DrawEllipse(p, rc.Left, rc.Bottom - size.Height,

  size.Width, size.Height);


 g.DrawLine(p, rc.Left, rc.Bottom - size.Height / 2,

  rc.Left, rc.Top + size.Height / 2);

 g.FillEllipse(fillBrush. rc.Left, rc.Top, size.Width, size.Height);

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


 // заполняем прямоугольник, скрывая внутренние эллипсы

 g.FillPolygon(fillBrush, points);

 // освобождаем ресурсы

 fillBrush.Dispose();

}


private void butDrawRoundedRectangle_Click(object sender, EventArgs e) {

 Graphics g = CreateGraphics();

 Rectangle rc = new Rectangle(10, 10, 200, 50);

 DrawRoundedRectangle(g,

  new Pen(Color.Black), Color.CadetBlue, rc, new Size(8, 8));

}

Результат работы этого кода показан на рис. 6.8.

Рис. 6.8. Отображение закрашенного прямоугольника со скругленными углами

Создание экранных снимков

Если при работе с мобильным устройством необходимо сделать скриншоты, то для реализации замысла необходимо использовать внешние устройства. Конечно, можно просто сфотографировать экран, но настоящий программист будет использовать функции Windows API. В этом разделе главы будет рассматриваться пример копирования определенной области окна, всего рабочего окна программы или любого другого окна. Для демонстрации примера надо разместить на форме список, три кнопки и один таймер. Сам код приведен в листинге 6.20.

Листинг 6.20

[DllImport("coredll.dll", EntryPoint = "GetDesktopWindow")]

public static extern IntPtr GetDesktopWindow();

[DllImport("coredll.dll", EntryPoint = "GetDC")]

public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("coredll.dll", EntryPoint = "ReleaseDC")]

public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll.dll")]

public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest,

 int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

 const int SRCCOPY = 0x00CC0020;


private void screenshot(string filename, Graphics gx, Rectangle rect) {

 Bitmap bmp = new Bitmap(rect.Width, rect.Height);

 Graphics g = Graphics.FromImage(bmp);

 BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, gx.GetHdc(),

  rect.Left, rect.Top, SRCCOPY);

 bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);

 bmp.Dispose();

 g.Dispose();

}


private void butPartOfWindow_Click(object sender, EventArgs e) {

 // Делаем снимок списка

 ScreenShot(@"\My Documents\save.bmp", this.CreateGraphics(),

  listBox1.Bounds);

}


private void butScreen_Click(object sender, EventArgs e) {

 // Делаем снимок экрана

 Rectangle rect = new Rectangle(0,0,240,240);

 Bitmap bmp = new Bitmap(rect.Width, rect.Height);

 Graphics g = Graphics.FromImage(bmp);

 IntPtr hwnd = GetDesktopWindow();

 IntPtr hdc = GetDC(hwnd);

 BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, hdc, rect.Left,

  rect.Top, SRCCOPY);

 bmp.Save(@"\My Documents\screen.bmp",

 System.Drawing.Imaging.ImageFormat.Bmp);

 // Освобождаем ресурсы

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

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

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

Стивен Прата

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