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家庭版设置桌面右键更换桌面壁纸.. ------------------------- 这是设置之前的右键快捷菜单.. ------------------------- 开始设置:右键桌面 ...
随机推荐
- python练习:http协议介绍
一.HTTP协议 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consortium)和Interne ...
- “京东金融”主页效果 RecyclerView联动
先上效果图吧: 第一个想到的实现方式是上面使用horizontalScrollview,下面使用Viewpager,经过尝试之后发现二者API有限,不能达到理想效果.几经折腾,最后上下都使用了自定义的 ...
- LearnX控件漏洞挖掘与利用
前言 大学英语会用到一个 ActiveX 插件 LearnX ,最近从网上下了一个下来分析了一下,找到了一些漏洞并完成了 exploit . 虽然涉及的知识比较老旧,不过还是挺有意思的.这里分享一下整 ...
- retrofit框架接口调用时候报Throwing new exception
最近在开发的时候遇到了一个很坑的问题,在三星6.0手机上请求接口时候报了一个异常:Throwing new exception 'length=1658; index=3248' with unexp ...
- Python之随机森林实战
代码实现: # -*- coding: utf-8 -*- """ Created on Tue Sep 4 09:38:57 2018 @author: zhen &q ...
- tkinter中button按钮控件(三)
button控件 简单的实现: import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry(" ...
- February 6th, 2018 Week 6th Tuesday
To be is to be perceived. 存在即被感知. How to interpret this quote? Maybe it means that everything in you ...
- Spring容器技术内幕之BeanDefinition类介绍
引言 org.springframework.beans.factory.config.BeanDefinition是配置文件< bean >元素标签在容器中地内部表示.< bean ...
- 添加Nginx为系统服务(设置开机启动)
在本节中,我们将创建一个脚本,将Nginx守护进程转换为实际的系统服务. 这有两个作用:守护程序可以使用标准命令控制,更重要的是,它可以在系统启动时自动启动,并在系统关闭时停止. System V s ...
- SQL查询语句,怎样查询重复数据
SQL查询语句,怎样查询重复数据 2012-02-22 14:50神奇的马骁勇 | 浏览 0 次 有表A, 中有字段id, name, memo现在有很多id重复的数据,怎么把这些重复的都查出来?gr ...