Brush types

There are mainly three types of brushes.

  1. Solid brush - Solid color filling
  2. Hash brush - Filling with Hash lines
  3. Pattern brush - Filling with a patthern image

Brush Win32 APIs

These are raw Win32 APIs for creating a handle to the brush. These APIs take verious attributes of the brush and returns the handle of the brush object.

  • CreateSolidBrush - Creates a brush with a solid color
  • CreateHatchBrush - Creates a brush with a hatch pattern and color
  • CreatePatternBrush - Creates a brush with a bitmap pattern
  • CreateDIBPatternBrushPt - Creates a brush with the pattern from a DIB
  • CreateBrushIndirect - Creates a brush with a specified style, color, and pattern

Note: CreateBrushIndirect is a single API create solid, hatch and pattern brushes. This takes a generic LOGBRUSH structure as an input. Style, color, pattern and attributes are the given in different members of this structure. CreateBrushIndirect call thus can replace CreateSolidBrush, CreateHatchBrush and CreatePatternBrush calls.

Device context can associate a brush to the drawing context using SelectObject() API. Brush object can be released from memory uing DeleteObject() call.

Solid Brush

Single RGB color is required to create a solid brush. Thus a single paramater is required. CreateSolidBrush API function cal be called or CBrush MFC class can be used. Brush can be created using overloaded constructor or calling CreateSolidBrush() member function.

class CMyWnd : public CFrameWnd
{
  public:
    CMyWnd()
    {
      Create(NULL, _T("Brush Demo"));
    }
 
    void OnPaint();
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_CLOSE()
END_MESSAGE_MAP()

void CMyWnd::OnPaint()
{

	CBrush Brush;
	Brush.CreateSolidBrush(RGB(232,166,153));
	
	PAINTSTRUCT ps;
	RECT rect;
	GetClientRect(&rect);
	CDC *dc = BeginPaint(&ps);
	dc->SelectObject(&Brush);
	dc->Rectangle(&rect);
	dc->SetBkColor(RGB(232,166,153));
	dc->DrawText("Brush Demo",10,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);

	EndPaint(&ps);
}
class CMyApp : public  CWinApp 
{
  public:
     virtual BOOL InitInstance()
    {
      m_pMainWnd = new CMyWnd();
      m_pMainWnd->ShowWindow(SW_SHOW);
      m_pMainWnd->UpdateWindow();
      m_pMainWnd->SetWindowPos(NULL,0,0,225,140,SWP_NOZORDER|SWP_NOMOVE);
      return TRUE;
    }
 };
CMyApp app;

Solid Brush output

BS_SOLID

Hatch Brush

Hatch Brush requires hatch line style and a solid color. Thus two paramaters are required. CreateHatchBrush API function cal be called or CBrush MFC class can be used. This brush is created using overloaded constructor or calling CreateHatchBrush() member function.

/* Hatch Styles */
#define HS_HORIZONTAL       0       /* ----- */
#define HS_VERTICAL         1       /* ||||| */
#define HS_FDIAGONAL        2       /* \\\\\ */
#define HS_BDIAGONAL        3       /* ///// */
#define HS_CROSS            4       /* +++++ */
#define HS_DIAGCROSS        5       /* xxxxx */

void CMyWnd::OnPaint()
{

	CBrush Brush;
	Brush.CreatePatternBrush(HS_HORIZONTAL, RGB(232,166,153));
	
	PAINTSTRUCT ps;
	RECT rect;
	GetClientRect(&rect);
	CDC *dc = BeginPaint(&ps);
	dc->SelectObject(&Brush);
	dc->Rectangle(&rect);
	dc->SetBkColor(RGB(232,166,153));
	dc->DrawText("Brush Demo",10,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
	EndPaint(&ps);
}

Hatch Brush output

HS_HORIZONTALHS_VERTICAL
HS_HORIZONTAL HS_VERTICAL
HS_FDIAGONALHS_BDIAGONAL
HS_FDIAGONAL HS_BDIAGONAL
HS_CROSSHS_DIAGCROSS
HS_CROSS HS_DIAGCROSS

Pattern Brush

Pattern Brush, as the name suggests is a brush with a bitmap pattern. It takes a single parameter of a bitmap handle. CreatePatternBrush API function can be called or CBrush MFC class can be used. This brush is created using overloaded constructor which takes a CBitmap or CreatePatternBrush() member function.

void CMyWnd::OnPaint()
{

	CBrush Brush;
	Brush.CreateSolidBrush(RGB(232,166,153));
	
	PAINTSTRUCT ps;
	RECT rect;
	GetClientRect(&rect);
	CDC *dc = BeginPaint(&ps);
	dc->SelectObject(&Brush);
	dc->Rectangle(&rect);
	dc->SetBkColor(RGB(232,166,153));
	dc->DrawText("Brush Demo",10,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);

	EndPaint(&ps);
}

Pattern Brush output

BS_PATTERN

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#