您的位置首页生活百科

vb通过findwindow获得窗口句柄后怎样判断该窗口是否为活动窗口(具有焦点)

vb通过findwindow获得窗口句柄后怎样判断该窗口是否为活动窗口(具有焦点)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetForegroundWindow Lib "user32" () As Long '可以通过查看前景窗口是否为指定窗口判断 hwd1 = FindWindow("#32770", "QQ用户登录") '获得指定窗口句柄 hwd2 = GetForegroundWindow'获得前森洞景窗口句柄 If hwd1 = hwd2 Then MsgBox "指定窗口为活动状态!"'判断是否相同'可以通过ModifyMenu修改系统菜单来禁止移动,但并不适用所有窗体,有的窗体不具有系统菜单(BorderStyle=0) '但通过子类化仍可以实现系统菜单具有的功能,新建窗体,添加command1,复制粘贴下段代码 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const SC_MOVE = &HF010& Private Const MF_BYCOMMAND = &H0& Private Const MF_ENABLED = &H0& Private Const MF_GRAYED = &H1& Private Sub Command1_Click() hwind = FindWindow("#32770", "QQ用户登录") '获取窗体句柄 hMenu = GetSystemMenu(hwind, False)'获取该窗体系统菜单句柄 ModifyMenu hMenu, SC_MOVE, MF_BYCOMMAND Or MF_GRAYED, -10, "禁止移动"'设置移动菜单项不可用,并指定新ID为小于0的-10以防止系统重迅游新加载该菜单,新标题为“禁止移动”,成功返回非0,失败0 'ModifyMenu hMenu, -10, MF_BYCOMMAND + MF_ENABLED, SC_MOVE, "移动(&M)" '恢复移动菜单 SendMessage hwind, WM_NCACTIVATE, True, 0 '发送WM_NCACTIVE消息此昌枯到指定窗体刷新非客户区(标题栏) End Sub