📄 unit1.cpp
字号:
//----------------------- TABS 4 ------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include <UTrayIcon3.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define ABSZONE ((240+Zone)%24)
//---------------------------------------------------------------------------
TDATE GMT_Local(TDATE date)
{ TIME_ZONE_INFORMATION tzi ;
bool IsDay = TIME_ZONE_ID_DAYLIGHT == GetTimeZoneInformation(&tzi ) ;
LONG Bias = tzi.Bias + (IsDay ? tzi.DaylightBias : tzi.StandardBias) ;
return date + (Bias / (24.0 * 60.0)) ;
/* WCHAR StandardName[ 32 ];
SYSTEMTIME StandardDate;
LONG StandardBias;
WCHAR DaylightName[ 32 ];
SYSTEMTIME DaylightDate;
LONG DaylightBias; */
}
//---------------------------------------------------------------------------
FAST TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
DirExe = ExtractFilePath(ParamStr(0)) ;
TrayIcon3 = new TTrayIcon3(this) ;
TrayIcon3->PopupMenu = PopupMenu1 ;
TrayIcon3->OnLeftClick = TrayLeftClick ;
TrayIcon3->Dir = DirExe ;
Greenwich = GMT_Local(Now()) ;
Brush->Color = clBlack ;
LastMinute = 834 ;
Period = ptDay ;
// Timer1->Interval = 1000 ; Timer1->Enabled = true ; Timer1Timer(this) ;
}
//---------------------------------------------------------------------------
FAST TForm1:: ~TForm1(void)
{ delete TrayIcon3 ;
delete BMEarth ;
delete BMDarkBrush ;
delete BMPasteUp ;
}
//---------------------------------------------------------------------------
void FAST TForm1:: TrayLeftClick(TOP Sender, TMessage& Msg)
{ if(wsMinimized == WindowState) Restore1Click(this) ;
else Minimize1Click(this) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1:: Minimize1Click(TObject *Sender)
{ Hide() ;
WindowState = wsMinimized;
}
//---------------------------------------------------------------------------
void __fastcall TForm1:: Restore1Click(TObject *Sender)
{ WindowState = wsNormal ;
Show() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Maximize1Click(TObject *Sender)
{ WindowState = wsMaximized ;
Show() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{ Close() ; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Configure1Click(TObject *Sender)
{ Timer1->Enabled = false ;
if(mrOk != Form2->ShowModal()) return ;
Period = (ptENUM)(Form2->RGPeriod->ItemIndex) ;
if(ptOff == Period) return ;
LastMinute = 834 ;
if(ptSecond == Period) Timer1->Interval = 42 ;
Timer1->Enabled = true ;
Timer1Timer(this) ;
}
//---------------------------------------------------------------------------
ASTR FAST TForm1:: TimeOfZone ( int Zone )
{ ASTR as = Form2->ZoneEdits[Zone]->Text ;
if(as.Length() && ('+' == as[1])) Zone ++ ;
TDATE dat = Greenwich + (Zone / 24.0) ;
return dat.FormatString("hh:nn") ;
}
//---------------------------------------------------------------------------
double FAST TForm1:: CalcFullSpin (void) // complete Spin period in days
{ double FullSpin = 1.0 ;
double ard[3] = { 24 , 60 , 60 } ;
int per = 3 - Period ;
for(int ix=0 ; ix<per ; ix++) FullSpin /= ard[ix] ;
return FullSpin ;
}
//---------------------------------------------------------------------------
ASTR NameIcon(UINT i24)
{ char buf[8] ;
sprintf(buf, "g%02u" , i24 % 24 ) ;
return ASTR(buf) ;
}
//---------------------------------------------------------------------------
void FAST TForm1:: ShowIcon(UINT i24 )
{ ASTR Tip = "" ;
int* ari = Form2->ISTR_Tip_Zones ;
int len = ari[0] ;
for(int ix=1 ; ix<=len ; ix++)
{ int Zone = ari[ix] ;
Tip += Form2->ASTR_Zone(Zone) +" "+ TimeOfZone(Zone) +" " ;
}
TrayIcon3->NameAndTip(NameIcon(i24), Tip.Trim()) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1:: Timer1Timer(TObject *Sender)
{
static UINT kkix = 0 ;
if(ptOff == Period) return ;
++kkix ;
Greenwich = GMT_Local(Now()) ;
ptENUM OldPeriod = Period ;
Period = (ptENUM)(Form2->RGPeriod->ItemIndex) ;
if(OldPeriod != Period && ptSecond == Period) Timer1->Interval = 42 ;
USHORT hour, min, sec, msec ;
Greenwich.DecodeTime( &hour, &min, &sec, &msec) ;
if(LastMinute==min && ptSecond==Period)
{ TrayIcon3->IconName = NameIcon(kkix) ;
return ;
}
Sun = (double)Greenwich - (int)Greenwich ; // GMT fractional day
if(LastMinute != min)
{ Caption = Greenwich.FormatString(" ddd, dd mmm yyyy hh:nn 'GMT'") ;
PaintBox1->Invalidate() ;
}
LastMinute = min ;
if(ptSecond != Period)
{ double Part = Sun / CalcFullSpin () ;
Part = Part - (int) Part ; // [0 --- .999]
kkix = (UINT) (24 * Part) ;
}
ShowIcon(kkix) ;
int Msec = msec + 1000 * sec ;
switch(Period)
{ case ptSecond : return ;
case ptMinute : Timer1->Interval = 2500 - Msec%2500 ; return ;
default : Timer1->Interval = 60000 - Msec ; return ;
}
}
//---------------------------------------------------------------------------
TPoint move_pt(int dist, double radian, TPoint pt)
{ pt.x -= dist * sin(radian) ;
pt.y -= dist * cos(radian) ;
return pt ;
}
//---------------------------------------------------------------------------
TPoint FAST TForm1:: Circumf_Zone (int Zone , bool IsTime)
{ TPoint pon ; pon.x = pon.y = 0 ; // center
rads = (Zone * M_PI) / 12 ;
int TimePad = IsTime * FTimeHeight / 4 ;
pon = move_pt ( FRadius + TimePad , rads, pon ) ;
return pon ; // point on circumference
}
//---------------------------------------------------------------------------
TPoint FAST TForm1:: Point_City(int Zone, ASTR City )
{ TPoint pon = Circumf_Zone(Zone, false) ; // point on circumference
if(12 <= ABSZONE)
{ int wid = FCanvas->TextWidth(City) ;
pon = move_pt(wid, rads-M_PI , pon ) ; // back toward center
pon = move_pt(FCityHeight/2, rads + M_PI/2 , pon) ;
}
else pon = move_pt(FCityHeight/2, rads - M_PI/2 , pon) ;
return pon ;
}
//---------------------------------------------------------------------------
TColor FAST TForm1:: ColorCity(int Zone)
{ double dark = (double)Greenwich + ((Zone-6) / 24.0) ;
dark = dark - (int) dark ;
return (0.5 <= dark) ? clYellow : clBlack ;
}
//---------------------------------------------------------------------------
void FAST TForm1:: AngleCity(int Zone, ASTR City)
{ logfont.lfEscapement = (ABSZONE%12 - 6) * 150 ; ;
FCanvas->Font->Handle = CreateFontIndirect (&logfont) ;
FCanvas->Font->Color = ColorCity(Zone) ;
FCanvas->Brush->Style = bsClear ;
TPoint tp = Point_City (Zone, City) ;
FCanvas->TextOut (XCenter + tp.x, YCenter + tp.y, City) ;
}
//---------------------------------------------------------------------------
void FAST TForm1:: TimeCity (int Zone)
{
double dd[24] = { -.5, -.8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -.8 ,
-.5, -.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, -.2 } ;
TPoint pon = Circumf_Zone(Zone, true) ; // point outside circumference
pon.y -= FTimeHeight * ( abs(12-Zone) / 12.0 ) ;
ASTR tim = TimeOfZone(Zone) ;
int wid = FCanvas->TextWidth(tim) ;
pon.x += dd[Zone] * wid ;
FCanvas->TextOut (XCenter + pon.x, YCenter + pon.y, tim ) ;
}
//---------------------------------------------------------------------------
TPoint FAST TForm1:: Circumf_rad(double rad)
{ TPoint pon ;
pon.x = XCenter ;
pon.y = YCenter ;
pon = move_pt ( FRadius , rad, pon ) ;
return pon ; // point on circumference
}
//---------------------------------------------------------------------------
void FAST TForm1:: EarthDraw (void)
{
TRect rr = Rect(0, 0, BMPasteUp->Width , BMPasteUp->Height ) ;
FCanvas->Brush->Color = clBlack ;
FCanvas->FillRect(rr);
int X1 = XCenter - FRadius ;
int Y1 = YCenter - FRadius ;
int X2 = XCenter + FRadius ;
int Y2 = YCenter + FRadius ;
double rad = (.25 - Sun ) * 2 * M_PI ;
TPoint pon = Circumf_rad(rad) ;
int X3 = pon.x ; int Y3 = pon.y ;
pon = Circumf_rad(rad + M_PI) ;
int X4 = pon.x ; int Y4 = pon.y ;
FCanvas->Pen->Style = psClear ;
FCanvas->Brush->Color = clWhite ;
FCanvas->Brush->Bitmap = BMDarkBrush ;
FCanvas->Pie(X1,Y1,X2,Y2,X4,Y4,X3,Y3) ;
FCanvas->Brush->Color = clWhite ;
FCanvas->Brush->Style = bsSolid ;
FCanvas->Pie(X1,Y1,X2,Y2,X3,Y3,X4,Y4) ;
rr = Rect(X1, Y1, X2, Y2) ;
FCanvas->CopyMode = cmSrcAnd ;
FCanvas->StretchDraw( rr , BMEarth) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
BMPasteUp->Width = XCenter = PaintBox1->Width ;
BMPasteUp->Height = YCenter = PaintBox1->Height ;
XCenter /= 2 ; YCenter /=2 ;
FRadius = JDDmin( .67 * XCenter , .80 * YCenter) ;
FTimeHeight = FRadius * .14 ;
FCityHeight = FRadius * .13 ;
EarthDraw() ;
memset (&logfont, 0, sizeof (logfont)) ;
strcpy(logfont.lfFaceName, Form2->FontDialog1->Font->Name.c_str()) ;
logfont.lfHeight = FCityHeight ;
logfont.lfWeight = FW_NORMAL ;
for(int Zone=0 ; Zone<24 ; Zone++)
{ ASTR City = Form2->ASTR_Zone(Zone) ;
if(!City.IsEmpty()) AngleCity(Zone, City) ;
}
FCanvas->Font = Form2->FontDialog1->Font ;
FCanvas->Font->Height = FTimeHeight ;
for(int Zone=0 ; Zone<24 ; Zone++)
{ ASTR City = Form2->ASTR_Zone(Zone) ;
if(!City.IsEmpty()) TimeCity(Zone) ;
}
PaintBox1->Canvas->Draw(0,0, BMPasteUp) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{ try
{ BMPasteUp = new Graphics::TBitmap( ) ;
FCanvas = BMPasteUp->Canvas ;
BMEarth = new Graphics::TBitmap( ) ;
BMDarkBrush = new Graphics::TBitmap( ) ;
BMEarth ->LoadFromFile(DirExe + "earth.bmp") ;
BMDarkBrush->LoadFromFile(DirExe + "DarkBrush.bmp") ;
}
catch(...)
{ PaintBox1->OnPaint = NULL ;
Period = ptOff ;
ShowMessage(DirExe + " lacks files earth.bmp & darkbrush.bmp") ;
// Close() ; // does not seem to work -- form opens all black
Application->Terminate() ;
} ;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -