已有23人关注
控件随窗体缩放问题(EXE运行正常,源码中运行不正常)
发表在VB答疑区 2020-09-14
是否精华
版块置顶:

Option Explicit

Private ObjOldWidth As Long  '保存窗体的原始宽度

Private ObjOldHeight As Long '保存窗体的原始高度

Private ObjOldFont As Single '保存窗体的原始字体比

Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _

 ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

'在调用ResizeForm前先调用本函数

Public Sub ResizeInit(FormName As Form)

 Dim Obj As Control

 ObjOldWidth = FormName.ScaleWidth

 ObjOldHeight = FormName.ScaleHeight

 ObjOldFont = FormName.Font.Size / ObjOldHeight

 On Error Resume Next

'   *******Each是一个关键字,作用是针对一个数组或集合中的每个元素,重复执行一组语句

 For Each Obj In FormName

   '*******Tag返回或设置一个表达式用来存储程序中需要的额外数据。

 If TypeOf Obj Is ComboBox Then

    Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.FontSize & " "

 ElseIf TypeOf Obj Is CommandButton Then

                Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " " & Obj.FontSize & " "

 ElseIf TypeOf Obj Is Line Then

    Obj.Tag = Obj.X1 & " " & Obj.X2 & " " & Obj.Y1 & " " & Obj.Y2 & " " & Obj.BorderWidth & " "

 Else

   If Obj.Left >= 0 Then Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "

    '**************Obj.Left < 0是隐藏控件,+75000来保存其没隐藏的坐标记录

   If Obj.Left < 0 Then Obj.Tag = Obj.Left + 75000 & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "

  End If

    Next Obj

On Error GoTo 0

End Sub

'按比例改变表单内各元件的大小,在调用ReSizeForm前先调用ReSizeInit函数

Public Sub ResizeForm(FormName As Form)

Dim Pos(4) As Double

Dim i As Long, TempPos As Long, StartPos As Long

Dim Obj As Control

Dim ScaleX As Double

Dim ScaleY As Double

ScaleX = FormName.ScaleWidth / ObjOldWidth          '保存窗体宽度缩放比例

ScaleY = FormName.ScaleHeight / ObjOldHeight        '保存窗体高度缩放比例

On Error Resume Next

 For Each Obj In FormName

   StartPos = 1

     For i = 0 To 4

'读取控件的原始位置与大小

'InStr函数,返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置。语法:InStr([start, ]string1, string2[, compare])

      TempPos = InStr(StartPos, Obj.Tag, " ", vbTextCompare)

        If TempPos > 0 Then

            Pos(i) = Mid(Obj.Tag, StartPos, TempPos - StartPos)

            StartPos = TempPos + 1

         Else

            Pos(i) = 0

         End If

     Next i

'根据控件的原始位置及窗体改变大小的比例对控件重新定位与改变大小

  If TypeOf Obj Is ComboBox Then

      Obj.FontSize = Pos(3) * ScaleY

      Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX

  ElseIf TypeOf Obj Is CommandButton Then

      Obj.FontSize = Pos(4) * ScaleY

      Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY

  ElseIf TypeOf Obj Is Line Then

   Obj.X1 = Pos(0) * ScaleX

   Obj.X2 = (Pos(1) - Pos(0)) * ScaleX + Obj.X1

   Obj.Y1 = Pos(2) * ScaleY

   Obj.Y2 = (Pos(3) - Pos(2)) * ScaleY + Obj.Y1

   Obj.BorderWidth = Pos(4) * Sqr(ScaleX * ScaleX + ScaleY * ScaleY)

  Else '*********解决SSTab隐藏属性

   If Obj.Left >= 0 Then Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY

  '**********Obj.Left < 0是隐藏控件,进行移动后-75000来隐藏【因为之前Obj.Tag记录的是未隐藏的坐标】

   If Obj.Left < 0 Then Obj.Move Pos(0) * ScaleX - 75000, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY

     Obj.Font.Size = ObjOldFont * FormName.ScaleHeight

   End If

 Next Obj

  On Error GoTo 0

End Sub

Private Sub Form_Load()

 Call ResizeInit(Me)


End Sub


Private Sub Form_Resize()

 Call ResizeForm(Me)

End Sub



分享到:
精彩评论 3
配变软件_1600050665
学分:5 LV1
2020-09-14
沙发

补充:家里电脑源码中出错,办公电脑中源码又运行正常

配变软件_1600050665
学分:5 LV1
2020-09-21
板凳

1. win7重装VB6.0后,在VB6.EXE属性中“兼容运行”不勾选后运行正常


2.  Obj.Font.Size = ObjOldFont * FormName.ScaleHeight  

某些控件无字体属性时仍会出错

只能通过判断控件类型来解决

淡.印象
学分:323 LV5
TA的每日心情
学习Python
2020-11-23 22:17:24
2020-12-22
地板

读者您好:

由于VB6是比较老的语言,因此对于WIN7和WIN10的系统支持不是很好。

首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照