左键双击关闭pagecontrol中的一个分页即一个tabsheet,功能像遨游浏览器一样

左键双击关闭pagecontrol中的一个分页即一个tabsheet,功能像遨游浏览器一样

procedure TfrmServerSetup.PageControl1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
begin
//左键点击并且双击
if (Button = mbLeft) and (ssDouble in Shift) then
begin
   Index := PageControl1.IndexOfTabAt(X, Y);
   if Index >= 0 then
     PageControl1.Pages[Index].Free;
end;
end;

//根据名称检查tab是否已经存在
function FindTab(pagecontrol:TPageControl;tabName:string):integer;
var
  i:integer;
begin
  Result := -1;
  for i := 0 to pagecontrol.PageCount - 1 do
  begin
    if pagecontrol.Pages[i].Name = tabName then
    begin
      Result := i;
      Break;
    end;
  end;
end;
 
procedure FormToSheet(pagecontrol:TPageControl;frm:TForm;tabName,tabCaption:string);
var
  i:integer;
  tab:TTabSheet;
begin
  i := FindTab(pagecontrol,tabName);
  if i > 0 then
  begin
    pagecontrol.Pages[i].TabVisible := True;
    pagecontrol.ActivePageIndex := i;
  end
  else
  begin
    tab := TTabSheet.Create(pagecontrol);
    tab.Name := tabName;
    tab.Caption := tabCaption;
    tab.PageControl := pagecontrol;
    pagecontrol.ActivePage := tab;
    frm.ParentWindow := tab.Handle;
    frm.Tag :=tab.TabIndex;
    tab.InsertControl(frm);
    frm.Show;
  end;
  frm.Show;
end;
//销毁时,可以查找tab上的组件,找到窗体,然后freeandnil,当然也可以通过指针处理,在tag中保存窗体指针地址,销毁时根据这个地址去处理

 

posted @ 2019-05-13 17:24  tc310  阅读(369)  评论(0编辑  收藏  举报