Delphi5 Constraints BUG Fix

翟二喜

解决 Delphi 窗体设置Constraints后
在左边界或上边界改变窗体大小右边界或下边界跟着动的问题

Delphi 5 调试成功,应在 D4 D5 D6 中适用
没脾气

//////////////////////////////////////////////
type 
  TForm1 = class(TCustomForm) 
    //... 
  protected 
    procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging); 
        message WM_WINDOWPOSCHANGING; 
  public 
    //... 
  end; 

procedure TForm1.WMWindowPosChanging(var Msg: TWMWindowPosChanging); 
var 
  aRect: TRect; 
begin 
  Windows.GetWindowRect( Msg.WindowPos.hwnd, aRect ); 

  if (Msg.WindowPos.flags and SWP_NOSIZE = 0 ) then 
  begin

    if Msg.WindowPos.cx < Self.Constraints.MinWidth then 
    begin 
      Msg.WindowPos.cx := Self.Constraints.MinWidth; 

      if Msg.WindowPos.x <> aRect.Left then 
      begin 
        Msg.WindowPos.x := aRect.Right - Msg.WindowPos.cx; 
      end; 
    end else

    if (Self.Constraints.MaxWidth > 0) 
    and(Msg.WindowPos.cx > Self.Constraints.MaxWidth) then 
    begin 
      Msg.WindowPos.cx := Self.Constraints.MaxWidth; 
      if Msg.WindowPos.x <> aRect.Left then 
      begin 
        Msg.WindowPos.x := aRect.Right - Msg.WindowPos.cx; 
      end; 
    end; 

    if Msg.WindowPos.cy < Self.Constraints.MinHeight then 
    begin 
      Msg.WindowPos.cy := Self.Constraints.MinHeight; 

      if Msg.WindowPos.y <> aRect.Top then 
      begin 
        Msg.WindowPos.y := aRect.Bottom - Msg.WindowPos.cy; 
      end; 
    end else

    if (Self.Constraints.MaxHeight > 0) 
    and(Msg.WindowPos.cy > Self.Constraints.MaxHeight) then 
    begin 
      Msg.WindowPos.cy := Self.Constraints.MaxHeight; 
      if Msg.WindowPos.y <> aRect.Top then 
      begin 
        Msg.WindowPos.y := aRect.Bottom - Msg.WindowPos.cy; 
      end; 
    end; 
  end; 

  inherited; 
end; 

// 没脾气,notemper@hoolee.com,zhaixudong@shareware.com.cn
// 2001.5.9 

// 可能每次消息触发都执行 GetWindowRect 会影响效率,若有更优解决办法请告知。
//////////////////////////////////////////////

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: