即使使用VBA将窗口程序最小化,也可以将窗口程序全屏显示在前台吗? [英] Bring a window programme to foreground in full screen even if it is minimized using VBA?
问题描述
Dim流程,流程为变体昏暗的壳作为对象设置进程= GetObject("winmgmts:").InstancesOf("Win32_Process")设置Shell = CreateObject("WScript.Shell")对于过程中的每个过程如果StrComp(Process.Name,"Nesttrader.exe",vbTextCompare)= 0,则Shell.AppActivate Process.ProcessId退出万一下一个
如果"nesttrader.exe"不起作用,则上述代码不起作用.最小化到任务栏.
最后,我为此找到了灵魂.下面的代码即使通过提供窗口标题的部分文本来最小化窗口,也将最大化您的窗口.
选项显式私有声明PtrSafe功能FindWindow Lib"user32"别名"FindWindowA"(ByVal lpClassName作为字符串,_ByVal lpWindowName作为字符串)私有声明PtrSafe函数GetWindowText Lib"user32";别名"GetWindowTextA";(ByVal hwnd为Long,ByVal lpString为String,_ByVal cch只要)私有声明PtrSafe函数GetWindowTextLength Lib"user32"别名"GetWindowTextLengthA";(ByVal拥有)私有声明PtrSafe函数GetWindow Lib"user32"(ByVal hwnd as Long,ByVal wCmd as Long)私有声明PtrSafe函数IsWindowVisible Lib"user32"(ByVal hwnd as Long)作为布尔值私人建筑GW_HWNDNEXT = 2私有声明PtrSafe功能ShowWindow Lib"user32"(ByVal hwnd as Long,ByVal nCmdShow as Long)只要常量SW_SHOWMAXIMIZED = 3私有声明PtrSafe函数BringWindowToTop Lib"user32"(ByVal拥有)私人子SHOW()昏暗lhWndP如果GetHandleFromPartialCaption(lhWndP,"NEST TRADER")= True,则如果lhWndP<>0然后BringWindowToTop(lhWndP)ShowWindow lhWndP,SW_SHOWMAXIMIZED万一万一结束子私有函数GetHandleFromPartialCaption(ByRef lWnd为long,ByVal sCaption为字符串)为布尔值昏暗lhWndPDim sStr作为字符串暗淡如弦GetHandleFromPartialCaption =假lhWndP = FindWindow(vbNullString,vbNullString)lhWndP<0sStr =字符串(GetWindowTextLength(lhWndP)+ 1,Chr $(0))GetWindowText lhWndP,sStr,Len(sStr)sStr =左$(sStr,Len(sStr)-1)ise = IsWindowVisible(lhWndP)如果InStr(1,sStr,sCaption,vbTextCompare)>0然后ise = True然后GetHandleFromPartialCaption = TruelWnd = lhWndP退出做万一lhWndP = GetWindow(lhWndP,GW_HWNDNEXT)环形结束功能
let's say there are four different window programs are running at a time. I want a macro which can bring a program "NESTTRADER.EXE" in the front with full screen and minimize other three (including excel). I tried Appactivate() but it works only if program ("NESTTRADER.EXE") is not minimized in the taskbar.Also tried some SetForegroundWindow method but it is giving error that I should update it for win 64.
Dim Processes, Process As Variant
Dim Shell As Object
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
Set Shell = CreateObject("WScript.Shell")
For Each Process In Processes
If StrComp(Process.Name, "Nesttrader.exe", vbTextCompare) = 0 Then
Shell.AppActivate Process.ProcessId
Exit For
End If
Next
Above code does not do the job if "nesttrader.exe" is minimized to the taskbar.
Finally I found soultion for this. Below code will maximize your window even if it is minimized by giving partial text of title of window.
Option Explicit
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Boolean
Private Const GW_HWNDNEXT = 2
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWMAXIMIZED = 3
Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Private Sub SHOW()
Dim lhWndP As Long
If GetHandleFromPartialCaption(lhWndP, "NEST TRADER") = True Then
If lhWndP <> 0 Then
BringWindowToTop (lhWndP)
ShowWindow lhWndP, SW_SHOWMAXIMIZED
End If
End If
End Sub
Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
Dim lhWndP As Long
Dim sStr As String
Dim ise As String
GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString)
Do While lhWndP <> 0
sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
GetWindowText lhWndP, sStr, Len(sStr)
sStr = Left$(sStr, Len(sStr) - 1)
ise = IsWindowVisible(lhWndP)
If InStr(1, sStr, sCaption, vbTextCompare) > 0 And ise = True Then
GetHandleFromPartialCaption = True
lWnd = lhWndP
Exit Do
End If
lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop
End Function
这篇关于即使使用VBA将窗口程序最小化,也可以将窗口程序全屏显示在前台吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!