GDI Brush |
A brush is used to fill an object. The most popular brush is the solid brush which requires of a color to be created. In the code below a blue brush is created and selected to draw a rectangle using Microsoft GDI. Una brocha permite pintar el interior de un objeto. La brocha más común es la brocha sólida la cual requiere de un color para ser creada. Como se muestra en el ejemplo de abajo una brocha azul se crea y se selecciona para dibujar un rectángulo usando Microsoft GDI. |
Program.cpp |
void Graficos::Window_Paint(Win::Event& e) { CG::Gdi gdi(hWnd, true, false); CG::Brush brushSolid; brushSolid.CreateSolid(RGB(190, 200, 255)); // A solid blue brush is created gdi.Select(brushSolid); // All figures draw after this point will be blue gdi.Rectangle(10, 5, 100, 200); } |
Tip |
In the previous example, the border of the rectangle is drawn using the default black pen. If you do not want to draw the border of the rectangle, the NULL pen can be used as shown in the code below. En el ejemplo previo, el borde del rectángulo se dibuja usando la pluma de defecto negra. Si usted no quiere dibujar el borde del rectángulo, la pluma nula puede ser usada como se muestra en el código de abajo. |
Program.cpp |
void Graficos::Window_Paint(Win::Event& e) { CG::Gdi gdi(hWnd, true, false); CG::Brush brushSolid; brushSolid.CreateSolid(RGB(190, 200, 255)); gdi.Select(brushSolid); gdi.SelectNullPen(); gdi.Rectangle(10, 5, 100, 200); } |
A Hatch Brush |
A hatch brush creates a brush that fills an object using patterns as shown below. The code below creates a pattern red brush using Microsoft GDI. Una hatch brocha permite crear una brocha que dibuja patrones preestablecidos como se muestra debajo. El ejemplo de abajo crea una brocha de cruces de color rojo usando Microsoft GDI. |
Program.cpp |
void Program::Window_Paint(Win::Event& e) { CG::Gdi gdi(hWnd, true, false); CG::Brush brush; brush.CreateHatch(HS_CROSS, RGB(200, 0, 0)); gdi.Select(brush); gdi.Rectangle(20, 10, 150, 260); } |
NULL or Hollow Brush |
Microsoft GDI has several stock brushes that are ready to use, they are: the black brush, the white brush, and the null brush. When a window or any area of the screen is painted using the null brush, nothing is painted. The null brush can be used to paint only the border of an object. In the code shown below, the border of the rectangle is painted using a green pen without affecting the interior of the rectangle. Microsoft GDI tiene varias brochas disponibles las cuales son: la brocha negra, la brocha blanca y la brocha nula. Cuando una ventana o un área de la pantalla son pintadas con la brocha nula no hay ningún efecto. Eso es extremadamente útil para pintar solamente el contorno de una figura. Por ejemplo, si se desea dibujar el contorno de un rectángulo se usa la brocha nula para que el relleno no se altere. |
Program.h |
void Graficos::Window_Paint(Win::Event& e) { CG::Gdi gdi(hWnd, true, false); CG::Pen pen; pen.Create(PS_SOLID, 1, RGB(0, 100, 0)); gdi.Select(pen); gdi.SelectNullBrush(); gdi.Rectangle(10, 5, 100, 200); } |
Problem 1 |
Create a Window Application called Sweden to paint the flag of Sweden using Microsoft GDI. To change the window background color, it is necessary to create a blue brush and use this brush when creating the main window as shown in the code below. Cree una applicación de ventana llamada Sweden para dibujar la bandera de Suecia usando Microsoft GDI. Para cambiar el color de fondo de la ventana, es necesario crear una brocha azul y usarla cuando se crea la ventana principal como se muestra en el código de abajo. |
Sweden.cpp |
#include "stdafx.h" //________________________________________ Sweden.cpp #include "Sweden.h" int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE , LPTSTR cmdLine, int cmdShow){ Sweden app; CG::Brush blueBrush(RGB(0, 0, 198)); app.CreateMainWindow(L"Sweden", cmdShow, IDI_SWEDEN, NULL, blueBrush, hInstance); return app.MessageLoop(IDC_SWEDEN); } void Sweden::Window_Open(Win::Event& e) { } void Sweden::Window_Paint(Win::Event& e) { CG::Gdi gdi(hWnd, true, false); CG::Brush brushSolid(RGB(255, 255, 0)); . . . } |
Problem 2 |
Create a Window Application called SwedenD to paint the flag of Sweden using Microsoft Direct2D. Cree una applicación de ventana llamada SwedenD para dibujar la bandera de Suecia usando Microsoft Direct2D. |
SwedenD.h |
#pragma once //______________________________________ SwedenD.h #include "Resource.h" class SwedenD: public Win::Window { public: SwedenD() { } ~SwedenD() { } Direct::Graphics graphics; Direct::SolidBrush brushYellow; . . . }; |
SwedenD.cpp |
. . . int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE , LPTSTR cmdLine, int cmdShow){ SwedenD app; app.CreateMainWindow(L"SwedenD", cmdShow, IDI_SwedenD, NULL, (HBRUSH)::GetStockObject(NULL_BRUSH), hInstance); return app.MessageLoop(IDC_SwedenD); } void SwedenD::Window_Open(Win::Event& e) { brushYellow.Create(graphics, . . .); // red, green, blue, alpha } void SwedenD::Window_Paint(Win::Event& e) { if (graphics.BeginDraw(hWnd, width, height, . . .) == false) return; // red, green, blue, alpha graphics.SetTransform(D2D1::Matrix3x2F::Identity()); D2D1_SIZE_F size = graphics.GetSize(); D2D1_RECT_F rect; //____________________________________ 1. Horizontal yellow rectangle rect.left = . . . rect.top = . . . rect.right = . . . rect.bottom = . . . graphics.FillRectangle(rect, brushYellow); //____________________________________ 2. Vertical yellow rectangle . . . // graphics.EndDraw(true); } |
Problem 3 |
Create a Window Application called Norway to paint the flag of Norway using Microsoft GDI. Cree una aplicación de ventana llamado Norway para dibujar la bandera de Norway usando Microsoft GDI. |
Problem 4 |
Create a Window Application called NorwayD to paint the flag of Sweden using Microsoft Direct 2D. Cree una applicación de ventana llamada NorwayD para dibujar la bandera de Suecia usando Microsoft Direct 2D. |
NorwayD.h |
#pragma once //______________________________________ NorwayD.h #include "Resource.h" class NorwayD: public Win::Window { public: NorwayD() { } ~NorwayD() { } Direct::Graphics graphics; Direct::SolidBrush brushRed; Direct::SolidBrush brushBlue; . . . }; |
NorwayD.cpp |
. . . int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE , LPTSTR cmdLine, int cmdShow){ NorwayD app; app.CreateMainWindow(L"NorwayD", cmdShow, IDI_NorwayD, NULL, (HBRUSH)::GetStockObject(NULL_BRUSH), hInstance); return app.MessageLoop(IDC_NorwayD); } void NorwayD::Window_Open(Win::Event& e) { brushRed.Create(graphics, . . .); // red, green, blue, alpha brushBlue.Create(graphics, . . .); // red, green, blue, alpha } void NorwayD::Window_Paint(Win::Event& e) { if (graphics.BeginDraw(hWnd, width, height, . . .) == false) return; // red, green, blue, alpha graphics.SetTransform(D2D1::Matrix3x2F::Identity()); D2D1_SIZE_F size = graphics.GetSize(); D2D1_RECT_F rect; //_____________________________________________ 1. Blue rectangles //________________________ vertical . . . graphics.FillRectangle(rect, brushBlue); //________________________ horizontal . . . graphics.FillRectangle(rect, brushBlue); //_____________________________________________ 2. Red rectangles . . . // graphics.EndDraw(true); } |