Click here to Skip to main content
15,031,132 members
Articles / Desktop Programming / MFC
Posted 29 Nov 1999


49 bookmarked

Enhanced Line-Drawing Class with Text

Rate me:
Please Sign up or sign in to vote.
4.86/5 (3 votes)
29 Nov 19992 min read
The CSeparator class is an enhancement to standard static text controls that duplicates the look of the Office 97 products' options screens, like Word
  • Download files - 13 Kb
  • Overview

    The CSeparator class is an enhancement to standard static text controls that duplicates the look of the Office 97 products' options screens, like Word (pictured).

    Sample image - 13K

    This separator element is a nice alternative when you want to avoid really big or nested group boxes, or just look like you're keeping up with Microsoft's ever-changing UI designs. ;)

    This class is an extension to Hans Buehler's original article "Control for drawing a bevel line." One of the enhancements he mentioned was adding text to horizontal lines, so that's what I've done!

    CSeparator was written with MSVC 5.0. The code will work in Unicode apps, and should compile fine with 6.0 as well.


    When laying out a vertical separator, make the static control as tall as you want the line to be. The line will be drawn horizontally centered in the control, so it's a good idea to make the control the minimum width of 8 DLUs so you can judge where the line will appear. Vertical separators do not display text.

    When laying out horizontal separators, place your static controls as usual, but make them as wide as you want the horizontal line to be. You can also set the alignment of the text (left, center, or right), and set the No Prefix style if you want. Those are the only static control styles supported at this time. If you do not set the No Prefix style, then you can use a shortcut key (i.e., put a & before a letter) just as with normal static text controls.

    A sample dialog is pictured below, first in the resource editor, and then with the separators in action.

     [Dlg in MSVC resource editor - 14K]  [Dlg with separators drawn - 17K]

    Each static control that will be used as a separator should have a real unique ID (not IDC_STATIC). Using ClassWizard, create member variables of type CStatic for each separator, and then go to the ClassWizard-generated code and change the type of the variables to CSeparator. That's it!

    Future Enhancements

    I tried for a little while to get vertically-drawn text, but it was pretty tricky to get the text aligned correctly over the line. Drawing the text rotated is no problem (as long as you select a TrueType font into the DC!) but the horizontal position of the text depends on the length of the text. The CSeparator class has some code to draw text vertically, although it is commented out since it doesn't work exactly right. It's just there as a starting point for anyone brave enough to take on the challenge. See the OnPaint() function for the code snippet and some comments.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Michael Dunn
    Software Developer (Senior) VMware
    United States United States
    Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
    Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

    He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

    Mike was a VC MVP from 2005 to 2009.

    Comments and Discussions

    GeneralMade the text looks like it splits the separator Pin
    EmoBemo17-Apr-09 6:01
    MemberEmoBemo17-Apr-09 6:01 
    GeneralSimple work around [modified] Pin
    itskumaranand9-Apr-08 23:53
    Memberitskumaranand9-Apr-08 23:53 
    GeneralSeperator fix for all platforms Pin
    katsyonak7-Dec-03 12:24
    Memberkatsyonak7-Dec-03 12:24 
    GeneralRe: Seperator fix for all platforms Pin
    sps-itsec466-Feb-05 12:41
    Membersps-itsec466-Feb-05 12:41 
    GeneralFix for disabled text Pin
    Andrew Welch1-Dec-02 16:13
    MemberAndrew Welch1-Dec-02 16:13 
    GeneralCool but no go with XP Property Sheet Pin
    glitch5-Nov-02 21:49
    Memberglitch5-Nov-02 21:49 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.