c# 设置桌面背景窗口 SetParent

摘要:
使用系统;使用系统。绘画使用System.Runtime。InteropServices;使用System.Windows。形式;namespaceSetDeskFrm{publicpartialclassForm1:表单{IntPtrhDesktop;publicconstantGW_CHILD=5;publicForm1(){Ini
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SetDeskFrm
{
    public partial class Form1 : Form
    {
        
  
        IntPtr hDesktop;
        public const int GW_CHILD = 5;
        public Form1() {
            InitializeComponent();
            this.hDesktop = GetDesktopHandle(DesktopLayer.Progman);
            EmbedDesktop(this, this.Handle, this.hDesktop);
      
        }
        public IntPtr GetDesktopHandle(DesktopLayer layer)
        {
            //hWnd = new HandleRef();    
            HandleRef hWnd;
            IntPtr hDesktop =new IntPtr();
            switch (layer)
            {
                case DesktopLayer.Progman:
                    hDesktop = Win32Support.FindWindow("Progman" , null);//第一层桌面  
                    break;
                case DesktopLayer.SHELLDLL:
                    hDesktop = Win32Support.FindWindow( "Progman" , null);//第一层桌面    
                    hWnd =new HandleRef(this, hDesktop);
                    hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第2层桌面     
                    break;
                case DesktopLayer.FolderView:
                    hDesktop = Win32Support.FindWindow( "Progman" , null);//第一层桌面    
                    hWnd =new HandleRef(this, hDesktop);
                    hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第2层桌面      
                    hWnd =new HandleRef(this, hDesktop);
                    hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第3层桌面    
                    break;
            }
            return hDesktop;
        }

        public void EmbedDesktop(Object embeddedWindow, IntPtr childWindow, IntPtr parentWindow)
        {
            Form window = (Form)embeddedWindow;
            HandleRef HWND_BOTTOM =new HandleRef(embeddedWindow, new IntPtr(1));
            const int SWP_FRAMECHANGED =0x0020;//发送窗口大小改变消息
            Win32Support.SetParent(childWindow, parentWindow);
            Win32Support.SetWindowPos(new HandleRef(window, childWindow), HWND_BOTTOM, 300, 300, window.Width, window.Height, SWP_FRAMECHANGED);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
   class Win32Support     {

    [DllImport("user32.dll" , CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string className, string windowName);
        [DllImport("user32.dll" , CharSet = CharSet.Auto, ExactSpelling =true)]
        public static extern IntPtr GetWindow(HandleRef hWnd, int nCmd);
        [DllImport( "user32.dll" )]
        public static extern IntPtr SetParent(IntPtr child, IntPtr parent);
        [DllImport( "user32.dll" , EntryPoint= "GetDCEx", CharSet = CharSet.Auto, ExactSpelling =true)]
        public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
        [DllImport( "user32.dll" , CharSet = CharSet.Auto, ExactSpelling =true)]
        public static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy, int flags);
        [DllImport("user32.dll" )]
        public static extern int ReleaseDC(IntPtr window, IntPtr handle);
    }

    public enum DesktopLayer
    {
        Progman =0,
        SHELLDLL =1,
        FolderView =2
    } 


}

  

方法2(在某些系统版本不成功,WIN7 、XP):

 [DllImport("user32.dll", SetLastError = true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        const int GWL_HWNDPARENT = -8;
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           

           var handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
         IntPtr hprog = FindWindowEx(
                FindWindowEx(
                    FindWindow("Progman", "Program Manager"),
                    IntPtr.Zero, "SHELLDLL_DefView", ""
                ),
                IntPtr.Zero, "SysListView32", "FolderView"
            );

          SetWindowLong(handle, GWL_HWNDPARENT, hprog);
          
            IntPtr hWnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
            IntPtr hWndProgMan = FindWindow("Progman", "Program Manager");

            SetParent(hWnd, hWndProgMan);


        }

  

方式3(大同小异):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetDeskFrm
{
    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);
    }

    public partial class Form2 : Form
    {

        public Form2()
        {
            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 ex)
            {
                MessageBox.Show(this, ex.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);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

  

参考:https://stackoverflow.com/questions/365094/window-on-desktop

免责声明:文章转载自《c# 设置桌面背景窗口 SetParent》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇sgdisk基本用法Excel透视表进阶之排序、筛选、分组、总计与分类汇总下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WebGL 内嵌网页的一种解决方案

  之前使用的 ZFBrowser 嵌入方案可以发布到 Win, OS, Linux 上, 可是其它的就不行, 因为它用的谷歌内核嘛, 在 WebGL 上照理来说应该是最方便的啊, 因为它本身就是运行在浏览器内核上的, 前面的 BrowserInput 已经研究过向网页注入代码以及调用了, 现在试试在 WebGL 环境下来注入代码, 来创建简单的内嵌网页看...

日记:VB调用C++ DLL注意事项

1. DLL的接口必须在.def中声明,否则VB无法调用。根据已有资料,.def文件完成的作用是与extern "C"相同,也就是说如果在接口定义的头文件中使用了extern "C",则不需要在.def中声明。在实际中,供C++、C#和Java调用的DLL都只需要使用extern "C"来声明接口即可,VB应属一个特例,具体原因尚不清楚。 2. VB声明原...

BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图

BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境。 函数原型 [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hObject, int nXDest, intnY...

extern 使用方法具体解释

在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的。要在此处引用”。(extern能够置于变量或者函数前,以标示变量或者函数的定义在别的文件里,提示编译器遇到此变量和函数时在其它模块中寻找其定义 ) 大概extern  使用方法为例如以下几种方式: 其主要使用方法是: 在此文件里声明别的文件的变量时用ext...

error LNK2005 已经在***.obj中定义 的解决办法

为什么会出现这个错误??“error LNK2005: 已经在aaa.obj中定义”编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误。弄清楚它形成的原因,就可以轻松解决它了。造成LNK2005错误主要有以下几种情况:1.重复定义全局变量。可能存在两种情况:A、对于一些初学编程的程序员,有时候会以为需要使用全...

VC++的DLL应用(含Demo演示)

在大学大一的时候学的是C,然后后来大二的时候专业又开了C++这个课程,然后再后来自己又自学了一点VC++,大三的时候也试着编写过一个MFC的最简单的窗口程序。到大四的时候,自己又做了一个GIS的项目,是用C#.NET来编写的,然后发现C#上手好容易,而且还大部分语法规则都沿用了C,C++的习惯,于是觉得C++实在是没有一点优势可言啊。但这个暑假的实习经历又...