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

📄 main.pas

📁 用delphi写的密码管理工具.
💻 PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ToolWin, ImgList, Menus, ExtCtrls, PassList, GenPass, Log, IniFiles,
  SendString;

type
  TfrmMain = class(TForm)
    Bevel1: TBevel;
    TreeView1: TTreeView;
    ImageList1: TImageList;
    Splitter1: TSplitter;
    MainMenu1: TMainMenu;
    qq1: TMenuItem;
    N1: TMenuItem;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure N1Click(Sender: TObject);
  private
    { Private declarations }
    GenPassNode, PassListNode, LogNode, SendStringNode: TTreeNode;
    WindowX, WindowY, WindowWidth, WindowHeight, TreeViewWidth: Integer;
    procedure ApplicationOnActivate(Sender: TObject);
    procedure ReadConfig;
    procedure ResizeWindow;
  public
    { Public declarations }
    LogFile: string;
    SelGrpIndex: Integer;
    slWinTitle, slWinHandle: TStringList;
    procedure GetWindows;
  end;

var
  frmMain: TfrmMain;
  fraPassList: TfraPassList;
  fraGenPass: TfraGenPass;
  fraLog: TfraLog;
  fraSendString: TfraSendString;

implementation

uses SelectWin, PassAction;

{$R *.DFM}

function EnumWindowsProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall;
var
  WindowName: PChar;
  w: string;
  i: Integer;
begin
  Result := True;

  WindowName := AllocMem(128);
  try
    GetWindowText(WHandle, WindowName, 128);
    w := WindowName;
  finally
    FreeMem(WindowName);
  end;
  if (w = '') or (w = 'Program Manager') or (w = frmMain.Caption) or (w = frmSelectWin.Caption) then Exit;

  i := GetWindowLong(WHandle, GWL_STYLE);
  if i and WS_VISIBLE = 0 then Exit;

  frmMain.slWinTitle.Add(w);
  frmMain.slWinHandle.Add(IntToStr(WHandle));
end;

procedure TfrmMain.ApplicationOnActivate(Sender: TObject);
begin
  GetWindows;
end;

procedure TfrmMain.ReadConfig;
var
  IniFile: TIniFile;

  procedure ReadAction;
  var
    SecNames: TStringList;
    i: Integer;
    sName, sActionType, sKeyString, sHotKeyString: string;
    ActionType: TActionType;
    HotKey: TShortCut;
    ShiftState: TShiftState;
    Key: Word;
  begin
    SecNames := TStringList.Create;
    try
      IniFile.ReadSections(SecNames);
      for i := SecNames.Count - 1 downto 0 do
        if Copy(LowerCase(SecNames[i]), 1, 6) <> 'action' then
          SecNames.Delete(i);

      for i := 0 to SecNames.Count - 1 do
        with fraPassList do
        begin
          sName := IniFile.ReadString(SecNames[i], 'Name', '');

          sActionType := LowerCase(IniFile.ReadString(SecNames[i], 'ActionType', ''));
          if sActionType = 'copy' then
            ActionType := atCopy
          else if sActionType = 'send' then
            ActionType := atSend
          else if sActionType = 'sendtowindow' then
            ActionType := atSendToWindow
          else
            ActionType := atCopy;

          sKeyString := IniFile.ReadString(SecNames[i], 'KeyString', '');

          sHotKeyString := IniFile.ReadString(SecNames[i], 'HotKey', '');
          if sHotKeyString <> '' then
          begin
            ShiftState := [];
            if Pos('+', sHotKeyString) > 0 then
              ShiftState := ShiftState + [ssShift];
            if Pos('^', sHotKeyString) > 0 then
              ShiftState := ShiftState + [ssCtrl];
            if Pos('%', sHotKeyString) > 0 then
              ShiftState := ShiftState + [ssAlt];
            Key := StrToInt(Copy(sHotKeyString, Pos('{', sHotKeyString) + 1,
              Pos('}', sHotKeyString) - Pos('{', sHotKeyString) - 1));
          end
          else
            Key := 0;
          HotKey := ShortCut(Key, ShiftState);

          PassActions[i] := TPassAction.Create(sName, ActionType, sKeyString, HotKey);
          Inc(PassActionCount);
        end;
    finally
      SecNames.Free;
    end;
  end;
  
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'PassMgr.ini');
  try
    ReadAction;

    WindowX := IniFile.ReadInteger('Window', 'X', (Screen.Width - Width) div 2);
    WindowY := IniFile.ReadInteger('Window', 'Y', (Screen.Height - Height) div 2);
    WindowWidth := IniFile.ReadInteger('Window', 'Width', Width);
    WindowHeight := IniFile.ReadInteger('Window', 'Height', Height);
    TreeViewWidth := IniFile.ReadInteger('Window', 'TreeViewWidth', TreeView1.Width);
  finally
    IniFile.Free;
  end;
end;

procedure TfrmMain.ResizeWindow;
begin
  MoveWindow(Handle, WindowX, WindowY, WindowWidth, WindowHeight, False);
  TreeView1.Width := TreeViewWidth;
end;

procedure TfrmMain.GetWindows;
begin
  slWinTitle.Clear;
  slWinHandle.Clear;
  EnumWindows(@EnumWindowsProc, 0);
end;

{**************************************************************************************************}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  slWinTitle := TStringList.Create;
  slWinHandle := TStringList.Create;

  fraPassList := TfraPassList.Create(Self);
  fraPassList.Initialize;

  fraGenPass := TfraGenPass.Create(Self);
  fraGenPass.btnCopy.Hint := '复制选中部分到剪贴板'#$D#$A'若未选中则复制全部';

  fraLog := TfraLog.Create(Self);
  fraSendString := TfraSendString.Create(Self);
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  slWinTitle.Free;
  slWinHandle.Free;

  fraPassList.Finalize;
  fraPassList.Free;

  fraGenPass.Free;
  fraLog.Free;
  fraSendString.Free;
end;

procedure TfrmMain.FormShow(Sender: TObject);
var
  i: Integer;
  tn: TTreeNode;
begin
  ReadConfig;
  ResizeWindow;
  fraPassList.LoadPassList;

  { 设置密码组界面 }
  for i := 0 to fraPassList.slPassGroup.Count - 1 do
  begin
    tn := TreeView1.Items.AddChild(TreeView1.Items[2], fraPassList.slPassGroup[i]);
    tn.ImageIndex := 2;
    tn.SelectedIndex := 2;
  end;

  GenPassNode := TreeView1.Items[0].Item[0];
  PassListNode := TreeView1.Items[0].Item[1];
  LogNode := TreeView1.Items[0].Item[2];
  SendStringNode := TreeView1.Items[0].Item[3];
  TreeView1.FullExpand;
  SelGrpIndex := -1;
  TreeView1.Selected := TreeView1.Items[2].GetFirstChild;
  TreeView1Change(nil, nil);

  LogFile := ExtractFilePath(Application.ExeName) + '密码管理器.log';

  fraPassList.Parent := Self;
  fraPassList.Align := alClient;
  fraPassList.CreateMenu;

  fraGenPass.Parent := Self;
  fraGenPass.Align := alClient;

  fraLog.Parent := Self;
  fraLog.Align := alClient;
  fraLog.LoadLog;

  fraSendString.Parent := Self;
  fraSendString.Align := alClient;

  GetWindows;
  Application.OnActivate := ApplicationOnActivate;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  IniFile: TIniFile;
  Rect: TRect;
begin
  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'PassMgr.ini');
    try
      GetWindowRect(Handle, Rect);
      IniFile.WriteInteger('Window', 'X', Rect.Left);
      IniFile.WriteInteger('Window', 'Y', Rect.Top);
      IniFile.WriteInteger('Window', 'Width', Rect.Right - Rect.Left);
      IniFile.WriteInteger('Window', 'Height', Rect.Bottom - Rect.Top);
      IniFile.WriteInteger('Window', 'TreeViewWidth', TreeView1.Width);
    finally
      IniFile.Free;
    end;
  except
  end;
end;

procedure TfrmMain.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  iPreIndex: Integer;
begin
  if TreeView1.Selected = nil then Exit;

  if (TreeView1.Selected.Parent <> nil) and (TreeView1.Selected.Parent = PassListNode) then
  begin
    fraPassList.Show;
    fraGenPass.Hide;
    fraLog.Hide;
    fraSendString.Hide;

    iPreIndex := SelGrpIndex;
    SelGrpIndex := fraPassList.slPassGroup.IndexOf(TreeView1.Selected.Text);
    if iPreIndex <> SelGrpIndex then
      fraPassList.GenList;
  end
  else if TreeView1.Selected = GenPassNode then
  begin
    fraPassList.Hide;
    fraGenPass.Show;
    fraLog.Hide;
    fraSendString.Hide;
  end
  else if TreeView1.Selected = LogNode then
  begin
    fraPassList.Hide;
    fraGenPass.Hide;
    fraLog.Show;
    fraSendString.Hide;
  end
  else if TreeView1.Selected = SendStringNode then
  begin
    fraPassList.Hide;
    fraGenPass.Hide;
    fraLog.Hide;
    fraSendString.Show;
  end;
end;

procedure TfrmMain.N1Click(Sender: TObject);
begin
  if (Sender as TMenuItem).Checked then
    SetWindowPos(frmMain.Handle, HWND_NOTOPMOST, WindowX, WindowY, WindowWidth, WindowHeight, 0)
  else
    SetWindowPos(frmMain.Handle, HWND_TOPMOST, WindowX, WindowY, WindowWidth, WindowHeight, 0);

  (Sender as TMenuItem).Checked := not (Sender as TMenuItem).Checked;
end;

end.

⌨️ 快捷键说明

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