zoukankan      html  css  js  c++  java
  • WPF与Win32的交互(2)

    为WPF添加Windows窗体控件

    1、通过XAML实现 

    1)添加WindowsFormIntegration.dll引用

         引用命名空间 xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

    2)通过XAML来实现 

        <Grid x:Name="myGrid">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <WindowsFormsHost>
                <wf:Button Text="WinForm Button" Click="Button_Click"></wf:Button>
            </WindowsFormsHost>
            <WindowsFormsHost Grid.Row="1">
                <wf:CheckBox Text="WinForm CheckBox"></wf:CheckBox>
            </WindowsFormsHost>
        </Grid>
    
     
            void button_Click(object sender, EventArgs e)
            {
                MessageBox.Show("this is the first windows form Button");
            }
    

     2.通过Code来实现

    1)使用命名空间

    using WindowsForms = System.Windows.Forms;
    using System.Windows.Forms.Integration;

    2)Code代码

     public partial class WPFWithWindowFormControl : Window
        {
            public WPFWithWindowFormControl()
            {
                this.Loaded += new RoutedEventHandler(WPFWithWindowFormControl_Loaded);
                InitializeComponent();
            }
    
            void WPFWithWindowFormControl_Loaded(object sender, RoutedEventArgs e)
            {
                WindowsFormsHost host = new WindowsFormsHost();
                WindowsForms.Button button = new System.Windows.Forms.Button();
                button.Text = "Button1";
                button.BackColor = System.Drawing.Color.LightBlue;
                button.Width = 100;
                button.Height = 50;
                button.Click += new EventHandler(button_Click);
    
                host.Child = button;
                host.VerticalAlignment = VerticalAlignment.Top;
                host.HorizontalAlignment = HorizontalAlignment.Left;
    
                WindowsFormsHost host2 = new WindowsFormsHost();
                WindowsForms.Button button2 = new System.Windows.Forms.Button();
                button2.Text = "Windows Form Button2";
                button2.BackColor = System.Drawing.Color.Red;
                button2.Width = 100;
                button2.Height = 50;
                
                host2.Child = button2;
                host2.Height = 50;
                host2.Width = 300;
                host2.VerticalAlignment = VerticalAlignment.Bottom;
                host2.HorizontalAlignment = HorizontalAlignment.Right;
    
                myGrid.Children.Add(host);
                myGrid.Children.Add(host2);
               
               
            }
    
            void button_Click(object sender, EventArgs e)
            {
                MessageBox.Show("this is the first windows form Button");
            }
    
        
        }
    

    前台代码:

        <Grid x:Name="myGrid"> 
        </Grid> 

  • 相关阅读:
    技术人员的找工之路(1
    技术人员的找工之路(3)
    Endian的由来
    android平台开发笔记1Spinner不能在sub activity中使用
    谈谈Groupon的成功
    线程安全的同步读写类的模板设计
    项目管理文件package.json
    10个每个开发人员都应该知道的强大JavaScript 解构技术
    绿色下载站上线了(MVC +Telerik开源控件)
    我开发的新浪微博应用“微词典”通过审核,欢迎朋友们试用,多多建议!
  • 原文地址:https://www.cnblogs.com/linlf03/p/2171941.html
Copyright © 2011-2022 走看看