打开一个python最小化或隐藏的程序

wtz*_*wtz 6 python windows

我要做的是编写一个脚本,只在进程列表中打开一个应用程序.意思是它会被"隐藏".我甚至不知道它是否可能在python中.

如果它不可能,我会满足于甚至一个函数,允许在最小化状态下使用python打开程序,可能是这样的:

import subprocess
def startProgram():
    subprocess.Hide(subprocess.Popen('C:\test.exe')) #  I know this is wrong but you get the idea...
startProgram()
Run Code Online (Sandbox Code Playgroud)

有人建议使用win32com.client,但问题是我想要启动的程序没有在名称下注册的COM服务器.

有任何想法吗?

Tom*_*ait 19

这很容易:)
Python Popen接受STARTUPINFO结构...
关于STARTUPINFO结构:https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v = vs.85).aspx

运行隐藏:

import subprocess

def startProgram():
    SW_HIDE = 0
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_HIDE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()
Run Code Online (Sandbox Code Playgroud)

运行最小化:

import subprocess

def startProgram():
    SW_MINIMIZE = 6
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_MINIMIZE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()
Run Code Online (Sandbox Code Playgroud)


Anu*_*yal 7

您应该使用win32api并隐藏您的窗口,例如使用win32gui.EnumWindows您可以枚举所有顶部窗口并隐藏您的窗口

这是一个小例子,您可以这样做:

import subprocess
import win32gui
import time

proc = subprocess.Popen(["notepad.exe"])
# lets wait a bit to app to start
time.sleep(3)

def enumWindowFunc(hwnd, windowList):
    """ win32gui.EnumWindows() callback """
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    #print hwnd, text, className
    if text.find("Notepad") >= 0:
        windowList.append((hwnd, text, className))

myWindows = []
# enumerate thru all top windows and get windows which are ours
win32gui.EnumWindows(enumWindowFunc, myWindows)

# now hide my windows, we can actually check process info from GetWindowThreadProcessId
# http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx
for hwnd, text, className in myWindows:
    win32gui.ShowWindow(hwnd, False)

# as our notepad is now hidden
# you will have to kill notepad in taskmanager to get past next line
proc.wait()
print "finished."
Run Code Online (Sandbox Code Playgroud)