对于在程序运行过程中,在窗体的panel上动态创建的Label,如何对它们进行拖放(自由移动)。

解决方案 »

  1.   

    不过可以用StaticText代替procedure TForm1.StaticText1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      if (Button = mbLeft) then
      begin
        ReleaseCapture;
        SendMessage((Sender as TStaticText).Handle, WM_SYSCOMMAND, SC_MOVE or 2, 0);
      end;
    end;
      

  2.   

    给你个简单的例子unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        StaticText1: TStaticText;
        procedure StaticText1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StaticText1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      if (Button = mbLeft) then
      begin
        ReleaseCapture;
        SendMessage((Sender as TStaticText).Handle, WM_SYSCOMMAND, SC_MOVE or 2, 0);
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      TT: TStaticText;
    begin
      TT := TStaticText.Create(Application);
      TT.Caption := 'Test';
      TT.Parent := Form1;
      TT.OnMouseDown := StaticText1MouseDown;
    end;end.
      

  3.   

    很好,怎么将动态创建的StaticText的显示的caption文本在程序运行过程中,动态地进行修改呢?有好方法吗?
      

  4.   

    1 把它的Handle记录下来,需要修改谁的Caption就给谁发个消息WM_SETTEXTSendMessage/PostMessage2 每个StaticText起个不同的名字,保存下来,直接修改
      

  5.   

    很感谢postren(小虫),对于每一个动态创建的StaticText的handle记录下来容易,可怎么来修改
    statictext的caption呢,我是这样用SendMessage(sthandle(stn),WM_SETTEXT,Memo1.Lines.Text,0);可是会报错,对于每个StaticText起个不同的名字,来保存的话要用指针数组吧,有这样的例子不?
      

  6.   

    SendMessage(StaticText1.Handle, WM_SETTEXT, 0, Integer(@Memo1.Text[1]));
      

  7.   

    消息的话,需要看WM_SETTEXT的Windows SDK帮助An application sends a WM_SETTEXT message to set the text of a window. WM_SETTEXT  
    wParam = 0;                     // not used; must be zero 
    lParam = (LPARAM)(LPCTSTR)lpsz; // address of window-text string 
      

  8.   

    动态改变left和Top还是可以实现拖放的。