如何将控制台应用程序窗口设置为最顶层窗口(C#)?
如何将控制台应用程序设置为最顶层窗口。我正在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从 Windows API P/Invoke
SetWindowPos
:You can P/Invoke
SetWindowPos
from the Windows API:您可以将
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 useWS_EX_TOPMOST
- seeSetWindowLong
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.