Mar 212008
 

How to draw text like this!
outlinedrawing.png

Looks interesting right! Well this is done using plain window’s GDI functions, here is a function which does this!

// Thanks to Charles Petzold!
void CCanvas::DrawOutlineText( CDC& dc, const CString& Text )
{
  const int RestorePoint = dc.SaveDC();

  // Create new font
  CFont NewFont;
  NewFont.CreatePointFont( 700, TEXT( "Verdana" ), &dc );

  // Use this font
  dc.SelectObject( &NewFont );

  // Brush for pen
  LOGBRUSH lBrushForPen = { 0 };
  lBrushForPen.lbColor = RGB( 200, 150, 100 );
  lBrushForPen.lbHatch = HS_CROSS;
  lBrushForPen.lbStyle = BS_SOLID;

  // New pen for drawing outline text
  CPen OutlinePen;
  OutlinePen.CreatePen( PS_GEOMETRIC | PS_SOLID, 2, &lBrushForPen, 0, 0 );

  // Use this pen
  dc.SelectObject( &OutlinePen );

  dc.SetBkMode( TRANSPARENT );

  dc.BeginPath();
  // This text is not drawn on screen, but instead each action is being
  // recorded and stored internally as a path, since we called BeginPath
  dc.TextOut( 20, 20, Text );
  // Stop path
  dc.EndPath();

  // Now draw outline text
  dc.StrokePath();

  dc.RestoreDC( RestorePoint );
}
Dec 062007
 

Ever felt the need to change the font of all child controls of a dialog, or a window, and you had to use GetWindow, GetNextWindow and stuff like that to set a font for a window. Well here is an easy way!

// Idea taken from MSDN...
BOOL CSomeDialog::OnInitDialog()
{
    // I am using a static font for demo purpose only, make sure
    // you have this as a member
    static CFont fnt;
    CDC dc;
    dc.Attach( GetDC()->GetSafeHdc() );
    fnt.CreatePointFont( 100, _T( "Courier new" ), &dc );

    // Set change font message to all child windows
    SendMessageToDescendants( GetSafeHwnd(),
                              WM_SETFONT,
                              (WPARAM)fnt.GetSafeHandle(),
                              MAKELONG(WORD( TRUE ), 0 ),
                              true,
                              false );
}// End OnInitDialog