I am trying to write a multi-window text editor. I have a Page Control containing 20 pages, and each page (tab sheet) contains an instance of my editor. Which works mostly ok, except I sometimes get weird results when painting my editor window. The attached code is a greatly cut-down version (which isn't a text editor any more, just a TWinControl that paints a single line of text) to illustrate the problem. Sometimes the message text is displayed correctly, and sometimes (i.e. for some tabs) either the message doesn't show at all or it appears in the wrong place or in a different font.
The code is:
__fastcall Tmixala::Tmixala(TComponent* Owner) : TForm(Owner)
{
int i, h, w;
PCmain = new TPageControl(this);
PCmain->Style = tsTabs;
PCmain->TabWidth = 60;
PCmain->MultiLine = false;
PCmain->Parent = this;
PCmain->Width = ClientWidth-10;
PCmain->Height = ClientHeight-10;
h = PCmain->Height - 30;
w = PCmain->Width - 10;
for (i=0; i<20; i++)
{
tabs[i] = new TTabSheet(this);
tabs[i]->Left = 4;
tabs[i]->Top = 4;
tabs[i]->Tag = i;
tabs[i]->PageControl = PCmain;
tabs[i]->TabVisible = true;
tabs[i]->Caption = "Tab " + IntToStr(i);
eds[i] = new editor(this);
eds[i]->Parent = tabs[i];
eds[i]->setCanvas();
eds[i]->resizeWindow(w, h);
}
}
__fastcall editor::editor(TComponent* Owner) : TCustomControl(Owner)
{
Color = clGreen;
Left = 0;
Top = 0;
Height = 100;
Width = 120;
Canvas = new TCanvas;
}
void __fastcall editor::setCanvas()
{
HWND dummy;
Canvas->Handle = GetDeviceContext(dummy);
}
void __fastcall editor::Paint()
{
char msg[12];
sprintf(msg, "Message: %d", ((TWinControl*)Parent)->Tag);
Canvas->TextOut(20, 20, msg);
}
void __fastcall editor::resizeWindow(int width, int height)
{
Width = width - Left - 1;
Height = height - Top - 1;
}
and the header file is
#include <stdio.h>
class Tmixala : public TForm
{
__published:
public:
__fastcall Tmixala(TComponent* Owner);
TPageControl *PCmain;
TTabSheet *tabs[20];
class editor *eds[20];
};
class editor : public TCustomControl
{
public:
__fastcall editor(TComponent* Owner);
void __fastcall Paint();
void __fastcall resizeWindow(int width, int height);
void __fastcall setCanvas();
TCanvas *Canvas;
};
Can anyone tell me please what I've missed. All sensible comments will be considered.