WPF 实现动态Windows桌面壁纸~
?? ??? 由于微信群人數(shù)太多入群請?zhí)砑?strong>小編微信號
?yanjinhuawechat 或 W_Feng_aiQ?邀請入群
?需備注WPF開發(fā)者?
? PS:有更好的方式歡迎推薦。
? 此項(xiàng)目靈感來源于?丑萌氣質(zhì)狗 B站同名??
? QQ群:560611514 ?(學(xué)習(xí)Unity3D)增加了播放視頻。
01
—
代碼如下
一、窗口介紹
Windows操作系統(tǒng)所有的地方都是窗口,可能這也是系統(tǒng)名字的由來吧,包括你看到的文件夾,桌面,右鍵菜單,這些都是由界面組成的, 這么多窗口需要有一個(gè)合理的顯示,就需要用到我們的層級關(guān)系,比如兩個(gè)窗體誰顯示在前,誰顯示在后。
VS給我們提供了一個(gè)查找和查看窗口信息的工具,叫做Spy++,在工具里面:
打開之后了,這里給我們展示了當(dāng)前系統(tǒng)所有的窗口信息,你也可以點(diǎn)擊紅色框中的查找工具,來查看你想知道的窗口信息:
來演示一下如何查找窗口,點(diǎn)擊上方紅色框中的查找窗口按鈕,兩個(gè)隨便選一個(gè),會(huì)彈出如下窗口:
然后你點(diǎn)擊紅色區(qū)域中的這個(gè)控件拖動(dòng)到你想獲取的信息窗口,就能看到當(dāng)前窗口的詳細(xì)信息了,包括窗口的句柄、標(biāo)題、類。
比如我直接將圖標(biāo)拖到桌面上,可以看到這是他顯示桌面的信息:
這里我們關(guān)掉這個(gè)窗口, 回到Spy++的主界面,拖到最底部:
可以看到,?Progman Manager是桌面窗口的父窗口,前面小窗口圖標(biāo)是灰色的表示的是此窗口是隱藏的(子窗口擁有和父窗口一致的顯示層級)。
二、原理操作
現(xiàn)在,我們只需要把我們的界面,也就是放到?Program Manager下面,然后再適當(dāng)調(diào)整它的顯示順序,就可以了,但是這一塊我們不好操作。有一個(gè)其他路子就是給窗口發(fā)送一個(gè)特殊的消息,來讓我們有操作的空間。
只需要給?Program Manager窗口發(fā)送一個(gè)消息0x52C,就可以將Program Manager拆分為多個(gè)窗口,分別是Program Manager窗口和兩個(gè)WorkerW窗口。
下面是已經(jīng)發(fā)送過此消息后的樣子:
可以看到Program Manager下面已經(jīng)什么都沒有了,內(nèi)容全都轉(zhuǎn)移到第一個(gè)WokerW窗口下,這時(shí)候我們只需要將我們的窗口掛在到Program Manager窗口的下方就能又有和它一樣的顯示層級了(窗口從下到上依次顯示,所以這里Program Manager顯示在最底層),不過需要注意的是,在Program Manager和第一個(gè)WorkerW窗口之間,還存在另外一個(gè)WorkerW窗口,在我的系統(tǒng)中,它默認(rèn)隱藏了,為了確保效果一致,我們需要手動(dòng)將它隱藏起來。
三、Win32ApiHelper?代碼如下
using?System; using?System.Runtime.InteropServices;namespace?WPFDevelopers.Helpers {public?class?Win32ApiHelper{[DllImport("user32.dll")]public?static?extern?IntPtr?FindWindow(string?className,?string?winName);[DllImport("user32.dll")]public?static?extern?IntPtr?SendMessageTimeout(IntPtr?hwnd,?uint?msg,?IntPtr?wParam,?IntPtr?lParam,?uint?fuFlage,?uint?timeout,?IntPtr?result);//查找窗口的委托?查找邏輯public?delegate?bool?EnumWindowsProc(IntPtr?hwnd,?IntPtr?lParam);[DllImport("user32.dll")]public?static?extern?bool?EnumWindows(EnumWindowsProc?proc,?IntPtr?lParam);[DllImport("user32.dll")]public?static?extern?IntPtr?FindWindowEx(IntPtr?hwndParent,?IntPtr?hwndChildAfter,?string?className,?string?winName);[DllImport("user32.dll")]public?static?extern?bool?ShowWindow(IntPtr?hwnd,?int?nCmdShow);[DllImport("user32.dll")]public?static?extern?IntPtr?SetParent(IntPtr?hwnd,?IntPtr?parentHwnd);} }四、DesktopBackground.xaml?代碼如下
<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.DesktopBackground"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"mc:Ignorable="d"?d:DesignHeight="450"?d:DesignWidth="800"><Grid><Button?Content="選擇視頻"?Height="40"?Width="120"?Click="Button_Click"/></Grid> </UserControl>五、DesktopBackground.xaml.cs?代碼如下
using?System.Windows; using?System.Windows.Controls; using?WPFDevelopers.Samples.ExampleViews.Desktop;namespace?WPFDevelopers.Samples.ExampleViews {///?<summary>///?WorkerWBackground.xaml?的交互邏輯///?</summary>public?partial?class?DesktopBackground?:?UserControl{public?DesktopBackground(){InitializeComponent();}private?void?Button_Click(object?sender,?RoutedEventArgs?e){new?DesktopPlayVideo().Show();}?} }六、DesktopPlayVideo.xaml?代碼如下
<Window?x:Class="WPFDevelopers.Samples.ExampleViews.Desktop.DesktopPlayVideo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:sys="clr-namespace:System;assembly=mscorlib"xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews.Desktop"xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"mc:Ignorable="d"?Background="Transparent"WindowStyle="None"ResizeMode="NoResize"AllowsTransparency="True"Height="{x:Static?SystemParameters.PrimaryScreenHeight}"Width="{x:Static?SystemParameters.PrimaryScreenWidth}"><Grid><MediaElement?Name="PART_MediaElement"/></Grid> </Window> <Window?x:Class="WPFDevelopers.Samples.ExampleViews.Desktop.DesktopPlayVideo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:sys="clr-namespace:System;assembly=mscorlib"xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews.Desktop"xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"mc:Ignorable="d"?Background="Transparent"WindowStyle="None"ResizeMode="NoResize"AllowsTransparency="True"Height="{x:Static?SystemParameters.PrimaryScreenHeight}"Width="{x:Static?SystemParameters.PrimaryScreenWidth}"><Grid><MediaElement?Name="PART_MediaElement"/></Grid> </Window>七、DesktopPlayVideo.xaml.cs?代碼如下
using?System; using?System.Windows; using?System.Windows.Interop; using?System.Windows.Media; using?System.Windows.Media.Animation; using?WPFDevelopers.Helpers;namespace?WPFDevelopers.Samples.ExampleViews.Desktop {///?<summary>///?DesktopPlayVideo.xaml?的交互邏輯///?</summary>public?partial?class?DesktopPlayVideo?:?Window{private?IntPtr?programHandle;public?DesktopPlayVideo(){InitializeComponent();this.Loaded?+=?DesktopPlayVideo_Loaded;}private?void?DesktopPlayVideo_Loaded(object?sender,?RoutedEventArgs?e){Microsoft.Win32.OpenFileDialog?openFileDialog?=?new?Microsoft.Win32.OpenFileDialog();openFileDialog.DefaultExt?=?".mp4";openFileDialog.Filter?=?"視頻文件(.MP4)|*.mp4;";if?(openFileDialog.ShowDialog()?==?true){SendMsgToProgman();Width?=?SystemParameters.PrimaryScreenWidth;?Height?=?SystemParameters.PrimaryScreenHeight;?Left?=?0;?Top?=?0;//PART_MediaElement.Source?=?new?Uri(openFileDialog.FileName);//PART_MediaElement.MediaEnded?+=?(s1,?e1)?=>?//{//????PART_MediaElement.Position?=?new?TimeSpan(0,?0,?1);//????PART_MediaElement.Play();//};var?storyboard?=?new?Storyboard();storyboard.RepeatBehavior?=?RepeatBehavior.Forever;var?mediaTimeline?=?new?MediaTimeline{Source?=?new?Uri(openFileDialog.FileName),};Storyboard.SetTargetName(mediaTimeline,?PART_MediaElement.Name);storyboard.Children.Add(mediaTimeline);//?設(shè)置當(dāng)前窗口為?Program?Manager的子窗口Win32ApiHelper.SetParent(new?WindowInteropHelper(this).Handle,?programHandle);PART_MediaElement.Loaded?+=?(s1,?e1)?=>{storyboard.Begin(PART_MediaElement);};App.CurrentMainWindow.WindowState?=?WindowState.Minimized;}}///?<summary>///?向桌面發(fā)送消息///?</summary>void?SendMsgToProgman(){//?桌面窗口句柄,在外部定義,用于后面將我們自己的窗口作為子窗口放入programHandle?=?Win32ApiHelper.FindWindow("Progman",?null);IntPtr?result?=?IntPtr.Zero;//?向?Program?Manager?窗口發(fā)送消息?0x52c?的一個(gè)消息,超時(shí)設(shè)置為2秒Win32ApiHelper.SendMessageTimeout(programHandle,?0x52c,?IntPtr.Zero,?IntPtr.Zero,?0,?2,?result);//?遍歷頂級窗口Win32ApiHelper.EnumWindows((hwnd,?lParam)?=>{//?找到第一個(gè)?WorkerW?窗口,此窗口中有子窗口?SHELLDLL_DefView,所以先找子窗口if?(Win32ApiHelper.FindWindowEx(hwnd,?IntPtr.Zero,?"SHELLDLL_DefView",?null)?!=?IntPtr.Zero){//?找到當(dāng)前第一個(gè) WorkerW 窗口的,后一個(gè)窗口,及第二個(gè) WorkerW 窗口。IntPtr?tempHwnd?=?Win32ApiHelper.FindWindowEx(IntPtr.Zero,?hwnd,?"WorkerW",?null);//?隱藏第二個(gè)?WorkerW?窗口Win32ApiHelper.ShowWindow(tempHwnd,?0);}return?true;},?IntPtr.Zero);}} }02
—
效果預(yù)覽
鳴謝素材提供者 -?丑萌氣質(zhì)狗(李飛)
源碼地址如下
Github:https://github.com/WPFDevelopersOrg
Gitee:https://gitee.com/WPFDevelopersOrg
作者:丑萌氣質(zhì)狗? ?學(xué)習(xí)Unity3D B站搜索?丑萌氣質(zhì)狗?
出處:https://www.cnblogs.com/choumengqizhigou/p/15702980.html
版權(quán):本文采用「署名-非商業(yè)性使用-相同方式共享 4.0 國際」知識(shí)共享許可協(xié)議進(jìn)行許可。
轉(zhuǎn)載請注明出處
QQ群:560611514 ?(學(xué)習(xí)Unity3D)
WPF開發(fā)者QQ群:?340500857
掃一掃關(guān)注我們,
更多知識(shí)早知道!
點(diǎn)擊閱讀原文可跳轉(zhuǎn)至源代碼
總結(jié)
以上是生活随笔為你收集整理的WPF 实现动态Windows桌面壁纸~的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软第二财季营收达 517 亿美元,净利
- 下一篇: 22543!Windows 11 新预览