San*_*osh 8 c# window console-application
如何将控制台应用程序设置为最顶层的窗口.我正在使用.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();
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我不确定Windows窗体上设置的属性是否适用于控制台UI.
Dir*_*mar 11
您可以SetWindowPos从Windows API 进行P/Invoke :
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8698 次 |
| 最近记录: |