摘要:本篇windows运维将讲解WPF窗体进行交互的知识点,看完这篇文章会让你对窗体交互的知识点有更加清晰的理解和运用。
本篇windows运维将讲解WPF窗体进行交互的知识点,看完这篇文章会让你对窗体交互的知识点有更加清晰的理解和运用。
混合窗口和窗体
1.显示模态窗口和窗体
从WPF应用程序中显示模态窗体很简单,如WPF中:
Form1 frm=new Form1();if(frm.ShowDialog()==System.Windows.Form.DialogResult.OK)
{
MessageBox.Show("You clicked OK in a Windows Forms from.");
}
在windows窗体应用程序显示WPF的方法:
Window1 win=new Window1;if(Win.ShowDailog()==ture)
{
MessageBox.Show("You clicked OK in a WPF window.");
}
1.显示非模态窗口和窗体
在Windows窗体中非模态显示WPF,需如下:
引入WindowsFormsIntegration.dll程序集
Window1 win=new Window();ElementHost.EnableModelessKeyboardInterop(win);win.show();
在WPF应用程序中显示非模态的Windows窗体,需:
只需要在显示任何窗体之前调用(一般启动时)调用
WindowsFormsHost.EnableWindowsFormsInterop();
Form1 frm=new Form1();
frm.Show();
PS:一个问题:在WPF中显示Form窗体时,Form会使用旧的XP的控件风格,如想支持新WPF风格,如下
解决办法:只需在显示所有Windows窗体内容之前调用EnableVisualStyle()方法。
public partial class App:System.Windows.Appliation{
protected override void OnStartup(StartupEventArgs e)
{
//Raises the Startup event.
base.OnStartup(e);
System.Windows.Forms.Application.EnableVisualStyles();
}
}
创建具有混合内容的窗口
1.WPF和Windows窗体“空域”--不允许的情况
“空域规则”:指示WPF和Windows窗体必须总是使用它们自己的不同窗口区域,即它们专门管理的区域
具体就是:如果在WPF内容之上放置Windows窗体内容,将发现Windows窗体内容总在上面。
在Windows窗体之上放置WPF内容,根据Z索引显示。ps:Windows窗体的每个控件为单独窗口,WPF为一个根窗口。
1.在WPF中驻留Windows窗体控件
代码如下:
<Window x:Class="InteroperabilityWPF.HostWinFormControl"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="HostWinFormControl" Height="300" Width="300"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<WindowsFormsHost Margin="10" Name="host">
<wf:MaskedTextBox x:Name="maskedTextBox" Mask="(999)-000-0000" MaskInputRejected="maskedTextBox_MaskInputRejected" ValidatingType="{x:Type sys:Int32}"></wf:MaskedTextBox>
</WindowsFormsHost>
<Label Margin="5" Name="lblErrorText" Grid.Row="1"></Label>
</Grid></Window>
使用WPF和Windows窗体用户控件
在Windows窗体中驻留WPF控件
Win32互操作
ps:可以使用System.Windows.Interop.HwndHost类在WPF中驻留Win32.WindowsFormsHost继承于HwndHost
HwndSource是HwndHost的补充,它可将任何WPF可视化对象或元素封装到hwnd中。同时HwndHost.RootVisual属性设置为顶级元素,然后将HwndSource放置到窗口中。
希望这篇文章可以帮助到你。总之,同学们,你想要的职坐标IT频道都能找到!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号