pagecontrol动态创建tabsheet并选择关闭右键菜单等

废话少说,直接看代码好了,在DELPHI2009中编译的,相信其他版本也可以通过的。
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, AppEvnts, Menus;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    Panel1: TPanel;
    Button1: TButton;
    ApplicationEvents1: TApplicationEvents;
    MainMenu1: TMainMenu;
    df1: TMenuItem;
    Button2: TButton;
    TabSheet1: TTabSheet;
    Button3: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    procedure Button1Click(Sender: TObject);
    procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure PageControl1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses unit2;

{$R *.dfm}

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);

begin
   if (Msg.message = WM_LBUTTONDBLCLK) and (Msg.hwnd = PageControl1.Handle) then
       PageControl1.Pages[PageControl1.ActivePageIndex].Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x:ttabsheet;
  i:integer;
begin
   randomize;
   form2:=tform2.Create(application);
   //form2.Show;
   i:=self.PageControl1.PageCount;
   x:=ttabsheet.Create(self.PageControl1);
   x.Name:='mytest1'+inttostr(i+1)+inttostr(random(1000)); ;
   x.Caption:='haha'+inttostr(i+1);
   form2.Parent:=x;
   x.PageControl:=self.PageControl1;
   self.PageControl1.ActivePage:=x;
   form2.ManualDock(self.PageControl1,nil,alClient) ;
   form2.Show;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
      i:   integer;
  begin
      //如果已经存在就退出
      for   i   :=   0   to   PageControl1.PageCount   -   1   do
          if   PageControl1.Pages[i].Caption   =   'Form2'   then//这里直接用Page
              Exit;
      //打开Form
      try
          Application.CreateForm(TForm2,   Form2);
          Form2.Show;
          Form2.ManualDock(PageControl1,   nil,   alClient);

      except
          Form2.Free;
      end;
  end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if application.FindComponent('form2')<>nil then
    application.MessageBox('you','zhaodao le',mb_ok)    ;
end;

procedure TForm1.N1Click(Sender: TObject);
begin
  self.PageControl1.Pages[self.PageControl1.ActivePageIndex].Free;
end;

procedure TForm1.N2Click(Sender: TObject);
var
  i:integer;
begin
  for I := self.PageControl1.PageCount - 1 DOWNTO 0  do
    begin
      //if SELF.PageControl1.Pages[I].Name <> self.PageControl1.ActivePage.Name then
      self.PageControl1.Pages[i].Free;
    end;
end;

procedure TForm1.N3Click(Sender: TObject);
var
  i:integer;
  activeName:string;
begin
  activeName:=self.PageControl1.ActivePage.Name;
  for i := self.PageControl1.PageCount - 1 downto 0 do
  begin
    if self.PageControl1.Pages[i].Name<>activeName then
       self.PageControl1.Pages[i].Free;
  end;

end;

procedure TForm1.PageControl1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  i:integer;
  pos:tpoint;
begin
if button=mbright then
begin
  i:=pagecontrol1.IndexOfTabAt(X,Y);
  self.PageControl1.ActivePageIndex:=i;
  pos:=PageControl1.ClientToScreen(Point(X,Y));
  self.PopupMenu1.popup(pos.X,pos.Y);
end;

end;

end.

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.