//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "lebcb_4a.h"
//---------------------------------------------------------------------------
#pragma link "LEDlp32"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LEdit1Paint(TObject *Sender, TCanvas *DCHandle,
	TRect &ClientArea)
{
//  HBRUSH Brush;
//  HPEN Pen;
  long X;
  long Y;

  // Fill background
  //Brush = CreateSolidBrush(ColorToRGB(clBtnFace));
  //FillRect(DCHandle, &(RECT)ClientArea, Brush);
  DCHandle->Brush->Color = clBtnFace;
  DCHandle->FillRect(ClientArea);
  //DeleteObject(Brush);

  if (SunLocked) {
    // Retrieve info on scrolling position
    Y = LEdit1->FirstVisibleLine - YSun;
    X = LEdit1->FirstVisiblePos - XSun;
    X = X * LEdit1->UnitsHorizontal;
    Y = Y * LEdit1->UnitsVertical;

    // Truncate values to 16-bit if they are large
    if (X > XScreen)
      X = XScreen;
    if (X < - XScreen)
      X = -XScreen;
    if (Y > YScreen)
      Y = YScreen;
    if (Y < - YScreen)
      Y = -YScreen;

    // Move ellipse rectangle according to scrolling
    OffsetRect(&(RECT)ClientArea, -X, -Y);
  }

  // Draw ellipse
  DCHandle->Brush->Style = bsSolid;
  DCHandle->Brush->Color = clRed;
  DCHandle->Ellipse(ClientArea.Left, ClientArea.Top, ClientArea.Right, ClientArea.Bottom);
  //Brush = SelectObject(DCHandle, CreateSolidBrush(ColorToRGB(clRed)));
  //Pen = SelectObject(DCHandle, GetStockObject(NULL_PEN));
  //Ellipse(DCHandle, (&ClientArea)->Left, (&ClientArea)->Top, (&ClientArea)->Right, (&ClientArea)->Bottom);
  //SelectObject(DCHandle, Pen);
  //DeleteObject(SelectObject(DCHandle, Brush));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
  if (SunLocked) {
    BitBtn2->Caption = "Lock";
    // Redraw window
    LEdit1->Invalidate();
    //InvalidateRect(LEdit1->Handle, NULL, FALSE);
  }
  else {
    BitBtn2->Caption = "Unlock";
    // Store the place where Sun is locked
    YSun = LEdit1->FirstVisibleLine;
    XSun = LEdit1->FirstVisiblePos;
  }
  SunLocked = !SunLocked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
  LEdit1->PaintMode = dsDirect;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
  LEdit1->PaintMode = dsUseMemoryDC;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  SunLocked = False;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
  LEdit1->LoadFromFile("lebcb_4a.cpp");
  //  LEdit1.LoadFromFile('d:\temp\zzz.txt');
  // Set constants
  XScreen = GetSystemMetrics(SM_CXSCREEN);
  YScreen = GetSystemMetrics(SM_CYSCREEN);
}
//---------------------------------------------------------------------------