var
  VLB:array [0..255] of TLabel;
{……}
定义该数组后,我又定义了一个新的过程来处理VLB[x].wndproc,这样可以处理每个lael的消息,可是收到消息后,我却无法判断这个消息是发给哪个label的,比如收到了wm_mousemove消息,那么鼠标究竟是在哪个label上移动发出的呢?
另外,如果我得到了一个label,那么怎么才能判断出它是数组中的第几个元素呢?当然我可以通过循环来遍历,有没有简单的方法呢?比如tstringlist中的indexof此类的方法。

解决方案 »

  1.   

    比如收到了wm_mousemove消息,那么鼠标究竟是在哪个label上移动发出的呢?
    你在发送消息时是怎样区别的,也就是说这个消息将发向何处,何个Label?先把这个问题弄明白有没有简单的方法呢?比如tstringlist中的indexof此类的方法
    请你读一下tstringlist中的indexof方法的源代码!
    你知道它是怎么做的吗?工作没有捷径。
      

  2.   

    Label[i].tag:= i;
    然后用tag属性就可以定位了
      

  3.   

    to HanJingJingHan(静) 
    先谢谢你的回答啊。不过请你再看一下我的表述。wm_mousemove是windows的标准消息,不是我发的,我只是用一个自定义过程来处理。也就是说接管Label的wndproc过程;也正因为如此,我才不知道这个过程中,收到的消息到底是发给哪个Label的消息。
    另,寻找捷径并不是偷懒,我希望懂的很多很深,但如果有更好的方法,为什么还要一切从头做起呢?也许你不习惯Delphi的RAD的快捷,但我欣赏。
    to  VisualLion(狮子) 
    我已经把tag属性用做其他用途了 :(,我觉得Delphi的Tag属性非常好,可惜如果每个control都有个data属性就更好了,那就可以把一个record给每个control,记载更多信息了。
      

  4.   

    可以用消息中的xPos和ypos来取得鼠标位置,其他的我就不知道怎么做了
      

  5.   

    何必要用窗口过程呢?
    你定义了控件数组之后,并且要拦截WM_MOUSEMOVE消息,你可以不使用窗口过程,因为那样就破坏掉了VCL的封装。你可以直接利用一个循环将所有的控件的OnMouseMove事件指向同一个事件,在那个事件的过程里面,你只需要利用Sender来判断是哪个TLabel就可以了。没有必要得到他在数组中的索引,对么?因为到头来你还是要转换成对象。如果你一定要用的话,你可以在设置事件的时候,修改Tag属性
      

  6.   

    同意楼上的说法,LABLE要响应及处理的消息,都可以为其提供相应的函数指针就ok了,消息
    被触发时,其中的Sender自然向你说明了哪个控件触发的
      

  7.   

    楼主的问题可以这样解决:运用面向对象的思想重新封装Tlabel 的 wndproc ,即可解决恼人的问题。解决方法简单。如下代码:unit Unit2; //written by zjy6631 2003.06.08interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;Type
       TLabelPrc=Class(TLabel)
       Protected
          Procedure WndProc(var Message: TMessage);override;
       end;type
      TForm1 = class(TForm)
        procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
         VLB:array [0..3] of TLabelPrc;
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      Caption:=(sender as TLabel).Name;
    end;procedure TForm1.FormCreate(Sender: TObject);
    Var
      i:integer;
    begin
      for i:=0 to 3 do
      begin
        VLB[i]:=TLabelPrc.Create(self);
        VLB[i].Name:='Label'+IntToStr(i);
        VLB[i].Top:=10;
        VLB[i].Left:=10+i*100;
        VLB[i].Width:=80;
        VLB[i].Height:=24;
        VLB[i].Parent:=self;
        VLB[i].OnMouseMove:=Label1MouseMove;
      end;
    end;{ TLabelPrc }procedure TLabelPrc.WndProc(var Message: TMessage);
    begin
      inherited;
      if Message.Msg=WM_LBUTTONDOWN then
      begin
        Form1.Caption:='Hi';
      end
      else
      if Message.Msg=WM_Mousemove then
      begin
        Form1.Caption:=self.Name;
      end;
    end;
      

  8.   

    下面从窗体的caption上获得标签在数组中的序号。unit Unit2; //Rewritten by zjy6631 2003.06.08interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;Type
       TLabelPrc=Class(TLabel)
       Protected
          Procedure WndProc(var Message: TMessage);override;
       end;type
      TForm1 = class(TForm)
        procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
         VLB:array [0..3] of TLabelPrc;
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      function getIndex(name:String):String;
    implementation{$R *.dfm}function getIndex(name:String):String;
    begin
      Result:=copy(Name,6,255);
    end;procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      Caption:=(sender as TLabel).Name;
    end;procedure TForm1.FormCreate(Sender: TObject);
    Var
      i:integer;
    begin
      for i:=0 to 3 do
      begin
        VLB[i]:=TLabelPrc.Create(self);
        VLB[i].Name:='Label'+IntToStr(i);
        VLB[i].Top:=10;
        VLB[i].Left:=10+i*100;
        VLB[i].Width:=80;
        VLB[i].Height:=24;
        VLB[i].Parent:=self;
        VLB[i].OnMouseMove:=Label1MouseMove;
      end;
    end;{ TLabelPrc }procedure TLabelPrc.WndProc(var Message: TMessage);
    begin
      inherited;
      if Message.Msg=WM_LBUTTONDOWN then
      begin
        Form1.Caption:='Hi';
      end
      else
      if Message.Msg=WM_Mousemove then
      begin
        Form1.Caption:='这是第'+getIndex(self.Name)+'个标签';
      end;
    end;end.
      

  9.   

    多谢各位!
    我发现CSDN最近好象又出了许多高手啊,呵呵,再次感谢回答问题的各位朋友!
    不过我要先回去把问题实验完毕了再说给分,各位不着急吧? :)
      

  10.   

    Eastunfail(恶鱼杀手) 的方法很好啊,我们也是这么用的,好像没有发现更有效的办法,如果谁有,顺便告诉我把!
      

  11.   

    Label[i].tag:= i;
    然后用tag属性就可以定位了