ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1572|回复: 7

[求助] VBA窗体控件随窗口大小而自动调整

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-12-24 15:14 | 显示全部楼层 |阅读模式
网上找到两段代码
1、以下代码可自由拉伸窗口大小,但窗口控件不会变化
#If VBA7 Then
  Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As LongPtr, _
    ByVal lpWindowName As LongPtr) As Long
  Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
  Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#Else
  Private Declare Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As Long, _
    ByVal lpWindowName As Long) As Long
  Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
  Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If

Dim hWnd As Long

  hWnd = FindWindow(StrPtr("ThunderDFrame"), StrPtr(Me.Caption))
  SetWindowLong hWnd, -16, GetWindowLong(hWnd, -16) Or &H40000 Or &H20000 Or &H10000


2、以下代码可以实现控件随窗口大小变化,但窗口不可以自由调整大小

'Option Explicit
'Dim frmW, frmH
'Dim i
'
'Private Sub Form_Load()
'If WindowState <> vbMaximized Then
'frmW = Me.Width
'frmH = Me.Height
'End If
'For i = 0 To Ms1.Cols - 1
'Ms1.ColWidth(i) = Int((Ms1.Width - 360) / Ms1.Cols)
'Next
'End Sub
'
'Private Sub Form_Resize()
'On Error Resume Next
'If frmW = 0 Or frmH = 0 Then Exit Sub
'Dim c As Control, x, y
'x = Me.Width / frmW
'y = Me.Height / frmH
'For Each c In Me.Controls
'With c
'.Left = Int(.Left * x)
'.Top = Int(.Top * y)
'.Width = Int(.Width * x)
'.Height = Int(.Height * y)
'End With
'Next
'For i = 0 To Ms1.Cols - 1
'Ms1.ColWidth(i) = Int(Ms1.ColWidth(i) * x)
'Next
'frmW = Me.Width
'frmH = Me.Height
'End Sub


'Dim p As Object, x, y
'With Application
'.WindowState = xlMaximized
'x = .Width / Me.Width
'y = .Height / Me.Height
'For Each p In Me.Controls
'p.Move p.Left * x, p.Top * y, p.Width * x, p.Height * y
'Next
'Me.Move .Left, .Top, .Width, .Height
'End With


求大神把这两段代码综合写一下新代码,可自由拉伸窗口,窗口控件随窗口大小自动调整大小。

TA的精华主题

TA的得分主题

发表于 2021-12-24 18:55 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
参考~
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As LongPtr, _
    ByVal lpWindowName As LongPtr) As Long
    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
    Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As Long, _
    ByVal lpWindowName As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If

Private Sub UserForm_Initialize()
    Dim hWnd As Long
    'hWnd = FindWindow(StrPtr("ThunderDFrame"), StrPtr(Me.Caption))
    'SetWindowLong hWnd, -16, GetWindowLong(hWnd, -16) Or &H40000 Or &H20000 Or &H10000
    'Debug.Print hWnd

    Application.WindowState = xlNormal

End Sub

Private Sub UserForm_Resize()
    Dim frmW, frmH
    frmW = Me.Width
    frmH = Me.Height

    Dim c As Control
    For Each c In Me.Controls
        c.Left = 0
        c.Width = frmW
    Next

End Sub



TA的精华主题

TA的得分主题

发表于 2021-12-24 19:01 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
可以实现,但我的方法很简单,没有使用API。
附件请测试,在窗体空白处拖拉即可。

任意拉伸窗体含控件.zip

13.97 KB, 下载次数: 44

TA的精华主题

TA的得分主题

发表于 2021-12-24 21:16 | 显示全部楼层
本帖最后由 大灰狼1976 于 2021-12-24 21:23 编辑

加一句repaint减少拉伸过程中的窗体闪动和残影,至少在我这边是有肉眼可见效果的。

任意拉伸窗体含控件.zip

17.49 KB, 下载次数: 86

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-12-24 21:18 | 显示全部楼层
大灰狼1976 发表于 2021-12-24 19:01
可以实现,但我的方法很简单,没有使用API。
附件请测试,在窗体空白处拖拉即可。

谢谢大佬,,非常感谢,已完美解决

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-12-24 21:19 | 显示全部楼层
exorca 发表于 2021-12-24 18:55
参考~
Option Explicit

已调用,可行,谢谢指点。

TA的精华主题

TA的得分主题

发表于 2021-12-25 09:42 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
谢谢   分享   很实用

TA的精华主题

TA的得分主题

发表于 2024-3-23 14:59 | 显示全部楼层
大灰狼1976 发表于 2021-12-24 21:16
加一句repaint减少拉伸过程中的窗体闪动和残影,至少在我这边是有肉眼可见效果的。

66666666666666666666666
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 10:24 , Processed in 0.042114 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表