60
Building ActiveX Building ActiveX Controls Controls רררר : ררררר רררר רררר : ררררר ררררMCSD Doron Amir MCSD Doron Amir www.doronamir.com

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

Embed Size (px)

Citation preview

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

Building ActiveX ControlsBuilding ActiveX Controls

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

MCSD Doron MCSD Doron

AmirAmir www.doronamir.com

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

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

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

ActiveX ControlsActiveX Controls

ActiveX Controls

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

WEBעבור דפי

IEוצפייה ע"י

Officeמסמכי

Written by: MCSD Doron Amir

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

בפקד מכולה

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

ControlsControls

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

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

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

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

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

Written by: MCSD Doron Amir

Page 5: Building ActiveX Controls מרצה : דורון אמיר 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

Page 6: Building ActiveX Controls מרצה : דורון אמיר 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

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

Written by: MCSD Doron Amir

OCX FileOCX File

C1.ctl

C2.ctl

C3.ctl

MyControl.OCX

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

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

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

The UserControl ObjectThe UserControl Object

Written by: MCSD Doron Amir

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

ממשק עיצוב

Code

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

Add Add constituentconstituent controls to a controls to a UserControlUserControl

Written by: MCSD Doron Amir

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

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

TimerTimer

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

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כאשר צבע הרקע של ה

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

Ambient PropertiesAmbient Properties

Written by: MCSD Doron Amir

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

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

הטופס

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

צבע

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

הטופס

UserControl_AmbientChanged

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

UserControl_ResizeUserControl_Resize)()(

Written by: MCSD Doron Amir

Private Sub UserControl_Resize)(

Text1.Width = )UserControl.ScaleWidth - 240(

End Sub

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

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

הפרש

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

Add About BoxAdd About Box

Private Sub Command1_Click)(

frmAbout.Show vbModal

Unload frmAbout

Set frmAbout = Nothing

End Sub

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

Written by: MCSD Doron Amir

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

ToolBox BitmapToolBox Bitmap

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

חיפוש התמונה

Written by: MCSD Doron Amir

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

The ActiveX Control Interface WizardThe ActiveX Control Interface Wizard

Written by: MCSD Doron Amir

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

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

שיטות •

אירועים•

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

The ActiveX Control Interface WizardThe ActiveX Control Interface Wizard

Written by: MCSD Doron Amir

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

Create ControlCreate Control

Written by: MCSD Doron Amir

Private Sub Command1_Click()

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

End Sub

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

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

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

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

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

Private Sub Command1_Click()

UserControl11.myColor1 = vbRed

UserControl11.myColor2 = vbBlue

UserControl11.myColor3 = vbGreen

End Sub

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

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

Select Available PropertySelect Available Property

Written by: MCSD Doron Amir

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

My Custom MembersMy Custom Members

Written by: MCSD Doron Amir

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

ושיטות הפקד

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

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

Written by: MCSD Doron Amir

הרכיב

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

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

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

stopTimer Method

startTimer Method

Private Sub Command1_Click()

ctlClook1.StopTimer

End Sub

Private Sub Command3_Click()

ctlClook1.StartTimer

End Sub

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

My Custom MembersMy Custom Members

Written by: MCSD Doron Amir

Page 25: Building ActiveX Controls מרצה : דורון אמיר 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

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

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

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

Mapping a Property to Multiple ControlMapping a Property to Multiple Control

Written by: MCSD Doron Amir

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

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

שימוש בלולאת

For Each…Next

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

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

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

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המרה ל

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

Available in the Available in the PropertiesProperties window window

Written by: MCSD Doron Amir

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

ActiveX Controlפקד ה

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

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 עבור עריכת = המאפיין

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

Storing & Retrieving Property ValuesStoring & Retrieving Property Values

Written by: MCSD Doron Amir

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

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

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

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

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 יצירת מאפיין

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

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 קריאה : חשיפת ערך המאפיין

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

RaiseRaise EventEvent

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

קריאה לאירוע

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

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

הגדרת האירוע

Raise EventRaise Event

Event AtTime)(

Private Sub Timer1_Timer)(

RaiseEvent AtTime

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

End Sub

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

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

Private Sub ctlClock1_AtTimePrivate Sub ctlClock1_AtTime)()(

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

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

Private Sub ctlClock1_AtTime)(

Print Now

End Sub

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

Using the Property Page WizardUsing the Property Page Wizard

Create the user interface for the property page.

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

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

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

Select the Property PageSelect the Property Page

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

Select Properties for DisplaySelect Properties for Display

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

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

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

Compile For OCXCompile For OCX

עיצוב פקד

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

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

Compile For OCXCompile For OCX

Written by: MCSD Doron Amir

File Make MyOCX.ocx

שמירת הקובץ

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

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

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

Written by: MCSD Doron Amir

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

ComponentsComponents

Written by: MCSD Doron Amir

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

System32System32

Written by: MCSD Doron Amir

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

ApplyApply

Written by: MCSD Doron Amir

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

Creating a data bound controlCreating a data bound control

Written by: MCSD Doron Amir

SQL server

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

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

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

Connecting DataBaseConnecting DataBase

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

Written by: MCSD Doron Amir

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

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

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

Written by: MCSD Doron Amir

1. Right Click

2. Add

3. More ActiveX Designers

4. Data Environment

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

Written by: MCSD Doron Amir

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

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

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

Written by: MCSD Doron Amir

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

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

Written by: MCSD Doron Amir

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

Add CommandAdd Command

Written by: MCSD Doron Amir

Right Click

Properties

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

Edit CommandEdit Command

Written by: MCSD Doron Amir

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

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

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

Microsoft DataRepeater Control Microsoft DataRepeater Control 6.06.0

repeaterהוספת פקד

Written by: MCSD Doron Amir

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

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

Repeaterבפקד ה-

Written by: MCSD Doron Amir

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

DataSource & DataMemberDataSource & DataMember

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

Written by: MCSD Doron Amir

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

MCSD Doron MCSD Doron

AmirAmir www.doronamir.com

Building ActiveX ControlsBuilding ActiveX Controls