Brushes

An artist takes different brushes to fill or draw different objects. A brush in Win32 is a graphical tool that a Win32-based application uses to fill the interior of polygons, ellipses, and paths.

Applications and usage

Drawing applications use brushes to paint shapes; word processing applications use brushes to paint rules; computer-aided design (CAD) applications use brushes to paint the interiors of cross-section views; and spreadsheet applications use brushes to paint the sections of pie charts and the bars in bar graphs.

Brush APIs

Brush is a GDI object. Each device context should associate a brush object for drawing. However there are default system brush for each screen and printer devices. Windows GDI subsystem returns a handle to the brush or HBRUSH for all create brush functions. SelectObject() the generic API for associating brush to a device context. DeleteObject() can be called for cleanup the brush object from memory.

  • 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
  • GetSysColorBrush - Gets a handle to a brush that corresponds to a color index

Brush class

MFC wraps these above API and HBRUSH member in a class called CBrush. Attributes of brush can be given in overloaded constructors or corresponding functions can be called. Here is the prototype and public member fucntions.

class CBrush : public CGdiObject
{
	DECLARE_DYNAMIC(CBrush)

public:

	CBrush();
	CBrush(COLORREF crColor);           /* CreateSolidBrush */
	CBrush(int nIndex, COLORREF crColor); /* CreateHatchBrush */
	CBrush(CBitmap* pBitmap);          /* CreatePatternBrush */

	BOOL CreateSolidBrush(COLORREF crColor);
	BOOL CreateHatchBrush(int nIndex, COLORREF crColor);
	BOOL CreateBrushIndirect(const LOGBRUSH* lpLogBrush);
	BOOL CreatePatternBrush(CBitmap* pBitmap);
	BOOL CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage);
	BOOL CreateDIBPatternBrush(const void* lpPackedDIB, UINT nUsage);
	BOOL CreateSysColorBrush(int nIndex);

	operator HBRUSH() const;
	int GetLogBrush(LOGBRUSH* pLogBrush);

};

/* Example of how to use CBrush */
void CMyWnd::OnPaint()
{
	PAINTSTRUCT ps;
	RECT rect;
	/* Create Solid Brush */
	CBrush Brush(RGB(255,0,0));
	/* Brush.CreateSolidBrush(RGB(255,0,0));*/
	GetClientRect(&rect);
	CDC *dc = BeginPaint(&ps);
	dc->SelectObject(&Brush); /* Associate Brush to DC */
	dc->Rectangle(&rect); /* Inner filling with solid red */
	EndPaint(&ps);
}

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 ...

#