欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 窗体置顶

程序员文章站 2022-06-10 11:04:44
...

实例链接:Windows窗体置顶C#/wpf编程-C#文档类资源-CSDN下载 

//引用
using System.Runtime.InteropServices;
using System.Windows.Interop;

//GetActiveWindow返回线程的活动窗口,而不是系统的活动窗口。如果要得到用户正在**的窗口,应该使用 GetForegroundWindow
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll ")]
//设置窗体置顶
private static extern bool SetForegroundWindow(IntPtr hWnd);

//窗体句柄
private IntPtr handle = IntPtr.Zero;
public IntPtr Handle { get => handle; set => handle = value; }



private void Window_Loaded(object sender, RoutedEventArgs e)
{
      handle = new WindowInteropHelper(this).Handle;
      SetForegroundWindow(handle);
}