c# 设置桌面背景窗口 SetParent
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的更多相关文章
- 利用API设置桌面背景
实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...
- windows 桌面背景设置实例
应用SystemParametersInfo函数可以获取和设置数量众多的windows系统参数.这个小程序就是运用了SystemParametersInfo函数来设置桌面的墙纸背景,而且程序可以让我们 ...
- Windows7获取、更换桌面背景,C#
使用的API原型是 BOOL SystemParametersinfo(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinlni); 在C#中定义如下 ...
- 设置Debian8 光秃秃的桌面(图标,窗口样式等)
在虚拟机里按抓了Debian8, 然后进入桌面后很不习惯,最主要是桌面光秃秃的, 今天终于不小心找到办法了: 按[Win]键, 找到"优化工具"程序; 或者是在右上角的[应用程序] ...
- Webstorm设置背景图为Windows桌面背景
桌面背景图会缓存在这个目录中,文件名不确定在改变桌面背景后会不会变. C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Themes\CachedFiles ...
- 找回误删除的UBUNTU16.04桌面壁纸图片,或把桌面背景图片另存。20170114
今天遇到一个小问题,之前下载并设置为桌面壁纸的一张图片在整理文件的时候不小心删除了.由于想不起来当时从哪里找到的图,所以就想把当前桌面壁纸重新保存.经网上查询,未见正确的保存方法,故写在此处备忘. 1 ...
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...
- QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)
QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg ...
- Win10家庭版设置桌面右键更换桌面壁纸
Win10家庭版设置桌面右键更换桌面壁纸.. ------------------------- 这是设置之前的右键快捷菜单.. ------------------------- 开始设置:右键桌面 ...
随机推荐
- 惊闻企业Web应用生成平台 活字格 V4.0 免费了,不单可视化设计器免费,服务器也免费!
官网消息: 针对活字格开发者,新版本完全免费!您可下载活字格 Web 应用生成平台 V4.0 Updated 1,方便的创建各类 Web 应用系统,任意部署,永不过期. 我之前学习过活字格,也曾经向用 ...
- SoapUI SoapUI测试WebService协议接口简介
SoapUI测试WebService协议接口简介 by:授客 QQ:1033553122 1. 创建项目,入口:File -> New SOAP Project,或者右键默认项目Project- ...
- (后端)Java新人入职——配置环境及安装开发工具(完全)
转自csdn:执笔记忆的空白 很多新人对于进入新公司,相关工具的安装和环境变量的设定很苦恼.又苦于没有完整的配置开发环境的资料,我这里写一篇操作步骤的案例, 至少让你能把开发工具安装起来,并实用起来, ...
- 利用python中的gensim模块训练和测试word2vec
word2vec的基础知识介绍参考上一篇博客和列举的参考资料. 首先利用安装gensim模块,相关依赖如下,注意版本要一致: Python >= 2.7 (tested with version ...
- TypeError: __init__() missing 1 required positional argument: 'on_delete'
报错的原因呢,就是在设计model时我弄了个外键,然后就报错了... 不难看出,它是想让我们在表与表关联时添加一个on_delete参数 解决办法: 如其所愿,加上on_delete=models.C ...
- solr-query
解释: 1.query:获取全部数据的SQL 2.deltaImportQuery:获取增量数据时使用的SQL 3.deltaQuery:获取pk的SQL 4.parentDeltaQuery:获取父 ...
- 06-OpenLDAP密码策略
阅读视图 openldap密码策略 OpenLDAP服务端定制密码策略 客户端策划策略实例 定义用户第一次登录就修改密码 问题排查手册 重点推荐官方文档 备注:本文依然承接系列文. 1. openld ...
- python遍历本地文件系统 按文件大小排序
在这个例子中,主要会用到python内置的和OS模块的几个函数: os.walk() : 该方法用来遍历指定的文件目录,返回一个三元tuple(dirpath, dirnames, filenames ...
- WCF REST模式下的UriTemplate路径问题
问题:在WCF4.0中,用UriTemplate进行表明路径参数的时候会出现问题 1.创建的项目是Service.Interface[4.0]和Service[4.0]还有Client[4.0] 2. ...
- C# 生成强命名程序集并添加到GAC
针对一些类库项目或用户控件项目(一般来说,这类项目最后编译生成的是一个或多个dll文件),在程序开发完成后,有时需要将开发的程序集(dll文件)安装部署到GAC(全局程序集缓存)中,以便其他的程序也可 ...