当前位置: 首页 > news >正文

做网站被骗网站搜索排名怎么做

做网站被骗,网站搜索排名怎么做,数字尾巴 wordpress,相亲网站如何做自我介绍WPF Hwnd窗口互操作系列 第一章 嵌入Hwnd窗口 第二章 嵌入WinForm控件 第三章 嵌入WPF控件 第四章 嵌入外部程序(本章) 第五章 底部嵌入HwndHost 文章目录 WPF Hwnd窗口互操作系列前言一、如何实现?1、定义属性2、进程嵌入(1&…

WPF Hwnd窗口互操作系列

第一章 嵌入Hwnd窗口
第二章 嵌入WinForm控件
第三章 嵌入WPF控件
第四章 嵌入外部程序(本章)
第五章 底部嵌入HwndHost


文章目录

  • WPF Hwnd窗口互操作系列
  • 前言
  • 一、如何实现?
    • 1、定义属性
    • 2、进程嵌入
      • (1)启动进程
      • (2)、进程加入作业对象
      • (3)、获取主窗口句柄
    • 3、销毁进程
  • 二、完整代码
  • 三、使用示例
    • 1、嵌入ffplay.exe
  • 总结


前言

实现嵌入各种窗口控件后,其实还会有一种需求:嵌入外部程序,我们有时可能需要嵌入一个浏览器或者或者播放器等一些已有的程序,其嵌入原理也和前面差不多,只要能获取进程的主窗口句柄,然后将窗口嵌入。


一、如何实现?

1、定义属性

定义一个依赖属性,提供给xaml设置进程运行的命令行

public class AppHost : HwndHost
{/// <summary>/// 进程运行的命令行/// </summary>public string Cmdline{get { return (string)GetValue(CmdlineProperty); }set { SetValue(CmdlineProperty, value); }}// Using a DependencyProperty as the backing store for Cmdline.  This enables animation, styling, binding, etc...public static readonly DependencyProperty CmdlineProperty =DependencyProperty.Register("Cmdline", typeof(string), typeof(AppHost), new PropertyMetadata(""));
}        

2、进程嵌入

在下列方法中进行进程嵌入,具体操作如下列步骤。

protected override HandleRef BuildWindowCore(HandleRef hwndParent)

(1)启动进程

var cmds = Cmdline.Split(" ", 2);
Process? _process;
_process.StartInfo.FileName = cmds.First();
_process.StartInfo.Arguments = cmds.Last();
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_process.Start();

(2)、进程加入作业对象

这个步骤是用于管理进程,确保《子进程跟随主进程关闭》。

static Job _job = new Job();
_job.AddProcess(_process.Handle);

(3)、获取主窗口句柄

下列提供的是简单获取主窗口句柄的方法。通过延时等待的方式获取。需要精确时间获取主窗口句柄则可以使用钩子,在子进程窗口创建事件中获取句柄。

for (int i = 0; i < 200 && _process.MainWindowHandle == 0; i++) Thread.Sleep(5);
if (_process.MainWindowHandle == 0)
{throw new Exception("process no window");
}
return new HandleRef(this, Handle);

3、销毁进程

protected override void DestroyWindowCore(HandleRef hwnd)
{_process?.Kill();_process?.Dispose();_process = null;
}

二、完整代码

其中Job对象在《子进程跟随主进程关闭》中。
AppHost.cs

using JobManagement;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using Process = System.Diagnostics.Process;
using TextBox = System.Windows.Controls.TextBox;
using Thread = System.Threading.Thread;namespace WpfHwndElement
{/// <summary>/// 需要手动dispose此控件。/// </summary>public class AppHost : HwndHost{static Job _job = new Job();Process? _process;/// <summary>/// 进程运行的命令行/// </summary>public string Cmdline{get { return (string)GetValue(CmdlineProperty); }set { SetValue(CmdlineProperty, value); }}// Using a DependencyProperty as the backing store for Cmdline.  This enables animation, styling, binding, etc...public static readonly DependencyProperty CmdlineProperty =DependencyProperty.Register("Cmdline", typeof(string), typeof(AppHost), new PropertyMetadata(""));new public IntPtr Handle{get { return (IntPtr)GetValue(HandleProperty); }private set { SetValue(HandleProperty, value); }}// Using a DependencyProperty as the backing store for Hwnd.  This enables animation, styling, binding, etc...public static readonly DependencyProperty HandleProperty =DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero));protected override HandleRef BuildWindowCore(HandleRef hwndParent){try{if (DesignerProperties.GetIsInDesignMode(this)) throw new Exception("design mode won't show app");var cmds = Cmdline.Split(" ", 2);_process = new Process();_process.StartInfo.FileName = cmds.First();_process.StartInfo.Arguments = cmds.Length > 1 ? cmds.Last() : "";_process.StartInfo.UseShellExecute = false;_process.StartInfo.CreateNoWindow = true;_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;_process.Start();_job.AddProcess(_process.Handle);for (int i = 0; i < 200 && _process.MainWindowHandle == 0; i++) Thread.Sleep(5);if (_process.MainWindowHandle == 0){throw new Exception("process no window");}Handle = _process.MainWindowHandle;var wndStyle = GetWindowLong(Handle, GWL_STYLE);wndStyle &= ~WS_THICKFRAME;wndStyle &= ~WS_CAPTION;SetWindowLong(Handle, GWL_STYLE, wndStyle | WS_CHILD);SetParent(Handle, hwndParent.Handle);}catch (Exception ex){var window = new Window() { Width = 0, Height = 0, ResizeMode = ResizeMode.NoResize, WindowStyle = WindowStyle.None, Content = new TextBox() { IsReadOnly = true, Text = ex.Message + " " + ex.StackTrace, TextWrapping = TextWrapping.Wrap } };var hwnd = new WindowInteropHelper(window).EnsureHandle();window.Show();SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_CHILD);SetParent(hwnd, hwndParent.Handle);Handle = hwnd;}return new HandleRef(this, Handle);}protected override void DestroyWindowCore(HandleRef hwnd){var window = HwndSource.FromHwnd(hwnd.Handle)?.RootVisual as Window;window?.Close();_process?.Kill();_process?.Dispose();_process = null;}const int WS_CAPTION = 0x00C00000;const int WS_THICKFRAME = 0x00040000;const int WS_CHILD = 0x40000000;const int GWL_STYLE = (-16);[DllImport("user32.dll", EntryPoint = "GetWindowLongW")]static extern int GetWindowLong(IntPtr hwnd, int nIndex);[DllImport("user32.dll", EntryPoint = "SetWindowLongW")]static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);[DllImport("user32.dll")]public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);}
}

三、使用示例

1、嵌入ffplay.exe

MainWindow.xaml

<Window x:Class="WpfHwndElement.MainWindow"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:local="clr-namespace:WpfHwndElement"mc:Ignorable="d"Title="MainWindow" Height="360" Width="640"       ><Grid><local:AppHost Cmdline="ffplay" Width="200" Height="200"></local:AppHost></Grid>
</Window>

效果预览
在这里插入图片描述


总结

以上就是今天要讲的内容,嵌入外部程序还是相对比较容易实现的,而且也有一定的使用场景。创建进程,并能获取到进程的主窗口句柄即可。另外要注意的是管理子进程的退出,其他都问题不大。

http://www.yayakq.cn/news/727372/

相关文章:

  • 保险网站建设平台商城网站开发企业
  • 哪里有做网站服务商淇县网站建设
  • 搭建一个网站教程毕业设计网站开发的中期报告
  • 二手网站建设论文答辩wordpress图片编辑插件
  • 建网站 域名北京顺义做网站
  • 怎么更改网站域名wordpress自动采集发布插件
  • 西安建设门户网站学院网站建设目标
  • 购物网站开发实例wordpress新建数据库
  • 免费网站建站+凡科建站东营大众网
  • wordpress建网站详细教程最好的搜索引擎
  • 长沙企业建站按效果付费达州建网站
  • 品牌网站建设专业定制汉口网站建设制作
  • 阿里云服务器做网站django素材网站建设需要多少费用
  • 广东专业网站建设龙岩人才网
  • 住房和城乡建设局网站职能演出备案在哪里查询
  • 东莞市公司网站建设服务机构做唯品客网站的感想
  • 东莞企业营销型网站广东建设集团
  • 微页制作网站模板免费下载青田网站建设
  • 自己做的网站可以有多个前端吗网站由什么组成
  • 网站名称推荐天津企业模板建站哪个好
  • 佛山出格建站深圳南山区住房和建设局网站
  • 购销网站建设视频百度云阿里+wordpress
  • 定制网站报价网站登录接口怎么做
  • 个人网站开发人员模仿茶叶的网站制作
  • 如何创建一个属于自己的网站郑州快速排名优化网站
  • 做网站绿色和什么颜色搭配4a网站建设公司
  • 免费网站在线观看人数在哪买邯山企业做网站推广
  • 企业免费网站制作比较好的一个网站3个相似域名
  • 免费网站导航建设小规模纳税人企业所得税怎么征收
  • 2018年网站优化怎么做网站制作素材