如何轻松统一软件界面的风格delphi

来源:互联网 发布:rar解压软件 mac版 编辑:程序博客网 时间:2024/05/21 11:23

老道的程序员们都知道,在软件业界有一个默认的原则,那就是软件各界面的风格要统一。包括界面上各控件的字体颜色和大小。是不是每要新加一个界面都要设置字体的颜色和大小呢。答案是可以不用那么麻烦。

首先设计一个父窗体,供其它窗体继承。在父窗体的构造函数里写上对界面控件字体风格的处理代码,那么其它子窗体就会自动复用了。

procedure TfrmFather.ClearIME;//输入法

var

  i:Integer

begin

  if self.Enabled and Self.Visible then self.setfocus;

  for i:= 0 to componentCount - 1 do

  begin

      if Component[i] is TMemo then

          TMemo(Components[i]).ImeName :=''

     else if Component[i] is TEdit then

          TEdit(Components[i]).ImeName :=''

     else if Component[i] is TDateTimePicker then

          TDateTimePicker(Components[i]).ImeName :=''

     else if Component[i] is TComboBox then

          TComboBox(Components[i]).ImeName :=''

     else if Component[i] is TDBGridEh then

          TDBGridEh(Components[i]).ImeName :=''

  end;

end;

procedure InitUserInterface;

begin

  if (Width >= Screen.Width) or (Height >= Screen.Height) then WindowState := wsMaximized;
  Self.Font.Charset := SysParams.FontCharSet;//sysParams可以是一个类的实例也可以是一个纪录。参数可以保存在数据库或ini中
  Self.Font.Name := SysParams.FontName;
  Self.Font.Size := SysParams.FontSize;
  for i := 0 to Self.ComponentCount - 1 do
  begin
   if Self.Components[i].Tag >=100 then Continue;
    if Self.Components[i].InheritsFrom(TEdit) then
    begin
      TEdit(Self.Components[i]).Text := '';
      TEdit(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TMemo) then
    begin
      TMemo(Self.Components[i]).Text := '';
      TMemo(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TComboBox) then
    begin
      TComboBox(Self.Components[i]).ItemIndex := -1;
      TComboBox(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TDateTimePicker) then
    begin
      TDateTimePicker(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TListBox) then
    begin
      TListBox(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TCheckListBox) then
    begin
      TCheckListBox(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TSpinEdit) then
    begin
      TSpinEdit(Self.Components[i]).Color := SysParams.EditClr;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TListView) then
    begin
      TListView(Self.Components[i]).Color := SysParams.TreeClr;
      TListView(Self.Components[i]).ReadOnly := True;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TTreeView) then
    begin
      TTreeView(Self.Components[i]).Color := SysParams.TreeClr;
      TTreeView(Self.Components[i]).ReadOnly := True;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TCheckTree) then
    begin
      TCheckTree(Self.Components[i]).Color := SysParams.TreeClr;
      TCheckTree(Self.Components[i]).ReadOnly := True;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TDBGridEh) then
    begin
      TDBGridEh(Self.Components[i]).Color := SysParams.GridClr;
      TDBGridEh(Self.Components[i]).FixedColor := SysParams.TitleClr;
      TDBGridEh(Self.Components[i]).ReadOnly := True;
      continue;
    end;
    if Self.Components[i].InheritsFrom(TDBGrid) then
    begin
      TDBGrid(Self.Components[i]).Color := SysParams.GridClr;
      TDBGrid(Self.Components[i]).FixedColor := SysParams.TitleClr;
      TDBGrid(Self.Components[i]).ReadOnly := True;
      continue;
    end;
  end;

end;