WPF 实现动态Windows桌面壁纸~
?? ??? 由于微信群人數太多入群請添加小編微信號
?yanjinhuawechat 或 W_Feng_aiQ?邀請入群
?需備注WPF開發者?
? PS:有更好的方式歡迎推薦。
? 此項目靈感來源于?丑萌氣質狗 B站同名??
? QQ群:560611514 ?(學習Unity3D)增加了播放視頻。
01
—
代碼如下
一、窗口介紹
Windows操作系統所有的地方都是窗口,可能這也是系統名字的由來吧,包括你看到的文件夾,桌面,右鍵菜單,這些都是由界面組成的, 這么多窗口需要有一個合理的顯示,就需要用到我們的層級關系,比如兩個窗體誰顯示在前,誰顯示在后。
VS給我們提供了一個查找和查看窗口信息的工具,叫做Spy++,在工具里面:
打開之后了,這里給我們展示了當前系統所有的窗口信息,你也可以點擊紅色框中的查找工具,來查看你想知道的窗口信息:
來演示一下如何查找窗口,點擊上方紅色框中的查找窗口按鈕,兩個隨便選一個,會彈出如下窗口:
然后你點擊紅色區域中的這個控件拖動到你想獲取的信息窗口,就能看到當前窗口的詳細信息了,包括窗口的句柄、標題、類。
比如我直接將圖標拖到桌面上,可以看到這是他顯示桌面的信息:
這里我們關掉這個窗口, 回到Spy++的主界面,拖到最底部:
可以看到,?Progman Manager是桌面窗口的父窗口,前面小窗口圖標是灰色的表示的是此窗口是隱藏的(子窗口擁有和父窗口一致的顯示層級)。
二、原理操作
現在,我們只需要把我們的界面,也就是放到?Program Manager下面,然后再適當調整它的顯示順序,就可以了,但是這一塊我們不好操作。有一個其他路子就是給窗口發送一個特殊的消息,來讓我們有操作的空間。
只需要給?Program Manager窗口發送一個消息0x52C,就可以將Program Manager拆分為多個窗口,分別是Program Manager窗口和兩個WorkerW窗口。
下面是已經發送過此消息后的樣子:
可以看到Program Manager下面已經什么都沒有了,內容全都轉移到第一個WokerW窗口下,這時候我們只需要將我們的窗口掛在到Program Manager窗口的下方就能又有和它一樣的顯示層級了(窗口從下到上依次顯示,所以這里Program Manager顯示在最底層),不過需要注意的是,在Program Manager和第一個WorkerW窗口之間,還存在另外一個WorkerW窗口,在我的系統中,它默認隱藏了,為了確保效果一致,我們需要手動將它隱藏起來。
三、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);//?設置當前窗口為?Program?Manager的子窗口Win32ApiHelper.SetParent(new?WindowInteropHelper(this).Handle,?programHandle);PART_MediaElement.Loaded?+=?(s1,?e1)?=>{storyboard.Begin(PART_MediaElement);};App.CurrentMainWindow.WindowState?=?WindowState.Minimized;}}///?<summary>///?向桌面發送消息///?</summary>void?SendMsgToProgman(){//?桌面窗口句柄,在外部定義,用于后面將我們自己的窗口作為子窗口放入programHandle?=?Win32ApiHelper.FindWindow("Progman",?null);IntPtr?result?=?IntPtr.Zero;//?向?Program?Manager?窗口發送消息?0x52c?的一個消息,超時設置為2秒Win32ApiHelper.SendMessageTimeout(programHandle,?0x52c,?IntPtr.Zero,?IntPtr.Zero,?0,?2,?result);//?遍歷頂級窗口Win32ApiHelper.EnumWindows((hwnd,?lParam)?=>{//?找到第一個?WorkerW?窗口,此窗口中有子窗口?SHELLDLL_DefView,所以先找子窗口if?(Win32ApiHelper.FindWindowEx(hwnd,?IntPtr.Zero,?"SHELLDLL_DefView",?null)?!=?IntPtr.Zero){//?找到當前第一個 WorkerW 窗口的,后一個窗口,及第二個 WorkerW 窗口。IntPtr?tempHwnd?=?Win32ApiHelper.FindWindowEx(IntPtr.Zero,?hwnd,?"WorkerW",?null);//?隱藏第二個?WorkerW?窗口Win32ApiHelper.ShowWindow(tempHwnd,?0);}return?true;},?IntPtr.Zero);}} }02
—
效果預覽
鳴謝素材提供者 -?丑萌氣質狗(李飛)
源碼地址如下
Github:https://github.com/WPFDevelopersOrg
Gitee:https://gitee.com/WPFDevelopersOrg
作者:丑萌氣質狗? ?學習Unity3D B站搜索?丑萌氣質狗?
出處:https://www.cnblogs.com/choumengqizhigou/p/15702980.html
版權:本文采用「署名-非商業性使用-相同方式共享 4.0 國際」知識共享許可協議進行許可。
轉載請注明出處
QQ群:560611514 ?(學習Unity3D)
WPF開發者QQ群:?340500857
掃一掃關注我們,
更多知識早知道!
點擊閱讀原文可跳轉至源代碼
總結
以上是生活随笔為你收集整理的WPF 实现动态Windows桌面壁纸~的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 微软第二财季营收达 517 亿美元,净利
 - 下一篇: Natasha 4.0 探索之路系列(三