[MFC] – Font, rectangle, line, brush, pen, random color


void CMFCView::OnDraw(CDC* pDC)
{
CMFCDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect r;
GetClientRect(&r);
// array of font name
CString font[4]={_T("Arial"),_T("Tahoma"),_T("Times New Roman"), _T("Consolas")};

CBrush brush; // init
brush.CreateSolidBrush(RGB(255,0,0)); // red brush
CBrush *pTempBrush = NULL;
CBrush origBrush;
pTempBrush = (CBrush*) pDC->SelectObject(&brush);
// save original brush
origBrush.FromHandle((HBRUSH)pTempBrush);
// paint upper left corner with red brush
pDC->Rectangle(0,0,r.Width()/2, r.Height()/2);

try
{
CBrush brush2(RGB(rand()%255,rand()%255,rand()%255));
CBrush brush3(RGB(rand()%255,rand()%255,rand()%255));
CBrush brush4(RGB(rand()%255,rand()%255,rand()%255));

pTempBrush = (CBrush*)pDC->SelectObject(&brush2);
// Paint upper right corner with red brush.
pDC->Rectangle(r.Width() / 2, 0, r.Width(), r.Height() / 2);
pTempBrush = (CBrush*)pDC->SelectObject(&brush3);
// Paint lower left corner with green hatched brush.
pDC->Rectangle(0, r.Height() / 2, r.Width() / 2, r.Height());
pTempBrush = (CBrush*)pDC->SelectObject(&brush4);
// Paint lower right corner with resource brush.
pDC->Rectangle(r.Width() / 2, r.Height() / 2, r.Width(), r.Height());
}
catch(CResourceException* e)
{
e->ReportError();
e->Delete();
}
pDC->SelectObject(&origBrush);

// set font for every times we create new single view
CFont *oldf, f;
// select a random font in font array.
f.CreateFont(rand()%80, rand()%30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, font[rand()%4]);
oldf= pDC->SelectObject(&f);

pDC->SetTextAlign(TA_CENTER); // align text to the center of the screen
pDC->SetTextColor(RGB(rand()%255,rand()%255,rand()%255));
pDC->TextOut(r.CenterPoint().x, r.CenterPoint().y, _T("hello world"));

// draw new rectangle
CPen pen(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255));
CPen *pOldPen = pDC->SelectObject(&pen);
pDC->Rectangle(50, 50, 250, 250);
pDC->SelectObject(pOldPen);

// draw new ellipse
CPen pen2(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255));
CPen *pOldPen2 = pDC->SelectObject(&pen2);
pDC->Ellipse(50, 50, 250, 250);
pDC->SelectObject(pOldPen2);

// duong cheo chinh
CPen pen3(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255));
CPen *pOldPen3 = pDC->SelectObject(&pen3);
pDC->MoveTo(50, 50);
pDC->LineTo(250, 250);
pDC->SelectObject(pOldPen3);

// duong cheo phu
CPen pen4(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255));
CPen *pOldPen4 = pDC->SelectObject(&pen4);
pDC->MoveTo(250, 50);
pDC->LineTo(50, 250);
pDC->SelectObject(pOldPen4);

// release object when it is no longer in use
f.DeleteObject();
pen.DeleteObject();
pen2.DeleteObject();
pen3.DeleteObject();
pen4.DeleteObject();
}

And below is the result :))

Advertisements

One thought on “[MFC] – Font, rectangle, line, brush, pen, random color

  1. Pingback: Lập trình trên Windows | David Ng

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s