5

WPF 获取 MediaContext 的方法

 2 years ago
source link: https://lindexi.gitee.io/post/WPF-%E8%8E%B7%E5%8F%96-MediaContext-%E7%9A%84%E6%96%B9%E6%B3%95.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
WPF 获取 MediaContext 的方法

本文告诉大家如何通过反射获取 MediaContext 对象。在 WPF 里面,通过 MediaContext 对象可以用来控制渲染

在调试下,通过 Dispatcher 对象,可以看到有 Reserved0 不公开的属性,此属性就是 MediaContext 对象,如下面方法,通过反射获取此属性

            var propertyInfo = typeof(Dispatcher).GetProperty("Reserved0", BindingFlags.NonPublic | BindingFlags.Instance);
            var mediaContext = propertyInfo.GetMethod.Invoke(Dispatcher, null);

如此即可获取到 MediaContext 对象

接下来可以通过程序集获取 MediaContext 类型,从而实现反射调用方法

            var mediaContextType = typeof(Visual).Assembly.GetType("System.Windows.Media.MediaContext");

如尝试自己调用 DUCE 触发渲染

            var methodInfoGetChannels = mediaContextType.GetMethod("GetChannels", BindingFlags.NonPublic | BindingFlags.Instance);
            var channelSet = methodInfoGetChannels.Invoke(mediaContext, null);

            var typeChannelSet = typeof(Visual).Assembly.GetType("System.Windows.Media.Composition.DUCE+ChannelSet");
            var fieldInfoChannel = typeChannelSet.GetField("Channel", BindingFlags.NonPublic | BindingFlags.Instance);
            var channel = fieldInfoChannel.GetValue(channelSet);

            var typeChannel = typeof(Visual).Assembly.GetType("System.Windows.Media.Composition.DUCE+Channel");
            var hChannelFieldInfo = typeChannel.GetField("_hChannel", BindingFlags.NonPublic | BindingFlags.Instance);
            var hChannel = (IntPtr)hChannelFieldInfo.GetValue(channel);

            ResourceHandle handle = default;
            var result = MilResource_CreateOrAddRefOnChannel(hChannel, ResourceType.TYPE_BRUSH, ref handle);

        [DllImport(MilCore)]
        internal static extern /*HRESULT*/ int MilResource_CreateOrAddRefOnChannel(
            IntPtr pChannel,
            ResourceType resourceType,
            ref ResourceHandle hResource
        );

        internal const string WCP_VERSION_SUFFIX = "_cor3";
        internal const string MilCore = "wpfgfx" + WCP_VERSION_SUFFIX + ".dll";

本文所有代码放在 githubgitee 欢迎访问

可以通过如下方式获取本文代码

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 23c0515af613acc5025f95c17ea254fc573375ce

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

获取代码之后,进入 LereleweawaLinojairgeefonechal 文件夹


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/WPF-%E8%8E%B7%E5%8F%96-MediaContext-%E7%9A%84%E6%96%B9%E6%B3%95.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK