📄 utaskchart.pas
字号:
begin
pnlStartLine.Visible:=false;
pnlEndLine.Visible:=false;
//调整时间条位置
// if lstTimeBar.scrbMother.Top<>0 then
// lstTimeBar.scrbMother.Top:= 0;
end;
//水平是否滚动
if iHorPosition<> sbMother.HorzScrollBar.Position then
begin
iHorPosition:= sbMother.HorzScrollBar.Position;
//调整时间条显示位置
lstTimeBar.scrbMother.HorzScrollBar.Position:= sbMother.HorzScrollBar.Position ;
end;
//垂直是否滚动
if iVerPosition<>sbMother.VertScrollBar.Position then
begin
iVerPosition:= sbMother.VertScrollBar.Position;
//机器列表位置
sbTest.VertScrollBar.Position:= sbMother.VertScrollBar.Position
// if pnlTop.Visible=false then
// pnlTop.Visible:= true;
end;
end;
//时间条
{*****************************************************************************}
//==========================================================================
constructor TlstTimeBar.Create();
begin
Inherited Create();
iPreLeft:= TaskChart.iStartLeft;
bStartFlag:= true; //第一次开刻度标志
lstLabDate:= TList.Create(); //时间指针
//生成时间刻度面板
// NewTimePanel;
end;
//--------------------------------------------------------------------------
destructor TlstTimeBar.Destroy;
begin
// scrbMother.Free;
end;
//--------------------------------------------------------------------------
//生成放时间刻度的面板
procedure TlstTimeBar.NewTimePanel();
begin
scrbMother:= TScrollBox.Create(nil);
with scrbMother do
begin
Left := 0;
Top := 0;
Width := 405;
Height := 50;
Align := alTop;
BevelKind := bkFlat;
BorderStyle := bsNone;
Parent:= self.Parent;
HorzScrollBar.Visible:= true;
VertScrollBar.Visible:= false;
Color := clActiveBorder;// $007777BB;//$00C0C0E0;
// ParentColor:= false;
end;
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.DrawDay(day,month:Integer;sbDatePanel:TWinControl);
var pbevel,pOld:TTimeBar;
i, iHourPerBar, iBarCount:Integer;
sTime: String;
begin
if TaskChart.iIncreaseVal<30 then
begin
iHourPerBar:= 4;
iBarCount:= 6; //固定值,每天画6条线
end
else
begin
iHourPerBar:= 1;
iBarCount:= 24;
end;
for i:=1 to iBarCount do
begin
pbevel:= TTimeBar.Create(nil);
//取时间(年月日)
sTime:= IntToStr(iCurYear)+'-'+ IntToStr(month)+'-'+ IntToStr(day);
//取小时
if i>1 then
sTime:= sTime+' '+IntToStr((i-1)*iHourPerBar)+':00:00'
else
sTime:= sTime+' '+'00:00:00';
with pbevel do
begin
Left := iPreLeft+TaskChart.iSpaceVal*TaskChart.iIncreaseVal;
iPreLeft:= left;
DateTime:= StrToDateTime(sTime);
//如果是开始画, 则left位置为刻度起始位置
if bStartFlag then
begin
Left :=TaskChart.iStartLeft;
iPreLeft:= TaskChart.iStartLeft;
bStartFlag:= false;
end;
Width := 8 ;
//如果是画一天的第一个刻度,则画长线
if(i=1)then
begin
Height :=25;
Top :=5 ;
end
else
begin
Height := 10 ;
Top :=20 ;
end;
Shape := bsLeftLine ;
Style := bsRaised;
parent:= sbDatePanel;
end;
self.Add(pbevel);
pbevel:= nil;
end;
// ShowMessage(IntToStr(iPreLeft));
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.DrawMonth(startDay,endDay,month:Integer;sbDatePanel:TWinControl);
var i:Integer;
begin
for i:=startDay to endDay do
begin
self.DrawDateLabel(i,month,sbDatePanel);
self.DrawDay(i,month,sbDatePanel);
end;
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.DrawDateLabel(day,month:Integer;sbDatePanel:TWinControl);
var pLab: TSolidLabel;
sMonth,sDay:string;
begin
sDay:= IntToStr(day) ;
sMonth:= IntToStr(month);
if (day<10) then
sDay:= '0'+IntToStr(day) ;
if (month<10) then
sMonth:= '0'+ IntToStr(month);
pLab:= TSolidLabel.Create(nil);
with pLab do
begin
//如果是开始画, 日期显示年,位置为刻度起始位置+2,
if (bStartFlag=true) then
begin
Left:= TaskChart.iStartLeft+2;
Caption:=IntToStr(iCurYear)+'/'+sMonth+'/'+sDay;
end
//如果为1月1日则显示年
else if (month=1)and(day=1) then
begin
Left:= iPreLeft+TaskChart.iSpaceVal*TaskChart.iIncreaseVal+2;
Caption:=IntToStr(iCurYear)+'/'+sMonth+'/'+sDay;
end
//否则为上一刻度位置+当前刻度间距(基本间距*倍数)+2
else
begin
Left:= iPreLeft+TaskChart.iSpaceVal*TaskChart.iIncreaseVal+2;
Caption:=sMonth+'/'+sDay;
end;
Top:= 5;
//显示位置(父窗体)
parent:= sbDatePanel;
end;
lstLabDate.Add(pLab);
end;
//--------------------------------------------------------------------------
//画最后个条长线
procedure TlstTimeBar.DrawEndBar(sbDatePanel:TWinControl);
var pEndLine:TTimeBar;
begin
pEndLine:= TTimeBar.Create(nil);
with pEndLine do
begin
Left := iPreLeft+TaskChart.iSpaceVal*TaskChart.iIncreaseVal;
iPreLeft:= left;
Height :=25;
Top :=5 ;
Shape := bsLeftLine ;
Style := bsRaised;
parent:= sbDatePanel;
end;
end;
//--------------------------------------------------------------------------
function TlstTimeBar.getDayCount(month,year:Integer):Integer;
begin
case month of
1: Result:= 31;
2:
if ((year mod 4)=0) then
Result:= 29
else
Result:= 28;
3: Result:= 31;
4: Result:= 30;
5: Result:= 31;
6: Result:= 30;
7: Result:= 31;
8: Result:= 31;
9: Result:= 30;
10: Result:= 31;
11: Result:= 30;
12: Result:= 31;
end;
end;
//--------------------------------------------------------------------------
//按 坐标值返回时间
function TlstTimeBar.GetTimeByValue(Value: Integer):TDateTime;
var pTemp: TTimeBar;
i, iVal, iCount: Integer;
begin
//位置 +起始时间条的位置 -滚动条位置
Value:= Value + TaskChart.iStartLeft - TaskChart.lstTimeBar.scrbMother.HorzScrollBar.Position;;
//时间条总数
iCount:= self.Count;
//找出位置小于目标位置的前一条刻度
for i := 0 to iCount-1 do
begin
pTemp:= TTimeBar(self.Items[i]);
if (pTemp.Left<= Value)and((pTemp.Left+ TaskChart.iSpaceVal* TaskChart.iIncreaseVal)> Value) then
break;
pTemp:= nil;
end;
//如果没找到则目标不在显示时间范围内
if pTemp=nil then
Result:= StrToDateTime('1899-12-30 00:00:00')
else
begin
iVal:= Value- pTemp.Left; //固定每天6条线
//计算目标位置的时间
Result:= pTemp.DateTime+ (iVal/(TaskChart.iSpaceVal / 4 * TaskChart.iIncreaseVal)/ 24);
end;
end;
//--------------------------------------------------------------------------
//按 时间返回坐标值
function TlstTimeBar.GetValueByTime(dDate: TDateTime):Integer;
var i, iCount: Integer;
pTemp: TTimeBar;
dTemp: TDateTime;
wHour, wMinute, wSecond, wMSecond:Word;
begin
//总时间刻度条数
iCount:= self.Count;
//找到时间小于目标时间且离目标时间最近的刻度条
for i :=0 to iCount-1 do
begin
pTemp:= TTimeBar(self.Items[i]);
if ((pTemp.DateTime)<=dDate) and ((pTemp.DateTime+ 0.166)>dDate) then
break;
pTemp:= nil;
end;
//如果找不到返回负1
if pTemp=nil then
Result:= -1
else
begin
//时间差(目标时间 - 最接近目标时间的刻度的时间)
dTemp:= dDate- pTemp.DateTime;
DecodeTime(dTemp, wHour, wMinute, wSecond, wMSecond );
wMinute:= wMinute+ wHour*60;
//换算成刻度距离
// Result:= pTemp.Left+ round(TaskChart.iIncreaseVal*TaskChart.iSpaceVal* wMinute/240)//+3
//返回位置= 时间最接近目标时间刻度条的位置 + round(时间差值对应的长度) - 最左刻度条位置 ( + 滚动条相对位置)
Result:= pTemp.Left+ round(TaskChart.iIncreaseVal * TaskChart.iSpaceVal * wMinute/240) - TaskChart.iStartLeft;//+ TaskChart.lstTimeBar.scrbMother.HorzScrollBar.Position;
end;
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.ClearPointerList();
var i:Integer;
pTempBevel:TBevel;
pTempLabel:TLabel;
begin
iPreLeft:= 0;
bStartFlag:= true;
//清空时间刻度指针
for i:=0 to self.Count-1 do
begin
pTempBevel:=TBevel(self.Items[i]);
pTempBevel.Free;
pTempBevel:= nil;
end;
if self.Count>0 then
self.Clear;
//清空日期指针
if lstLabDate<>nil then
begin
for i:=0 to lstLabDate.Count-1 do
begin
pTempLabel:=TLabel(lstLabDate.Items[i]);
pTempLabel.Free;
pTempLabel:= nil;
end;
lstLabDate.Clear;
end;
//清空面板对象
scrbMother.Free;
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.setSpaceIncreaseVal(InceaseVal:Integer);
begin
if InceaseVal<=20 then
TaskChart.iIncreaseVal:=InceaseVal
else
TaskChart.iIncreaseVal:= 1;
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.setStartLeftVal(LeftVal:Integer);
begin
TaskChart.iStartLeft:= LeftVal;
end;
//--------------------------------------------------------------------------
function TlstTimeBar.getLendthByHour(dHour:Double):Integer;
begin
// Result:= round(TaskChart.iIncreaseVal*2*dHour);
end;
//--------------------------------------------------------------------------
procedure TlstTimeBar.AutoSizeScale(p:TPanel);
begin
p.Width:= iPreLeft+30;
end;
//==========================================================================
procedure TlstTimeBar.ShowDateScale(StartDate,EndDate:TDateTime;sbDatePanel:TScrollBox;iScale:Integer);
var iStartDay,iEndDay,iStartMonth,iEndMonth,iStartYear,iEndYear:word;
iDayCount,iYear,iMonth,iSMonth,iEMonth,iSDay,iEDay:Integer;
begin
//如果还没有生成时间
if lstLabDate <> nil then
self.ClearPointerList; //清空所有对象指针链
//设定刻度间距倍数
self.setSpaceIncreaseVal(iScale);
//指TimeBar所在原容器
self.Parent:= TaskChart.pnlTimeBar ;//sbDatePanel;
//生成时间刻度面板
NewTimePanel;
// scrbMother.Parent:= sbDatePanel;
DecodeDate(StartDate, iStartYear,iStartMonth,iStartDay);
DecodeDate(EndDate, iEndYear,iEndMonth,iEndDay);
//年循环
for iYear := iStartYear to iEndYear do
begin
//记录当前年
iCurYear:= iYear;
//如果当前年是选定的开始年,则月从选定的月开始,否则从1月开始
if (iYear=iStartYear) then
iSMonth:= iStartMonth
else
iSMonth:= 1;
//如果当前年是选定的结束年,则月到选定的月结束,否则到 12月结束
if (iYear=iEndYear) then
iEMonth:= iEndMonth
else
iEMonth:= 12;
//月循环
for iMonth:=iSMonth to iEMonth do
begin
//返回指定年的月有多少天
iDayCount:= getDayCount(iMonth,iYear);
iSDay:= 1; //缺省从一号开始
iEDay:= iDayCount; //缺省结束天为该有总天数
//按月显示时间
//如果是开始那年,开始那月,则该月从天始的那一天显示
if (iYear=iStartYear)and(iMonth=iStartMonth) then
iSDay:= iStartDay;
//如果最后那年,最后那月,则该月显示到结束那天止
if (iYear=iEndYear)and(iMonth=iEndMonth) then
iEDay:= iEndDay;
//画该月时间刻度
DrawMonth(iSDay,iEDay,iMonth,scrbMother)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -