الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

استخدام تحكمات Windows Forms من داخل تطبيق Wpf

بدأه محمد سامر أبو سلو في 29 فبراير 2008 · 0 رد · 373 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1

يمكن استخدام تحكمات Windows Forms من داخل تطبيق WPF من خلال استخدام الفئة WindowsFormsHost Class والتي يكون تعريفها ضمن Visual Basic كالتالي

<ContentPropertyAttribute("Child")> _
Public Class WindowsFormsHost _
	Inherits HwndHost _
	Implements IKeyboardInputSink

واستخدامها

Dim instance As WindowsFormsHost

وهي موجودة ضمن مجال الأسماء System.Windows.Forms.Integration وفي المجمع WindowsFormsIntegration وفي المكتبة WindowsFormsIntegration.dll حيث يبين لنا المثال التالي كيفية استخدام العنصر WindowsFormsHost لاستضافة تحكمات Windows Forms

<Window x:Class="HostingWfInWpf.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
	Title="HostingWfInWpf"
	>
	<Grid>
		<WindowsFormsHost>
			<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
		</WindowsFormsHost>
	</Grid>
</Window>

<Window x:Class="Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
	Title="HostingWfInWpf"
	>
  <Grid>
	<WindowsFormsHost>
	  <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
	</WindowsFormsHost>
  </Grid>
</Window>

ولاستضافة تحكم Windows forms من ضمن تطبيق WPF يجب عليك تحديد تحكم Windows Forms إلى الخاصية Child وذلك باستخدام الخاصية PropertyMap لتحديد الربط بين العنصر WindowsFormsHost و تحكم Windows Forms المستضاف ضمنه

أنشئ تطبيقا من النوع WPF Application و سمه HostingWfInWpf وفي Solution Explorer أضف مرجعا للمجمع WindowsFormsIntegration والمسمى WindowsFormsIntegration.dll ثم قم بفتح Window1.xaml واستبدل كود xaml المولد تلقائيا بالكود التالي

<Window x:Class="HostingWfInWpf.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="HostingWfInWpf"
	Loaded="WindowLoaded"
	>
	<Grid Name="grid1">

	</Grid>
</Window>

<Window x:Class="Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="HostingWfInWpf" Height="300" Width="300"
	Loaded="WindowLoaded"
	>
  <Grid Name="grid1">

	</Grid>
</Window>

افتح الملف Window1.xaml.vb في محرر الكود وقم باستبدال جميع الكود الموجود بالكود التالي

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes

Imports System.Windows.Forms

' Interaction logic for Window1.xaml
Partial Public Class Window1
	Inherits Window

	Public Sub New()
		InitializeComponent()
	End Sub

	Private Sub WindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
		' Create the interop host control.
		Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()

		' Create the MaskedTextBox control.
		Dim mtbDate As New MaskedTextBox("00/00/0000")

		' Assign the MaskedTextBox control as the host control's child.
		host.Child = mtbDate

		' Add the interop host control to the Grid
		' control's collection of child controls.
		Me.grid1.Children.Add(host)

	End Sub 'WindowLoaded

End Class

اضغط F5 لاختبار المشروع

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

مواضيع مشابهة