vb通过findwindow获得窗口句柄后怎样判断该窗口是否为活动窗口(具有焦点)
谢谢一楼的回答这个问题已经搞定用的和你一样的方法我还有个问题要解决就是如何锁定这个窗体使其不能移动我知道一个方法是移除系统菜单里的移动项但这样就没法恢复了我想用程序控制窗...
谢谢一楼的回答
这个问题已经搞定 用的和你一样的方法
我还有个问题要解决
就是如何锁定这个窗体
使其不能移动
我知道一个方法是移除系统菜单里的移动项
但这样就没法恢复了
我想用程序控制窗口是否可以移动
随时锁定随时移除
能帮我解决的话给你加分 展开
这个问题已经搞定 用的和你一样的方法
我还有个问题要解决
就是如何锁定这个窗体
使其不能移动
我知道一个方法是移除系统菜单里的移动项
但这样就没法恢复了
我想用程序控制窗口是否可以移动
随时锁定随时移除
能帮我解决的话给你加分 展开
1个回答
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询