vb获取窗口的name属性

shanyonggang 2009-10-31 10:24:15
如何获取活动窗口的name属性,而不是标题名称。
...全文
406 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2009-11-01
  • 打赏
  • 举报
回复
name获取不了,因为对于窗口,name根本就不存在。这是VB对某个窗口的标示而已。

窗口的宽、高可以用GetWindowRect得到。
zzhgb 2009-11-01
  • 打赏
  • 举报
回复
看错了
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
zzhgb 2009-11-01
  • 打赏
  • 举报
回复
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
shanyonggang 2009-11-01
  • 打赏
  • 举报
回复
能否告诉我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 Long

Private Const GWL_ID = (-12) '对话框中一个子窗口的标识符
Private Const GWL_STYLE = (-16) '窗口样式
Private Const GWL_EXSTYLE = (-20) '扩展窗口样式

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Sub Command1_Click()
Timer1.Interval = Text4.Text
Timer1.Enabled = True
Text4.Enabled = False
Command1.Enabled = False
Command2.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
Text4.Enabled = True
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Command2.Enabled = False
End Sub



Private Sub Timer1_Timer()
Dim hwnd As Long
Dim pt As POINTAPI
Dim st As String

st = Space(256)

GetCursorPos pt '取得鼠标坐标信息
Text2.Text = pt.X
Text3.Text = pt.Y

hwnd = WindowFromPoint(pt.X, pt.Y)

GetWindowText hwnd, st, 256 '取得窗口标题
Text1.Text = st

GetClassName hwnd, st, 256 '取得窗口类名
Text5.Text = st

Text6.Text = GetWindowLong(hwnd, GWL_ID) '取得窗口ID
Text7.Text = GetWindowLong(hwnd, GWL_STYLE) '取得窗口风格
Text8.Text = GetWindowLong(hwnd, GWL_EXSTYLE) '取得窗口扩展风格
End Sub


SYSSZ 2009-11-01
  • 打赏
  • 举报
回复
窗口一般用窗口句柄标识,通过窗口句柄操作窗口.
getemail 2009-10-31
  • 打赏
  • 举报
回复
首先要得到句柄
[Quote=引用 5 楼 shanyonggang 的回复:]
我想利用这个名子进一步获取窗口的其他属性值。如窗口的宽、高等。难道只能通过api获取吗?有什么 API函数可用呢?
[/Quote]
shanyonggang 2009-10-31
  • 打赏
  • 举报
回复
api还真够复杂呀!
shanyonggang 2009-10-31
  • 打赏
  • 举报
回复
我想利用这个名子进一步获取窗口的其他属性值。如窗口的宽、高等。难道只能通过api获取吗?有什么 API函数可用呢?
getemail 2009-10-31
  • 打赏
  • 举报
回复
别人编写程序叫什么名字编译后就不重要了,所以只能通过类名和标题去判断

[Quote=引用 3 楼 shanyonggang 的回复:]
我想通过鼠标在屏幕上移动,而在自制程序的文本框中显示鼠标所在点对应窗口的name属性值。
[/Quote]
shanyonggang 2009-10-31
  • 打赏
  • 举报
回复
我想通过鼠标在屏幕上移动,而在自制程序的文本框中显示鼠标所在点对应窗口的name属性值。
贝隆 2009-10-31
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Debug.Print Command1.Name
End Sub
getemail 2009-10-31
  • 打赏
  • 举报
回复
什么窗口?你自己的还是别人的?是MDI子窗体的?

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧