pagecontrol动态创建tabsheet并选择关闭右键菜单等
By:Roy.LiuLast updated:2008-12-05
废话少说,直接看代码好了,在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.
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.
From:一号门
Previous:收集了很多JAVASCRIPT 事件
Next:ASP.NET多文件上传
COMMENTS