效果图:
一期功能概要:
a.双击tab关闭tab,双击tab右边空白添加tab(标题为以hhnnsszzz的时间格式命名)
b.切换tab将数据存入dictionary,key为标题,value为memo的内容
实现代码:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, System.Generics.Collections; type TForm1 = class(TForm) TabControl1: TTabControl; Memo1: TMemo; procedure onAppMessage(var Msg: TMsg; var Handled: Boolean); procedure FormCreate(Sender: TObject); procedure TabControl1Change(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var dic: TDictionary<string, string>; preTabCaption: string; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin FreeAndNil(dic); end; procedure TForm1.FormCreate(Sender: TObject); begin //创建dictionary dic := TDictionary<string, string>.Create; //转接消息处理 Application.OnMessage := onAppMessage; //设置上一个key的值为当前的标题 preTabCaption := TabControl1.Tabs[TabControl1.TabIndex]; //把当前的key和value插入到dictionary dic.Add(preTabCaption, Memo1.Text); end; procedure TForm1.onAppMessage(var Msg: tagMSG; var Handled: Boolean); var tmp: string; begin //关闭一个tab if (Msg.message = WM_LBUTTONDBLCLK) and (Msg.hwnd = TabControl1.Handle) then begin // 如果标签只剩下一个,不做任何操作 if TabControl1.Tabs.Count = 1 then begin // 设置上一个key的值为当前标题并读取内容 preTabCaption := TabControl1.Tabs[0]; Memo1.Text := dic.Items[preTabCaption]; Exit; end; // 关闭标签前,通过把标题当做key,删除dictionary中对应的value dic.Remove(TabControl1.Tabs[TabControl1.TabIndex]); if TabControl1.TabIndex = 0 then begin // 标签index=0的时候设置当前index=1 TabControl1.TabIndex := 1; // 删除前一个tab TabControl1.Tabs.Delete(TabControl1.TabIndex - 1); // 设置上一个key的值为当前标题并读取内容 preTabCaption := TabControl1.Tabs[TabControl1.TabIndex]; Memo1.Text := dic.Items[preTabCaption]; Exit; end; // 标签为其他index,设置当前index为index-1 TabControl1.TabIndex := TabControl1.TabIndex - 1; // 删除指定的tab TabControl1.Tabs.Delete(TabControl1.TabIndex + 1); // 设置上一个key的值为当前标题并读取内容 preTabCaption := TabControl1.Tabs[TabControl1.TabIndex]; Memo1.Text := dic.Items[preTabCaption]; end; //新建一个tab if (Msg.message = WM_LBUTTONDBLCLK) and (Msg.hwnd = Self.Handle) then begin // 添加tab 标题为时间分秒毫秒 tmp := FormatDateTime('hhnnsszzz', now); // 添加标题 TabControl1.Tabs.Add(tmp); // 设置当前活动页 TabControl1.TabIndex := TabControl1.Tabs.Count - 1; // 新建tab的value=''添加到dictionary dic.Add(tmp, ''); // 插入到对应value dic.Items[preTabCaption] := Memo1.Text; // 把当前标题设置成上一个key preTabCaption := tmp; //当前新建tab显示内容为'' Memo1.Text := ''; end; end; procedure TForm1.TabControl1Change(Sender: TObject); begin // // 把tab改变之前的内容通过上一个key存入dictionary dic.Items[preTabCaption] := Memo1.Text; // ShowMessage(TabControl1.Tabs[TabControl1.TabIndex]); // 把当前标题当做key,取出value Memo1.Text := dic.Items[TabControl1.Tabs[TabControl1.TabIndex]]; // 把当前标题设置成上一个key preTabCaption := TabControl1.Tabs[TabControl1.TabIndex]; end; end.