1.自启动
- 打开命令提示符。
- 输入以下命令,然后按
Enter
键:
explorer shell:startup
这将打开启动文件夹。将 exe
文件或其快捷方式复制到这个文件夹中。
要实现双击运行exe不出现窗口和图标,只在后台运行或者放在托盘里,可以使用以下方法:
方法一:使用 pyinstaller
打包
如果你的 exe
是由 Python 脚本生成的,可以使用 pyinstaller
来打包,并设置运行时不显示窗口。
2.隐藏cmd窗口
(1)方法一:使用 pyinstaller 打包
如果你的 exe 是由 Python 脚本生成的,可以使用 pyinstaller 来打包,并设置运行时不显示窗口。
- 安装
pyinstaller
:pip install pyinstaller
- 打包你的 Python 脚本,添加
--noconsole
参数:
这样打包后的pyinstaller --noconsole --onefile your_script.py
exe
运行时就不会显示控制台窗口。
(2) 方法二:修改exe
的属性
如果你有现成的exe
文件,可以尝试修改其属性来隐藏控制台窗口。可以使用VBS
脚本来实现这一点: - 创建一个
run_silent.vbs
文件,内容如下:
将Set objShell = CreateObject("WScript.Shell") objShell.Run "your_executable.exe", 0, False
your_executable.exe
替换为你的exe
文件的名称。 - 双击运行
run_silent.vbs
,它将以隐藏模式运行你的exe
。
(3) 方法三:使用托盘程序
如果你希望exe
运行时在系统托盘中显示图标,可以使用Python
的pystray
库。如果你的exe
不是用 Python 编写的,可以参考其他编程语言中实现托盘图标的方法。
以下是一个使用pystray
的简单示例: - 安装
pystray
库:pip install pystray
- 创建一个托盘程序:
from PIL import Image, ImageDraw import pystray def create_image(): Generate an image and draw a pattern width = 64 height = 64 color1 = "black" color2 = "white" image = Image.new("RGB", (width, height), color1) dc = ImageDraw.Draw(image) dc.rectangle( (width // 2, 0, width, height // 2), fill=color2) dc.rectangle( (0, height // 2, width // 2, height), fill=color2) return image def on_exit(icon, item): icon.stop() icon = pystray.Icon("test") icon.icon = create_image() icon.menu = pystray.Menu( pystray.MenuItem("Exit", on_exit) ) icon.run()
- 使用
pyinstaller
打包:pyinstaller --noconsole --onefile your_script.py
上述代码创建了一个简单的托盘图标,右键点击图标可以选择退出程序。你可以根据需要修改托盘图标和菜单。
(4) 方法四:修改 Windows 应用程序属性
如果你的 exe
文件是一个 Windows 应用程序,可以通过修改其属性来隐藏窗口。
- 在你的应用程序代码中,查找创建窗口的部分,确保其设置为隐藏或最小化。
- 如果你有访问源代码的权限,可以使用 API 来控制窗口显示。
include <windows.h>
// Your entry point function
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Hide the console window
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
// Your application code here
return 0;
}
将以上代码添加到你的应用程序的入口函数中,确保在应用程序启动时隐藏控制台窗口。
通过以上方法,你可以实现exe程序双击运行时不显示窗口和图标,并且可以选择在后台运行或放在托盘里。