Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

A Comfortable Custom Text-control for Generating helptext in Dialogs

4.11/5 (2 votes)
25 May 2011CPOL3 min read 20.9K   1.3K  
Extends the CStatic with image, frame, shadow and using individual font

Introduction

For a project of my own, I was looking for a dialog-control which I can use in the project's dialogs for showing helptexts above of the dialog. Because I found no fitting control, I wrote it by myself, and now I want to share it with the community. If you want to see the control in action, download the demo-project. If you want to use the control, download the source-code of the new class.

HelpStatic.jpg

Background

CHelpStatic is derived from CStatic and extends its appearance with drawing an image on the left side, custom font, a frame, backgroundcolour/textcolour and a shadow. If you are using an image, the image is automatically sized to fit into the size of the control.

You can also define a number of pixels for the distance between the text and the frame-sides. This makes the helptext better for reading.

The control-class also can be used only as a color-static-control by only setting the text-colour.

Using the Code

The following functions are available. Note that you don't have to use any of them, but if used, the appearance will extend.

  • C++
    void SetColorBack(COLORREF rgb);

    Sets the colour for the background. If not used, windows-standard colour (COLOR_3DFACE) for dialogs is used.

  • C++
    void SetColorText(COLORREF rgb);

    Sets the colour of the text. If not used, windows-standard colour (COLOR_BTNTEXT) for buttons is used.

  • C++
    void SetFont(LPCTSTR czFace, int iSize=10);

    Sets the face and size of the desired font. If not used, windows standard font (DEFAULT_GUI_FONT) is used.

  • C++
    void SetMargin(int iPixel);

    Sets the number of pixels you want between the frame-sides and the drawing text. If not used, no distance is given. Set it to 0 if you want to disable the margin.

  • C++
    void SetText(LPCTSTR czText);

    Sets the text for the control.

  • C++
    void LoadPicture(int iResID);

    Loads an image from the resource and activates the drawing of the image on the left side of control. The text of the control is automatically shifted to the right side of the image.

  • C++
    void EnableFrame(BOOL bEnable,COLORREF rgb=RGB(0,0,0));

    Enable or disables the drawing of a frame around the control. If you enable the frame, you can also define the colour of the frame.

  • C++
    void EnableShadow(BOOL bEnable=TRUE);

    Enables or disables a shadow on the bottom and right side of the control. The shadow will be drawn into the size of the control like you defined in the resource. This means, that, if a shadow is used, the extend of the whole control is the same as without a shadow.

For a detailed description of the functions, take a look at the remarks about the functions in the source-code.

How to Implement

  1. In your dialog, create a static text-control-element for each helptext you want and position it on the desired place (for example, on the top of your dialog).
  2. Add these two files to your project:
    • HelpStatic.cpp
    • HelpStatic.h
  3. Add #include "HelpStatic.h" to your dialogs header file.
  4. Declare an instance of CHelpStatic for each inserted control which you have created in the dialog-resource.

    Example:

    C++
    CHelpStatic m_help;
  5. In your OnInitDialog(), add a SubclassDlgItem() for each CHelpStatic member variable.

    Example:

    C++
    m_help.SubclassDlgItem(IDC_STATICHELP,this);

    Alternatively, you can use the wizard in VS to add a member-variable for the static-ctrl. In the field for the variable-type, type "CHelpStatic". The wizard then creates an entry in DoDataExchange().

  6. In OnInitDialog(), initialize every help-control using one or a number of the above mentioned functions.

    Example:

    C++
    m_help.SetText(czText);
    m_help.SetColorBack(RGB(255,128,0));
    m_help.SetColorText(RGB(0,0,255));
    m_help.LoadPicture(IDB_LOVECRAFT);
    m_help.SetFont("Arial",10);
    m_help.SetMargin(5);
    m_help.EnableFrame(TRUE);
    m_help.EnableShadow();

If you don't use any of these functions, the control is shown with default settings (like a normal CStatic-object).

History

  • 18th May, 2011: First released
  • 20th May, 2011: Updated article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)