Control for drawing a bevelline

.

This is one of my first extensions to the MFC ... since I just have been asked to publish it, here is what I expected to come from Microsoft:
A static control that displays a bevel line (i.e. a 3d-shaded-Line).

draw_bevel.gif (1273 Byte)

How to put a bevelline in my dialog:

  1. In the dialog resource editor, put a CStatic (Text) in the position of the bevelline. Give the control an ID other than IDC_STATIC.
    I recommend to give it a minimal height if you want a horzontal line, a minimal width if you want a vertical line (see further notes below).
  2. Using ClassWizard, add a member variable (type control) for the control (i.e. m_wndBevel).
  3. Open the dialog's implementation file, #include the header file "cdxCBevelLine.h" and replace the word "CStatic" of your newly created control by "cdxCBevelLine".
  4. Don't forget to add both cdxCBevelLine.h and cdxCBevelLine.cpp to your project's file list.

Further notes:

  1. Using the constructor, you can alternatively change the public member m_bSunken to false which indicates that the bevelline should appear high, not lowered.
    During run-time, you can manually set m_bSunken and call CWnd::Invalidate() and CWnd::UpdateWindow().
  2. The control will determine its orientation from the size of the area it occupies; i.e. if the window's height is bigger than its width, the bevelline will be drawn vertically, otherwise horizontally.

Future plannings:

  1. Add the possibility to add a text to a horizontal line :)

Source code follows....

cdxCBevelLine.h:

#if !defined(AFX_CDXCBEVELLINE_H__0609E262_670A_11D1_A589_444553540000__INCLUDED_)

#define AFX_CDXCBEVELLINE_H__0609E262_670A_11D1_A589_444553540000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// cdxCBevelLine.h : header file

/*
 * cdxCBevelLine
 * =============
 * Display a bevelline.
 *
 * To put a bevelline into your dialog, create a static control that acts as a placeholder,
 * create a member variable of type control ("CStatic") for it, open the dialog's
 * include file, include this file and replace the "CStatic" by a "cdxCBevelLine".
 *
 * (w)1997 Hans B|hler, hans.buehler@student.hu-berlin.de
 * freeware.
 */

class cdxCBevelLine : public CStatic
{
public:
   enum
   {
      LineWid = 1,
      LineHi  = 1
   };

// Construction
public:
   cdxCBevelLine(bool bSunken = true) : m_bSunken(bSunken) {}
   virtual ~cdxCBevelLine() {}
   BOOL Create(const RECT& rect, CWnd* pParentWnd)   
         { return CStatic::Create(NULL,WS_CHILD|WS_VISIBLE,rect,pParentWnd); }

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(cdxCBevelLine)
    //}}AFX_VIRTUAL

// Implementation
public:
   bool m_bSunken;   // call Invalidate() and UpdateWindow() to redraw control

// Generated message map functions
protected:
   //{{AFX_MSG(cdxCBevelLine)
   afx_msg void OnPaint();
   //}}AFX_MSG

   DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CDXCBEVELLINE_H__0609E262_670A_11D1_A589_444553540000__INCLUDED_)

cdxCBevelLine.cpp:

// cdxCBevelLine.cpp : implementation file

#include "stdafx.h"
#include "cdxCBevelLine.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

// cdxCBevelLine

BEGIN_MESSAGE_MAP(cdxCBevelLine, CStatic)
    //{{AFX_MSG_MAP(cdxCBevelLine)
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////

// cdxCBevelLine message handlers

void cdxCBevelLine::OnPaint() 
{
   CPaintDC dc(this); // device context for painting
   CRect r;
   GetClientRect(&r);

   DWORD hiCol = ::GetSysColor(!m_bSunken ? COLOR_3DHIGHLIGHT : COLOR_3DSHADOW);
   DWORD loCol = ::GetSysColor(m_bSunken ? COLOR_3DHIGHLIGHT : COLOR_3DSHADOW);

   if(r.bottom > r.right)
   {
      // vertical
      r.right /= 2;
      r.left = r.right - LineWid;
      r.right += LineWid;
   }
   else
   {
      // horizonzal
      r.bottom /= 2;
      r.top = r.bottom - LineHi;
      r.bottom += LineHi;
   }
   dc.Draw3dRect(&r,hiCol,loCol);
}