Dephi下有无让控件跟追窗体大小自动变换大小的OCX控件?

beizhi 2000-09-10 05:09:00
Dephi下有无让控件跟追窗体大小自动变换大小的OCX控件?也就是
当form改变时,窗体内的控件的大小,位置会随之自动改变,我可不
想花太多的代码来给控件定位.
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Fred 2000-10-03
  • 打赏
  • 举报
回复
把控件的Anchors的属性的Right和Bottom都设成True不就行了吗?
100percent 2000-09-11
  • 打赏
  • 举报
回复
有两个控件,FormResize,FormMinMax,可以实现。
代码如下:
unit FormMinMax;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TMinMaxOptions = set of (moMinW,moMaxW,moMinH,moMaxH);
TFormMinMax = class(TComponent)
private
OldWndProc : TFarProc;
NewWndProc : Pointer;
FMinW : Integer;
FMinH : Integer;
FMaxW : Integer;
FMaxH : Integer;
FDTProp : TMinMaxOptions;
procedure HookParent;
procedure UnhookParent;
procedure HookWndProc(var Message: TMessage);
protected
{ Protected declarations }
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
published
property MinWidth:Integer Read FMinW Write FMinW;
property MaxWidth:Integer Read FMaxW Write FMaxW;
property MinHeight:Integer Read FMinH Write FMinH;
property MaxHeight:Integer Read FMaxH Write FMaxH;
property DTOptions:TMinMaxOptions read fdtProp write FdtProp;
end;

procedure Register;

implementation

constructor TFormMinMax.Create(AOwner:TComponent);
Begin
Inherited Create(AOwner);
NewWndProc := NIL;
OldWndProc := NIL;
HookParent;
End;

destructor TFormMinMax.Destroy;
begin
UnhookParent;
inherited Destroy;
end;

procedure TFormMinMax.HookParent;
begin
if (Owner As TWinControl) = NIL then exit;
OldWndProc := TFarProc(GetWindowLong((Owner As TWinControl).Handle, GWL_WNDPROC));
NewWndProc := MakeObjectInstance(HookWndProc);
SetWindowLong((Owner As TWinControl).Handle, GWL_WNDPROC, LongInt(NewWndProc));
end;

procedure TFormMinMax.UnhookParent;
begin
if ((Owner As TWinControl)<>NIL) and Assigned(OldWndProc) then
SetWindowLong((Owner As TWinControl).Handle, GWL_WNDPROC, LongInt(OldWndProc));
if assigned(NewWndProc) then
FreeObjectInstance(NewWndProc);
NewWndProc := NIL;
OldWndProc := NIL;
end;

procedure TFormMinMax.HookWndProc(var Message: TMessage);
begin
if (Owner As TWinControl) = NIL then
exit;
With Message Do
Begin
Result := CallWindowProc(OldWndProc, (Owner As TWinControl).Handle, Msg, wParam, lParam);
If (Msg=WM_SIZE) And (ComponentState=[csDesigning]) Then
Begin
If moMinW In FdtProp Then
FMinW := (Owner As TWinControl).Width;
If moMaxW In FdtProp Then
FMaxW := (Owner As TWinControl).Width;
If moMinH In FdtProp Then
FMinH := (Owner As TWinControl).Height;
If moMaxH In FdtProp Then
FMaxH := (Owner As TWinControl).Height;
End;
If ComponentState=[csDesigning] Then
Exit;
If (Msg = WM_GETMinMaxINFO) Then
Begin
With PMinMaxInfo(Message.lParam)^ do
Begin
If (FMinW>0) Then
ptMinTrackSize.X := FMinW;
If (FMaxW>0) Then
ptMaxTrackSize.X := FMaxW;
If (FMinH>0) Then
ptMinTrackSize.Y := FMinH;
If (FMaxH>0) Then
ptMaxTrackSize.Y := FMaxH;
End;
End;
End;
end;

procedure Register;
begin
RegisterComponents('Sample', [TFormMinMax]);
end;

end.



unit FormResize;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, StdCtrls, TabNotBk;

type
pControlInfo = ^tControlInfo;
tControlInfo = record
Obj:TControl;
ClassName:String;
lRatio,tRatio,wRatio,hRatio:Real;
Contains:pControlInfo;
Next:pControlInfo;
End;
TFormResize = class(TComponent)
private
ResizerInitialized:Boolean;
Function GetClientControlCount(Sender:TObject):LongInt;
procedure GetNewWidthHeight(Sender:TObject;Var NewW,NewH:LongInt);
Procedure PerformResize(CurCont:pControlInfo;CWidth,CHeight:LongInt);
Function GetClientControlObject(Sender:TObject;Index:LongInt):TControl;
Function Initialize(Sender:TObject;CWidth,CHeight:LongInt):pControlInfo;
Procedure DeleteControlList(CurCont:pControlInfo);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ReSize(ParentForm:TObject);
procedure NewSnapshot(ParentForm:TObject);
end;

procedure Register;

implementation

Var ControlList:pControlInfo;

constructor TFormResize.Create(AOwner: TComponent);
Begin
inherited Create(AOwner);
ResizerInitialized:=False;
ControlList:=nil;
End;

destructor TFormResize.Destroy;
Begin
DeleteControlList(ControlList);
ResizerInitialized:=False;
ControlList:=nil;
inherited Destroy;
End;

Procedure TFormResize.DeleteControlList(CurCont:pControlInfo);
Var NextCont:pControlInfo;
Begin
While(CurCont<>nil) Do
Begin
If((CurCont^.Contains)<>nil) Then
DeleteControlList(CurCont^.Contains);
NextCont:=CurCont^.Next;
FreeMem(CurCont,SizeOf(tControlInfo));
CurCont:=NextCont;
End;
End;

procedure TFormResize.GetNewWidthHeight(Sender:TObject;Var NewW,NewH:LongInt);
Begin
If(Sender is TForm) Then
With TForm(Sender) Do
Begin
NewW:=ClientWidth;
NewH:=ClientHeight;
End
Else
If(Sender is TPanel) Then
With TPanel(Sender) Do
Begin
NewW:=Width;
NewH:=Height;
End
Else
If(Sender is TGroupBox) Then
With TGroupBox(Sender) Do
Begin
NewW:=Width;
NewH:=Height;
End
Else
If(Sender is TTabbedNotebook) Then
With TTabbedNotebook(Sender) Do
Begin
NewW:=Width;
NewH:=Height;
End
Else
Begin
NewW:=0;
NewH:=0;
End;
End;

Function TFormResize.GetClientControlCount(Sender:TObject):LongInt;
Begin
If(Sender is TForm) Then
Result:=TForm(Sender).ControlCount
Else
If(Sender is TPanel) Then
Result:=TPanel(Sender).ControlCount
Else
If(Sender is TGroupBox) Then
Result:=TGroupBox(Sender).ControlCount
Else
If(Sender is TTabbedNotebook) Then
Result:=TTabbedNotebook(Sender).ControlCount
Else
Result:=0;
End;

Function TFormResize.GetClientControlObject(Sender:TObject;Index:LongInt):TControl;
Begin
If(Sender is TForm) Then
Result:=TForm(Sender).Controls[Index]
Else
If(Sender is TPanel) Then
Result:=TPanel(Sender).Controls[Index]
Else
If(Sender is TGroupBox) Then
Result:=TGroupBox(Sender).Controls[Index]
Else
If(Sender is TTabbedNotebook) Then
Result:=TTabbedNotebook(Sender).Controls[Index]
Else
Result:=nil;
End;

Function TFormResize.Initialize(Sender:TObject;CWidth,CHeight:LongInt):pControlInfo;
Var Temp:pControlInfo;
Count:Integer;
Begin
Result:=nil;
For Count:=0 to (GetClientControlCount(Sender)-1) Do
Begin
GetMem(Temp,SizeOf(tControlInfo));
Temp^.Obj:=GetClientControlObject(Sender,Count);
With(Temp^) Do
Begin
lRatio:=(Obj.Left) / CWidth;
tRatio:=(Obj.Top) / CHeight;
wRatio:=(Obj.Width) / CWidth;
hRatio:=(Obj.Height) / CHeight;
Contains:=nil;
Next:=Result;
End;
Result:=Temp;
If((Temp^.Obj is TForm) or
(Temp^.Obj is TPanel) or
(Temp^.Obj is TGroupBox) or
(Temp^.Obj is TTabbedNotebook)) Then
With(Temp^) Do
Contains:=Initialize(Obj,Obj.Width,Obj.Height);
End;
End;

Procedure TFormResize.PerformResize(CurCont:pControlInfo;CWidth,CHeight:LongInt);
Begin
While(CurCont<>nil) Do
Begin
With(CurCont^) Do
Begin
With(Obj) Do
SetBounds(Round(lRatio*CWidth),Round(tRatio*CHeight),
Round(wRatio*CWidth),Round(hRatio*CHeight));
If(Contains<>nil) Then
PerformResize(Contains,Obj.Width,Obj.Height);
End;
CurCont:=CurCont^.Next;
End;
End;

procedure TFormResize.NewSnapshot(ParentForm:TObject);
Var NewClientWidth,NewClientHeight:LongInt;
Begin
GetNewWidthHeight(ParentForm,NewClientWidth,NewClientHeight);
DeleteControlList(ControlList);
ControlList:=nil;
ResizerInitialized:=False;
ControlList:=Initialize(ParentForm,NewClientWidth,NewClientHeight);
If(ControlList<>nil) Then
ResizerInitialized:=True;
End;

procedure TFormResize.ReSize(ParentForm:TObject);
Var NewClientWidth,NewClientHeight:LongInt;
Begin
GetNewWidthHeight(ParentForm,NewClientWidth,NewClientHeight);
If(ResizerInitialized) Then
PerformResize(ControlList,NewClientWidth,NewClientHeight)
Else
NewSnapshot(ParentForm);
End;

procedure Register;
begin
RegisterComponents('Sample', [TFormResize]);
end;

end.


01 , 01.txt 《 VB6.0中通过MSChart控件调用数据库 》 02 , 02.txt 《 用VB6实现动态增减控件 》 03 , 03.txt ActiveX控件的创建 04 , 04.txt ADO控件和DATA控件的冲突(不能共存)的解决方法 05 , 05.txt Combo的自动查询技术 06 , 06.txt DirectX7.0使用心得(1) 07 , 07.txt DirectX7.0使用心得(2) 08 , 08.txt DirectX7.0使用心得(3) 09 , 09.txt FSO对象模型在VB中的应用 10 , 10.txt MsComm 控件的文字传输范例 11 , 11.txt Office或IE4风格的ToolBar 12 , 12.txt Regsvr32.exe注册控件的具体用法 13 , 13.txt TextBox的自动调节 14 , 14.txt TextBox实现打印机效果 15 , 15.txt TreeView的基本操作 16 , 16.txt VB5中DBGRID控件在VB6中使用 17 , 17.txt VB6.0动态加载ActiveX控件漫谈 18 , 18.txt VB与MS-Draw开发通用作图软件 19 , 19.txt VB中APP对象及其应用 20 , 20.txt VB中list控件的功能扩充 21 , 21.txt VB中防止将重复项目添加到列表框控件中 22 , 22.txt VB中用Multimedia MCI控件开发多媒体应用 23 , 23.txt Win Api在VB中的妙用 24 , 24.txt WINDOWS SCRIPT HOST对象在VB中的使用 25 , 25.txt 安装向导生成程序组并建立多个程序项 26 , 26.txt 保存复选框选项 27 , 27.txt 不用OCX来创建自己的控件(一) 28 , 28.txt 成组更新控件属性 29 , 29.txt 创建数据驱动窗体 30 , 30.txt 得到鼠标位置 31 , 31.txt 调整 Combo 下拉部分的宽度 32 , 32.txt 动态加入控件到VB控件数组中 33 , 33.txt 对ListView中的列排序 34 , 34.txt 放一个Combo到Toolbar中 35 , 35.txt 改变 ListIndex而不发生 Click 事

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧