除了创建时指定窗口位置之外,还有3种移动窗口位置的办法(移动的同时往往可以改变窗口大小)(SetWindowPos最有用,它有许多标志位)

首先,在创立窗口对象的时候,CreateWindowEx就可以指定窗口的位置。除此之外,还有三种方法可以改变窗口的位置:

procedure TWinControl.CreateWindowHandle(const Params: TCreateParams);
begin
  // 根据之前准备的Params参数使用API创建窗口。其10个参数都是Params的参数,0表示Menu,WindowClass的十项内容只用到了hInstance一项
  // important 控件移到正确的显示位置,就是靠这个X和Y,会移到父控件区域的相对位置(实践检测)。而VC里一般使用CW_USEDEFAULT
  
  with Params do
    FHandle := CreateWindowEx(ExStyle, WinClassName, Caption, Style, X, Y, Width, Height, WndParent, 0, WindowClass.hInstance, Param);

  // API,一共12个参数,注意,CreateWindowEx比CreateWindow多一个参数:dwExStle(Delphi根本没有用到CreateWindow)
  // 前9个参数都是TCreateParams的参数,中规中矩
  // 第10个参数是menu
  // 第11个参数表示,这个新创建的窗口附属于哪个hInstance
  // 第12个参数用不到
end;

--------------------------------------------------------------------

第一种办法改变Windows窗口的坐标(最有用,因为它有许多有用的标志位):

SetWindowPos(FHandle, 0, ALeft, ATop, AWidth, AHeight, SWP_NOZORDER + SWP_NOACTIVATE);

第二种办法改变Windows窗口的坐标(亲测):

procedure TForm1.Button2Click(Sender: TObject);
var
  WindowPlacement: TWindowPlacement; // Windows结构类型,包含最大化最小化窗口位置等6项内容
begin
  WindowPlacement.Length := SizeOf(WindowPlacement);
  GetWindowPlacement(Panel1.Handle, @WindowPlacement);  // API,取得结构体指针,包括7项内容
  // 更改窗口的位置信息
  WindowPlacement.rcNormalPosition := Panel1.BoundsRect;
  WindowPlacement.rcNormalPosition.Left := WindowPlacement.rcNormalPosition.Left - 10;
  WindowPlacement.rcNormalPosition.Right := WindowPlacement.rcNormalPosition.Right - 10;
  SetWindowPlacement(Panel1.Handle, @WindowPlacement);
end;

第三种办法改变Windows窗口的坐标(亲测):

  MoveWindow(panel1.Handle, 10, 10, 300, 100, true);

--------------------------------------------------------------------

另外,SetScrollPos也有些相关的作用,值得研究

原文地址:https://www.cnblogs.com/findumars/p/4772125.html