Go...

当前位置: 首页>>世界杯冠军魔咒

vb.net 如何自动调整窗体大小

社区

VB 帖子详情 vb.net 如何自动调整窗体大小 mecjudy 2006-12-20 09:02:24 看了用vb写的例子,但不晓得怎么用vb.net写出来,各位高手请指点一下

...全文

982 21 打赏 收藏 vb.net 如何自动调整窗体大小 看了用vb写的例子,但不晓得怎么用vb.net写出来,各位高手请指点一下 复制链接

扫一扫 分享 转发到动态 举报

写回复 配置赞助广告取 消

确 定

用AI写文章 21 条回复 切换为时间正序 请发表友善的回复… 发表回复 打赏红包 需支付: 0.00 元 取 消 确 定 雅各布奇 2010-05-30 打赏举报 回复 [Quote=引用 18 楼 vinsonhwj 的回复:]

此示例以像素为单位确定桌面的宽度和高度。

示例

Dim desktopSize As Size

desktopSize = System.Windows.Forms.SystemInformation.PrimaryMonitorSize

Dim height As Integer = desktopSize.Height

Dim width ……

[/Quote]

这用的是2005? mecjudy 2006-12-28 打赏举报 回复 谢谢各位的解答 vinsonhwj 2006-12-27 打赏举报 回复 此示例以像素为单位确定桌面的宽度和高度。

示例

Dim desktopSize As Size

desktopSize = System.Windows.Forms.SystemInformation.PrimaryMonitorSize

Dim height As Integer = desktopSize.Height

Dim width As Integer = desktopSize.Width

tianbaobao 2006-12-27 打赏举报 回复 学习 SeeSunSet 2006-12-25 打赏举报 回复 设置DOCK和anchor属性就好了.非常简单.不像VB6的那么麻烦.效果也不好. 阿牛138588 2006-12-23 打赏举报 回复 容器里的控件(可显示的)都有Anchor和Dock属性

dock是使用何种形式填满,假如是fill的话就填满容器所有,如果是button的话就填满容器下部分(左,右,下三边保持紧贴容器的三边),就是说,如果是button的话,这个控件的高是不变的,但宽随时容器的增加而增加,详细你可以试一下

anchor有left,top,right,button4个属性,表示4条边与容器保持距离,一般控件是默认left和top被选中的,表示,不管容器怎么变,这个控件的top和left属性是不变的,假如right被选中的话,那么容器的宽-控件的left-控件的width的值是固定的,表示,控件的右边界与容器的右边界保持距离(此时如果容器的width增加了,那么控件的width也增加了)

同理其他 mecjudy 2006-12-22 打赏举报 回复 还有,我现在用resize调整窗体大小时能很明显看到控件大小变化,怎么解决这个问题阿 mecjudy 2006-12-22 打赏举报 回复 我也不大理解Anchor或者Dock这两个属性,谁能在这讲解一下阿 fys249931556 2006-12-22 打赏举报 回复 在窗体属性的DOCK 里就可以调整 `

任真学编程 2006-12-21 打赏举报 回复 msdn里有碍 Liyj_88 2006-12-21 打赏举报 回复 谁能给我说说什么地方有比较详细的Anchor或者Dock属性的讲解

CloneCenter 2006-12-21 打赏举报 回复 控件比例可以使用Anchor或者Dock属性。 mecjudy 2006-12-21 打赏举报 回复 windowsstate属性只可设置窗体大小,但是窗体内控件的比例不对

谢谢Qim,在你的启发下问题终于解决了 huwei001982 2006-12-20 打赏举报 回复 如果是 winforms 的话, 直接设置 dock, anchor mecjudy 2006-12-20 打赏举报 回复 SetBounds替代vb中的move? Qim 2006-12-20 打赏举报 回复 SetBounds(left,top,width,height) Qim 2006-12-20 打赏举报 回复 me.Width= ?

me.Height = ? Qim 2006-12-20 打赏举报 回复 this.Width = ?;

this.Height = ?; kingj2018 2006-12-20 打赏举报 回复 窗口的windowstata属性就可以设置.

huwei001982 2006-12-20 打赏举报 回复 晕, 直接设置 Dock, anchor 就搞定, 还需要费事写 Resize>> 加载更多回复(1) vb.net控件随窗口大小调整 vb.net控件随窗口大小调整 vb.net像QQ一样自动边缘隐藏窗体 vb.net像QQ一样上部及左右边缘自动隐藏程序,利用控件移动窗体,及添加后台ICON VB窗体控件大小随窗体大小变化而变化 VB窗体控件大小随窗体大小变化自動調整

有时窗体变化后,如改变分辨率后控件大小却不能随之改变。手工代码调整实在麻烦,下面的模块实现自动查找窗体上控件并使其改变大小以适应窗体变化。

在Form的Resize事件中调用函数Resize_All就能实现控件自动调整大小,如:

Private Sub Form_Resize()

Dim H, i As Integer

On Error Resume Next

Resize_ALL Me 'Me是窗体名,Form1,Form2等等都可以

End Sub

在模块中添加以下代码:

Public Type ctrObj

Name As String

Index As Long

Parrent As String

Top As Long

Left As Long

Height As Long

Width As Long

ScaleHeight As Long

ScaleWidth As Long

End Type

Private FormRecord() As ctrObj

Private ControlRecord() As ctrObj

Private bRunning As Boolean

Private MaxForm As Long

Private MaxControl As Long

Private Const WM_NCLBUTTONDOWN = &HA1

Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function ReleaseCapture Lib "USER32" () As Long

Function ActualPos(plLeft As Long) As Long

If plLeft < 0 Then

ActualPos = plLeft + 75000

Else

ActualPos = plLeft

End If

End Function

Function FindForm(pfrmIn As Form) As Long

Dim i As Long

FindForm = -1

If MaxForm > 0 Then

For i = 0 To (MaxForm - 1)

If FormRecord(i).Name = pfrmIn.Name Then

FindForm = i

Exit Function

End If

Next i

End If

End Function

Function AddForm(pfrmIn As Form) As Long

Dim FormControl As Control

Dim i As Long

ReDim Preserve FormRecord(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

For Each FormControl In pfrmIn

i = FindControl(FormControl, pfrmIn.Name)

If i < 0 Then

i = AddControl(FormControl, pfrmIn.Name)

End If

Next FormControl

End Function

Function FindControl(inControl As Control, inName As String) As Long

Dim i As Long

FindControl = -1

For i = 0 To (MaxControl - 1)

If ControlRecord(i).Parrent = inName Then

If ControlRecord(i).Name = inControl.Name Then

On Error Resume Next

If ControlRecord(i).Index = inControl.Index Then

FindControl = i

Exit Function

End If

On Error GoTo 0

End If

End If

Next i

End Function

Function AddControl(inControl As Control, inName As String) As Long

ReDim Preserve ControlRecord(MaxControl + 1)

On Error Resume Next

ControlRecord(MaxControl).Name = inControl.Name

ControlRecord(MaxControl).Index = inControl.Index

ControlRecord(MaxControl).Parrent = inName

If TypeOf inControl Is Line Then

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

End If

inControl.IntegralHeight = False

On Error GoTo 0

AddControl = MaxControl

MaxControl = MaxControl + 1

End Function

Function PerWidth(pfrmIn As Form) As Long

Dim i As Long

i = FindForm(pfrmIn)

If i < 0 Then

i = AddForm(pfrmIn)

End If

PerWidth = (pfrmIn.ScaleWidth * 100) \ FormRecord(i).ScaleWidth

End Function

Function PerHeight(pfrmIn As Form) As Double

Dim i As Long

i = FindForm(pfrmIn)

If i < 0 Then

i = AddForm(pfrmIn)

End If

PerHeight = (pfrmIn.ScaleHeight * 100) \ FormRecord(i).ScaleHeight

End Function

Public Sub ResizeControl(inControl As Control, pfrmIn As Form)

On Error Resume Next

Dim i As Long

Dim widthfactor As Single, heightfactor As Single

Dim minFactor As Single

Dim yRatio, xRatio, lTop, lLeft, lWidth, lHeight As Long

yRatio = PerHeight(pfrmIn)

xRatio = PerWidth(pfrmIn)

i = FindControl(inControl, pfrmIn.Name)

If inControl.Left < 0 Then

lLeft = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)

Else

lLeft = CLng((ControlRecord(i).Left * xRatio) \ 100)

End If

lTop = CLng((ControlRecord(i).Top * yRatio) \ 100)

lWidth = CLng((ControlRecord(i).Width * xRatio) \ 100)

lHeight = CLng((ControlRecord(i).Height * yRatio) \ 100)

If TypeOf inControl Is Line Then

If inControl.X1 < 0 Then

inControl.X1 = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)

Else

inControl.X1 = CLng((ControlRecord(i).Left * xRatio) \ 100)

End If

inControl.Y1 = CLng((ControlRecord(i).Top * yRatio) \ 100)

If inControl.X2 < 0 Then

inControl.X2 = CLng(((ControlRecord(i).Width * xRatio) \ 100) - 75000)

Else

inControl.X2 = CLng((ControlRecord(i).Width * xRatio) \ 100)

End If

inControl.Y2 = CLng((ControlRecord(i).Height * yRatio) \ 100)

Else

inControl.Move lLeft, lTop, lWidth, lHeight

inControl.Move lLeft, lTop, lWidth

inControl.Move lLeft, lTop

End If

End Sub

Public Sub ResizeForm(pfrmIn As Form)

Dim FormControl As Control

Dim isVisible As Boolean

Dim StartX, StartY, MaxX, MaxY As Long

Dim bNew As Boolean

If Not bRunning Then

bRunning = True

If FindForm(pfrmIn) < 0 Then

bNew = True

Else

bNew = False

End If

If pfrmIn.Top < 30000 Then

isVisible = pfrmIn.Visible

On Error Resume Next

If Not pfrmIn.MDIChild Then

On Error GoTo 0

' ' pfrmIn.Visible = False

Else

If bNew Then

StartY = pfrmIn.Height

StartX = pfrmIn.Width

On Error Resume Next

For Each FormControl In pfrmIn

If FormControl.Left + FormControl.Width + 200 > MaxX Then

MaxX = FormControl.Left + FormControl.Width + 200

End If

If FormControl.Top + FormControl.Height + 500 > MaxY Then

MaxY = FormControl.Top + FormControl.Height + 500

End If

If FormControl.X1 + 200 > MaxX Then

MaxX = FormControl.X1 + 200

End If

If FormControl.Y1 + 500 > MaxY Then

MaxY = FormControl.Y1 + 500

End If

If FormControl.X2 + 200 > MaxX Then

MaxX = FormControl.X2 + 200

End If

If FormControl.Y2 + 500 > MaxY Then

MaxY = FormControl.Y2 + 500

End If

Next FormControl

On Error GoTo 0

pfrmIn.Height = MaxY

pfrmIn.Width = MaxX

End If

On Error GoTo 0

End If

For Each FormControl In pfrmIn

ResizeControl FormControl, pfrmIn

Next FormControl

On Error Resume Next

If Not pfrmIn.MDIChild Then

On Error GoTo 0

pfrmIn.Visible = isVisible

Else

If bNew Then

pfrmIn.Height = StartY

pfrmIn.Width = StartX

For Each FormControl In pfrmIn

ResizeControl FormControl, pfrmIn

Next FormControl

End If

End If

On Error GoTo 0

End If

bRunning = False

End If

End Sub

Public Sub SaveFormPosition(pfrmIn As Form)

Dim i As Long

If MaxForm > 0 Then

For i = 0 To (MaxForm - 1)

If FormRecord(i).Name = pfrmIn.Name Then

FormRecord(i).Top = pfrmIn.Top

FormRecord(i).Left = pfrmIn.Left

FormRecord(i).Height = pfrmIn.Height

FormRecord(i).Width = pfrmIn.Width

Exit Sub

End If

Next i

AddForm (pfrmIn)

End If

End Sub

Public Sub RestoreFormPosition(pfrmIn As Form)

Dim i As Long

If MaxForm > 0 Then

For i = 0 To (MaxForm - 1)

If FormRecord(i).Name = pfrmIn.Name Then

If FormRecord(i).Top < 0 Then

pfrmIn.WindowState = 2

ElseIf FormRecord(i).Top < 30000 Then

pfrmIn.WindowState = 0

pfrmIn.Move FormRecord(i).Left, FormRecord(i).Top, FormRecord(i).Width, FormRecord(i).Height

Else

pfrmIn.WindowState = 1

End If

Exit Sub

End If

Next i

End If

End Sub

Public Sub Resize_ALL(Form_Name As Form)

Dim OBJ As Object

For Each OBJ In Form_Name

ResizeControl OBJ, Form_Name

Next OBJ

End Sub

Public Sub DragForm(frm As Form)

On Local Error Resume Next

Call ReleaseCapture

Call SendMessage(frm.hwnd, WM_NCLBUTTONDOWN, 2, 0)

End Sub 从vb6升级到vb.net 有关从 Visual Basic 6.0 升级到 Visual Basic.NET 的其他信息,请参阅白皮书《准备将 Visual Basic 6.0 应用程序升级到 Visual Basic.NET》(英文)。此白皮书介绍了升级过程并提供了尽可能顺利升级的建设性意见

VB

16,721

社区成员

110,537

社区内容

发帖 与我相关 我的任务 VB VB技术相关讨论,主要为经典vb,即VB6.0 复制链接

扫一扫 分享 确定 社区描述 VB技术相关讨论,主要为经典vb,即VB6.0 社区管理员

加入社区

获取链接或二维码

近7日

近30日

至今

加载中

查看更多榜单

社区公告

暂无公告 试试用AI创作助手写篇文章吧

+ 用AI写文章