var
VLB:array [0..255] of TLabel;
{……}
定义该数组后,我又定义了一个新的过程来处理VLB[x].wndproc,这样可以处理每个lael的消息,可是收到消息后,我却无法判断这个消息是发给哪个label的,比如收到了wm_mousemove消息,那么鼠标究竟是在哪个label上移动发出的呢?
另外,如果我得到了一个label,那么怎么才能判断出它是数组中的第几个元素呢?当然我可以通过循环来遍历,有没有简单的方法呢?比如tstringlist中的indexof此类的方法。
VLB:array [0..255] of TLabel;
{……}
定义该数组后,我又定义了一个新的过程来处理VLB[x].wndproc,这样可以处理每个lael的消息,可是收到消息后,我却无法判断这个消息是发给哪个label的,比如收到了wm_mousemove消息,那么鼠标究竟是在哪个label上移动发出的呢?
另外,如果我得到了一个label,那么怎么才能判断出它是数组中的第几个元素呢?当然我可以通过循环来遍历,有没有简单的方法呢?比如tstringlist中的indexof此类的方法。
解决方案 »
- 文件的读写问题,针对对象的读写!
- 一个好笑的问题:怎么在Delphi(我用的是Delphi5.0)的代码环境中设置显示代码行数。
- 大家帮忙推荐一款和Delphi结合的比较好的流程组件.
- 拥有导航窗口的应用程序如何设计???
- 怎样在fastreport实现按一个订单号码打印出相应的一页来!!!在线等...
- 一个简单问题,但是不好下手,大家来帮忙啊!(关于在DBGrid中编辑数据)
- 我的程序前面莫名其妙多了这些代码,什么意思啊,运行到不碍事,帮忙看看!
- 在DELPHI中如何读出指定EXECL文件的行和列的总数?
- 在存取DB时出现如下错误,如何解决:corrupt table-index-header-file:c\*****\?????.db
- 我怎样才能在另一个程序的窗体的Edit实现输入
- 如何实现录音功能
- DBGrid多重选择问题, 急啊,在线等待
你在发送消息时是怎样区别的,也就是说这个消息将发向何处,何个Label?先把这个问题弄明白有没有简单的方法呢?比如tstringlist中的indexof此类的方法
请你读一下tstringlist中的indexof方法的源代码!
你知道它是怎么做的吗?工作没有捷径。
然后用tag属性就可以定位了
先谢谢你的回答啊。不过请你再看一下我的表述。wm_mousemove是windows的标准消息,不是我发的,我只是用一个自定义过程来处理。也就是说接管Label的wndproc过程;也正因为如此,我才不知道这个过程中,收到的消息到底是发给哪个Label的消息。
另,寻找捷径并不是偷懒,我希望懂的很多很深,但如果有更好的方法,为什么还要一切从头做起呢?也许你不习惯Delphi的RAD的快捷,但我欣赏。
to VisualLion(狮子)
我已经把tag属性用做其他用途了 :(,我觉得Delphi的Tag属性非常好,可惜如果每个control都有个data属性就更好了,那就可以把一个record给每个control,记载更多信息了。
你定义了控件数组之后,并且要拦截WM_MOUSEMOVE消息,你可以不使用窗口过程,因为那样就破坏掉了VCL的封装。你可以直接利用一个循环将所有的控件的OnMouseMove事件指向同一个事件,在那个事件的过程里面,你只需要利用Sender来判断是哪个TLabel就可以了。没有必要得到他在数组中的索引,对么?因为到头来你还是要转换成对象。如果你一定要用的话,你可以在设置事件的时候,修改Tag属性
被触发时,其中的Sender自然向你说明了哪个控件触发的
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;
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.
我发现CSDN最近好象又出了许多高手啊,呵呵,再次感谢回答问题的各位朋友!
不过我要先回去把问题实验完毕了再说给分,各位不着急吧? :)
然后用tag属性就可以定位了