前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >wpf怎么使用WindowsFormsHost

wpf怎么使用WindowsFormsHost

作者头像
zls365
发布2020-12-15 15:10:15
发布2020-12-15 15:10:15
1.9K00
代码可运行
举报
文章被收录于专栏:CSharp编程大全CSharp编程大全
运行总次数:0
代码可运行

使用方法:

  1、首先,我们需要向项目中的引用(baireference)中添加两个动态库dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration;

  2、添加完两个动态dll以后,就可以在控件库中找到WindowsFormsHost这个控件;

  3、将这个控件放入窗体,放置完以后在xmal代码中会自动生成相应代码:

代码语言:javascript
代码运行次数:0
运行
复制
 <Grid>
  <WindowsFormsHost Height="100" HorizontalAlignment="Left" Margin="36,29,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200" />
  </Grid>

  4、然后,需要在xmal的开始处添加两行代码 :

代码语言:javascript
代码运行次数:0
运行
复制
  xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

  5、这样就可以在WindowsFormsHost下放置需要的Windows Form控件了。

代码语言:javascript
代码运行次数:0
运行
复制
 <WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286" >
  <WinFormControls:Button Text="WinformButton" Width="150"/>
  </WindowsFormsHost>

实例:

代码语言:javascript
代码运行次数:0
运行
复制
<Window x:Class="thzSoftware.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:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:local="clr-namespace:thzSoftware"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized" >

    <Grid ShowGridLines="True" >
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <wfi:WindowsFormsHost Grid.Row="0" Grid.Column="1">
            <wf:PictureBox x:Name="thz1" />
        </wfi:WindowsFormsHost>
        <wfi:WindowsFormsHost Grid.Row="1" Grid.Column="1">
            <wf:PictureBox x:Name="thz2" />
        </wfi:WindowsFormsHost>
        <StackPanel Grid.Row="0" Grid.Column="0">
            <Label Name="labelCamera1Status" Content="摄像头连接状态" HorizontalContentAlignment="Center"/>
            <Image Name="Cam1"  Source="C:\Users\admin\source\repos\thzSoftware\thzSoftware\bin\Debug\Cam1.jpg"/>
        </StackPanel>
        <StackPanel Grid.Row="1" Grid.Column="0">
            <Label Name="labelCamera2Status"  Content="摄像头连接状态" HorizontalContentAlignment="Center"/>
            <Image Name="Cam2"  Source="C:\Users\admin\source\repos\thzSoftware\thzSoftware\bin\Debug\Cam2.jpg"/>
        </StackPanel>
        
    </Grid>
</Window>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档