delphi 程序运行时移动控件

程序在运行时用户需要对一些控件进行重新移动布局,下次进入界面时显示布局后的

方法1:每移动控件时就把位置写入INI文件中

只需在控件的OnMouseDown事件写如下代码:

procedure TFMain.SpeedButton4MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
const sc_move = $F011;
var
  SysFile: TiniFile;
begin
    releasecapture;
    TSpeedButton(sender).Perform(wm_syscommand, sc_move, 0);
    try
      SysFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Sys.ini');
      sysFile.WriteInteger(TSpeedButton(sender).Name, 'TOP', TSpeedButton(sender).Top);
      sysFile.WriteInteger(TSpeedButton(sender).Name, 'Left', TSpeedButton(sender).Left);
    finally
      SysFile.Free;
    end;
end;

方法2:

 封装一函数用于实现控件移动,在控件的OnMouseDown事件中调用即可,然后在关闭时遍利所有控件把位置写入INI

procedure MoveControl(Control: TControl; Shift: TShiftState; X, Y, Prec: integer);

begin   

    if Shift=[ssLeft] then
  begin
    ReleaseCapture;
    Control.Perform(WM_SYSCOMMAND, SC_MANIPULATE, 0);
  end;

 end;

 //关闭写入

  SysFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Sys.ini');

  Try

     for i := 0 to ComponentCount - 1 do
     begin
          sysFile.WriteString(TControl(Components[i]).Name ,TOP,TControl(Components[i]).TOP );

          sysFile.WriteString(TControl(Components[i]).Name ,Left,TControl(Components[i]).Left );
      end;

   finally
      SysFile.Free;
   end;

 

 

 

posted on 2012-05-31 23:01  廖波涛  阅读(448)  评论(0编辑  收藏  举报