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的更多相关文章

  1. 利用API设置桌面背景

    实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...

  2. windows 桌面背景设置实例

    应用SystemParametersInfo函数可以获取和设置数量众多的windows系统参数.这个小程序就是运用了SystemParametersInfo函数来设置桌面的墙纸背景,而且程序可以让我们 ...

  3. Windows7获取、更换桌面背景,C#

    使用的API原型是 BOOL SystemParametersinfo(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinlni); 在C#中定义如下 ...

  4. 设置Debian8 光秃秃的桌面(图标,窗口样式等)

    在虚拟机里按抓了Debian8, 然后进入桌面后很不习惯,最主要是桌面光秃秃的, 今天终于不小心找到办法了: 按[Win]键, 找到"优化工具"程序; 或者是在右上角的[应用程序] ...

  5. Webstorm设置背景图为Windows桌面背景

    桌面背景图会缓存在这个目录中,文件名不确定在改变桌面背景后会不会变. C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Themes\CachedFiles ...

  6. 找回误删除的UBUNTU16.04桌面壁纸图片,或把桌面背景图片另存。20170114

    今天遇到一个小问题,之前下载并设置为桌面壁纸的一张图片在整理文件的时候不小心删除了.由于想不起来当时从哪里找到的图,所以就想把当前桌面壁纸重新保存.经网上查询,未见正确的保存方法,故写在此处备忘. 1 ...

  7. Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())

    在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...

  8. QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)

    QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg ...

  9. Win10家庭版设置桌面右键更换桌面壁纸

    Win10家庭版设置桌面右键更换桌面壁纸.. ------------------------- 这是设置之前的右键快捷菜单.. ------------------------- 开始设置:右键桌面 ...

随机推荐

  1. web全栈架构师[笔记] — 01 ECMAScript6新特性

    ES6新特性 一.变量 var:重复定义不报错:没有块级作用域:不能限制修改 let:变量,不能重复定义,有块级作用域 const:常量,不能重复定义,有块级作用域 二.函数/参数 箭头函数——简写: ...

  2. Android系统启动流程(三)解析SystemServer进程启动过程

    1.Zygote启动SystemServer进程 在上一篇文章中我们讲到在ZygoteInit.java的startSystemServer函数中启动了SyetemServer进程,如下所示. fra ...

  3. 【第五篇】SAP ABAP7.5x新语法之命名规约

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:SAP ABAP7.5x系列之命名规约   命名 ...

  4. sysbench对MySQL的压测,使用sysbench压测磁盘io

    QPS - query per secondTPS - transaction per second 不是特别关注,每个业务场景中事务标准是不一样的 Ⅰ.sysbench测试框架 Ⅱ.常用测试脚本 [ ...

  5. 合理配置SQLSERVER内存

    合理配置SQLSERVER内存 原文地址:https://www.cnblogs.com/lyhabc/archive/2012/09/28/2707857.html SQLSERVER是个很喜欢内存 ...

  6. EaseType 缓动函数

    EaseType(动画曲线) EaseType 缓动函数或者我习惯叫它动画曲线,在很多的软件或动画中都有涉及到,下面是摘取的一些资料: 缓函数图例 Tween效果 每一幅图像当鼠标移上去,会有路径效果 ...

  7. .net 调用R语言的函数(计算统计值pvalue 对应excel :ttest)

    Pvalue 计算 项目设计pvalue计算,但是由于.net 没有类似的公式或者函数,最终决定使用.net 调用R语言 采用.net 调用r语言的公用函数 需要安装 r语言环境 https://mi ...

  8. Win10更换电脑,又不想重装系统的解决方法

    问题描述: 在公司因为两年前用的i3的电脑很卡,然后想换i5的电脑,但是又不想重装系统,因为安装的东西太多了,重装很麻烦 Windows to go介绍: Windows To Go是Windows ...

  9. 解决eclipse启动慢

    每次启动eclipse时都要等待半分钟左右,对于追求效率的程序员来说,等待每一秒都是一种折磨,出于高效开发的精神,这个问题必须解决掉.方案如下: ①在工具栏中的help中找到about eclipse ...

  10. Highcharts属性与Y轴数据值刻度显示Y轴最小最大值

    Highcharts 官网:https://www.hcharts.cn/demo/highcharts Highcharts API文档:https://api.hcharts.cn/highcha ...