//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TFrmMain *FrmMain;
//---------------------------------------------------------------------------
__fastcall TFrmMain::TFrmMain(TComponent* Owner)
    : TForm(Owner)
{
    OriginalProc = pnDraw->WindowProc ;
    pnDraw->WindowProc = PanelProc    ;





}
//---------------------------------------------------------------------------

void __fastcall TFrmMain::FormCreate(TObject *Sender)
{
    hdc = GetDC(pnDraw->Handle);
}

void __fastcall TFrmMain::FormDestroy(TObject *Sender)
{
    //delete Gdi ;
    ReleaseDC(pnDraw->Handle , hdc);
}


//---------------------------------------------------------------------------
void __fastcall TFrmMain::PanelProc(Messages::TMessage &msg)
{

    //Application -> ProcessMessages() ;
    OriginalProc(msg);
    if (msg.Msg == WM_PAINT) {
       OnPaint();
    }
    //else if(msg.Msg == WM_ERASEBKGND) {
    //   OnPaint();
    //   msg.Result = 0 ;
    //   return ;
    //}




}
int iDpCnt = 0 ;
//---------------------------------------------------------------------------
void __fastcall TFrmMain::OnPaint()
{
    //return ;
    int a = Img.GetImageWidth () ;
    int b = Img.GetImageHeight() ;

    int c = pnDraw -> Width  ;
    int d = pnDraw -> Height ;

    if(pnDraw -> Width  != Img.GetImageWidth ()) {pnDraw -> Width  = Img.GetImageWidth () ;  }
    if(pnDraw -> Height != Img.GetImageHeight()) {pnDraw -> Height = Img.GetImageHeight() ;  }

    int Ret = Img.ShowImg(hdc, 0, 0, pnDraw -> Width   , pnDraw -> Height );

    iDpCnt++;
    Label2 -> Caption = AnsiString("DP_CNT=") + iDpCnt + AnsiString(" Line=")+g_iDrawLine + AnsiString(" Pixel=") + g_iDrawPx + AnsiString( " B=") + Img.GetImageBit()+ AnsiString( " W=") + Img.GetImageWidth()+ AnsiString( " H=") + Img.GetImageHeight() + AnsiString(" Ret=") + Ret;
}

void __fastcall TFrmMain::btLoadClick(TObject *Sender)
{
    if(!OpenPictureDialog1 -> Execute())return ;
    Img.LoadBmp(OpenPictureDialog1 -> FileName) ;
    OnPaint();
}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::btSaveClick(TObject *Sender)
{
    if(!SavePictureDialog1 -> Execute()) return ;
    Img.SaveBmp(SavePictureDialog1 -> FileName) ;
}

//---------------------------------------------------------------------------
void __fastcall TFrmMain::Timer1Timer(TObject *Sender)
{
    Timer1 -> Enabled = false ;

    int i = rand();

    AnsiString sss ;
    int j = i%8 ;
    sss = "d:\\BC6Works\\ImageView\\image\\" ;
    sss += AnsiString(j) ;

    Img.LoadBmp(sss);

    Label1 -> Caption = sss ;

    //pnDraw -> Repaint() ;
    OnPaint();

    return ;
    if(Img.GetImageBit() == 24) return ;

    Timer1 -> Enabled = true ;

}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::btRandomClick(TObject *Sender)
{
    int i = rand();

    AnsiString sss ;
    int j = i%8 ;
    sss = "d:\\BC6Works\\ImageView\\image\\" ;
    sss += AnsiString(j) ;

    Img.LoadBmp(sss);

    Label1 -> Caption = sss ;

    OnPaint();


}
//---------------------------------------------------------------------------



void __fastcall TFrmMain::Button1Click(TObject *Sender)
{
     Img.SaveBmIf();
}
//---------------------------------------------------------------------------

