Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC
Article

Dialog Header

Rate me:
Please Sign up or sign in to vote.
4.85/5 (27 votes)
22 Jul 20022 min read 435K   4.7K   84   27
Add a header to any dialog or property sheet

Introduction

If you want to give your users some feedback and make your apps look more professional, than just use these simple classes. This code was inspired by Mustafa Demirhan's article about hacking the CPropertySheet.

The CDialogHeader class will add a header area to your dialogs that will display an icon, a title, and some description text.

Below is an image of a standard dialog with a header control added.
The header can display an icon, a title, and description text with a custom font size, alignment, and background color.

Image 1

The CMyPropertySheet class incorporates the dialog header into a property sheet control.

Below are two tabs of a property sheet control with the header control built in. Each tab can have it's own individual icon and text associated with it.

Image 2

How to Use the CDialogHeader Class

  1. Include the files DialogHeader.h and DialogHeader.cpp in your project.

  2. Add the include statement to your dialog header file:
    #include 'DialogHeader.h'             //For the header ctrl area
  3. Add a member variable of type CDialogHeader to your dialog class
    CDialogHeader m_HeaderCtrl;        //Dialogheader 

    In your dialogs OnInit function do the following:

    1. Set the headers display data:
      m_HeaderCtrl.SetHeaderData(hIcon,"Title","Description");

      Or you can set/change each member individually using:

      m_HeaderCtrl.SetIconHandle(hIcon);
      m_HeaderCtrl.SetTitleText("Title");
      m_HeaderCtrl.SetDescText("Description.");
    2. The above functions will be the most commonly used, however you can set all of the following options:

      Font sizes

      m_HeaderCtrl.SetFontSizes(TitleSize,DescSize);

      Text alignment

      m_HeaderCtrl.SetTextAlign(DT_LEFT); //Options are {DT_LEFT,DT_CENTER,DT_RIGHT}

      Background color

      m_HeaderCtrl.SetBackgroundColor(RGB(255,255,255));

      Blank icon mode

      m_HeaderCtrl.SetBlankIcon(TRUE); //TRUE=space always 
        allocated for icon

      Icon offsets

      m_HeaderCtrl.SetIconOffset(5); //Defines top left 
        corner of icon and border

      Title offset

      m_HeaderCtrl.SetTitleOffset(10); //Defines space between 
        icon and title text

      Description offset

      m_HeaderCtrl.SetDescOffset(10); //Defines the 
        amount of indent for desc text when align is DT_LEFT or DT_RIGHT

      Header height

      m_HeaderCtrl.SetHeaderHeight(x); //If x=0, automatically 
        sets height

      Note: Font sizes and offset must be set before setting header height with x=0. See the header file for a more complete description of all functions.

    3. Display the header and resize the dialog window by calling:
      m_HeaderCtrl.Init(this);
    4. Move dialog controls down to accommodate the header area by calling:
      m_HeaderCtrl.MoveCtrls(this);

That’s it! The code below shows a typical example of how to initialize and display the dialog header:

//Example of using CDialogHeader
BOOL CDlgFileTypes::OnInitDialog()
{
    CDialog::OnInitDialog();


    // TODO: Add extra initialization here

    //Set initial header data
    m_HeaderCtrl.SetTitleText("Title");
    m_HeaderCtrl.SetDescText("Description text.");
    m_HeaderCtrl.SetIconHandle(AfxGetApp()->LoadIcon(IDI_HARDDRIVE));

    //Create the header ctrl
    m_HeaderCtrl.Init(this);

    //Move all dialog ctrls down to accommodate header area
    m_HeaderCtrl.MoveCtrls(this);

    return TRUE;
    // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

How to Use the MyPropertySheet Class:

  1. Include the files DialogHeader.h, DialogHeader.cpp,
    MyPropertySheet.h
    
    , and MyPropertyShett.cpp in your project.
  2. Add the include statement in every header file where you want to replace the standard CPropertySheet
    #include "MyPropertySheet.h"        //For the custom Propertysheet ctrl
  3. Add a new CMyPropertySheet member variable/local variable or replace CPropertySheet with CMyPropertySheet.
  4. Add the property pages to the property sheet using a PSHEETITEM variable.

Note: See the header file for a more complete description of all the functions and options.

The example below creates a property sheet with two tabs each with different icon, text, and layout:

void CDialogHeaderDemoDlg::OnButton1()
{
    //Variables
    CMyPropertySheet sheet("Options");
    PropPage1 page1;
    CPropPage2 page2;
    PSHEETITEM pItem;
    
    //Get a default header item
    pItem=sheet.GetDefaultPSItem;
    
    //Add tab 1 to sheet
    pItem.sTitle="Title 1";
    pItem.sDesc="Propertypage number 1 options.";
    pItem.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    sheet.AddPage(&page1,pItem);
    
    //Add tab 2 to sheet
    pItem.sTitle="Title 2";
    pItem.sDesc="Propertypage number 2 options.";
    pItem.hIcon=AfxGetApp()->LoadIcon(IDI_HARDDRIVE);
    
    //Change the alignment for tab 2
    pItem.uAlign=DT_CENTER;
    
    //Add page two to the propertysheet
    sheet.AddPage(&page2,pItem);
    
    //Display the propertysheet ctrl
    sheet.DoModal();
}

I hope you enjoy the code and find it useful.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsSmall modification to use different icon sizes Pin
Jörg Anslik3-May-11 13:57
Jörg Anslik3-May-11 13:57 
Generallicense of CDialogHeader Pin
Jan777777710-May-10 5:08
Jan777777710-May-10 5:08 
Generalgood,thanks Pin
hpxs3-Sep-08 17:41
hpxs3-Sep-08 17:41 
QuestionHow to make this PropertySheet as a stand alone application like a dialog Pin
Shatyamm Kumar21-Jul-06 1:36
Shatyamm Kumar21-Jul-06 1:36 
AnswerRe: How to make this PropertySheet as a stand alone application like a dialog Pin
Shatyamm Kumar21-Jul-06 8:19
Shatyamm Kumar21-Jul-06 8:19 
GeneralI want to use it in the CFormView, But... Pin
Anonymous8-Oct-05 15:38
Anonymous8-Oct-05 15:38 
GeneralRe: I want to use it in the CFormView, But... Pin
breakpoint19-Apr-06 22:21
breakpoint19-Apr-06 22:21 
QuestionHow to use this class with wizard type Pin
GloryHan17-May-04 22:56
GloryHan17-May-04 22:56 
Questionhow to vertically align the Property Sheets ? Pin
elza18-Apr-04 0:29
elza18-Apr-04 0:29 
GeneralA Little Extension Pin
pfluger11-Jan-04 8:12
pfluger11-Jan-04 8:12 
GeneralVery good ,thanks for sharing (and some changes) Pin
Max Santos22-Sep-03 7:47
Max Santos22-Sep-03 7:47 
GeneralRe: Very good ,thanks for sharing (and some changes) Pin
imzhangming14-Dec-04 20:54
imzhangming14-Dec-04 20:54 
GeneralChange the color of fonts... Pin
kevindu21-Aug-03 22:12
kevindu21-Aug-03 22:12 
GeneralRe: Change the color of fonts... Pin
imzhangming14-Dec-04 20:43
imzhangming14-Dec-04 20:43 
QuestionAnything like this for a CFormView? Pin
DanYELL18-Jul-03 4:15
DanYELL18-Jul-03 4:15 
QuestionHow do i get the icon to appear Pin
matty1593-Mar-03 1:40
matty1593-Mar-03 1:40 
AnswerRe: How do i get the icon to appear Pin
Max Santos22-Sep-03 7:52
Max Santos22-Sep-03 7:52 
GeneralDialog Footer Pin
mohdalwi12-Jan-03 7:49
mohdalwi12-Jan-03 7:49 
QuestionHow do I add a bitmap to the header Pin
Anonymous8-Nov-02 10:43
Anonymous8-Nov-02 10:43 
QuestionHow do I replace the bland background with a bitmap Pin
Anonymous18-Oct-02 7:36
Anonymous18-Oct-02 7:36 
AnswerRe: How do I replace the bland background with a bitmap Pin
imzhangming14-Dec-04 20:35
imzhangming14-Dec-04 20:35 
GeneralGood class - Suggestions Pin
John O'Byrne23-Jul-02 22:39
John O'Byrne23-Jul-02 22:39 
GeneralRe: Good class - Suggestions - TextBackground color (Is theree any way to make transperent) Pin
Anonymous24-Jul-02 4:33
Anonymous24-Jul-02 4:33 
GeneralRe: Good class - Suggestions - TextBackground color (Is theree any way to make transperent) Pin
jmgurgel24-Jul-02 5:00
jmgurgel24-Jul-02 5:00 
GeneralRe: Good class - Suggestions - TextBackground color (Is theree any way to make transperent) Pin
Anonymous24-Jul-02 17:47
Anonymous24-Jul-02 17:47 

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.