//此单元的目的是用锁定任一窗体中的最大宽度与高度及最小宽度与高度.
//函数入口 在 form1.oncreate中写入
//   lockwindow(self.handle,200,200,500,500)
//卸载: 在form1.ondestroy中与入
//  unlockwindow(self.handle)//中调试时产生错误,但又找不到原因,恳请那位高人帮忙调试.
//======================================================================
unit pub_windowface ;
interface
uses
  Windows,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;var Sproclist:Tstringlist;
var Oldproc:pointer;
var MINMAXINFO : tagMINMAXINFO;type tagMINMAXINFO =record
ptReserved : TPoint; //ϵͳ±£Áô
ptMaxSize : TPoint; //×î´ó»¯ºóµÄ´óС
ptMaxPosition : TPoint; //×î´ó»¯ºóµÄλÖÃ
ptMinTrackSize : TPoint; //´°¿ÚµÄ×îС³ß´ç
ptMaxTrackSize : TPoint; //´°¿ÚµÄ×î´ó³ß´ç
End;function lockwindow(handle:hwnd;minWidth:longint=0;MinHeight:longint=0; MaxWidth :Longint=0;  MaxHeight:longint=0) : Longint;
function NewlockWndProc(Handle:HWND;Msg:Integer;wParam:Longint;lParam :Longint):Longint;
procedure Unlockwindow(handle:hwnd);implementation
function lockwindow(handle:hwnd;minWidth:longint=0;MinHeight:longint=0; MaxWidth :Longint=0;  MaxHeight:longint=0) : Longint;
begin
  result:=-1;
  Oldproc:=Pointer(SetWindowLong(Handle,GWL_WNDPROC,Integer(@NewlockWndProc)));
  sproclist:=tstringlist.Create;
  Sproclist.Add(inttostr(handle)+'='+inttostr(integer(oldproc))+';'+inttostr(minwidth)+';'+inttostr(minheight)+';'+inttostr(maxwidth)+';'+inttostr(maxheight)) ;
  result:=0;end ;
function NewlockWndProc(Handle:HWND;Msg:Integer;wParam:Longint;lParam :Longint):Longint;
 var
 Idx:integer;
 lpmmi:^tagMINMAXINFO;
 Plist:Tstringlist;
 Pmax:Tpoint ;
 Pmin:Tpoint;
begin
 sproclist.Find(inttostr(handle),Idx);
 if idx=-1 then
   begin
   callwindowproc(oldproc,handle,msg,wparam,lparam);
   exit;
   end ;
 case msg of
 WM_GETMINMAXINFO:
   begin
   plist.Delimiter:=';';
   plist.DelimitedText:=sproclist.ValueFromIndex[idx];
   pmin.X:=integer(plist.Strings[1]) ;
   pmin.y:=integer(plist.Strings[2]) ;
   pmax.X:=integer(plist.Strings[3]);
   pmax.y:=integer(plist.Strings[4]);   lpmmi:=Pointer(LParam);
   lpmmi^.ptMaxSize:=pmax;
   lpmmi^.ptMaxTrackSize:=pmax;
   lpmmi^.ptMinTrackSize:=pmin;
   lpmmi^.ptMaxPosition:=Point(0,0);
   exit;
   end;
 end;
  callwindowproc(oldproc,handle,msg,wparam,lparam);
end;procedure Unlockwindow(handle:hwnd);
 var Idx:integer;Proc:integer;ProcStr:string;
begin
 sproclist.Find(inttostr(handle),Idx);
 if Idx=-1 then
   exit;
 Procstr:=Sproclist.ValueFromIndex[idx];
 Proc:=integer(copy(procstr,1,pos(';',procstr)-1));
 setWindowLong(Handle,GWL_WNDPROC,proc);
 sproclist.Delete(Idx);end;
end.

解决方案 »

  1.   

    procedure TForm1.lockwindow(fm:Tform; top, left, width,
      height: integer);
    begin
      fm.Top:=top;
      fm.Left:=left;
      fm.Width:=width;
      fm.Height:=height;
    end;
      

  2.   

    to xjspa
      我误会的意思了,我需要的锁定任一窗体的大小(拦截它的消息).
    经我进一步调试问题出在
    function NewlockWndProc(Handle:HWND;Msg:Integer;wParam:Longint;lParam :Longint):longint
    加入参数:lParam :Longint就会产生错误报警.
      

  3.   

    不让改变窗体大小,把它的BorderStyle设成bsSingle就行了
    设定窗体的最大,最小高度,宽度,设置它的Constraints属性中的MaxHeight, MinHeight, MaxWidth, MinWidth就行了