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. Spark机器学习——模型选择与参数调优之交叉验证

    spark 模型选择与超参调优 机器学习可以简单的归纳为 通过数据训练y = f(x) 的过程,因此定义完训练模型之后,就需要考虑如何选择最终我们认为最优的模型. 如何选择最优的模型,就是本篇的主要内 ...

  2. Stackoverflow 珠玑:用于分组的 LINQ 扩展方法

    从 stackoverflow.com 上抄来的,将 IEnumerable 中的元素进行切分的方法,无动态内存分配,地球上最快的实现: public static class LinqExtensi ...

  3. python自动化开发-6-常用模块-续

    python的常用模块(续) shelve模块:是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式. configparser模块:对配置文件进行 ...

  4. linux上用newman+postman进行自动化测试

    第一步:导出postman文件 Postman就是根据collection和enviroment这两个json文件来自动化运行的! 所以从Postman中导出collection和enviroment ...

  5. 遇到npm报错read ECONNRESET怎么办

    遇到npm 像弱智一样报错怎么办 read ECONNRESET This is most likely not a problem with npm itselft 'proxy' config i ...

  6. 命令行选项 - Mozilla 产品与私有技术 | MDN - Google Chrome

    命令行选项 在本文章中 语法规则 使用命令行选项 示例 用户配置档 -CreateProfile profile_name -CreateProfile "profile_name prof ...

  7. [20170623]利用传输表空间恢复数据库2.txt

    [20170623]利用传输表空间恢复数据库2.txt --//继续上午的测试,测试truncate,是否可行,理论讲应该没有问题.我主要的目的测试是否要切换日志.--//参考链接 : http:// ...

  8. Asp.Net配置不允许通过url方式访问目录下的资源

    Asp.Net网站发布后,有部分文件为了安全性,是不能直接通过url访问获取 通常有2种做法: 1.将文件目录建立在 App_code 或者App_Data 等默认的隐藏目录下 2.将文件的目录添加到 ...

  9. 高通GPIO驱动(DTS方式)

    gpio调试的方式有很多,linux3.0以上ARM架构的处理器基本上都采用了DTS的方式,在linux3.0可以通过获取sysfs的方式来获取gpio状态: sysfs文件系统的建立可以参照下面的博 ...

  10. Servlet (HttpServletResponse)对象

    1.setStatus(int status)方法:用于设置HTTP响应消息的状态码,并生成响应状态行.响应状态行中的状态描述信息直接与状态码相关,HTTP版本由服务器确定,因此只需要通过这个方法设置 ...