停止进程显示 C# 窗口

发布于 2024-11-02 12:53:57 字数 680 浏览 1 评论 0原文

我正在尝试自动化一个应用程序,该应用程序在启动时创建一个没有用户交互的 GUI 窗口,但我不知道如何隐藏实际的窗口。

我尝试使用 ProcessStartInfo :

Process.Start(new ProcessStartInfo {
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,

    // other properties here
});

但窗口仍然显示。

我还尝试过旋转等待窗口存在,然后隐藏它:

while (process.MainWindowHandle == IntPtr.Zero) {}
ShowWindowAsync(process.MainWindowHandle, SW_HIDE);

不幸的是,这使窗口闪烁大约 1/16 秒左右,如果可能的话,我想避免它。

我目前的想法是创建一个 hook ,但我不确定要抓住什么钩子,也不确定它是否会起作用。

有什么建议吗?

I'm trying to automate an application that creates a GUI window on startup that has no user interaction, but I can't figure out how to hide the actual window.

I've tried using ProcessStartInfo thus:

Process.Start(new ProcessStartInfo {
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,

    // other properties here
});

But the window still shows up.

I've also tried spin-waiting for the window to exist, and then hiding it:

while (process.MainWindowHandle == IntPtr.Zero) {}
ShowWindowAsync(process.MainWindowHandle, SW_HIDE);

This, unfortunately, makes the window flash for about 1/16th of a second or so, and I'd like to avoid it if at all possible.

My current thoughts are along the line of creating a hook, but am unsure of what hooks to grab, nor if it will even work.

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深府石板幽径 2024-11-09 12:53:57

所需的进程窗口样式(实际上映射到 SW_ 常量之一)被传递到其他应用程序的 WinMain 函数,但这并不意味着粗鲁的应用程序不会只是忽略您发送的任何值。

您可以通过使用 User32.CreateDesktop 创建另一个虚拟桌面,然后使用 Kernel32.CreateProcess 来实现这一点,确保将正确的桌面名称作为 的一部分传递STARTINFO 结构。

The desired process window style (actually mapped to one of the SW_ constants) is passed to the other application's WinMain function, but that doesn't mean a rude application won't just ignore whatever value you send.

You can pull it off by creating another virtual desktop using User32.CreateDesktop, then use Kernel32.CreateProcess, making sure to pass the correct desktop name as part of the STARTINFO structure.

为你拒绝所有暧昧 2024-11-09 12:53:57

CreateNoWindow 选项仅适用于控制台模式应用程序,您无法阻止 GUI 应用程序创建其主窗口。最好的选择是 WindowStyle 属性,将其设置为隐藏或最小化。一个行为良好的 GUI 应用程序在调用 ShowWindow() 时使用它。但它覆盖或忽略这个值当然并不罕见。在你的情况下,赔率并不好,最小化是你剩下的尝试。

The CreateNoWindow option only applies to console mode applications, you cannot prevent a GUI app from creating its main window. Your best bet is the WindowStyle property, set it to Hidden or Minimized. A well behaved GUI app uses it in its call to ShowWindow(). But it certainly not unusual for it to override or ignore this value. Odds are not good in your case, Minimized is all you got left to try.

童话里做英雄 2024-11-09 12:53:57

尝试选项 1
UseShellExecute = true,并且 WindowStyle = ProcessWindowStyle.Hidden
或者不设置 UseShellExecute (默认为 true)

Try option 1 with
UseShellExecute =true, and WindowStyle = ProcessWindowStyle.Hidden
or do not set UseShellExecute (it defaults to true)

呆萌少年 2024-11-09 12:53:57

为什么不直接设置 WinForm 项目的 Startup Object 属性呢?
您可以在不显示表单或实际窗口的情况下启动该过程。

    static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // Application.Run(new Form1());
        // TODO: Start you process here?
    }

希望这有帮助,谢谢。

Why don't you just set the Startup Object property of your WinForm project?
You can start the process without showing the Form or the actual window.

    static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // Application.Run(new Form1());
        // TODO: Start you process here?
    }

Hope this help, thanks.

粉红×色少女 2024-11-09 12:53:57

您能否在整个窗体区域的桌面内容中覆盖窗体 Paint 和 blit,以度过第 16 秒?

我是低级别胜利形式的相对新手,但我最近只是在 OnPaint 上玩得开心,所以我想到了这一点。

Could you override the form Paint and blit in the contents of the desktop for the entire form region in order to get over the 16th second?

I'm a relative newbie to low level win forms stuff, but i was just having fun with OnPaint recently so I thought of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文