获取和设置access主窗体大小及位置代码
时 间:2009-02-17 08:31:14
作 者:lxh1968 ID:11 城市:上海 QQ:3002789054
摘 要:获取和设置ACCESS主窗体大小及位置代码
正 文:
获取和设置ACCESS主窗体大小及位置代码
'//按 ALT+F11 转到 vba 界面,
'//新建一个模块
'//将以下代码 COPY 进去
'//将光标停在 Function RunTest() 这行
'//按 F5 即可运行
'//运行结束后转到 ACCESS 使用界面,即可看到效果
'-----------------------------------------------
'自定义数据类型,GetAccessWindow的返回值
Public Type AWPix
Left As Long
Top As Long
Width As Long
Height As Long
End Type
'-----------------------------------------------
'获取、设置 Window状态的API
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Type RECT '屏幕坐标中随同窗口装载的矩形
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'-----------------------------------------------
'获取分辩率设置的 API
Public Declare Function apiGetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
Public Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, _
ByVal hdc As Long) As Long
Public Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Public Const LOGPIXELSX = 88
Public Const LOGPIXELSY = 90
Public Const DIRECTION_VERTICAL = 1
Public Const DIRECTION_HORIZONTAL = 0
'-----------------------------------------------
'获取窗体缩放状态的 API
'缩放状态
Public Declare Function IsZoomed Lib "user32" (ByVal hwnd As Long) As Long
'是否最小化
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
'是否可见
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
'---------------------------------------------
'设置窗体状态的 API
Public Const SW_HIDE = 0 '隐藏
Public Const SW_SHOWNORMAL = 1 '普通(还原)
Public Const SW_SHOWMINIMIZED = 2 '最小化
Public Const SW_SHOWMAXIMIZED = 3 '最大化
Public Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
'----------------------------------------------
'像素转换成缇,本站以前文章中已经介绍过了。
' 关于单位“缇”与“像素”的转换,以及缇与其他单位(例如:厘米)之间的转换《窗体》
' http://access911.net/index.asp?u1=a&u2=72FAB41E13DCE9F3
Function PixelsToTwips(rlngPixels As Long, rlngDirection As Long) As Long
On Error GoTo PixelsToTwips_Err
Dim lngDeviceHandle As Long
Dim lngPixelsPerInch As Long
lngDeviceHandle = apiGetDC(0)
If rlngDirection = DIRECTION_HORIZONTAL Then '水平X方向
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSX)
Else '垂直Y方向
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSY)
End If
lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
PixelsToTwips = rlngPixels * 1440 / lngPixelsPerInch
PixelsToTwips_Exit:
On Error Resume Next
Exit Function
PixelsToTwips_Err:
MsgBox Err.Description, vbExclamation, "access911.net"
Resume PixelsToTwips_Exit
End Function
'===========================================================
' 过程及函数名: RunTest
' 版本号 : --
' 说明 : 本过程只用于演示如何用VBA+WINAPI 控制
' Access 主窗体的位置和大小
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : --
' 调用演示 : RunTest
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function RunTest()
'显示当前Access主窗体的高度
Debug.Print GetAccessWindow.Height
'设置当前Access窗体:
'宽 553像素,高400像素,距离上边20像素,左边12像素
SetAccessWindow 12, 20, 553, 400
End Function
'===========================================================
' 过程及函数名: GetAccessWindow
' 版本号 : --
' 说明 : 获取 ACCESS 主窗体的大小及位置,获取单位是
' 像素,如果要转为ACCESS的度量衡单位“Twip缇”
' 可以用函数 PixelsToTwips 转换。
' 注意,本函数还定义了一个 Type AWPix
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : 返回自定义类型 AWPix 数据。
' 调用演示 : Debug.Print GetAccessWindow.Height
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function GetAccessWindow() As AWPix
Dim intWidth As Long, intHeight As Long
Dim tAWPix As AWPix
Dim lngRet As Long
Dim Rc As RECT
Dim lngHwndMDI As Long
'获取ACCESS主窗体内嵌子对象的句柄
lngHwndMDI = FindWindowEx(Application.hWndAccessApp, _
0&, "MDIClient", "")
'上边距中不包含工具栏和菜单栏。尝试去掉工具栏看一下结果,然后再加上工具栏再看看结果
'lngRet = GetWindowRect(lngHwndMDI, Rc)
'获取整个ACCESS窗体最外侧的尺寸,在Win2003+acc2003的情况下最大化时每边都需要+4
lngRet = GetWindowRect(Application.hWndAccessApp, Rc)
With tAWPix
.Top = Rc.Top
.Left = Rc.Left
.Height = Rc.Bottom - Rc.Top
.Width = Rc.Right - Rc.Left
End With
GetAccessWindow = tAWPix
End Function
'===========================================================
' 过程及函数名: SetAccessWindow
' 版本号 : --
' 说明 : 设置 ACCESS 主窗体的大小及位置,设置单位是像素
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : --
' 调用演示 : SetAccessWindow 0,0,150,566
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function SetAccessWindow(ByVal XLeft As Long, _
ByVal YTop As Long, _
ByVal XWidth As Long, _
ByVal YHeight As Long)
Dim lngHwndMDI As Long
Dim lngRet As Long
Dim Rc As RECT
If IsZoomed(Application.hWndAccessApp) = 1 Or _
IsIconic(Application.hWndAccessApp) = 1 Then
apiShowWindow Application.hWndAccessApp, SW_SHOWNORMAL
End If
MoveWindow Application.hWndAccessApp, XLeft, YTop, XWidth, YHeight, True
End Function
'//按 ALT+F11 转到 vba 界面,
'//新建一个模块
'//将以下代码 COPY 进去
'//将光标停在 Function RunTest() 这行
'//按 F5 即可运行
'//运行结束后转到 ACCESS 使用界面,即可看到效果
'-----------------------------------------------
'自定义数据类型,GetAccessWindow的返回值
Public Type AWPix
Left As Long
Top As Long
Width As Long
Height As Long
End Type
'-----------------------------------------------
'获取、设置 Window状态的API
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Type RECT '屏幕坐标中随同窗口装载的矩形
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'-----------------------------------------------
'获取分辩率设置的 API
Public Declare Function apiGetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
Public Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, _
ByVal hdc As Long) As Long
Public Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Public Const LOGPIXELSX = 88
Public Const LOGPIXELSY = 90
Public Const DIRECTION_VERTICAL = 1
Public Const DIRECTION_HORIZONTAL = 0
'-----------------------------------------------
'获取窗体缩放状态的 API
'缩放状态
Public Declare Function IsZoomed Lib "user32" (ByVal hwnd As Long) As Long
'是否最小化
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
'是否可见
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
'---------------------------------------------
'设置窗体状态的 API
Public Const SW_HIDE = 0 '隐藏
Public Const SW_SHOWNORMAL = 1 '普通(还原)
Public Const SW_SHOWMINIMIZED = 2 '最小化
Public Const SW_SHOWMAXIMIZED = 3 '最大化
Public Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
'----------------------------------------------
'像素转换成缇,本站以前文章中已经介绍过了。
' 关于单位“缇”与“像素”的转换,以及缇与其他单位(例如:厘米)之间的转换《窗体》
' http://access911.net/index.asp?u1=a&u2=72FAB41E13DCE9F3
Function PixelsToTwips(rlngPixels As Long, rlngDirection As Long) As Long
On Error GoTo PixelsToTwips_Err
Dim lngDeviceHandle As Long
Dim lngPixelsPerInch As Long
lngDeviceHandle = apiGetDC(0)
If rlngDirection = DIRECTION_HORIZONTAL Then '水平X方向
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSX)
Else '垂直Y方向
lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, LOGPIXELSY)
End If
lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
PixelsToTwips = rlngPixels * 1440 / lngPixelsPerInch
PixelsToTwips_Exit:
On Error Resume Next
Exit Function
PixelsToTwips_Err:
MsgBox Err.Description, vbExclamation, "access911.net"
Resume PixelsToTwips_Exit
End Function
'===========================================================
' 过程及函数名: RunTest
' 版本号 : --
' 说明 : 本过程只用于演示如何用VBA+WINAPI 控制
' Access 主窗体的位置和大小
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : --
' 调用演示 : RunTest
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function RunTest()
'显示当前Access主窗体的高度
Debug.Print GetAccessWindow.Height
'设置当前Access窗体:
'宽 553像素,高400像素,距离上边20像素,左边12像素
SetAccessWindow 12, 20, 553, 400
End Function
'===========================================================
' 过程及函数名: GetAccessWindow
' 版本号 : --
' 说明 : 获取 ACCESS 主窗体的大小及位置,获取单位是
' 像素,如果要转为ACCESS的度量衡单位“Twip缇”
' 可以用函数 PixelsToTwips 转换。
' 注意,本函数还定义了一个 Type AWPix
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : 返回自定义类型 AWPix 数据。
' 调用演示 : Debug.Print GetAccessWindow.Height
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function GetAccessWindow() As AWPix
Dim intWidth As Long, intHeight As Long
Dim tAWPix As AWPix
Dim lngRet As Long
Dim Rc As RECT
Dim lngHwndMDI As Long
'获取ACCESS主窗体内嵌子对象的句柄
lngHwndMDI = FindWindowEx(Application.hWndAccessApp, _
0&, "MDIClient", "")
'上边距中不包含工具栏和菜单栏。尝试去掉工具栏看一下结果,然后再加上工具栏再看看结果
'lngRet = GetWindowRect(lngHwndMDI, Rc)
'获取整个ACCESS窗体最外侧的尺寸,在Win2003+acc2003的情况下最大化时每边都需要+4
lngRet = GetWindowRect(Application.hWndAccessApp, Rc)
With tAWPix
.Top = Rc.Top
.Left = Rc.Left
.Height = Rc.Bottom - Rc.Top
.Width = Rc.Right - Rc.Left
End With
GetAccessWindow = tAWPix
End Function
'===========================================================
' 过程及函数名: SetAccessWindow
' 版本号 : --
' 说明 : 设置 ACCESS 主窗体的大小及位置,设置单位是像素
' 引用 : --
' 输入参数 : --
' 输出值 : --
' 返回值 : --
' 调用演示 : SetAccessWindow 0,0,150,566
' 最后修改日期: 2008-1-30 16:36:00
'===========================================================
Function SetAccessWindow(ByVal XLeft As Long, _
ByVal YTop As Long, _
ByVal XWidth As Long, _
ByVal YHeight As Long)
Dim lngHwndMDI As Long
Dim lngRet As Long
Dim Rc As RECT
If IsZoomed(Application.hWndAccessApp) = 1 Or _
IsIconic(Application.hWndAccessApp) = 1 Then
apiShowWindow Application.hWndAccessApp, SW_SHOWNORMAL
End If
MoveWindow Application.hWndAccessApp, XLeft, YTop, XWidth, YHeight, True
End Function
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access系统自带的日期选择器不...(11.08)
- 分享一下Access工程中的acw...(11.07)
- Access快速开发平台--让有权...(11.04)
- Access快速开发平台--审批选...(11.01)
- ACCESS两张表先各自排序,然后...(10.31)
- Access对子窗体数据进行批量+...(10.30)
- SqlServer中如何用SQL命...(10.29)
- Access报表中的分组功能用代码...(10.28)
- 用Access计算库存结余的一个方...(10.26)
- 最精简的组合框行来源数据快速输入(...(10.25)