Click here to Skip to main content
Click here to Skip to main content

A Comfortable Custom Text-control for Generating helptext in Dialogs

, 25 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

  • void SetColorBack(COLORREF rgb);

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

  • void SetColorText(COLORREF rgb);

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

  • 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.

  • 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.

  • void SetText(LPCTSTR czText);

    Sets the text for the control.

  • 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.

  • 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.

  • 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:

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

    Example:

    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:

    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)

Share

About the Author

Rob A. Fraydl
Team Leader
Austria Austria
No Biography provided

Comments and Discussions

 
GeneralOther solution PinmemberJimmyO25-May-11 21:43 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 25 May 2011
Article Copyright 2011 by Rob A. Fraydl
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid