如何获取活动窗口的name属性,而不是标题名称。

解决方案 »

  1.   

    什么窗口?你自己的还是别人的?是MDI子窗体的?
      

  2.   

    Private Sub Command1_Click()
        Debug.Print Command1.Name
    End Sub
      

  3.   

    我想通过鼠标在屏幕上移动,而在自制程序的文本框中显示鼠标所在点对应窗口的name属性值。
      

  4.   

    我想利用这个名子进一步获取窗口的其他属性值。如窗口的宽、高等。难道只能通过api获取吗?有什么 API函数可用呢?
      

  5.   

    name获取不了,因为对于窗口,name根本就不存在。这是VB对某个窗口的标示而已。窗口的宽、高可以用GetWindowRect得到。
      

  6.   

    能否告诉我API函数:GetWindowRect的用法吗?请写出代码:获取屏幕上鼠标点所在窗口的宽度值和高度值立即给分。caozhy(cfx)亲爱的朋友等你:
    Option Explicit
    '获取目前选择的鼠标指针的句柄
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    '返回包含了指定点的窗口的句柄
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    '取得一个窗体的标题文字
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    '为指定的窗口取得类名
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    '从指定窗口的结构中取得信息
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPrivate Const GWL_ID = (-12) '对话框中一个子窗口的标识符
    Private Const GWL_STYLE = (-16) '窗口样式
    Private Const GWL_EXSTYLE = (-20) '扩展窗口样式Private Type POINTAPI
    X As Long
    Y As Long
    End TypePrivate Sub Command1_Click()
    Timer1.Interval = Text4.Text
    Timer1.Enabled = True
    Text4.Enabled = False
    Command1.Enabled = False
    Command2.Enabled = True
    End SubPrivate Sub Command2_Click()
    Timer1.Enabled = False
    Text4.Enabled = True
    Command1.Enabled = True
    Command2.Enabled = False
    End SubPrivate Sub Command3_Click()
    End
    End SubPrivate Sub Form_Load()
    Timer1.Enabled = False
    Command2.Enabled = False
    End SubPrivate Sub Timer1_Timer()
    Dim hwnd As Long
    Dim pt As POINTAPI
    Dim st As Stringst = Space(256)GetCursorPos pt '取得鼠标坐标信息
    Text2.Text = pt.X
    Text3.Text = pt.Yhwnd = WindowFromPoint(pt.X, pt.Y)GetWindowText hwnd, st, 256 '取得窗口标题
    Text1.Text = stGetClassName hwnd, st, 256 '取得窗口类名
    Text5.Text = stText6.Text = GetWindowLong(hwnd, GWL_ID) '取得窗口ID
    Text7.Text = GetWindowLong(hwnd, GWL_STYLE) '取得窗口风格
    Text8.Text = GetWindowLong(hwnd, GWL_EXSTYLE) '取得窗口扩展风格
    End Sub
      

  7.   

    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Sub Timer1_Timer()
        Dim hWnd1 As Long
        hWnd1 = GetForegroundWindow()
        
        Dim lRect As RECT
        Call GetWindowRect(hWnd1, lRect)
        Debug.Print "left:" & lRect.Left & ";top:" & lRect.Top & ";right:" & lRect.Right & ";bottom:" & lRect.Bottom
    End Sub
      

  8.   

    看错了
    Private Sub Timer1_Timer()
        Dim lPoint As POINTAPI
        Call GetCursorPos(lPoint)
        Dim hWnd1 As Long
        hWnd1 = WindowFromPoint(lPoint.X, lPoint.Y)
    '    hWnd1 = GetForegroundWindow()    Dim lRect As RECT
        Call GetWindowRect(hWnd1, lRect)
        Debug.Print "left:" & lRect.Left & ";top:" & lRect.Top & ";right:" & lRect.Right & ";bottom:" & lRect.Bottom
    End Sub