VB窗体控件大小随窗体大小变化而变化.docx
《VB窗体控件大小随窗体大小变化而变化.docx》由会员分享,可在线阅读,更多相关《VB窗体控件大小随窗体大小变化而变化.docx(11页珍藏版)》请在冰点文库上搜索。
![VB窗体控件大小随窗体大小变化而变化.docx](https://file1.bingdoc.com/fileroot1/2023-5/8/0c21f39e-8e1c-4a21-9e42-9d3a08555557/0c21f39e-8e1c-4a21-9e42-9d3a085555571.gif)
VB窗体控件大小随窗体大小变化而变化
VB窗体控件大小随窗体大小变化自動調整
有时窗体变化后,如改变分辨率后控件大小却不能随之改变。
手工代码调整实在麻烦,下面的模块实现自动查找窗体上控件并使其改变大小以适应窗体变化。
在Form的Resize事件中调用函数Resize_All就能实现控件自动调整大小,如:
PrivateSubForm_Resize()
DimH,iAsInteger
OnErrorResumeNext
Resize_ALLMe'Me是窗体名,Form1,Form2等等都可以
EndSub
在模块中添加以下代码:
PublicTypectrObj
NameAsString
IndexAsLong
ParrentAsString
TopAsLong
LeftAsLong
HeightAsLong
WidthAsLong
ScaleHeightAsLong
ScaleWidthAsLong
EndType
PrivateFormRecord()AsctrObj
PrivateControlRecord()AsctrObj
PrivatebRunningAsBoolean
PrivateMaxFormAsLong
PrivateMaxControlAsLong
PrivateConstWM_NCLBUTTONDOWN=&HA1
PrivateDeclareFunctionSendMessageLib"USER32"Alias"SendMessageA"(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,ByVallParamAsLong)AsLong
PrivateDeclareFunctionReleaseCaptureLib"USER32"()AsLong
FunctionActualPos(plLeftAsLong)AsLong
IfplLeft<0Then
ActualPos=plLeft+75000
Else
ActualPos=plLeft
EndIf
EndFunction
FunctionFindForm(pfrmInAsForm)AsLong
DimiAsLong
FindForm=-1
IfMaxForm>0Then
Fori=0To(MaxForm-1)
IfFormRecord(i).Name=pfrmIn.NameThen
FindForm=i
ExitFunction
EndIf
Nexti
EndIf
EndFunction
FunctionAddForm(pfrmInAsForm)AsLong
DimFormControlAsControl
DimiAsLong
ReDimPreserveFormRecord(MaxForm+1)
FormRecord(MaxForm).Name=pfrmIn.Name
FormRecord(MaxForm).Top=pfrmIn.Top
FormRecord(MaxForm).Left=pfrmIn.Left
FormRecord(MaxForm).Height=pfrmIn.Height
FormRecord(MaxForm).Width=pfrmIn.Width
FormRecord(MaxForm).ScaleHeight=pfrmIn.ScaleHeight
FormRecord(MaxForm).ScaleWidth=pfrmIn.ScaleWidth
AddForm=MaxForm
MaxForm=MaxForm+1
ForEachFormControlInpfrmIn
i=FindControl(FormControl,pfrmIn.Name)
Ifi<0Then
i=AddControl(FormControl,pfrmIn.Name)
EndIf
NextFormControl
EndFunction
FunctionFindControl(inControlAsControl,inNameAsString)AsLong
DimiAsLong
FindControl=-1
Fori=0To(MaxControl-1)
IfControlRecord(i).Parrent=inNameThen
IfControlRecord(i).Name=inControl.NameThen
OnErrorResumeNext
IfControlRecord(i).Index=inControl.IndexThen
FindControl=i
ExitFunction
EndIf
OnErrorGoTo0
EndIf
EndIf
Nexti
EndFunction
FunctionAddControl(inControlAsControl,inNameAsString)AsLong
ReDimPreserveControlRecord(MaxControl+1)
OnErrorResumeNext
ControlRecord(MaxControl).Name=inControl.Name
ControlRecord(MaxControl).Index=inControl.Index
ControlRecord(MaxControl).Parrent=inName
IfTypeOfinControlIsLineThen
ControlRecord(MaxControl).Top=inControl.Y1
ControlRecord(MaxControl).Left=ActualPos(inControl.X1)
ControlRecord(MaxControl).Height=inControl.Y2
ControlRecord(MaxControl).Width=ActualPos(inControl.X2)
Else
ControlRecord(MaxControl).Top=inControl.Top
ControlRecord(MaxControl).Left=ActualPos(inControl.Left)
ControlRecord(MaxControl).Height=inControl.Height
ControlRecord(MaxControl).Width=inControl.Width
EndIf
inControl.IntegralHeight=False
OnErrorGoTo0
AddControl=MaxControl
MaxControl=MaxControl+1
EndFunction
FunctionPerWidth(pfrmInAsForm)AsLong
DimiAsLong
i=FindForm(pfrmIn)
Ifi<0Then
i=AddForm(pfrmIn)
EndIf
PerWidth=(pfrmIn.ScaleWidth*100)\FormRecord(i).ScaleWidth
EndFunction
FunctionPerHeight(pfrmInAsForm)AsDouble
DimiAsLong
i=FindForm(pfrmIn)
Ifi<0Then
i=AddForm(pfrmIn)
EndIf
PerHeight=(pfrmIn.ScaleHeight*100)\FormRecord(i).ScaleHeight
EndFunction
PublicSubResizeControl(inControlAsControl,pfrmInAsForm)
OnErrorResumeNext
DimiAsLong
DimwidthfactorAsSingle,heightfactorAsSingle
DimminFactorAsSingle
DimyRatio,xRatio,lTop,lLeft,lWidth,lHeightAsLong
yRatio=PerHeight(pfrmIn)
xRatio=PerWidth(pfrmIn)
i=FindControl(inControl,pfrmIn.Name)
IfinControl.Left<0Then
lLeft=CLng(((ControlRecord(i).Left*xRatio)\100)-75000)
Else
lLeft=CLng((ControlRecord(i).Left*xRatio)\100)
EndIf
lTop=CLng((ControlRecord(i).Top*yRatio)\100)
lWidth=CLng((ControlRecord(i).Width*xRatio)\100)
lHeight=CLng((ControlRecord(i).Height*yRatio)\100)
IfTypeOfinControlIsLineThen
IfinControl.X1<0Then
inControl.X1=CLng(((ControlRecord(i).Left*xRatio)\100)-75000)
Else
inControl.X1=CLng((ControlRecord(i).Left*xRatio)\100)
EndIf
inControl.Y1=CLng((ControlRecord(i).Top*yRatio)\100)
IfinControl.X2<0Then
inControl.X2=CLng(((ControlRecord(i).Width*xRatio)\100)-75000)
Else
inControl.X2=CLng((ControlRecord(i).Width*xRatio)\100)
EndIf
inControl.Y2=CLng((ControlRecord(i).Height*yRatio)\100)
Else
inControl.MovelLeft,lTop,lWidth,lHeight
inControl.MovelLeft,lTop,lWidth
inControl.MovelLeft,lTop
EndIf
EndSub
PublicSubResizeForm(pfrmInAsForm)
DimFormControlAsControl
DimisVisibleAsBoolean
DimStartX,StartY,MaxX,MaxYAsLong
DimbNewAsBoolean
IfNotbRunningThen
bRunning=True
IfFindForm(pfrmIn)<0Then
bNew=True
Else
bNew=False
EndIf
IfpfrmIn.Top<30000Then
isVisible=pfrmIn.Visible
OnErrorResumeNext
IfNotpfrmIn.MDIChildThen
OnErrorGoTo0
''pfrmIn.Visible=False
Else
IfbNewThen
StartY=pfrmIn.Height
StartX=pfrmIn.Width
OnErrorResumeNext
ForEachFormControlInpfrmIn
IfFormControl.Left+FormControl.Width+200>MaxXThen
MaxX=FormControl.Left+FormControl.Width+200
EndIf
IfFormControl.Top+FormControl.Height+500>MaxYThen
MaxY=FormControl.Top+FormControl.Height+500
EndIf
IfFormControl.X1+200>MaxXThen
MaxX=FormControl.X1+200
EndIf
IfFormControl.Y1+500>MaxYThen
MaxY=FormControl.Y1+500
EndIf
IfFormControl.X2+200>MaxXThen
MaxX=FormControl.X2+200
EndIf
IfFormControl.Y2+500>MaxYThen
MaxY=FormControl.Y2+500
EndIf
NextFormControl
OnErrorGoTo0
pfrmIn.Height=MaxY
pfrmIn.Width=MaxX
EndIf
OnErrorGoTo0
EndIf
ForEachFormControlInpfrmIn
ResizeControlFormControl,pfrmIn
NextFormControl
OnErrorResumeNext
IfNotpfrmIn.MDIChildThen
OnErrorGoTo0
pfrmIn.Visible=isVisible
Else
IfbNewThen
pfrmIn.Height=StartY
pfrmIn.Width=StartX
ForEachFormControlInpfrmIn
ResizeControlFormControl,pfrmIn
NextFormControl
EndIf
EndIf
OnErrorGoTo0
EndIf
bRunning=False
EndIf
EndSub
PublicSubSaveFormPosition(pfrmInAsForm)
DimiAsLong
IfMaxForm>0Then
Fori=0To(MaxForm-1)
IfFormRecord(i).Name=pfrmIn.NameThen
FormRecord(i).Top=pfrmIn.Top
FormRecord(i).Left=pfrmIn.Left
FormRecord(i).Height=pfrmIn.Height
FormRecord(i).Width=pfrmIn.Width
ExitSub
EndIf
Nexti
AddForm(pfrmIn)
EndIf
EndSub
PublicSubRestoreFormPosition(pfrmInAsForm)
DimiAsLong
IfMaxForm>0Then
Fori=0To(MaxForm-1)
IfFormRecord(i).Name=pfrmIn.NameThen
IfFormRecord(i).Top<0Then
pfrmIn.WindowState=2
ElseIfFormRecord(i).Top<30000Then
pfrmIn.WindowState=0
pfrmIn.MoveFormRecord(i).Left,FormRecord(i).Top,FormRecord(i).Width,FormRecord(i).Height
Else
pfrmIn.WindowState=1
EndIf
ExitSub
EndIf
Nexti
EndIf
EndSub
PublicSubResize_ALL(Form_NameAsForm)
DimOBJAsObject
ForEachOBJInForm_Name
ResizeControlOBJ,Form_Name
NextOBJ
EndSub
PublicSubDragForm(frmAsForm)
OnLocalErrorResumeNext
CallReleaseCapture
CallSendMessage(frm.hwnd,WM_NCLBUTTONDOWN,2,0)
EndSub