zoukankan      html  css  js  c++  java
  • 【转】C#如何让WinForm嵌入桌面窗口最底层

    C#如何让WinForm嵌入桌面窗口最底层

    2010-03-26  来自:csdnBlog 
    • 摘要:本文介绍C#使用Windows API函数:SetParent、SetWindowPos和FindWindow实现让WinForm嵌入桌面窗口最底层,并提供详细的实现代码供参考。
    此类将窗体永远置于窗口最底层。

    首先, 调用一些User32.dll的WinAPI函数。

    internal class User32
    {
    public const int SE_SHUTDOWN_PRIVILEGE = 0x13;

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

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

    [DllImport(
    "user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx,
    int cy, uint uFlags);
    }
    然后, 在WinForm里面:
    public MainForm()
    {
    InitializeComponent();

    try
    {
    if (Environment.OSVersion.Version.Major < 6)
    {
    base.SendToBack();

    IntPtr hWndNewParent
    = User32.FindWindow("Progman", null);
    User32.SetParent(
    base.Handle, hWndNewParent);
    }
    else
    {
    User32.SetWindowPos(
    base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
    }
    }
    catch (ApplicationException exx)
    {
    MessageBox.Show(
    this, exx.Message, "Pin to Desktop");
    }
    }

    private void MainForm_Activated(object sender, EventArgs e)
    {
    if (Environment.OSVersion.Version.Major >= 6)
    {
    User32.SetWindowPos(
    base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
    }
    }

    private void MainForm_Paint(object sender, PaintEventArgs e)
    {
    if (Environment.OSVersion.Version.Major >= 6)
    {
    User32.SetWindowPos(
    base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
    }
    }
    以上介绍的就是C#如何让WinForm嵌入桌面窗口最底层,希望对你有所帮助。
    作者:zfrong
    作者:Asion Tang
    凡是没有注明[转载]的文章,本Blog发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Ubuntu使用命令行打印文件
    Spring ConditionalOnProperty
    Spring EnableWebMvc vs WebMvcConfigurationSupport
    commons-httpclient中的超时设置
    jdb调试命令
    caching redirect views leads to memory leak (Spring 3.1)
    Clojure web初探
    在现有原生开发Android项目中集成hbuilder开发
    MessageBoard
    CSS布局(五) 圣杯布局
  • 原文地址:https://www.cnblogs.com/AsionTang/p/1741509.html
Copyright © 2011-2022 走看看