用controlcount列出窗体上所有对控件数目,用controls[i]来表示某一个控件 例如,for i := 0 to Form1.ControlCount - 1 do if Form1.Controls[i] is TObject then begin listbox1.Visible:=false; ...... ...... end;
procedure TForm1.Onclick(Sender: tobject); begin lv1.Visible := False; end;procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin for i := 0 to form1.ControlCount -1 do begin if (Form1.Controls[i] is TEdit) then (Form1.Controls[i] as TEdit).OnClick := Onclick; if (Form1.Controls[i] is TLabel) then (Form1.Controls[i] as TLabel).OnClick := Onclick;
end; end;
呵呵,前面幾位的方法是太笨了,而且彈性不好,若控件有單擊事件,切換存在問題! 其實只要理解了所謂的“click”事件,各位就明白怎么實現了,參考代碼如下:interface ... //重寫父類的wm_command消息處理函數 procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; ...implementationprocedure WMCommand(var Message: TWMCommand); begin if not (message.Ctl=listBox1.Handle) then ListBox1.Visible := False; inherited; end; 所謂的click,是由wm_command轉化而來,因為當鼠標單擊時,都會產生wm_command
上面代碼嚴謹點,應該多一些判斷procedure WMCommand(var Message: TWMCommand); begin if (message.Ctl<>0) and (message.Ctl<>listBox1.Handle) then ListBox1.Visible := False else ListBox1.Visible := True; inherited; end;同時修正上面的觀點“所謂的click,是由wm_command轉化而來”,準確地說,大部分的控件的click是由這個轉化的,若部分控件沒有處理cn_command(由wm_command轉化而來),在TControl里面會處理WMLButtonUp消息,來形成click事件。因此,這段代碼對groupbox無效
當然,對沒有handle的控件也無效
顶下 简单点是不是可以这么写:ListBox1.Visable := not ((message.Ctl <>0) and (message.Ctl <>listBox1.Handle));
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
ListBox1: TListBox;
Edit1: TEdit;
GroupBox1: TGroupBox;
procedure FormCreate(Sender: TObject);
private
procedure OneClick(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Label1.OnClick:=OneClick;
Edit1.OnClick:=OneClick;
GroupBox1.OnClick:=OneClick;
end;procedure TForm1.OneClick(Sender: TObject);
begin
ListBox1.Visible:=not ListBox1.Visible;
end;end.
例如,for i := 0 to Form1.ControlCount - 1 do
if Form1.Controls[i] is TObject then
begin
listbox1.Visible:=false;
......
......
end;
begin
lv1.Visible := False;
end;procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to form1.ControlCount -1 do
begin
if (Form1.Controls[i] is TEdit) then
(Form1.Controls[i] as TEdit).OnClick := Onclick;
if (Form1.Controls[i] is TLabel) then
(Form1.Controls[i] as TLabel).OnClick := Onclick;
end;
end;
其實只要理解了所謂的“click”事件,各位就明白怎么實現了,參考代碼如下:interface
...
//重寫父類的wm_command消息處理函數
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; ...implementationprocedure WMCommand(var Message: TWMCommand);
begin
if not (message.Ctl=listBox1.Handle) then
ListBox1.Visible := False;
inherited;
end; 所謂的click,是由wm_command轉化而來,因為當鼠標單擊時,都會產生wm_command
begin
if (message.Ctl<>0) and (message.Ctl<>listBox1.Handle) then
ListBox1.Visible := False
else
ListBox1.Visible := True;
inherited;
end;同時修正上面的觀點“所謂的click,是由wm_command轉化而來”,準確地說,大部分的控件的click是由這個轉化的,若部分控件沒有處理cn_command(由wm_command轉化而來),在TControl里面會處理WMLButtonUp消息,來形成click事件。因此,這段代碼對groupbox無效
顶下 简单点是不是可以这么写:ListBox1.Visable := not ((message.Ctl <>0) and (message.Ctl <>listBox1.Handle));
之所以要多一個判斷,是要避免替換其他控件可能已存在的click事件