⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xcontrols.pas

📁 我自己用的Delphi函数单元 具体说明见打包文件的HELP目录下面
💻 PAS
字号:
unit xControls;

interface

uses Windows, Messages, SysUtils, Forms, Classes, Controls, ComCtrls;

//------------------------------------------------------------------//
procedure EnableControl(AControl: TControl; Enable: Boolean);
procedure EnableChildControls(AControl: TControl; Enable: Boolean);
procedure EnableClassControl(AControl: TControl; Enable: Boolean; ControlClass: TControlClass);

//------------------------------------------------------------------//
procedure SelectPageIndex(const PC: TPageControl; const iIndex: Integer; const Animated: Boolean);
procedure MakeSureTabVisible(const PC: TPageControl);

//------------------------------------------------------------------//
procedure LoadTreeViewFromTextFile(Nodes: TTreeNodes; Filename: string);
procedure SaveTreeViewToTextFile(Nodes: TTreeNodes; Filename: string);

implementation

uses xStrings;

//------------------------------------------------------------------//
//控件及所属子控件使能或禁止。
procedure EnableControl(AControl: TControl; Enable: Boolean);
var
  I: Integer;
begin
  AControl.Enabled := Enable;
  if AControl is TWinControl then
    with TWinControl(AControl) do
    begin
      for I := 0 to ControlCount - 1 do
        EnableControl(Controls[I], Enable);
    end;
end;

//------------------------------------------------------------------//
//控件所属子控件使能或禁止。
procedure EnableChildControls(AControl: TControl; Enable: Boolean);
var
  I: Integer;
begin
  if AControl is TWinControl then
    with TWinControl(AControl) do
    begin
      for I := 0 to ControlCount - 1 do
        EnableControl(Controls[I], Enable);
    end;
end;

//------------------------------------------------------------------//
//某类控件使能或禁止。如所有的TEdit禁止使用。
procedure EnableClassControl(AControl: TControl; Enable: Boolean; ControlClass: TControlClass);
var
  I: Integer;
begin
  if (AControl is ControlClass) then AControl.Enabled := Enable;
  
  if AControl is TWinControl then
    with TWinControl(AControl) do
    begin
      for I := 0 to ControlCount - 1 do
        EnableClassControl(Controls[I], Enable, ControlClass);
    end;
end;

//------------------------------------------------------------------//
//选择TPageControl的某个页面,Animated = True时从当前页一页页地翻到指定页。
procedure SelectPageIndex(const PC: TPageControl; const iIndex: Integer; const Animated: Boolean);
var
  oIndex, I: Integer;
begin
  with PC do
  begin
    oIndex := ActivePage.pageindex;
    if Animated then
    begin
      while (ActivePage.pageindex <> iIndex) do
      begin
        SelectNextPage(iIndex > oIndex);
        if ActivePage.pageindex = oIndex then Break;
      end;
      // if not ActivePage.TabVisible then ActivePage := Pages[oIndex];
    end
    else
    begin
      for I := 0 to PageCount - 1 do
        if Pages[I].pageindex = iIndex then
        begin
          if Pages[I].TabVisible then ActivePage := Pages[I] else ActivePage := Pages[oIndex];
          Exit;
        end;
    end;
  end;
end;

//------------------------------------------------------------------//
//若TPageControl组件页面动态隐藏或显示。必须小心若被隐藏的页面正是当前页,
//则TPageControl陷入没有当前页面的情况,此时须切换一下可视页,隐藏页面后
//调用本函数可保证不出现此情况。
procedure MakeSureTabVisible(const PC: TPageControl);
begin
  while not PC.ActivePage.TabVisible do PC.SelectNextPage(True);
end;

//------------------------------------------------------------------//
//将TreeView组件节点内容从文件装入。
procedure LoadTreeViewFromTextFile(Nodes: TTreeNodes; Filename: string);
var
  F: TextFile;
  
  function ProcessNode(Node: TTreeNode; LevelNo: Integer): TTreeNode;
  var
    S : string; 
    No: Integer;
  begin
    Result := Node;
    repeat
      readln(F, S);
      No := ParseRPLNo(S);
      if No > LevelNo then // 下一层
      begin
        Node := ProcessNode(Nodes.addchild(Node, S), No);
      end else if No < LevelNo then // 上一层
      begin
        Result := Nodes.Add(Node.Parent, S);
        Exit;
      end else // 同一层
        Node := Nodes.Add(Node, S);
      
    until EOF(F);
  end;
  
begin
  Assignfile(F, Filename);
  reset(F);
  
  ProcessNode(nil, 1);
  
  CloseFile(F);
end;

//------------------------------------------------------------------//
//将TreeView组件节点内容保存到文本文件。
procedure SaveTreeViewToTextFile(Nodes: TTreeNodes; Filename: string);
var
  F: TextFile;
  
  procedure ProcessNode(Node: TTreeNode; Depth: Integer);
  begin
    while Node <> nil do
    begin
      Writeln(F, IntToStr(Depth) + ' ' + Node.Text);
      
      if Node.HasChildren then
        ProcessNode(Node.GetFirstChild, Depth + 1);
      
      Node := Node.getNextSibling;
    end;
  end;
  
begin
  Assignfile(F, Filename);
  rewrite(F);
  
  ProcessNode(Nodes.GetFirstNode, 1);
  
  CloseFile(F);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -