Office中国论坛/Access中国论坛

标题: [已解决] 关于如何获取控件在屏幕中的实际坐标问题 [打印本页]

作者: eyewitnes    时间: 2008-4-4 11:23
标题: [已解决] 关于如何获取控件在屏幕中的实际坐标问题
在旧贴中搜索到可以使用ME.COMMAND0.TOP和ME.COMMAND0.LEFT
可是在我使用的时候提示应用程序定义或对象定义错误是怎么回事?
Dim Frm As Form
Dim Ctl As Control
Dim frmLeft As Long
Dim frmTop As Long
    Set Frm = Screen.ActiveForm
    Set Ctl = Screen.ActiveControl
    Debug.Print Ctl.Height
    Debug.Print Frm.WindowTop
    Debug.Print Ctl.Top
    Debug.Print Frm.command0.Top-----运行到此行出错

我想实现的是在双击某个控件的时候打开另外一个窗体b,窗体b的top为控件的下方,left为控件的left
应该如何实现?

[ 本帖最后由 cuxun 于 2008-4-4 14:43 编辑 ]
作者: eyewitnes    时间: 2008-4-4 13:05
谢谢[:50] [:50] [:50]
作者: andymark    时间: 2008-4-4 14:48
供参考:

Private Declare Sub 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)
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long


Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40

Const SM_CYCAPTION = 4    '标题高度

Const SM_CXFRAME = 32    '可调边框宽度
Const SM_CYFRAME = 33    ''可调边框高度


Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

    Dim FrmHwnd As Long
    Dim Rec As RECT
   
    '获得整个窗口的范围矩形
    GetWindowRect Me.hwnd, Rec
   
    '假设Command7在窗体主体上的某个控件(这里不考虑页眉)
    '则Command7的Left坐标为:
    Rec.Left = Rec.Left + (Me.Command7.Left / 15) + GetSystemMetrics(SM_CXFRAME)
    '则Command7的Top坐标为:
    Rec.Top = Rec.Top + (Me.Command7.Top + Me.Command7.Height) / 15 + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)
    '这里以打开"选择"窗体作例子
    DoCmd.OpenForm "选择", acNormal
    '取得"选择"窗体的句柄
    FrmHwnd = Forms!选择.hwnd
    '重置"选择"窗体位置
    SetWindowPos FrmHwnd, HWND_TOPMOST, Rec.Left, Rec.Top, 0, 0, SWP_NOSIZE




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3