Product Home Class IndexDownload

CK_Label

CK_Object
   |
   +----CK_Component
           |
           +----CK_Primitive
                   |
                   +----CK_Label

The CK_Label is a good, all-purpose primitive component. It can be used to display a bitmap, a text label, a button, etc. A label component has the following properties:

Properties

How a CK_Label is rendered:

A label component can have text, a bitmap, a background color, etc. The list below shows the steps the Chilkat engine takes in rendering a label.

  1. The Chilkat engine first adds the label's extent to the clipping region.
  2. If a movie has been set, the movie is drawn and the background color is skipped.
  3. If a background color has been set, the label's region is filled with the background color.
  4. If a bitmap has been set, the bitmap is drawn.
  5. If a border was specified, the engine reduces the clipping rectangle by the size of the border.
  6. If the label has text, it will be drawn.

Examples of creating a label:

The following examples try to set as many properties as possible for demonstration purposes. If a bitmap property is set, the width and height do not need to be set because the size of the label will automatically match the size of the bitmap. If a bitmap property is not set, then width and height must be set, otherwise the width and height will be 0.

CK_Bitmap *bitmap;
char *labelText = "Label Example";
CK_Font *font;
CK_BBoard *bb;
CK_RowColumn *rc;
RGBQUAD bg;

// The parent is a bulletin board, so the CP_RelativeX and CP_RelativeY must be set.
CK_Label *label1 = (CK_Label *) CreateComponent(CT_Label, bb,
    CP_BgColor, &bg,
    CP_BorderX, 5,
    CP_BorderY, 5,
    CP_CallbackArg1, 99,
    CP_Width, 200,
    CP_Height, 100,
    CP_RelativeX, 50,
    CP_RelativeY, 50,
    CP_Text, labelText,
    CP_Font, font,
    CP_VerticalAlign, CV_VertAlignCenter,
    CP_HorizontalAlign, CV_HorizAlignCenter,
    0);

// This example adds a label to a row-column.  The bitmap is drawn using the
// third palette in the bitmap's palette set.
CK_Label *label2 = (CK_Label *) CreateComponent(CT_Label, rc,
    CP_Bitmap, bitmap,
    CP_PaletteIndex, 2,
    0);

Implementing a button

A button can be implemented using a CK_Label. The way to do this is to handle the left-button down event in the screen class. When the event is received, the application checks to see if the event's component is the button, and if so, changes the bitmap property to show the pressed version of the button. The screen class should also handle the button-up event to restore the button's bitmap and do any action associated with the button. This is shown in the following example.


extern CK_BitmapTable *bitmapTable;
extern CK_Screen *initialScreen;
extern CK_Font *font;

// The constructor creates the component tree.
// MyScreen overrides CK_Screen's componentCB() member function
// to handle mouse events.
class MyScreen : public CK_Screen
    {
    private:
	CK_Label *exitBtn;

    public:
	MyScreen(void);
	virtual ~MyScreen(void);

	virtual void componentCB(CK_Component *comp,
	    int event, int arg1, int arg2);

    };

MyScreen::MyScreen(void) 
    {
    CK_BBoard *bb = (CK_BBoard *)CreateComponent(CT_BBoard,0,
	CP_Width,640,
	CP_Height,480,
	0);

    exitBtn = (CK_Label *)CreateComponent(CT_Label,bb,
	CP_Bitmap,bitmapTable->getBitmap(0),
	CP_Text,"Exit",
	CP_HorizontalAlign,CV_HorizAlignCenter,
	CP_VerticalAlign,CV_VertAlignCenter,
	CP_Font,font,
	0);

    setTopComponent(bb);

    return;
    }


void MyScreen::componentCB(CK_Component *comp,
	    int event, int arg1, int arg2)
    {
    if (event == CEV_LeftButtonDown)
	{
	if (comp == exitBtn)
	    {
	    // Show the pressed button graphic.
	    comp->setProp(CP_Bitmap,bitmapTable->getBitmap(1));
	    }

	}
    else if (event == CEV_LeftButtonUp)
	{
	if (comp == exitBtn)
	    {
	    // Show the unpressed button graphic.
	    comp->setProp(CP_Bitmap,bitmapTable->getBitmap(0));

	    // Switch the display to a different screen.
	    initialScreen->makeVisible();
	    }

	}
    return;
    }