如何将控制台应用程序窗口设置为最顶层窗口(C#)?

发布于 2024-09-12 06:49:31 字数 526 浏览 2 评论 0原文

如何将控制台应用程序设置为最顶层窗口。我正在 .NET 中构建控制台应用程序(我正在使用 C#,甚至可能 pinvokes 到非托管代码也可以)。

我以为我可以让我的控制台应用程序从 Form 类派生,

class MyConsoleApp : Form {
    public MyConsoleApp() {
        this.TopLevel = true;
        this.TopMost = true;
        this.CenterToScreen();
    }

    public void DoSomething() {
        //....
    }

    public static void Main() {
        MyConsoleApp consoleApp = new MyConsoleApp();
        consoleApp.DoSomething();
    }
}

但这不起作用。我不确定 Windows 窗体上设置的属性是否适用于控制台 UI。

How do i set a console application to be the top most window. I am building the console application in .NET (i am using C# and maybe even pinvokes to unmanaged code is ok).

I thought that i could have my console application derive from Form class

class MyConsoleApp : Form {
    public MyConsoleApp() {
        this.TopLevel = true;
        this.TopMost = true;
        this.CenterToScreen();
    }

    public void DoSomething() {
        //....
    }

    public static void Main() {
        MyConsoleApp consoleApp = new MyConsoleApp();
        consoleApp.DoSomething();
    }
}

However this doesn't work. I am not sure if the properties set on the windows form is applicable to the console UI.

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

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

发布评论

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

评论(2

呢古 2024-09-19 06:49:31

您可以从 Windows API P/Invoke SetWindowPos

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetWindowPos(
        IntPtr hWnd, 
        IntPtr hWndInsertAfter, 
        int x, 
        int y, 
        int cx, 
        int cy, 
        int uFlags);

    private const int HWND_TOPMOST = -1;
    private const int SWP_NOMOVE = 0x0002;
    private const int SWP_NOSIZE = 0x0001;

    static void Main(string[] args)
    {
        IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

        SetWindowPos(hWnd, 
            new IntPtr(HWND_TOPMOST), 
            0, 0, 0, 0, 
            SWP_NOMOVE | SWP_NOSIZE);

        Console.ReadKey();
    }
}

You can P/Invoke SetWindowPos from the Windows API:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetWindowPos(
        IntPtr hWnd, 
        IntPtr hWndInsertAfter, 
        int x, 
        int y, 
        int cx, 
        int cy, 
        int uFlags);

    private const int HWND_TOPMOST = -1;
    private const int SWP_NOMOVE = 0x0002;
    private const int SWP_NOSIZE = 0x0001;

    static void Main(string[] args)
    {
        IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

        SetWindowPos(hWnd, 
            new IntPtr(HWND_TOPMOST), 
            0, 0, 0, 0, 
            SWP_NOMOVE | SWP_NOSIZE);

        Console.ReadKey();
    }
}
南烟 2024-09-19 06:49:31

您可以将 FindWindow 与 P/Invoke (http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx) 然后以某种方式设置扩展样式以使用 WS_EX_TOPMOST - 请参阅SetWindowLong 在 P/Invoke (http://www .pinvoke.net/default.aspx/coredll/SetWindowLong.html )。

然而,这有点老套,建议使用 Windows 窗体或 WPF 创建自己的控制台窗口。

You could use FindWindow with P/Invoke (http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx) then somehow set the extended style to use WS_EX_TOPMOST - see SetWindowLong at P/Invoke (http://www.pinvoke.net/default.aspx/coredll/SetWindowLong.html ).

However it's all a bit hacky and would recommend creating your own console window using Windows Forms or WPF.

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