C#让Console控制台程序在最顶层TopMost

来源:互联网 发布:英国工资 知乎 编辑:程序博客网 时间:2024/04/27 09:59

调用Windows API中的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();    }}
0 0
原创粉丝点击