zoukankan      html  css  js  c++  java
  • 使用SetWindowPos API函数移动窗口后,还需修改Delphi的属性值,以备下次使用,否则就会出问题(不是API不起作用,而是使用了错误的坐标值)

    单独改变坐标的代码如下,可不断左移:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SetWindowPos(panel1.Handle, 0, panel1.Left-10, panel1.Top, panel1.Width, panel1.Height, SWP_NOZORDER + SWP_NOACTIVATE);
    end;

    照理每次改变坐标,都会调用SetBounds,都会调用SetWindowPos函数,可是一旦我屏蔽UpdateBounds函数,控件从第二次开始,就没有效果了:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      panel1.Left:=panel1.Left-10;
    end;

    这是为什么呢?原因是,在使用API达到相应的目的之后,必须修改Delphi的属性值,以备下次使用。否则Delphi下次还是按照原来的left值再减去10,这样看上去就没有被移动。所以关键是UpdateBounds里的代码:

    procedure TWinControl.UpdateBounds;
    var
      ParentHandle: HWnd;
      Rect: TRect;
      WindowPlacement: TWindowPlacement;
    begin
      GetWindowRect(FHandle, Rect); // API,取得客户区,注意第二个参数是指针,但Delphi里直接使用
    
      // important 如果具有子窗口风格,就要根据父控件在屏幕上的位置重新显示
      if GetWindowLong(FHandle, GWL_STYLE) and WS_CHILD <> 0 then
      begin
        ParentHandle := GetWindowLong(FHandle, GWL_HWNDPARENT); // API,fixme 还可以这样简单取得父控件句柄?就这一处
        if ParentHandle <> 0 then
        begin
          Windows.ScreenToClient(ParentHandle, Rect.TopLeft); // API
          Windows.ScreenToClient(ParentHandle, Rect.BottomRight);
        end;
      end;
    
      // important 使用API取得实际效果后,也要改变Delphi的属性值,否则属性值还是保持上一次的值,这样下一次看起来就无法移动了
      // 错误:显示完以后,根据windows内部信息更新Win控件的属性
      // important 如果这里exit,那么只有第一次的时候坐标可以移动,后面都无法移动了。说到底都是通过这里起作用的
      FLeft := Rect.Left;
      FTop := Rect.Top;
      FWidth := Rect.Right - Rect.Left;
      FHeight := Rect.Bottom - Rect.Top;
      // 更新坐标和长宽后,要重新铆接
      // fixme 难道不用对齐吗,别处好多地方也都是这样
      UpdateAnchorRules;
    end;

    的最后几句。这样就达到了修改值的目的。

  • 相关阅读:
    UIPasteboard 粘贴板
    UIViewController没有随着设备一起旋转的原因
    UIButton 应用选择状态(附:UIButton 常用状态)
    WebService 中参数为枚举时引发的血案
    设计模式(1)之面向对象设计原则 阿正
    2012年年终总结 阿正
    生活工作如登山 阿正
    感谢我的技术总监 阿正
    尽孝要尽早 阿正
    我老了吗?不 你依然年轻 阿正
  • 原文地址:https://www.cnblogs.com/findumars/p/4770277.html
Copyright © 2011-2022 走看看