برای امتحان باز وارد ویژوال سی 2003 می شوم.
دو پروژه باز می کنم. اولی با نام one_dll از نوع ((کلاس لایبرری)) و دومی به نام user_of_dll از نوع ((ویندوز فرم اپلیکیشن)).
به پروژه اول یک فرم اضافه می کنیم. با نام formi_dar_dll از نوع (( ویندوز فرم دات نت)) .
----------------------------------
تست : دکمه بیلد را در پروژه اول می زنیم.دقیق تر بگویم : از منوی بیلد گزینه بیلد سولوشن را زدم.حال در کنار فایل پروژه one_dll یک پوشه داریم حاوی dll ما با نام one_dll.dll .
در پروژه دوم یعنی user_of_dll یک دکمه ابتدا روی فرم می اندازیم. و در کد آن این خطوط را اضافه می کنیم:
کد:
one_dll::formi_dar_dll *yek_form=new one_dll::formi_dar_dll();
yek_form->Show();
همچنین در پنجره سولوشن اکسپلورر روی رفرنسس کلیک راست کرده و ادد رفرنس را زده و بروس کرده و فایل one_dll را به آن معرفی می کنیم.
پروژه user_of_dll را run می کنیم.
نتیجه : با کلیک روی دکمه فرم داخل dll باز می شود.
این کارها را قبلا هم انجام داده بودیم. حالا می خواهیم به فرم داخل dll چیزهایی بیافزاییم.
==========================================================
در پروژه one_dll وارد فایل formi_dar_dll.h می شویم. همان طور که می بینید namespace آن همان one_dll است و کلاس فرم یعنی :
کد:
ublic __gc class formi_dar_dll : public System::Windows::Forms::Form
کلاس formi_dar_dll در این فایل قرار دارد. می توانیم به این کلاس یک متد اضافه کنیم . متد زیر را به کلاس می افزاییم.
کد:
{
MessageBox ::Show("test!");
}
نکته : باید در قسمت پابلیک پیست شود وگرنه در یوزر dll خواهد گفت که چنین متدی وجود ندارد . و باید بعد از :
کد:
formi_dar_dll(void)
{
InitializeComponent();
}
فکر کنم باشد. قبلا در یک پروژه فکر کنم گفته بود قبل این قسمت نگذارید. حالا کل این فایل(formi_dar_dll.h) چنین است:
کد:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace one_dll
{
/// <summary>
/// Summary for formi_dar_dll
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class formi_dar_dll : public System::Windows::Forms::Form
{
public:
formi_dar_dll(void)
{
InitializeComponent();
}
void examfunc(void)
{
MessageBox ::Show("test!");
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container* components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = new System::ComponentModel::Container();
this->Size = System::Drawing::Size(300,300);
this->Text = S"formi_dar_dll";
}
};
}
حالا بیلد را می زنیم. و بعد استفاده کننده در پروژه دیگر کدش چنین است:
کد:
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
one_dll::formi_dar_dll *yek_form=new one_dll::formi_dar_dll();
yek_form->Show();
yek_form->examfunc();
}
نتیجه اجرا : با زدن دکمه در پروژه استفاده کننده .... هم فرم موجود در dll باز می شود و هم یک مسیج باکس.
=====================================
گام 3 :
در این گام می خواهیم روی فرم موجود در dll هم دکمه ای داشته باشیم.
پس روی فرم مزبور یک دکمه قرار می دهیم و کد زیر را درونش می نویسیم:
کد:
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
MessageBox ::Show("button of dll_form clicked");
}
در اینجا پروژه دوم را تغییری نمی دهیم. فقط اجرایش می کنیم و بعد از اینکه فرم داخل dll ظاهر شد دکمه روی این فرم را می زنیم و می بینیم که یک مسیج باکس ظاهر می شود و رویش همین عبارت button of dll_form clicked نوشته شده.
کل فایل(formi_dar_dll.h) حالا چنین است:
کد:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace one_dll
{
/// <summary>
/// Summary for formi_dar_dll
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class formi_dar_dll : public System::Windows::Forms::Form
{
public:
formi_dar_dll(void)
{
InitializeComponent();
}
void examfunc(void)
{
MessageBox ::Show("test!");
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container* components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(104, 128);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// formi_dar_dll
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button1);
this->Name = S"formi_dar_dll";
this->Text = S"formi_dar_dll";
this->ResumeLayout(false);
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
MessageBox ::Show("button of dll_form clicked");
}
};
}
توجه داشته باشید که در اینجا نیاز نبود که به رفرنس های پروژه dll ئی چیزی بیافزاییم یا به یوزینگ های آن چیزی بیافزاییم چون فرم خودش تمام اشکال نمایشی را ساپورت می کند. در پروژه های قبلی که این کار را کردیم در یک پروژه dll ئی بود که اولش فرم نداشت و به این خاطر باید اعلام می کردیم که می خواهیم از سیستم.ویندوز .فرمز استفاده کنیم.