设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 2631|回复: 12
打印 上一主题 下一主题

[模块/函数] Access开发者手册中的窗体自动适应大小的代码,营养丰富

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2015-6-6 08:28:54 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
Access开发者手册中的窗体自动适应屏幕大小(窗体及窗体里的控件自动适应伸缩)的代码,营养丰富,使用了API,是Access代码中的经典,有兴趣的网友,可深入研究一下。
Option Compare Database 'Use database order for string comparisons
Option Explicit


' From Microsoft Access 95 Developer's Handbook
' by Litwin, Getz, Gilbert, and Reddick (Sybex)
' Copyright 1995. All rights reserved.


' Store rectangle coordinates.
Type glrTypeRect
X1 As Long
Y1 As Long
X2 As Long
Y2 As Long
End Type


Declare Function glr_apiIsIconic Lib "user32" _
Alias "IsIconic" (ByVal hwnd As Long) As Long


Declare Function glr_apiGetDeviceCaps Lib "gdi32" _
Alias "GetDeviceCaps" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long


Declare Function glr_apiGetWindowRect Lib "user32" _
Alias "GetWindowRect" (ByVal hwnd As Long, _
lpRect As glrTypeRect) As Long


Declare Function glr_apiGetParent Lib "user32" _
Alias "GetParent" (ByVal hwnd As Long) As Long


Declare Function glr_apiGetClientRect Lib "user32" _
Alias "GetClientRect" (ByVal hwnd As Long, _
lpRect As glrTypeRect) As Long


Declare Function glr_apiGetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long


Declare Function glr_apiGetSystemMetrics Lib "user32" _
Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long


Declare Function glr_apiGetSystemMenu Lib "user32" _
Alias "GetSystemMenu" (ByVal hwnd As Long, _
ByVal bRevert As Long) As Long


Declare Function glr_apiGetActiveWindow Lib "user32" _
Alias "GetActiveWindow" () As Long






'================================================= ======================


' Store group/subform dimensions.
Type glrTypeDimensions
sglLeft As Single
sglTop As Single
sglWidth As Single
sglHeight As Single
strCtlName As String
End Type
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

12#
发表于 2015-7-10 15:44:40 | 只看该作者
good
回复

使用道具 举报

点击这里给我发消息

11#
发表于 2015-6-7 06:05:25 来自手机 | 只看该作者
没我们论坛资料多,MVP不少

点击这里给我发消息

10#
发表于 2015-6-7 06:04:10 来自手机 | 只看该作者
Ms的,人气不错

点击这里给我发消息

9#
发表于 2015-6-7 06:03:41 来自手机 | 只看该作者
http://answers.microsoft.com/en-us/office/forum/access?tab=Threads

点击这里给我发消息

8#
发表于 2015-6-7 05:09:01 来自手机 | 只看该作者
方法不错,回头试下,感觉有点外挂的味道,不象VBA了,哈哈

点击这里给我发消息

7#
发表于 2015-6-7 05:07:33 来自手机 | 只看该作者
缇与象素互换比较有意义,这应是老外想出处理上-一篇用类打开子窗体的方法?

点击这里给我发消息

6#
发表于 2015-6-6 14:24:13 | 只看该作者
占个位,好好消化一下。。谢谢分享。。

点击这里给我发消息

5#
 楼主| 发表于 2015-6-6 08:30:15 | 只看该作者

Private Sub SetFormSize(frm As Form, sglFactorX As Single, sglFactorY As
Single, rct As glrTypeRect, fMove As Integer)

' Actually do the work to resize all the controls
' on the given form, and then resize the form
' itself.

Dim intTemp As Integer
Dim intWidth As Integer
Dim intHeight As Integer
Dim Ctl As Control
Dim sglFontSize As Single
Dim intI As Integer
Dim intGroups As Integer
Dim aGroups() As glrTypeDimensions
Dim colGroups As New Collection
Dim varTemp As Variant

On Error GoTo SetFormSizeError

DoCmd.Hourglass True
frm.Painting = False

' If the form is growing vertically, then need to
' fix up the section heights now. If it's shrinking,
' fix up the heights AFTER you place the controls.
' The same goes for the form width.
If sglFactorY > 1 Then
' Fix up all the section heights.
FixSections frm, sglFactorY
varTemp = frm.Width * sglFactorX
If varTemp > glrcMaxTwips Then
frm.Width = glrcMaxTwips
Else
frm.Width = varTemp
End If
End If

' Now deal with all the controls
' Go through and deal with all the groups and subforms first.
intGroups = FixGroups(frm, aGroups(), sglFactorX, sglFactorY)

' Now go back and deal with all the rest of the controls.
For Each Ctl In frm.Controls

Select Case Ctl.ControlType
Case acOptionGroup
GoTo NextCtl

Case acSubform
' If you've got a subform, then recurse on down into this
' routine again, dealing with all the controls inside of
' that subform.
SetFormSize Ctl.Form, sglFactorX, sglFactorY, rct, False

GoTo NextCtl
Case acPage
'an acPage, is a 'tab page' on a tab control
'The 'tab page' is automatically resized by the tab control
when the tab control itself is resized
'so.. don't resize the tab page. If the tab page WERE TO BE
resized, the tab control will automatically
'resize itself to match the page size (i.e. tab control
resizes increasing the page size, then page is resized
'increasing the tabcontrol size, then the next page is
resized, which again resizes everything etc, etc etc....
'the tab control would grow X times for each page on it)
GoTo NextCtl
End Select

' So the control isn't a subform and it's not a group.
' Therefore, just scale it to the correct size.

' First, fix up the font, if this control has a font
' that needs to be fixed up.
If ChangeFont(Ctl) Then
sglFontSize = Ctl.FontSize * sglFactorY
Else
sglFontSize = -1
End If

' Set the top, left and width values.

If frm.CurrentView <> acDatasheetView Then
Ctl.Top = Ctl.Top * sglFactorY
Ctl.Left = Ctl.Left * sglFactorX
Ctl.Width = Ctl.Width * sglFactorX
End If

' Change the height, if that's required.
If ChangeHeight(Ctl) Then
Ctl.Height = Ctl.Height * sglFactorY
End If

' Only attempt to change the font size for
' certain types of controls.
If sglFontSize >= 1 And sglFontSize <= 127 Then
Ctl.FontSize = sglFontSize
End If
NextCtl:
Next Ctl

' If the form is shrinking vertically, fix up the
' section heights now that all the controls have been
' placed. The same goes for the form width.
If sglFactorY < 1 Then
' Fix up all the section heights.
FixSections frm, sglFactorY
frm.Width = frm.Width * sglFactorX
End If

' Go through and fix up the option groups/subforms,
' which may have been distorted by changes to
' the internal controls.
For intI = 1 To intGroups
With frm(aGroups(intI).strCtlName)
.Top = aGroups(intI).sglTop
.Left = aGroups(intI).sglLeft
.Width = aGroups(intI).sglWidth
.Height = aGroups(intI).sglHeight
End With
Next intI

If fMove Then
intWidth = Int((rct.X2 - rct.X1) * sglFactorX)
intHeight = Int((rct.Y2 - rct.Y1) * sglFactorY)

rct.X1 = Int(rct.X1 * sglFactorX)
rct.Y1 = Int(rct.Y1 * sglFactorY)
rct.X2 = rct.X1 + intWidth
rct.Y2 = rct.Y1 + intHeight

intTemp = glr_apiMoveWindow(frm.hwnd, rct.X1, rct.Y1, intWidth,
intHeight, True)

' Use the Window-Size To Fit menu item.
DoCmd.DoMenuItem acFormBar, acWindow, acSizeToFit, , acMenuVer70

End If

SetFormSizeExit:
frm.Painting = True
DoCmd.Hourglass False
Exit Sub

SetFormSizeError:
Select Case Err
Case glrcErrInvalidProperty
Resume Next
Case Else
'HandleError "SetFormSize", Err.Number, Error.Description
Resume Next
End Select
End Sub

点击这里给我发消息

4#
 楼主| 发表于 2015-6-6 08:30:01 | 只看该作者

Private Function GetTwips(intXResolution As Integer, intAxis As Integer)

' Experience has shown that the twips/pixel ratios
' are dependent on the screen resolution. If you find this
' not to be true in your particular case, you'll need to modify
' this routine.

Select Case intXResolution
Case 1024, 1280
GetTwips = IIf(intAxis = glrcXAxis, _
glrcDesignXTwipsHiRes, glrcDesignYTwipsHiRes)
Case 640, 800
GetTwips = IIf(intAxis = glrcXAxis, _
glrcDesignXTwipsLoRes, glrcDesignYTwipsLoRes)
Case Else
' If the value is invalid, just assume the designed used
' a high-res screen. The worst this can do is cause
' an image that's a little small.
GetTwips = IIf(intAxis = glrcXAxis, _
glrcDesignXTwipsHiRes, glrcDesignYTwipsHiRes)
End Select
End Function

Function glrResizeForm(frm As Form, ByVal fDoResize As Variant, rctOriginal
As glrTypeRect)

' Called from the Resize event of forms.
' Attempt to resize the form and all its
' controls. Don't do anything if the
' current height of the form is 0, or if it's iconic.

' From Microsoft Access 95 Developer's Handbook
' by Litwin, Getz, Gilbert, and Reddick (Sybex)
' Copyright 1995. All rights reserved.

' In:
' frm: A reference to the form in question
' fDoResize: Yes/No (Actually do the resize, or just track the
information?)
' rctOriginal: the original coordinates
' Out:
' Nothing

Dim rctNew As glrTypeRect
Dim rctClient As glrTypeRect
Dim varTemp As Variant
Dim intWidth As Integer
Dim intHeight As Integer
Dim sglFactorX As Single
Dim sglFactorY As Single
On Error GoTo glrResizeWindowError
' Make sure the user hasn't sized this thing down
' to the nubs. If the client area is 0 height,
' it's time to call it quits.
glr_apiGetClientRect frm.hwnd, rctNew
intHeight = (rctNew.Y2 - rctNew.Y1)
If intHeight = 0 Or glr_apiIsIconic(frm.hwnd) Then
Exit Function
End If

' Get the current width. Already found the
' current height.
intWidth = (rctNew.X2 - rctNew.X1)

' Calc the scaling factor, given the current
' height/width and the previous height/width.
' Could be that rctOriginal has not yet been
' initialized, so trap for that error.

sglFactorX = intWidth / (rctOriginal.X2 - rctOriginal.X1)
sglFactorY = intHeight / (rctOriginal.Y2 - rctOriginal.Y1)

sglFactorOK:
' Store away the current values for
' the next time through here.
With rctOriginal
.X1 = rctNew.X1
.X2 = rctNew.X2
.Y1 = rctNew.Y1
.Y2 = rctNew.Y2
End With
' If the ratios are 1, there's nothing to do.
If (sglFactorX <> 1) Or (sglFactorY <> 1) Then
' If you actually want to do some resizing, do it now.
If fDoResize Then
SetFormSize frm, sglFactorX, sglFactorY, rctNew, False
End If
End If

glrResizeWindowExit:
Exit Function

glrResizeWindowError:
If Err = glrcErrDivisionByZero Then
sglFactorX = 1
sglFactorY = 1
Resume sglFactorOK
Else
HandleError "glrResizeForm", Err.Number, Err.Description
Resume Next
End If
End Function

Function glrScaleForm(frm As Form, intX As Integer, intY As Integer,
rctOriginal As glrTypeRect)

' Called from the Open event of forms.
' Attempts to scale the form appropriately
' for the given screen size, as compared
' to the size screen on which it was designed.

' From Microsoft Access 95 Developer's Handbook
' by Litwin, Getz, Gilbert, and Reddick (Sybex)
' Copyright 1995. All rights reserved.
'
' In:
' frm: A reference to the form in question
' intX: the horizontal screen resolution at which the form was
designed.
' intY: the vertical screen resolution at which the form was
designed.
' rctOriginal: original coordinates
'
' Out:
' Nothing
' Comments:
' Use a function call like this:
' intRetval = glrScaleForm(Me, 640, 480, rctOriginal)
' to autoscale a form created at 640x480 resolution.

Dim intTwipsPerPixelX As Integer
Dim intTwipsPerPixelY As Integer
Dim intScreenX As Integer
Dim intScreenY As Integer

Dim sglFactorX As Single
Dim sglFactorY As Single

GetScreenScale intX, intY, sglFactorX, sglFactorY

' Whether or not this form gets rescaled,
' you'll need to store away the current size
' for later. The reason you must call GetFormSize
' here, rather than glr_apiGetClientRect, is that
' you need the screen positioning information
' which you don't get with GetClientRect.
GetFormSize frm, rctOriginal

' If the x and y factors are both 1, there's nothing
' to do, so get out here.
If (sglFactorX = 1) And (sglFactorY = 1) Then Exit Function

' If you don't want forms to expand (they were created on a
' lower-resolution device than the current device), but only
' shrink (they were created on a higher-resolution device
' than the current device), then uncomment the next line.
'If (sglFactorX > 1) And (sglFactorY > 1) Then Exit Sub
DoCmd.RepaintObject
SetFormSize frm, sglFactorX, sglFactorY, rctOriginal, True
End Function
Private Sub HandleError(strFunction As String, intErr As Integer, strError
As String)
MsgBox "Error: " & strError & " (" & intErr & ")", vbExclamation,
strFunction
End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-9-24 22:20 , Processed in 0.086915 second(s), 34 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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