高手:   小弟想请教一下怎么用程序动态得到我当前点击的控件的Tag值呀?请高手给出实例,小弟高分相送~

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(IntToStr((Sender as TButton).Tag));
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if Sender is TWinControl then
        ShowMessage(IntToStr((Sender as TWinControl).Tag));
    end;
      

  3.   

    if Activecontrol<>nil then
      ShowMessage(IntToStr(ActiveControl.Tag)):
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        procedure MouseTagAppMessage(var Msg: TMsg; var Handled: Boolean);  public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.MouseTagAppMessage(var Msg: TMsg; var Handled: Boolean);
    begin
      if Msg.message = WM_LBUTTONDOWN then  Label1.Caption:=IntToStr(FindVCLWindow(Mouse.CursorPos).Tag);end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMessage := MouseTagAppMessage;
      Button1.Tag := 3;
      Edit1.Tag := 1;
      Edit2.Tag := 2 ;
      Label1.Tag:=4;
    end;procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    beginend;end.能处理TWinControl的后代!
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
      private
        procedure MouseTagAppMessage(var Msg: TMsg; var Handled: Boolean);  public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.MouseTagAppMessage(var Msg: TMsg; var Handled: Boolean);
    begin
      if Msg.message = WM_LBUTTONDOWN then    Label1.Caption := IntToStr(FindVCLWindow(Mouse.CursorPos).Tag);end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMessage := MouseTagAppMessage;
      Button1.Tag := 3;
      Edit1.Tag := 1;
      Edit2.Tag := 2;
      Label1.Tag := 4;
    end;end.能处理TWinControl的后代!
      

  6.   

    写一个通用过程,只要把它塞给相应的控件事件处理就可以用了::
    procedure ControlTag(Sender: TObject);
    begin
      if Sender is TWinControl then
        ShowMessage(IntToStr((Sender as TWinControl).Tag));
    end;
      

  7.   

    showmessage(inttostr(TComponent(form1.ActiveControl).tag));