C++ Builder

La Isla Bonita



Elaborated Hints: The Component Method
Elaborated Hints: The Component Method


Description

The most general method of creating Hints is via existing components, such as, a TRichEdit, a TMemo, a TListBox, a TImage, a TImageList, etc.

In this article I have chosen a TRichEdit component as the "Hint Component". The Hint Component is the area of the screen where the Hint will be displayed.

The border style of the Hint component was set to bsNone so that the TRichEdit component will look like a conventional Hint.

You can also chose any color for the Hint background as well as different fonts. I used the ListBox1MouseDown event to trigger the Hint when the right button of the mouse is pressed.

The Hint component is created by the ListBox1MouseDown event handler, and the Hint is destroyed by the Timer1Timer Event Handler after the specified interval has elapsed (in this case is 1 S)

This method is particularly suitable to create complex Hints, such as Hints containing both text and images. Hints can be developed with several components simultaneously to obtain different shapes, for example, an inverted T shape, a round shape, etc, rather than the traditional rectangular shape.

In coming articles I will explain how to create a complex Hint through the component method.

Files

Hints.cpp

#include <vcl.h>
#pragma hdrstop

#include "Hints.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TTimer *Timer1;
TListBox* ListBox1;
TRichEdit* p;
static int line_count = 1;


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
      //SET THE FORM DIMENSIONS
    Width  = 500;
    Height = 500;

      //CREATE A LISTBOX
    ListBox1 = new TListBox( this );
    ListBox1->Parent = this;
    ListBox1->Top    = 10;
    ListBox1->Left   = 30;
    ListBox1->Width  = 300;
    ListBox1->Height = Height - (5*ListBox1->Top);
    ListBox1->Color       = clBlue;
    ListBox1->Font->Color = clBlack;
    ListBox1->Style       = lbOwnerDrawVariable;
    ListBox1->OnMouseDown = ListBox1MouseDown;
    ListBox1->Columns = 0;

      //CREATE A TIMER
    Timer1 = new TTimer(this);
    Timer1->Interval = 1000;
    Timer1->Enabled = false;
    Timer1->OnTimer = Timer1Timer;

      //CREATE A BUTTON TO ADD LINES TO THE LISTBOX: Button1
    TButton* Button1 = new TButton( this );
    Button1->Parent  = this;
    Button1->Top     = ListBox1->Top;
    Button1->Left    = ListBox1->Left + ListBox1->Width + 30;
    Button1->Width   = 100;
    Button1->Height  = 50;
    Button1->Caption = "Add Item";
    Button1->OnClick = Button1Click;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
                                          TMouseButton Button,
                                          TShiftState Shift,
                                          int X, int Y)
{
    if ( !Timer1->Enabled )
    {
        if (Button == mbRight)
        {
            p = new TRichEdit(Owner);
            p->Visible = false;
            p->Parent = Form1;
            p->BorderStyle = bsNone;
            p->Top = Y + 15;
            p->Left  = X;
            p->Height = 40;
            p->Lines->Add("Hello, this a a hint created with the component method");
            p->Width = 200;
            p->Color = 0x9012fa;  //Dark Pink = 0x9012fa
            p->Font->Color = clWhite;
            p->Visible = true;
            Timer1->Enabled = true;
        }
    }
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
     delete p;
     p = NULL;
     Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
    static int i = 0;
    String Text = "This is a line of text";

    ListBox1->Items->Add( String( line_count++ ) + Text );

    //Highlight the last added item
    ListBox1->ItemIndex = i++;
}
//---------------------------------------------------------------------------

Hints.h

#ifndef HintsH
#define HintsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall ListBox1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.