A Journey 2 Eternity

Painting a Gradient back ground

Posted by: Neel Aakash on: June 8, 2009

Here’s a sample which draws background of a dialog with gradient colors.

BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)

{

CRect rect;

GetClientRect(&rect);

TRIVERTEX vert[4] = {

{ rect.left, rect.top, 0xff00, 0xff00, 0xff00, 0 },

{ rect.right, rect.top, 0, 0xff00, 0xff00, 0 },

{ rect.right, rect.bottom, 0xff00, 0, 0xff00, 0 },

{ rect.left, rect.bottom, 0xff00, 0xff00, 0, 0 }

};

GRADIENT_TRIANGLE grad[2] = {

{ 0, 1, 2 },

{ 0, 2, 3 }

};

::GradientFill( pDC->m_hDC, vert, 4, grad, 2, GRADIENT_FILL_TRIANGLE );

return TRUE;

}

void CMyDlg::OnSize(UINT nType, int cx, int cy)

{

// To ensure the background of the whole dialog is re-drawn

Invalidate();

CDialog::OnSize(nType, cx, cy);

}

//! Replaces a color with a fill color in a given area of a

//! Device Context

//!

//! \param hDC                            : Specifies the Device Context

//! \param rcReplaceArea     : Specifies the portion of the DC area

//!                to apply the replace operation

//! \param clrColorReplace    : Specifies the color to be replaced

//! \param clrColorFill : Specifies the fill color

void ReplaceColor(HDC hDC, CRect rcReplaceArea, COLORREF clrColorReplace, COLORREF clrColorFill)

{

CDC* pDC = CDC::FromHandle(hDC);

CPoint pt = rcReplaceArea.TopLeft();

CDC memDCMonoChrome;

memDCMonoChrome.CreateCompatibleDC(pDC);

CBitmap bmpMonoChrome;

bmpMonoChrome.CreateCompatibleBitmap(&memDCMonoChrome,

rcReplaceArea.Width(), rcReplaceArea.Height());

CBitmap* pOldMonoBitmap =

memDCMonoChrome.SelectObject(&bmpMonoChrome);

COLORREF nOldBkColor = pDC->SetBkColor(clrColorReplace);

// BLT to mono dc so that mask color will have 1 set and the other colors 0

memDCMonoChrome.BitBlt(0, 0, rcReplaceArea.Width(),

rcReplaceArea.Height(), pDC, pt.x, pt.y, SRCCOPY);

CDC memDC;

memDC.CreateCompatibleDC(pDC);

CBitmap bmp;

bmp.CreateCompatibleBitmap(pDC, rcReplaceArea.Width(),

rcReplaceArea.Height());

CBitmap* pOldBitmap = memDC.SelectObject(&bmp);

COLORREF nOldMemDCBkColor = memDC.SetBkColor(clrColorFill);

COLORREF nOldMemDCTextColor =

memDC.SetTextColor(RGB(255, 255, 255));

// BLT to mem DC so that the monochrome white is set to fill color and the

//       monochrome black is set to white

memDC.BitBlt(0, 0, rcReplaceArea.Width(), rcReplaceArea.Height(),

&memDCMonoChrome, 0, 0, SRCCOPY);

// AND pDC with mem dc so that the replace color part is blackened out and

//       all other colors remains same

pDC->BitBlt(pt.x, pt.y, rcReplaceArea.Width(), rcReplaceArea.Height(),

&memDC, 0, 0, SRCAND);

memDC.SetTextColor(RGB(0, 0, 0));

// BLT to mem DC so that the monochrome white is set to fill color and the

//       monochrome black is set to black

memDC.BitBlt(0, 0, rcReplaceArea.Width(), rcReplaceArea.Height(),

&memDCMonoChrome, 0, 0, SRCCOPY);

// OR pDC with mem dc so that all colors remains as they where except the

//       blackened out (replace color) part receives the fill color

pDC->BitBlt(pt.x, pt.y, rcReplaceArea.Width(), rcReplaceArea.Height(),

&memDC, 0, 0, SRCPAINT);

// Set the original values back

memDC.SetTextColor(nOldMemDCTextColor);

memDC.SetBkColor(nOldMemDCBkColor);

pDC->SetBkColor(nOldBkColor);

// Set the original bitmaps back

memDCMonoChrome.SelectObject(pOldMonoBitmap);

memDC.SelectObject(pOldBitmap);

}

Sequence of messages sent on Double Click Event

Posted by: Neel Aakash on: May 19, 2009

1. LButtonDown

2. LButtonUp

3. LButtonDblClick

4. LButtonUp

Wild-Card Comparison

Posted by: Neel Aakash on: May 13, 2009

Sometimes we have needed to compare a Wild-Card string in a given string. Here is a handy function for doing this:

int WildCmp(const TCHAR* wild, const TCHAR* string)

{

const TCHAR *cp = NULL, *mp = NULL;

while ((*string) && (*wild != ‘*’))

{

if ((*wild != *string) && (*wild != ‘?’))

{

return 0;

}

wild++;

string++;

}

while (*string)

{

if (*wild == ‘*’)

{

if (!*++wild)

{

return 1;

}

mp = wild;

cp = string+1;

}

else if ((*wild == *string) || (*wild == ‘?’))

{

wild++;

string++;

}

else

{

wild = mp;

string = cp++;

}

}

while (*wild == ‘*’)

{

wild++;

}

return !*wild;

}

Draw vertically centered multi-line text in a given rectangle

Posted by: Neel Aakash on: April 22, 2009

DT_WORDBREAK and DT_VCENTER don’t work together. So here is an alternative:

void CEMCHotButton::DrawCaption (CDC* pDC, LPDRAWITEMSTRUCT

lpDrawItemStruct, CRect& rectCaption, DWORD dwFormat)

{

// Get caption text

CString strCaption;

GetWindowText (strCaption);

if(!strCaption.IsEmpty())

{

if((GetStyle() & BS_MULTILINE) == BS_MULTILINE)

{

dwFormat &= ~DT_SINGLELINE;

dwFormat &= ~DT_VCENTER;

dwFormat |= DT_WORDBREAK;

RECT rct = rectCaption;

int height = ::DrawText(pDC->m_hDC, (LPCTSTR) strCaption, strCaption.GetLength(), &rct, DT_CALCRECT | DT_WORDBREAK);

rectCaption.top = rectCaption.CenterPoint().y – height / 2;

::DrawTextEx (pDC->m_hDC, strCaption.GetBuffer(0),

strCaption.GetLength(), &rectCaption,

dwFormat, NULL);

}

}

}

Categories

 

February 2010
S M T W T F S
« Jun    
 123456
78910111213
14151617181920
21222324252627
28