📄 fctreeheader.pas
字号:
if Value > FMaxWidth then Value := FMaxWidth;
FMinWidth := Value;
SetWidth(FWidth);
end;
procedure TfcTreeHeaderSection.SetStyle(Value: THeaderSectionStyle);
begin
if FStyle <> Value then
begin
FStyle := Value;
Changed(False);
end;
end;
procedure TfcTreeHeaderSection.SetText(const Value: string);
begin
if FText <> Value then
begin
FText := Value;
Changed(False);
end;
end;
procedure TfcTreeHeaderSection.SetImageIndex(Value: integer);
begin
if FImageIndex<>Value then
begin
FImageIndex:= Value;
Changed(False);
end
end;
procedure TfcTreeHeaderSection.SetImageAlignment(Value: TAlignment);
begin
if FImageAlignment<>Value then
begin
FImageAlignment:= Value;
Changed(False);
end
end;
procedure TfcTreeHeaderSection.SetWidth(Value: Integer);
begin
if Value < FMinWidth then Value := FMinWidth;
if Value > FMaxWidth then Value := FMaxWidth;
if FWidth <> Value then
begin
FWidth := Value;
Changed(Index < Collection.Count - 1);
end;
end;
constructor TfcTreeHeaderSections.Create(HeaderControl: TfcTreeHeaderControl);
begin
inherited Create(TfcTreeHeaderSection);
self.HeaderControl := HeaderControl;
end;
function TfcTreeHeaderSections.Add: TfcTreeHeaderSection;
begin
Result := TfcTreeHeaderSection(inherited Add);
end;
function TfcTreeHeaderSections.GetItem(Index: Integer): TfcTreeHeaderSection;
begin
Result := TfcTreeHeaderSection(inherited GetItem(Index));
end;
function TfcTreeHeaderSections.GetOwner: TPersistent;
begin
Result := HeaderControl;
end;
procedure TfcTreeHeaderSections.SetItem(Index: Integer; Value: TfcTreeHeaderSection);
begin
inherited SetItem(Index, Value);
end;
procedure TfcTreeHeaderSections.Update(Item: TCollectionItem);
begin
if Item <> nil then
HeaderControl.UpdateSection(Item.Index) else
HeaderControl.UpdateSections;
end;
procedure TfcTreeHeaderControl.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) then
begin
if AComponent = Tree then Tree:= nil;
if AComponent = FImageList then FImageList:= nil;
end
end;
constructor TfcTreeHeaderControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [];
Align := alTop;
Align := alNone;
Height := 17;
FSections := TfcTreeHeaderSections.Create(Self);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self;
FOptions:= [thcoAllowColumnMove, thcoSortTreeOnClick, thcoRightBorder];
HotTrackSection:= -1;
end;
destructor TfcTreeHeaderControl.Destroy;
begin
FCanvas.Free;
FSections.Free;
DesignerForm.Free;
inherited Destroy;
end;
procedure TfcTreeHeaderControl.CreateParams(var Params: TCreateParams);
const HDS_DRAGDROP = $0040; {W2W}
begin
InitCommonControl(ICC_LISTVIEW_CLASSES);
inherited CreateParams(Params);
CreateSubClass(Params, 'SysHeader32');
with Params do
begin
Style := Style or HDS_BUTTONS;
if FHotTrack then Style := Style or HDS_HOTTRACK;
WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW);
end;
if thcoAllowColumnMove in Options then
Params.Style := Params.Style or HDS_DRAGDROP; { W2W}
end;
procedure TfcTreeHeaderControl.CreateWnd;
begin
inherited CreateWnd;
UpdateSections;
end;
procedure TfcTreeHeader.DrawSection(Section: TfcTreeHeaderSection;
const Rect: TRect; Pressed: Boolean);
begin
if Assigned(FOnDrawSection) then
FOnDrawSection(self, Section, Rect, Pressed) else
HeaderControl.FCanvas.FillRect(Rect);
end;
procedure TfcTreeHeader.SectionMove(Section: TfcTreeHeaderSection;
DragFrom, DragTo: integer; var AllowMove: boolean);
begin
if Assigned(FOnSectionMove) then
FOnSectionMove(Self, Section, DragFrom, DragTo, AllowMove);
end;
procedure TfcTreeHeader.SectionClick(Section: TfcTreeHeaderSection);
begin
if Assigned(FOnSectionClick) then FOnSectionClick(Self, Section);
end;
procedure TfcTreeHeader.SectionResize(Section: TfcTreeHeaderSection);
begin
if Assigned(FOnSectionResize) then FOnSectionResize(Self, Section);
if HeaderControl.Tree<>Nil then
begin
TfcDBCustomTreeView(HeaderControl.Tree).LayoutChanged; // Horz Scrollbar needs to be adjusted
end;
end;
procedure TfcTreeHeader.SectionTrack(Section: TfcTreeHeaderSection;
Width: Integer; State: TSectionTrackState);
begin
if Assigned(FOnSectionTrack) then FOnSectionTrack(Self, Section, Width, State);
end;
procedure TfcTreeHeaderControl.SetHotTrack(Value: Boolean);
begin
if FHotTrack <> Value then
begin
FHotTrack := Value;
RecreateWnd;
end;
end;
procedure TfcTreeHeaderControl.SetSections(Value: TfcTreeHeaderSections);
begin
FSections.Assign(Value);
end;
procedure TfcTreeHeaderControl.UpdateItem(Message, Index: Integer);
var
Item: THDItem;
begin
with Sections[Index] do
begin
FillChar(Item, SizeOf(Item), 0);
Item.mask := HDI_WIDTH or HDI_TEXT or HDI_FORMAT;
Item.cxy := Width;
Item.pszText := PChar(Text);
Item.cchTextMax := Length(Text);
case Alignment of
taLeftJustify: Item.fmt := HDF_LEFT;
taRightJustify: Item.fmt := HDF_RIGHT;
else
Item.fmt := HDF_CENTER;
end;
if True or (Style = hsOwnerDraw) then
Item.fmt := Item.fmt or HDF_OWNERDRAW else
Item.fmt := Item.fmt or HDF_STRING;
SendMessage(Handle, Message, Index, Integer(@Item));
end;
end;
procedure TfcTreeHeaderControl.UpdateSection(Index: Integer);
begin
if HandleAllocated then UpdateItem(HDM_SETITEM, Index);
end;
procedure TfcTreeHeaderControl.UpdateSections;
var
I: Integer;
begin
if HandleAllocated then
begin
for I := 0 to SendMessage(Handle, HDM_GETITEMCOUNT, 0, 0) - 1 do
SendMessage(Handle, HDM_DELETEITEM, 0, 0);
for I := 0 to Sections.Count - 1 do UpdateItem(HDM_INSERTITEM, I);
end;
end;
procedure TfcTreeHeaderControl.CNDrawItem(var Message: TWMDrawItem);
var
SaveIndex: Integer;
pressed: boolean;
offset: integer;
Section: TfcTreeHeaderSection;
tempRect: TRect;
TopPosition: integer;
RequiredSpace: integer;
TextRect, ImageRect: TRect;
ImageListWidth: Integer;
pt: TPoint;
{$ifdef fcUseThemeManager}
Details: TThemedElementDetails;
{$endif}
begin
with Message.DrawItemStruct^ do
begin
SaveIndex := SaveDC(hDC);
FCanvas.Handle := hDC;
FCanvas.Font := Font;
FCanvas.Brush.Color := clBtnFace;
FCanvas.Brush.Style := bsSolid;
GetCursorPos(pt);
pt:= ScreenToClient(pt);
pressed:= itemState and ODS_SELECTED <> 0;
if fcUseThemes(self) then //ThemeServices.ThemesEnabled then
begin
{$ifdef fcUseThemeManager}
if (HotTrackSection>=0) and PtInRect(rcItem, pt) then
begin
if pressed then
Details := ThemeServices.GetElementDetails(thHeaderItemPressed)
else
Details := ThemeServices.GetElementDetails(thHeaderItemHot)
end
else
Details := ThemeServices.GetElementDetails(thHeaderItemNormal);
ThemeServices.DrawElement(Canvas.Handle, Details, rcItem);
{$endif}
// exit;
end;
if HotTrack and (HotTrackSection>=0) then begin
if (HotTrackSection>=0) and PtInRect(rcItem, pt) then
begin
FCanvas.Font.Color:= clBlue;
end;
end;
if Assigned(Header.FOnDrawSection) then
try
Header.DrawSection(Sections[itemID], rcItem,
itemState and ODS_SELECTED <> 0);
finally
end;
begin
Section:= Sections[itemID];
if pressed then offset:= 1 else offset:= 0;
tempRect:= rcItem;
if Section.ImageIndex<0 then
ImageListWidth:= 0
else if Images=nil then ImageListWidth:=0
else ImageListWidth:= Images.Width+1;
RequiredSpace:= Canvas.TextWidth(Section.Text) + ImageListWidth;
TextRect:= tempRect;
ImageRect:= TempRect;
case Section.Alignment of
taLeftJustify:
if Section.ImageAlignment = taLeftJustify then
begin
TextRect.Left:= TempRect.Left + offset + ImageListWidth + 4;
TextRect.Right:= TextRect.Left + Canvas.TextWidth(Section.Text) + 2;
ImageRect.Left:= TempRect.Left + 4 + offset;
ImageRect.Right:= ImageRect.Left + ImageListWidth;
end
else begin
TextRect.Left:= TempRect.Left+offset + 4;
TextRect.Right:= TextRect.Left + Canvas.TextWidth(Section.Text) + 2;
ImageRect.Left:= TextRect.Right + 2;
ImageRect.Right:= ImageRect.Left + ImageListWidth;
end;
taRightJustify:
if Section.ImageAlignment = taLeftJustify then
begin
TextRect.Right:= TextRect.Right - 4;
TextRect.Left:= TextRect.Right+offset-2-Canvas.TextWidth(Section.Text);
ImageRect.Right:= TextRect.Left - 2;
ImageRect.Left:= ImageRect.Right - ImageListWidth;
end
else begin
TextRect.Right:= TextRect.Right - ImageListWidth - 4;
TextRect.Left:= TextRect.Right+offset-2-Canvas.TextWidth(Section.Text);
ImageRect.Right:= ImageRect.Right;
ImageRect.Left:= ImageRect.Right - ImageListWidth - 2 + offset;
end;
taCenter:
if Section.ImageAlignment = taLeftJustify then
begin
ImageRect.Left:= TextRect.Left + offset + ((TempRect.Right-TempRect.Left) - RequiredSpace) div 2;
ImageRect.Right:= ImageRect.Left + ImageListWidth;
TextRect.Left:= ImageRect.Right + 1;
TextRect.Right:= TextRect.Left + Canvas.TextWidth(Section.Text) + 2;
end
else begin
TextRect.Left:= TextRect.Left + offset + ((TempRect.Right-TempRect.Left) - RequiredSpace) div 2;
TextRect.Right:= TextRect.Left + Canvas.TextWidth(Section.Text) + 2;
ImageRect.Left:= TextRect.Right + 2;
ImageRect.Right:= ImageRect.Left + ImageListWidth;
end;
end;
if (Images<>Nil) and (Section.ImageIndex>=0) then
begin
TopPosition:= rcItem.Top + offset + (rcItem.Bottom - rcItem.Top - Images.Height) div 2;
Images.Draw(Canvas, ImageRect.Left, TopPosition, Section.ImageIndex);
end;
WriteTextLines(Canvas, TextRect, 0, 2+offset, PChar(Section.Text),
taLeftJustify,
[wtoAmpersandToUnderline, wtoEllipsis]);
{ if ThemeServices.ThemesEnabled and False then
begin
Details := ThemeServices.GetElementDetails(ttbSeparatorNormal);
TempRect:= rcItem;
TempRect.Left:= TempRect.Left - 2;
ThemeServices.DrawElement(Canvas.Handle, Details, TempRect);
end;
}
end;
FCanvas.Handle := 0;
RestoreDC(hDC, SaveIndex);
end;
Message.Result := 1;
end;
procedure TfcTreeHeaderControl.RearrangeTreeColumns;
var s:string;
i:integer;
OldDisplayFieldCount: integer;
fldName: string;
begin
s:='';
for i:=0 to Sections.count-1 do begin
fldName:= Sections[i].FieldName;
if fldName = '' then
fldName:= Sections[i].Text;
s:=s+'"'+Sections[i].FieldName+'"';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -