Building ActiveX Controls מרצה : דורון אמיר MCSD Doron Amir

Preview:

Citation preview

Building ActiveX ControlsBuilding ActiveX Controls

מרצה : דורון אמירמרצה : דורון אמיר

MCSD Doron MCSD Doron

AmirAmir www.doronamir.com

Building ActiveX ControlsBuilding ActiveX Controls

•An ActiveX Control

•Exposing Properties,Methods,Events

•Testing a control

•Creating Properties pages

•Creating a data bound control

•Creating a data source control

Written by: MCSD Doron Amir

ActiveX ControlsActiveX Controls

ActiveX Controls

שוניםVBיישומי

WEBעבור דפי

IEוצפייה ע"י

Officeמסמכי

Written by: MCSD Doron Amir

אובייקט שניתן לשיבוץ בטופס או

בפקד מכולה

ControlsControls

אובייקט לשימוש חוזר המכיל קוד וממשק וויזואלי•

מקל על המפתח Controlsשימוש ב •

ToolBox יש פקדים מוכנים ב VBב •

ToolBoxניתן ליצור פקד ולהוסיפו לתיבת הכלים •

•ActiveX Controlsחייבים מכולה

Written by: MCSD Doron Amir

Control ClassesControl Classes

כאשר יוצרים פקד אנו יוצרים מחלקה עבור אותו פקד

OCXמחלקה ליצירת פקדים מיוצגת בקובץ

design-time instanceכאשר מציבים פקד על הטופס נוצר

run-time instanceכאשר מריצים את היישום נוצר

Control.ocxDesign – time

control

run – time

control

Form1

Written by: MCSD Doron Amir

OCX FileOCX File

Source Code

Property Values

Control Class

.ctlTextFile

Graphical elements .ctx

Like .frx file for forms

Control.OCX

Control Class

(ctlמכיל קובץ טקסט עבור הקוד וערכי המאפיינים )

עבור אלמנטים גרפייםctx וקובץ

Written by: MCSD Doron Amir

Written by: MCSD Doron Amir

OCX FileOCX File

C1.ctl

C2.ctl

C3.ctl

MyControl.OCX

ctl ניתן להשתמש בכמה קבצי ActiveX Controlביצירת

אחדOCXאך לאחר ההידור כל הקבצים יתאחדו לקובץ

The UserControl ObjectThe UserControl Object

Written by: MCSD Doron Amir

UserControl מכיל קוד + ממשק עיצוב

ממשק עיצוב

Code

Add Add constituentconstituent controls to a controls to a UserControlUserControl

Written by: MCSD Doron Amir

ToolBox יכול להכיל פקדים רגילים מה- usercontrolפקד ה

TimerTimer

Using Ambient PropertiesUsing Ambient Properties

Written by: MCSD Doron Amir

מספקים רשימת מאפיינים אשר Containersה - • שנמצא ActiveXלהשפיע ישירות על פקד ה יכולים

. בו

• BackColor הוא מאפיין Ambient .

•AmbientChanged אירוע זה מופעל כאשר יש שינוי Ambientבמאפיין מסוג

•Ambient Object מאפשר לקרוא ערך מאפיין AmbientPrivate Sub UserControl_AmbientChanged)PropertyName As String(

If PropertyName = "BackColor“ Then

UserControl.BackColor = Ambient.BackColor

End If

End Sub ישתנה גם רקע הפקד ישתנהContainerכאשר צבע הרקע של ה

Ambient PropertiesAmbient Properties

Written by: MCSD Doron Amir

רקע הפקד משנה צבע

כאשר משנים את צבע הרקע של

הטופס

רקע הפקד אינו משנה

צבע

כאשר משנים את צבע הרקע של

הטופס

UserControl_AmbientChanged

UserControl_ResizeUserControl_Resize)()(

Written by: MCSD Doron Amir

Private Sub UserControl_Resize)(

Text1.Width = )UserControl.ScaleWidth - 240(

End Sub

האירוע מופעל בעת שינוי גודל הפקד

הקוד הנ"ל מאפשר לפקד הטקסט לשמור על פרופורציה בהתאם לרוחב מסגרת הפקד

הפרש

Add About BoxAdd About Box

Private Sub Command1_Click)(

frmAbout.Show vbModal

Unload frmAbout

Set frmAbout = Nothing

End Sub

הוספת דף מידע עבור הפקד

Written by: MCSD Doron Amir

ToolBox BitmapToolBox Bitmap

bitmapבחירת תמונה עבור הפקד :

חיפוש התמונה

Written by: MCSD Doron Amir

The ActiveX Control Interface WizardThe ActiveX Control Interface Wizard

Written by: MCSD Doron Amir

ActiveX Controlאשף העוזר לבנות •

הגדרת מאפיינים•

שיטות •

אירועים•

The ActiveX Control Interface WizardThe ActiveX Control Interface Wizard

Written by: MCSD Doron Amir

Create ControlCreate Control

Written by: MCSD Doron Amir

Private Sub Command1_Click()

Text1.Text = "": Text2.Text = "": Text3.Text = ""

End Sub

תיבות טקסט3המטרה : צור פקד המכיל

ופקד ניקוי עבור התיבות טקסט

בעזרת האשף נגדיר מאפיינים שיאפשרו לגשת אל כל רכיבי הפקד בנפרד

תרגיל לבניית מאפייניםתרגיל לבניית מאפיינים

Private Sub Command1_Click()

UserControl11.myColor1 = vbRed

UserControl11.myColor2 = vbBlue

UserControl11.myColor3 = vbGreen

End Sub

עבור כל רכיב myColorיצירת מאפיין בשם בנפרד

Select Available PropertySelect Available Property

Written by: MCSD Doron Amir

My Custom MembersMy Custom Members

Written by: MCSD Doron Amir

בחר שמות למאפיינים

ושיטות הפקד

התאמה בין המאפיינים החדשים לרכיבי התאמה בין המאפיינים החדשים לרכיבי הפקדהפקד

Written by: MCSD Doron Amir

הרכיב

המאפייןכינוי

תרגיל לבניית שיטותתרגיל לבניית שיטות

stopTimer Method

startTimer Method

Private Sub Command1_Click()

ctlClook1.StopTimer

End Sub

Private Sub Command3_Click()

ctlClook1.StartTimer

End Sub

My Custom MembersMy Custom Members

Written by: MCSD Doron Amir

User Control SubUser Control Sub

Written by: MCSD Doron Amir

Public Sub StartTimer)(

Timer1.Enabled = True

End Sub

Public Sub StopTimer)(

Timer1.Enabled = False

End Sub

האשף הכין את השגרות

יש לקודד את הפקודות הרצויות

Mapping a Property to Multiple ControlMapping a Property to Multiple Control

Written by: MCSD Doron Amir

יצירת מאפיין צבע עבור

מספר פקדים לא ידוע

שימוש בלולאת

For Each…Next

Mapping a Property to Multiple ControlMapping a Property to Multiple Control

Written by: MCSD Doron Amir

Public Property Let MyColor)ByVal vNewValue As OLE_COLOR(

Dim ctl As Object

For Each ctl In Controls

If )TypeOf ctl Is Label( Then

ctl.BackColor = vNewValue

End If

Next

End Property

Mapping a Property to Multiple ControlMapping a Property to Multiple Control

Written by: MCSD Doron Amir

Private Sub Command1_Click)(

UserControl11.MyColor = RGB)CInt)Text1(, CInt)Text2(, CInt)Text3((

End Sub

RGBהמרה ל

Available in the Available in the PropertiesProperties window window

Written by: MCSD Doron Amir

יצירת מאפיין בחלון המאפיינים עבור

ActiveX Controlפקד ה

Available in the Available in the PropertiesProperties window window

Written by: MCSD Doron Amir

Public Property Get MyCaption)( As String

MyCaption = Label1.Caption

End Property

Public Property Let MyCaption)ByVal NewCaption As String(

Label1.Caption = NewCaption

PropertyChanged "MyCaption"

End Property

PropertyChanged

רושם את המאפיין בחלון

המאפיינים

GET עבור קריאת = המאפיין

LET עבור עריכת = המאפיין

Storing & Retrieving Property ValuesStoring & Retrieving Property Values

Written by: MCSD Doron Amir

קיבל צבעים שוניםMyTextColorהמאפיין

בעת זמן עיצוב הטופס , ללא רישום+קריאת

המאפיינים הצבעים הנבחרים היו משתחררים

Storing & Retrieving Property ValuesStoring & Retrieving Property Values

Written by: MCSD Doron Amir

Public Property Get MyTextColor)( As OLE_COLOR

MyTextColor = Text1.BackColor

PropertyChanged "MyTextColor"

End Property

Public Property Let MyTextColor)ByVal vNewValue As OLE_COLOR(

Text1.BackColor = vNewValue

PropertyChanged "MyTextColor"

End Property

MyTextColor יצירת מאפיין

Storing & Retrieving Property ValuesStoring & Retrieving Property Values

Written by: MCSD Doron Amir

Private Sub UserControl_WriteProperties)PropBag As PropertyBag(

PropBag.WriteProperty "MyTextColor", Text1.BackColor, &H8000000F

End Sub

Private Sub UserControl_ReadProperties)PropBag As PropertyBag(

Text1.BackColor = PropBag.ReadProperty)"MyTextColor", &H8000000F(

End Sub

MyTextColor רישום : שמירת ערך המאפיין

MyTextColor קריאה : חשיפת ערך המאפיין

RaiseRaise EventEvent

AtTimeיצירת אירוע בשם

קריאה לאירוע

הפעלת שיגרת האירוע

הגדרת האירוע

Raise EventRaise Event

Event AtTime)(

Private Sub Timer1_Timer)(

RaiseEvent AtTime

txtTime.Text = Format)Now, "HH:MM:SS"(

End Sub

קריאה לאירוע בכל כניסה לפרוצדורת הטיימר

Private Sub ctlClock1_AtTimePrivate Sub ctlClock1_AtTime)()(

כניסה אל שגרת הפקד

AtTimeובחירת פרוצדורת

Private Sub ctlClock1_AtTime)(

Print Now

End Sub

Using the Property Page WizardUsing the Property Page Wizard

Create the user interface for the property page.

Add-In : vb6 Property Page WizardAdd-In : vb6 Property Page Wizard

Select the Property PageSelect the Property Page

Select Properties for DisplaySelect Properties for Display

Next..Next..NextNext..Next..Next

Compile For OCXCompile For OCX

עיצוב פקד

מאפייני פרוייקט

Compile For OCXCompile For OCX

Written by: MCSD Doron Amir

File Make MyOCX.ocx

שמירת הקובץ

רישום הפקדרישום הפקד

הידור הפקד הידור הפקד העתק לתיקיה העתק לתיקיהSystem32System32 פרוייקט פרוייקטEXEEXEחדש חדש Components + Brows Components + Brows Apply Apply

Written by: MCSD Doron Amir

ComponentsComponents

Written by: MCSD Doron Amir

System32System32

Written by: MCSD Doron Amir

ApplyApply

Written by: MCSD Doron Amir

Creating a data bound controlCreating a data bound control

Written by: MCSD Doron Amir

SQL server

Creating a Control OCXCreating a Control OCX

Written by: MCSD Doron Amir

txtFname txtLname txtEmpId

Code :Let/Get + Read/WritePublic Property Get EmpId() As String

EmpId = txtEmpId.Text

End Property

Public Property Let EmpId(ByVal New_EmpId As String)

txtEmpId.Text() = New_EmpId

PropertyChanged "EmpId"

End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

……….

txtEmpId.Text = PropBag.ReadProperty("EmpId", "Text3")

End Sub

Make …OCX

עריכת מאפיינים לכל פקד •פנימי

שמירת נתונים•

הידור הפקד•

Read/write

Let/Get

Connecting DataBaseConnecting DataBase

הוספת סביבת נתונים הוספת סביבת נתונים((Data EnvironmentData Environment))( בניית קישור( בניית קישורConnectionConnection)) בחירת ספק בחירת ספק((ProviderProvider ) )בחירתבחירת ((ServerServer)) בחירת מסד נתונים בחירת מסד נתונים((DataBaseDataBase))

Written by: MCSD Doron Amir

Pubsהתחברות למסד הנתונים

((Data EnvironmentData Environment))הוספת סביבת נתונים הוספת סביבת נתונים

Written by: MCSD Doron Amir

1. Right Click

2. Add

3. More ActiveX Designers

4. Data Environment

Written by: MCSD Doron Amir

((ConnectionConnectionבניית קישור )בניית קישור )

( ( ProviderProvider))בחירת ספק בחירת ספק

Written by: MCSD Doron Amir

((DataBaseDataBase)) מסד נתונים מסד נתונים ((ServerServer+ )+ ) בחירתבחירת

Written by: MCSD Doron Amir

Add CommandAdd Command

Written by: MCSD Doron Amir

Right Click

Properties

Edit CommandEdit Command

Written by: MCSD Doron Amir

לקישור Commandהתאמת 1.הנתונים

SQLעריכת משפט בחירה 2.

Microsoft DataRepeater Control Microsoft DataRepeater Control 6.06.0

repeaterהוספת פקד

Written by: MCSD Doron Amir

RepeaterControlNameRepeaterControlNameשיבוץ הפקד שיצרנו

Repeaterבפקד ה-

Written by: MCSD Doron Amir

DataSource & DataMemberDataSource & DataMember

התאמת מסד הנתונים + שליחת השאילתה

Written by: MCSD Doron Amir

MCSD Doron MCSD Doron

AmirAmir www.doronamir.com

Building ActiveX ControlsBuilding ActiveX Controls

Recommended