如何创建Windows应用程序BottomMost [英] How to create a Windows Application BottomMost

查看:60
本文介绍了如何创建Windows应用程序BottomMost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索一些示例代码,以了解如何正确地使Windows应用程序最底层.经过挖掘和一些试验和错误之后,似乎找不到适合我想要的代码.我想知道是否有可能.

我想将Windows应用程序放在桌面的顶部,但放在所有其他应用程序和任务栏的下面.

到目前为止,这是我发现的内容:

Hi, I have been searching online for some sample codes on how to correctly make Windows Application bottom most. After digging and some trials and errors, it can''t seem to find the right code for what I want. I wonder if it is possible or not.

I want to put the windows application right on top of the desktop, but under all other applications and taskbar.

Here is what I found so far:

private IntPtr m_DesktopClass;
private IntPtr m_DesktopFileListView;
private IntPtr m_PreviousParent;

[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpClassName, String lpWindowName);

public bool DesktopAttached
{
get { return m_DesktopAttached; }
set
{
      m_DesktopAttached = value;
      this.MinimizeBox = !value;
      if (value)
      {
            m_DesktopClass = FindWindowEx(FindWindow("Progman", null), new IntPtr(0), "shelldll_defview", null);
                    
            m_DesktopFileListView = FindWindowEx(m_DesktopClass, new IntPtr(0), "syslistview32", null);

            m_PreviousParent = SetParent(this.Handle, m_DesktopFileListView);
      }
      else
      {
            SetParent(this.Handle, m_PreviousParent);
      }
}



上面的代码确实将Windows应用程序放置在桌面顶部以及所有其他应用程序和任务栏的下面.但是,应用程序上的所有按钮都不起作用,不是我想要的.



The above code does put the windows application right on top of the desktop and under all other applications and taskbar; however, all buttons on the application don''t work, not what I want.

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

const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;

static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

public static void SetBottom()
{
    SetWindowPos(this.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
}



上面的代码确实将Windows应用程序置于桌面顶部,所有其他应用程序下方,并且该应用程序上的所有按钮正常工作;但是,应用程序位于任务栏的顶部,不是我想要的.



The above code does put the windows application right on top of the desktop and under all other applications and all buttons on the application work; however, the application is on top of the taskbar, not what I want.

推荐答案

感谢您的帮助,史蒂文·平托(Steven Pinto),但是您的代码不允许应用程序落在任务栏后面.再次使用这些代码后,我发现第二组代码有效.以前没有用,因为代码中有一行会干扰SetBottom方法,如下所示:

Thanks for your help, Steven Pinto, but your code doesn''t allow the application to fall behind the taskbar. After playing with the codes again, I found that the second set of the code works. It didn''t work before, because there was a line to the code which interfere with SetBottom method as shown below:

public Form1()
{
       InitializeComponent();
       Bounds = Screen.PrimaryScreen.Bounds; //Interfere with SetBottom()
       SetBottom();
}



为了正确地使应用程序全屏显示并保持在z顺序的底部,我必须使用WorkingArea而不是Bounds并重写WndProc函数,并为WM_WINDOWPOSCHANGING Windows消息调用SetWindowPos.完整的代码如下所示:



In order to correctly cause the application to be full screen and remain on the bottom of the z-order, I have to use WorkingArea instead of Bounds and override WndProc function and call SetWindowPos for WM_WINDOWPOSCHANGING Windows message. The complete code is shown below:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;
const int WM_WINDOWPOSCHANGING = 0x0046;

protected override void WndProc(ref Message m)
{
       // Listen for operating system messages.
       switch (m.Msg)
       {
              // The WM_WINDOWPOSCHANGING message occurs when a window whose
              // size, position, or place in the Z order is about to change.
              case WM_WINDOWPOSCHANGING:
                  SetWindowPos(this.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
                  break;
       }
       base.WndProc(ref m);
}

public Form1()
{
       InitializeComponent();
       Bounds = Screen.PrimaryScreen.WorkingArea;
       CenterToParent();
}


这篇关于如何创建Windows应用程序BottomMost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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