【自己动手,打造自己的Wallpaper引擎(Win10/Win11)】

在这里插入图片描述

本篇开发环境是Win11x64+VS2019+.Net5.0,代码兼容Win10
程序集推荐用x64,什么年代了,你难道还在用32位的系统吗,不可能吧,不可能吧!

首先,我们这里用C#.Net5运行时作为图形界面,方便统一调用,不同程序集直接可以无缝衔接;你也可以用其他框架,但是最终要可以获取窗口句柄为目标,因为我们可以用WPF/Winform嵌入一个外部exe作为子窗体

首先,我们这里用WPF调用Winform,.现在两个框架都有.Net5的版本,你就不需要安装两个SDK了
工程目录:
在这里插入图片描述

Winform作为dll,我们在WPF实例化它,然后一个.Show()就可以显示Winform窗体了

这里我们的Winform的主窗体的属性有几个是必须的:

FormBorderStyle=None
BackgroundImageLayout=Stretch
DoubleBuffered=True
WindowState=Normal
ShowIcon=False
ShowInTaskbar=False

你可以在属性栏设置,也可以在Form1的构造函数里用代码设置,其中WindowState=Normal这个必须设置为Normal,否则后面会翻车

在Winform嵌入外部exe窗体到控件:https://blog.csdn.net/swjian1997/article/details/82027065
PS:并不是所有exe都可以嵌入,这个看缘分了😉
这里直接贴一下,防丢失:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Wallpaper
{
    //https://blog.csdn.net/swjian1997/article/details/82027065?utm_medium=distribute.pc_relevant.none-task-blog-title-8&spm=1001.2101.3001.4242
    public class exetowinform
    {
        EventHandler appIdleEvent = null;
        Control ParentCon = null;
        string strGUID = "";

        public exetowinform(Control C, string Titlestr)
        {
            appIdleEvent = new EventHandler(Application_Idle);
            ParentCon = C;
            strGUID = Titlestr;
        }

        /// <summary>  
        /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器  
        /// </summary>  
        public IntPtr Start(string FileNameStr, string cmd = null)
        {
            if (m_AppProcess != null)
            {
                Stop();
            }
            try
            {
                ProcessStartInfo info = new ProcessStartInfo(FileNameStr);
                info.UseShellExecute = true;
                info.WindowStyle = ProcessWindowStyle.Minimized;
                info.Arguments = cmd;
                m_AppProcess = System.Diagnostics.Process.Start(info);
                m_AppProcess.WaitForInputIdle();
                Application.Idle += appIdleEvent;
            }
            catch
            {
                if (m_AppProcess != null)
                {
                    if (!m_AppProcess.HasExited)
                        m_AppProcess.Kill();
                    m_AppProcess = null;
                }
            }
            return m_AppProcess.Handle;
        }


        /// <summary>  
        /// 确保应用程序嵌入此容器  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void Application_Idle(object sender, EventArgs e)
        {
            if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
            {
                this.m_AppProcess = null;
                Application.Idle -= appIdleEvent;
                return;

            }

            Thread.Sleep(300);//这里加阻塞 ,时间可以大些
            Application.DoEvents();
            if (m_AppProcess.MainWindowHandle == IntPtr.Zero)
                return;
            Application.Idle -= appIdleEvent;
            EmbedProcess(m_AppProcess, ParentCon);
        }
        /// <summary>  
        /// 应用程序结束运行时要清除这里的标识  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void m_AppProcess_Exited(object sender, EventArgs e)
        {
            m_AppProcess = null;
        }
        /// <summary>  
        /// 将属性<code>AppFilename</code>指向的应用程序关闭  
        /// </summary>  
        public void Stop()
        {
            if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero)  
            {
                try
                {
                    if (!m_AppProcess.HasExited)
                        m_AppProcess.Kill();
                }
                catch (Exception)
                {
                }
                m_AppProcess = null;
            }
        }
        #region 属性  
        /// <summary>  
        /// application process  
        /// </summary>  
        Process m_AppProcess = null;

        /// <summary>  
        /// 标识内嵌程序是否已经启动  
        /// </summary>  
        public bool IsStarted { get { return (this.m_AppProcess != null); } }

        #endregion 属性  

        #region Win32 API  
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
             CharSet = CharSet.Unicode, ExactSpelling = true,
             CallingConvention = CallingConvention.StdCall)]
        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetParent(IntPtr hwnd);

        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private const int SWP_NOOWNERZORDER = 0x200;
        private const int SWP_NOREDRAW = 0x8;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int WS_EX_MDICHILD = 0x40;
        private const int SWP_FRAMECHANGED = 0x20;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_ASYNCWINDOWPOS = 0x4000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;

        private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标}  
        private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活}  
        private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}  
        private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}  
        private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}  
        private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}  
        private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活}  
        private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}  
        private const int SW_MINIMIZE = 6; //{最小化, 不激活}  
        private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}  
        private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}  
        private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}  
        private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}  
        private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}  

        #endregion Win32 API  

        /// <summary>  
        /// 将指定的程序嵌入指定的控件  
        /// </summary>  
        private void EmbedProcess(Process app, Control control)
        {
            // Get the main handle  
            if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
            System.IO.File.WriteAllText("app.MainWindowHandle.txt", app.MainWindowHandle.ToString());
            try
            {
                // Put it into this form  
                SetParent(app.MainWindowHandle, control.Handle);
            }
            catch (Exception)
            { }
            try
            {
                // Remove border and whatnot                 
                SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
                SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);
            }
            catch (Exception)
            { }
            try
            {
                // Move the window to overlay it on this window  
                MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, false);
            }
            catch (Exception)
            { }
        }
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        const int WM_SETTEXT = 0x000C;
    }
}

然后在WPF里,我们用win32api( Win32.SetParent(form.Handle, programIntPtr)😉,把winform的窗体句柄设置为桌面WorkerW的子窗体
具体代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    public static class Win32
    {
        [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);

        [DllImport("user32.dll")]
        public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr lParam);
        public delegate bool EnumWindowsProc(IntPtr hwnd, 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);
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
          
            Init();
            //this.WindowState = WindowState.Maximized;
        }
        #region MyRegion
        private IntPtr programIntPtr = IntPtr.Zero;
        bool inited { get; set; }
        public void Init()
        {
            try
            {
                if (inited) return;
                // 通过类名查找一个窗口,返回窗口句柄。
                programIntPtr = Win32.FindWindow("Progman", null);

                // 窗口句柄有效
                if (programIntPtr != IntPtr.Zero)
                {
                    IntPtr result = IntPtr.Zero;

                    // 向 Program Manager 窗口发送 0x52c 的一个消息,超时设置为0x3e8(1秒)。
                    Win32.SendMessageTimeout(programIntPtr, 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 0x3e8, result);

                    // 遍历顶级窗口
                    Win32.EnumWindows((hwnd, lParam) =>
                    {
                        // 找到包含 SHELLDLL_DefView 这个窗口句柄的 WorkerW
                        if (Win32.FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null) != IntPtr.Zero)
                        {
                            // 找到当前 WorkerW 窗口的,后一个 WorkerW 窗口。 
                            IntPtr tempHwnd = Win32.FindWindowEx(IntPtr.Zero, hwnd, "WorkerW", null);

                            // 隐藏这个窗口
                            Win32.ShowWindow(tempHwnd, 0);
                        }
                        return true;
                    }, IntPtr.Zero);
                }

                WinFormsApp1.Form1 form = new WinFormsApp1.Form1();
                form.Hide();
                //Win32.SetParent(new System.Windows.Interop.WindowInteropHelper(this).Handle, programIntPtr);
                Win32.SetParent(form.Handle, programIntPtr);
                
                form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                form.Show();

                inited = true;

            }
            catch (Exception ex)
            {
            }
        }
        #endregion
    }
}

PS:上面设置完父子关系后,form.WindowState = System.Windows.Forms.FormWindowState.Maximized;这一句非常重要,这句可以让Winform窗体最大化,覆盖到整个桌面背景!至于其他非Winform的窗体,我们怎么最大化呢,我们可以用Win32Api强制它最大化显示!具体可以参考https://blog.csdn.net/slyzly/article/details/5350260这篇博文,核心的函数是 int ShowWindow(IntPtr hwnd,int nCmdShow),里面的nCmdShow包含窗口的三个状态,也可以用win32api强制exe窗口去除边框,这里就不贴了

结合我上一篇博文:https://blog.csdn.net/weixin_44029053/article/details/119838858
我们可以把视频控件装载在Winform内,实现桌面视频播放;
然后你可以放大你的脑洞,装一个cefsharp,你就可以桌面播放网页,播放WebGL
然后你再弄一个OpenGL的控件(比如SharpGL、Silk.Net)甚至可以摆弄一些神奇的玩意儿!

如果你按上面要求做,没有意外的话,是这种效果:
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值