05-08-13, 04:38 PM
(آخر تعديل لهذه المشاركة : 06-08-13, 06:58 PM {2} بواسطة mohamed samy.)
سوف اشرح اليوم كيفية عمل AddIn لبرنامجك و جعله يقبل اى AddIn يصنعها المطورون (C# - VB.NET)
قم باعداد ثلاث مشاريع كما فى الصور و بعد ذلك سوف نكتب الأكواد. أتبع الصور
سنبدأ الأن بكتابة الأكواد
فى مشروع AddInManager الكلاس AddIn
VB:
PHP كود :
Public Interface AddIn
'اذا كنت تريد كتابة اى كود يستدعى فى ال Sub New
Sub OnCreateAddIn()
'اذا كان برنامجك يحتوى على
'MenuStrip
'وتريد اضافة عنصر له
Sub OnCreateMenuStrip(menuStrip As Object)
'اذا كان برنامجك يحتوى على
'ToolStrip
'وتريد اضافة عنصر له
Sub OnCreateToolStrip(toolStrip As Object)
'الفورم الرئيسى فى برنامجك
Sub OnCreateMainForm(form As Object)
End Interface
C#:
PHP كود :
public interface AddIn
{
//اذا كنت تريد كتابة اى كود يستدعى فى ال Sub New
void OnCreateAddIn();
//اذا كان برنامجك يحتوى على
//MenuStrip
//وتريد اضافة عنصر له
void OnCreateMenuStrip(object menuStrip);
//اذا كان برنامجك يحتوى على
//ToolStrip
//وتريد اضافة عنصر له
void OnCreateToolStrip(object toolStrip);
//الفورم الرئيسى فى برنامجك
void OnCreateMainForm(object form);
}
عندما نصنع الأضافة سوف نعمل Implements من هذا ال Interface. لماذا؟
حتى تكون جميع ال AddIns التى ستصنع لبرنامجك بهذا الشكل او النظام حتى يستطيع برنامجك قراءة ال AddIn
فى المشروع AddInManager فى الكلاس AddInManager
VB:
PHP كود :
Imports System.Reflection
Public Class AddInManager
Public Shared Sub LoadAddIn(path As String, ParamArray args As Object())
Dim AddIn As [Assembly] = [Assembly].LoadFrom(path)
Dim t As Type = AddIn.GetType(AddIn.GetName.Name & ".Main")
InvokeMethod(t, "OnCreateAddIn", Nothing)
InvokeMethod(t, "OnCreateMainForm", {args(0)})
InvokeMethod(t, "OnCreateMenuStrip", {args(1)})
InvokeMethod(t, "OnCreateToolStrip", {args(2)})
End Sub
Private Shared Sub InvokeMethod(t As Type, name As String, args() As Object)
Dim mtd As MethodInfo = t.GetMethod(name)
Dim classInstance As Object = Activator.CreateInstance(t, True)
mtd.Invoke(classInstance, args)
End Sub
End Class
C#:
PHP كود :
using System.Reflection;
public class AddInManager
{
public static void LoadAddIn(string path, params object[] args)
{
Assembly AddIn = Assembly.LoadFrom(path);
Type t = AddIn.GetType(AddIn.GetName.Name + ".Main");
InvokeMethod(t, "OnCreateAddIn", null);
InvokeMethod(t, "OnCreateMainForm", new object[] { args(0) });
InvokeMethod(t, "OnCreateMenuStrip", new object[] { args(1) });
InvokeMethod(t, "OnCreateToolStrip", new object[] { args(2) });
}
private static void InvokeMethod(Type t, string name, object[] args)
{
MethodInfo mtd = t.GetMethod(name);
object classInstance = Activator.CreateInstance(t, true);
mtd.Invoke(classInstance, args);
}
}
فى المشروع CalculatorAddIn فى الكلاس Main
VB:
PHP كود :
Imports AddInManager
Imports System.Windows.Forms
Imports System.Drawing
Public Class Main
Implements AddIn
Public Sub New()
OnCreateAddIn()
End Sub
Public Sub OnCreateAddIn() Implements AddIn.OnCreateAddIn
Button1 = New Button
Label1 = New Label
ComboBox1 = New ComboBox
TextBox1 = New TextBox
TextBox2 = New TextBox
TextBox3 = New TextBox
End Sub
Public Sub OnCreateMainForm(form As Object) Implements AddIn.OnCreateMainForm
'
'Button1
'
Me.Button1.Location = New Point(254, 56)
Me.Button1.Name = "Button1"
Me.Button1.Size = New Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Calculate"
Me.Button1.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New Point(208, 33)
Me.Label1.Name = "Label1"
Me.Label1.Size = New Size(15, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = "="
'
'TextBox1
'
Me.TextBox1.Location = New Point(12, 30)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New Size(65, 20)
Me.TextBox1.TabIndex = 3
'
'TextBox2
'
Me.TextBox2.Location = New Point(137, 30)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New Size(65, 20)
Me.TextBox2.TabIndex = 4
'
'TextBox3
'
Me.TextBox3.Location = New Point(229, 30)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New Size(100, 20)
Me.TextBox3.TabIndex = 5
'
'ComboBox1
'
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Items.AddRange(New Object() {"+", "-", "*", "/"})
Me.ComboBox1.Location = New Point(83, 30)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New Size(48, 21)
Me.ComboBox1.TabIndex = 6
'
'Form
'
form.Controls.Add(Me.ComboBox1)
form.Controls.Add(Me.TextBox3)
form.Controls.Add(Me.TextBox2)
form.Controls.Add(Me.TextBox1)
form.Controls.Add(Me.Label1)
form.Controls.Add(Me.Button1)
'
'Event Handles
'
AddHandler Button1.Click, AddressOf Button1_Click
End Sub
Public Sub OnCreateMenuStrip(menuStrip As Object) Implements AddIn.OnCreateMenuStrip
'
'Don't do any thing
'
End Sub
Public Sub OnCreateToolStrip(toolStrip As Object) Implements AddIn.OnCreateToolStrip
'
'Don't do any thing
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Select Case ComboBox1.SelectedItem
Case "+"
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Case "-"
TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text)
Case "*"
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
Case "/"
TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text)
End Select
End Sub
Private Button1 As Button
Private Label1 As Label
Private TextBox1 As TextBox
Private TextBox2 As TextBox
Private TextBox3 As TextBox
Private ComboBox1 As ComboBox
End Class
C#:
PHP كود :
using AddInManager;
using System.Windows.Forms;
using System.Drawing;
public class Main : AddIn
{
public Main()
{
OnCreateAddIn();
}
public void OnCreateAddIn()
{
Button1 = new Button();
Label1 = new Label();
ComboBox1 = new ComboBox();
TextBox1 = new TextBox();
TextBox2 = new TextBox();
TextBox3 = new TextBox();
}
public void OnCreateMainForm(object form)
{
//
//Button1
//
this.Button1.Location = new Point(254, 56);
this.Button1.Name = "Button1";
this.Button1.Size = new Size(75, 23);
this.Button1.TabIndex = 0;
this.Button1.Text = "Calculate";
this.Button1.UseVisualStyleBackColor = true;
//
//Label1
//
this.Label1.AutoSize = true;
this.Label1.Location = new Point(208, 33);
this.Label1.Name = "Label1";
this.Label1.Size = new Size(15, 13);
this.Label1.TabIndex = 2;
this.Label1.Text = "=";
//
//TextBox1
//
this.TextBox1.Location = new Point(12, 30);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new Size(65, 20);
this.TextBox1.TabIndex = 3;
//
//TextBox2
//
this.TextBox2.Location = new Point(137, 30);
this.TextBox2.Name = "TextBox2";
this.TextBox2.Size = new Size(65, 20);
this.TextBox2.TabIndex = 4;
//
//TextBox3
//
this.TextBox3.Location = new Point(229, 30);
this.TextBox3.Name = "TextBox3";
this.TextBox3.Size = new Size(100, 20);
this.TextBox3.TabIndex = 5;
//
//ComboBox1
//
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox1.FormattingEnabled = true;
this.ComboBox1.Items.AddRange(new object[] {
"+",
"-",
"*",
"/"
});
this.ComboBox1.Location = new Point(83, 30);
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new Size(48, 21);
this.ComboBox1.TabIndex = 6;
//
//Form
//
form.Controls.Add(this.ComboBox1);
form.Controls.Add(this.TextBox3);
form.Controls.Add(this.TextBox2);
form.Controls.Add(this.TextBox1);
form.Controls.Add(this.Label1);
form.Controls.Add(this.Button1);
//
//Event Handles
//
Button1.Click += Button1_Click;
}
public void OnCreateMenuStrip(object menuStrip)
{
//
//Don't do any thing
//
}
public void OnCreateToolStrip(object toolStrip)
{
//
//Don't do any thing
//
}
private void Button1_Click(object sender, EventArgs e)
{
switch (ComboBox1.SelectedItem) {
case "+":
TextBox3.Text = Conversion.Val(TextBox1.Text) + Conversion.Val(TextBox2.Text);
break;
case "-":
TextBox3.Text = Conversion.Val(TextBox1.Text) - Conversion.Val(TextBox2.Text);
break;
case "*":
TextBox3.Text = Conversion.Val(TextBox1.Text) * Conversion.Val(TextBox2.Text);
break;
case "/":
TextBox3.Text = Conversion.Val(TextBox1.Text) / Conversion.Val(TextBox2.Text);
break;
}
}
private Button Button1;
private Label Label1;
private TextBox TextBox1;
private TextBox TextBox2;
private TextBox TextBox3;
private ComboBox ComboBox1;
}
والأن سوف نجرب البرنامج
ملاحظات :
1-يجب ان يكون الكلاس الرئيسى فى ال AddIn اسمه Main
يمكنك تغيرة ولكن يجب تغير اسمه فى هذا السطر
PHP كود :
Dim t As Type = AddIn.GetType(AddIn.GetName.Name & ".الأسم")
2-يمكنك تعديل الكلاس AddIn حتى يتوافق مع برنامجك اكثر
3- اذا كنت تريد ان يصنع الموطورين AddIns لبرنامجك فعلك اعطئهم ملف AddInManager حتى يستطيعوا عمل Implements من ال AddIn
4- لم اقم بتوضح هذا السطر
PHP كود :
LoadAddIn(Application.StartupPath & "/CalculatorAddIn.dll", {Me, Nothing, Nothing})
بالنسبة لل args ان تكون كالتالى
{MainForm, MenuStrip, ToolStrip}
يمكنك تعديل الكود كما تشاء ليناسبك مثل اضافة كلاس اسمه LoadArgs
وتضع فيه ال args التى تريد
واى استفسار انا موجود دائما لخدمتكم
المثال