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

 我来答
匿名用户
2013-11-18
展开全部
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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式