在windows10操作系统上,如何实现顶层? [英] On the windows10 operating system, how to achieve the top layer?

查看:93
本文介绍了在windows10操作系统上,如何实现顶层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I hope my application window show up on top of every other window.On the windows10 operating system.
Just like the mouse, it's on the top.





我尝试了什么:





What I have tried:

while (true)
                {
                    try
                    {
                        Thread.Sleep(2);
                        lock (_thisLock)
                        {
                            this.Dispatcher.Invoke(new Action(() =>
                            {
                               
                                this.Topmost = false;
                                //Thread.Sleep(2);
                                this.Topmost = true;
                               
                            }), null);
                        }
                    }
                    catch (Exception ex)
                    {
 
                        MessageBox.Show(ex.Message);
                    }
 
                }

推荐答案

尝试在表单加载时调用此方法:

Try calling this when the form loads:
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);     // Always on top.

您将需要:

You will need this:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

以及这些:

And these:

#region Used with SetWindowPos
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
#endregion


WPF很简单...

WPF is easy...
public MainWindow()
{
    Activated += Window_Activated;
}

private void Window_Activated(object sender, System.EventArgs e)
{
    this.Topmost = true;
}


这篇关于在windows10操作系统上,如何实现顶层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆