怎样用VB编程实现获取一个正在运行的程序的焦点?

2024-10-31 08:22:27
有3个网友回答
网友(1):

窗体置顶代码

Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long,ByVal hWndInsertAfter As Long,ByVal x As Long,ByVal y As Long, ByVal cx As Long, ByVal cy As Long,ByVal wFlags As Long) As Long
Public Const SWP_NOMOVE= &H2 '不移动窗体
Public Const SWP_NOSIZE=&H1 '不改变窗体尺寸
Public Const Flag=SWP_NOMOVE Or SWP_NOSIZE
Public Const HWND_TOPMOST=-1 '窗体总在最前面
Public Const HWND_NOTOPMOST=-2 '窗体不在最前面
为窗体添加代码如下:
Private Sub cmdNormal_Click()
cmdTopMost.Enabled=True
cmdNormal. Enabled= False
Form1.Captio = "现在是普通窗口"
SetWindowPos Form1.hwnd,HWND_NOTOPMOST,0,0,0,0,Flag
End Sub
Private Sub cmdTopMost_Click()
cmdTopMost.Enabled = False
cmdNormal.Enabled = True
Form1.Caption = "现在是TopMost窗口"
SetWindowPos Form1.hwnd,HWND_TOPMOST,0,0,0,0,Flag
End Sub

网友(2):

将那个窗体的一个属性(我忘了是什么属性)激活!

网友(3):

Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 Sub Command1_Click()
Dim Resule As Long
Resule = FindWindow("AlarmLogViewer.exe", vbNullString)
SetForegroundWindow (Resule)
End Sub