天天看點

如何輕松統一軟體界面的風格delphi

老道的程式員們都知道,在軟體業界有一個預設的原則,那就是軟體各界面的風格要統一。包括界面上各控件的字型顔色和大小。是不是每要新加一個界面都要設定字型的顔色和大小呢。答案是可以不用那麼麻煩。

首先設計一個父窗體,供其它窗體繼承。在父窗體的構造函數裡寫上對界面控件字型風格的處理代碼,那麼其它子窗體就會自動複用了。

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;