EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 20:52:36(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.