ɎȿȾȿɊȺɅɖɇɈȿ ȺȽȿɇɌɋɌȼɈ ɉɈ ɈȻɊȺɁɈȼȺɇɂɘ ȽɈɋɍȾȺɊɋɌȼȿɇɇɈȿ ɈȻɊȺɁɈȼȺɌȿɅɖɇɈȿ ɍɑɊȿɀȾȿɇɂȿ ȼɕɋɒȿȽɈ ɉɊɈɎȿɋɋɂɈɇȺɅɖɇɈȽɈ ɈȻɊȺɁɈȼȺɇɂə «ȼɈɊɈɇȿɀɋɄɂɃ ȽɈɋɍȾȺɊɋɌȼȿɇɇɕɃ ɍɇɂȼȿɊɋɂɌȿɌ»
ɊȺɁɊȺȻɈɌɄȺ ɄɈɆɉɈɇȿɇɌɈȼ ȼ DELPHI ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ ɋɨɫɬɚɜɢɬɟɥɢ: Ƚ.ɗ. ȼɨɳɢɧɫɤɚɹ, ȼ.Ƚ. Ɋɭɞɚɥɟɜ, Ɇ.Ⱥ. Ⱥɪɬɟɦɨɜ
ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ 2007
ɍɬɜɟɪɠɞɟɧɨ ɧɚɭɱɧɨ-ɦɟɬɨɞɢɱɟɫɤɢɦ ɫɨɜɟɬɨɦ ɮɚɤɭɥɶɬɟɬɚ ɩɪɢɤɥɚɞɧɨɣ ɦɚɬɟɦɚɬɢɤɢ, ɢɧɮɨɪɦɚɬɢɤɢ ɢ ɦɟɯɚɧɢɤɢ 14 ɦɚɹ 2007 ɝ., ɩɪɨɬɨɤɨɥ ʋ 9
Ɋɟɰɟɧɡɟɧɬ ɡɚɜ. ɤɚɮ. ɉɢɂɌ ɮ-ɬɚ ɎɄɇ ȼȽɍ
ɇ.Ⱥ. Ɍɸɤɚɱɟɜ
ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɩɨɞɝɨɬɨɜɥɟɧɨ ɧɚ ɤɚɮɟɞɪɟ ɩɪɨɝɪɚɦɦɧɨɝɨ ɨɛɟɫɩɟɱɟɧɢɹ ɢ ɚɞɦɢɧɢɫɬɪɢɪɨɜɚɧɢɹ ɢɧɮɨɪɦɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ ɮɚɤɭɥɶɬɟɬɚ ɩɪɢɤɥɚɞɧɨɣ ɦɚɬɟɦɚɬɢɤɢ, ɢɧɮɨɪɦɚɬɢɤɢ ɢ ɦɟɯɚɧɢɤɢ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. Ɋɟɤɨɦɟɧɞɭɟɬɫɹ ɞɥɹ ɫɬɭɞɟɧɬɨɜ ɮɚɤɭɥɶɬɟɬɚ ɩɪɢɤɥɚɞɧɨɣ ɦɚɬɟɦɚɬɢɤɢ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ.
Ⱦɥɹ ɫɩɟɰɢɚɥɶɧɨɫɬɢ: 351500 – Ɇɚɬɟɦɚɬɢɱɟɫɤɨɟ ɨɛɟɫɩɟɱɟɧɢɟ ɢ ɚɞɦɢɧɢɫɬɪɢɪɨɜɚɧɢɟ ɢɧɮɨɪɦɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ 2
ɋɨɞɟɪɠɚɧɢɟ ȼɜɟɞɟɧɢɟ .............................................................................................................. 4 1. ɂɫɯɨɞɧɵɟ ɩɨɥɨɠɟɧɢɹ.................................................................................... 4 2. ɋɬɪɭɤɬɭɪɚ ɤɨɦɩɨɧɟɧɬɚ................................................................................. 8 2.1. Ɉɛɥɚɫɬɢ ɜɢɞɢɦɨɫɬɢ....................................................................... 8 2.2. ɋɨɡɞɚɧɢɟ ɫɜɨɣɫɬɜ ɤɨɦɩɨɧɟɧɬɚ ............................................ 11 2.3. Ɍɢɩɵ ɫɜɨɣɫɬɜ................................................................................. 14 2.4. Ɇɟɬɨɞɵ ............................................................................................... 16 2.5. ɋɨɛɵɬɢɹ............................................................................................. 18 3. ɉɪɨɟɤɬɢɪɨɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ.................................................................... 23 3.1. ɗɬɚɩɵ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ............................................................. 23 3.2. ɋɨɡɞɚɧɢɟ ɮɚɣɥɚ ɦɨɞɭɥɹ ........................................................... 25 3.3. ɇɚɫɬɪɨɣɤɚ ɤɨɦɩɨɧɟɧɬɚ ............................................................. 28 3.4. Ɉɛɴɹɜɥɟɧɢɟ ɤɨɧɫɬɪɭɤɬɨɪɚ ...................................................... 29 3.5. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ........ 30 3.6. ɍɫɬɚɧɨɜɤɚ ɤɨɦɩɨɧɟɧɬɚ ............................................................. 35 3.7. Ɋɟɫɭɪɫ ɤɨɦɩɨɧɟɧɬɚ..................................................................... 40 4. ɉɪɢɦɟɪɵ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ................................................................... 42 4.1. Ʉɨɦɩɨɧɟɧɬ, ɧɚɫɥɟɞɧɢɤ ɤɥɚɫɫɚ TGraphicControl...... 42 4.2. Ʉɨɦɩɨɧɟɧɬ ɞɥɹ ɨɬɨɛɪɚɠɟɧɢɹ ɬɟɤɭɳɟɝɨ ɜɪɟɦɟɧɢ ........ 49 4.3. Ʉɨɦɩɨɧɟɧɬ, ɜɵɩɨɥɧɹɸɳɢɣ ɜɵɱɢɫɥɟɧɢɹ ɧɚɞ ɫɨɞɟɪɠɢɦɵɦ ɨɩɭɛɥɢɤɨɜɚɧɧɨɝɨ ɫɜɨɣɫɹɬɜɚ TStrings ............. 52 Ɂɚɞɚɧɢɹ .............................................................................................................. 55 Ʌɢɬɟɪɚɬɭɪɚ........................................................................................................ 57
3
ȼɜɟɞɟɧɢɟ Cɪɟɞɚ ɜɢɡɭɚɥɶɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ Borland Delphi ɫɨɞɟɪɠɢɬ ɜ ɫɟɛɟ ɛɨɥɶɲɨɟ ɤɨɥɢɱɟɫɬɜɨ ɝɨɬɨɜɵɯ ɤ ɢɫɩɨɥɶɡɨɜɚɧɢɸ ɤɨɦɩɨɧɟɧɬɨɜ, ɧɨ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɫɥɨɠɧɵɯ ɩɪɢɥɨɠɟɧɢɣ ɞɚɠɟ ɬɚɤɨɝɨ ɦɧɨɝɨɨɛɪɚɡɢɹ ɧɟ ɯɜɚɬɚɟɬ. ȼ ɬɚɤɢɯ ɫɥɭɱɚɹɯ ɦɨɠɧɨ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɜ ɫɪɟɞɭ ɤɨɦɩɨɧɟɧɬɵ ɫɬɨɪɨɧɧɢɯ ɩɪɨɢɡɜɨɞɢɬɟɥɟɣ ɥɢɛɨ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɫɨɛɫɬɜɟɧɧɵɟ ɤɨɦɩɨɧɟɧɬɵ. ȼ ɞɚɧɧɨɦ ɭɱɟɛɧɨɦ ɩɨɫɨɛɢɢ ɢɡɥɚɝɚɸɬɫɹ ɨɫɧɨɜɵ ɫɨɡɞɚɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɯ ɤɨɦɩɨɧɟɧɬɨɜ ɜ ɫɪɟɞɟ Delphi 7. Ɉɩɢɫɵɜɚɸɬɫɹ ɫɬɪɭɤɬɭɪɚ ɤɨɦɩɨɧɟɧɬɚ ɢ ɦɟɬɨɞɢɤɚ ɪɚɡɪɚɛɨɬɤɢ. ɉɪɢɜɨɞɹɬɫɹ ɩɪɢɦɟɪɵ ɪɟɲɟɧɢɹ ɬɢɩɨɜɵɯ ɡɚɞɚɱ. Ⱦɥɹ ɪɚɛɨɬɵ ɫ ɩɨɫɨɛɢɟɦ ɬɪɟɛɭɟɬɫɹ ɡɧɚɧɢɟ ɨɫɧɨɜ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɧɚ ɹɡɵɤɟ Delphi. 1. ɂɫɯɨɞɧɵɟ ɩɨɥɨɠɟɧɢɹ Ʉɨɦɩɨɧɟɧɬ – ɷɬɨ ɪɚɡɧɨɜɢɞɧɨɫɬɶ ɤɥɚɫɫɚ, ɨɛɥɚɞɚɸɳɚɹ ɫɩɨɫɨɛɧɨɫɬɶɸ ɜɡɚɢɦɨɞɟɣɫɬɜɨɜɚɬɶ ɫ ɢɧɬɟɝɪɢɪɨɜɚɧɧɨɣ ɫɪɟɞɨɣ ɪɚɡɪɚɛɨɬɤɢ (IDE) Delphi ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɩɪɢɥɨɠɟɧɢɣ. Ʉɨɝɞɚ ɪɚɡɪɚɛɨɬɱɢɤ ɩɪɢɥɨɠɟɧɢɹ ɩɨɦɟɳɚɟɬ ɤɨɦɩɨɧɟɧɬ ɧɚ ɮɨɪɦɭ, ɬɨ ɞɢɡɚɣɧɟɪ ɮɨɪɦ ɫɨɡɞɚɟɬ ɷɤɡɟɦɩɥɹɪ ɜɵɛɪɚɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɢɡ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɤɨɦɩɨɧɟɧɬɧɨɝɨ ɤɥɚɫɫɚ. Ʉɨɦɩɨɧɟɧɬ ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ɮɨɪɦɟ, ɚ ɟɝɨ ɨɩɭɛɥɢɤɨɜɚɧɧɵɟ ɫɜɨɣɫɬɜɚ ɫɬɚɧɨɜɹɬɫɹ ɞɨɫɬɭɩɧɵɦɢ ɞɥɹ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɧɚ ɷɬɚɩɟ ɪɚɡɪɚɛɨɬɤɢ ɥɢɛɨ ɫ ɩɨɦɨɳɶɸ ɢɧɫɩɟɤɬɨɪɚ ɨɛɴɟɤɬɨɜ, ɥɢɛɨ ɱɟɪɟɡ ɫɨɡɞɚɧɧɵɟ ɪɚɡɪɚɛɨɬɱɢɤɨɦ ɤɨɦɩɨɧɟɧɬɚ ɪɟɞɚɤɬɨɪɵ ɤɨɦɩɨɧɟɧɬɨɜ ɢ ɪɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ [1]. Ȼɚɡɨɜɭɸ ɮɭɧɤɰɢɨɧɚɥɶɧɨɫɬɶ ɤɨɦɩɨɧɟɧɬɨɜ ɨɛɟɫɩɟɱɢɜɚɟɬ ɤɥɚɫɫ TComponent, ɹɜɥɹɸɳɢɣɫɹ ɩɪɟɞɤɨɦ ɜɫɟɯ ɤɨɦɩɨɧɟɧɬɨɜ. ɇɚɩɪɢɦɟɪ, TComponent ɪɟɚɥɢɡɭɟɬ ɫɜɨɣɫɬɜɚ Name ɢ Tag, ɤɨɬɨɪɵɟ ɧɚɫɥɟɞɭɸɬɫɹ ɜɫɟɦɢ ɤɨɦɩɨɧɟɧɬɚɦɢ. Ȼɨɥɟɟ ɬɨɝɨ, TComponent ɫɨɞɟɪɠɢɬ ɦɟɬɨɞɵ ɢ ɫɜɨɣɫɬɜɚ, 4
ɤɨɬɨɪɵɟ ɩɪɟɞɨɫɬɚɜɥɹɸɬ ɤɨɦɩɨɧɟɧɬɚɦ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɛɚɬɵɜɚɬɶɫɹ ɞɢɡɚɣɧɟɪɨɦ ɮɨɪɦ. ɏɨɬɹ ɤɨɦɩɨɧɟɧɬɵ ɢ ɪɟɚɥɢɡɭɸɬɫɹ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɤɥɚɫɫɨɜ ɹɡɵɤɚ Delphi, ɧɨ ɨɛɴɹɜɥɟɧɢɟ ɤɥɚɫɫɚ ɧɟ ɹɜɥɹɟɬɫɹ ɨɛɴɹɜɥɟɧɢɟɦ ɤɨɦɩɨɧɟɧɬɚ, ɟɫɥɢ ɤɥɚɫɫ ɧɟ ɩɨɪɨɠɞɟɧ ɨɬ Ɍɋɨmɪɨnent. ɗɤɡɟɦɩɥɹɪɵ ɤɥɚɫɫɨɜ, ɤɨɬɨɪɵɟ ɩɪɨɢɡɜɟɞɟɧɵ ɨɬ TComponent, ɧɚɡɵɜɚɸɬɫɹ ɤɨɦɩɨɧɟɧɬɚɦɢ, ɚ ɷɤɡɟɦɩɥɹɪɵ ɜɫɟɯ ɞɪɭɝɢɯ ɤɥɚɫɫɨɜ ɧɚɡɵɜɚɸɬɫɹ ɨɛɴɟɤɬɚɦɢ. Ƚɥɚɜɧɚɹ ɪɚɡɧɢɰɚ ɦɟɠɞɭ ɷɬɢɦɢ ɞɜɭɦɹ ɩɨɧɹɬɢɹɦɢ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɬɨɦ, ɱɬɨ ɤɨɦɩɨɧɟɧɬɚɦɢ ɦɨɠɧɨ ɦɚɧɢɩɭɥɢɪɨɜɚɬɶ ɧɚ ɮɨɪɦɟ, ɬɨɝɞɚ ɤɚɤ ɨɛɴɟɤɬɚɦɢ – ɧɟɥɶɡɹ. Ɉɫɨɛɟɧɧɨɫɬɢ ɩɨɫɬɪɨɟɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ: – ɤɨɦɩɨɧɟɧɬɵ ɢɦɟɸɬ ɞɪɭɝɢɯ ɤɨɧɟɱɧɵɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ; – ɤɨɦɩɨɧɟɧɬɵ ɹɜɥɹɸɬɫɹ ɢɧɫɬɪɭɦɟɧɬɚɦɢ ɢ ɢɫɩɨɥɶɡɭɸɬɫɹ ɪɚɡɪɚɛɨɬɱɢɤɚɦɢ ɩɪɢɥɨɠɟɧɢɣ; – ɧɚɩɢɫɚɧɢɟ ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɟɬɫɹ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɦ ɧɟɜɢɡɭɚɥɶɧɵɦ ɩɪɨɰɟɫɫɨɦ. ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɩɪɨɰɟɫɫ ɪɚɡɪɚɛɨɬɤɢ ɤɨɦɩɨɧɟɧɬɚ ɞɨɥɠɟɧ ɜɤɥɸɱɚɬɶ ɫɥɟɞɭɸɳɢɟ ɷɬɚɩɵ: – ɮɨɪɦɭɥɢɪɨɜɤɚ ɬɪɟɛɨɜɚɧɢɣ ɤ ɤɨɦɩɨɧɟɧɬɭ ɢ ɜɵɛɨɪ ɤɥɚɫɫɚ-ɩɪɟɞɤɚ; – ɧɚɩɢɫɚɧɢɟ ɤɨɞɚ ɤɨɦɩɨɧɟɧɬɚ; – ɨɬɥɚɞɤɚ ɢ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨɟ ɬɟɫɬɢɪɨɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ; – ɜɤɥɸɱɟɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɜ ɫɪɟɞɭ Delphi; – ɨɤɨɧɱɚɬɟɥɶɧɨɟ ɬɟɫɬɢɪɨɜɚɧɢɟ. ɉɟɪɜɵɣ ɲɚɝ ɩɪɢ ɫɨɡɞɚɧɢɢ ɤɨɦɩɨɧɟɧɬɨɜ – ɢɡɭɱɟɧɢɟ ɢɟɪɚɪɯɢɢ ɛɢɛɥɢɨɬɟɤɢ ɜɢɡɭɚɥɶɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ VCL (ɫɦ. ɪɢɫ. 1) ɫ ɰɟɥɶɸ ɜɵɛɨɪɚ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨɝɨ ɩɪɟɞɤɚ. Ɂɞɟɫɶ ɬɪɟɛɭɟɬɫɹ ɜɵɹɜɢɬɶ ɫɭɳɟɫɬɜɭɸɳɢɣ ɤɨɦɩɨɧɟɧɬ, ɤɨɬɨɪɵɣ ɧɚɢɛɨɥɟɟ ɛɥɢɡɤɨ ɩɨɞɯɨɞɢɬ ɩɨɞ ɬɨ, ɱɬɨ ɬɪɟɛɭɟɬɫɹ ɩɨɥɭɱɢɬɶ, ɢ ɢɡɦɟɧɢɬɶ ɭɧɚɫɥɟɞɨɜɚɧɧɨɟ ɩɨɜɟɞɟɧɢɟ. ɇɚɩɪɢɦɟɪ, ɞɥɹ ɫɨɡɞɚɧɢɹ ɫɩɟɰɢɚɥɢɡɢɪɨ-
5
ɜɚɧɧɨɝɨ ɩɨɥɹ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ ɤɚɤ ɩɨɬɨɦɨɤ ɤɥɚɫɫɚ TCustomEdit ɢ ɫɞɟɥɚɬɶ ɧɭɠɧɵɟ ɦɨɞɢɮɢɤɚɰɢɢ. Ʉɨɦɩɨɧɟɧɬɵ ɞɟɥɹɬɫɹ ɧɚ ɞɜɟ ɝɪɭɩɩɵ – ɜɢɡɭɚɥɶɧɵɟ ɢ ɧɟɜɢɡɭɚɥɶɧɵɟ. ȼɢɡɭɚɥɶɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɨɬɨɛɪɚɠɚɸɬɫɹ ɧɚ ɮɨɪɦɟ ɜɨ ɜɪɟɦɹ ɪɚɛɨɬɵ ɩɪɨɝɪɚɦɦɵ. Ɉɧɢ ɹɜɥɹɸɬɫɹ ɩɨɬɨɦɤɚɦɢ ɤɥɚɫɫɚ TControl ɢ ɩɨɷɬɨɦɭ ɢɧɨɝɞɚ ɧɚɡɵɜɚɸɬɫɹ ɷɥɟɦɟɧɬɚɦɢ ɭɩɪɚɜɥɟɧɢɹ. ɉɪɢɦɟɪɚɦɢ ɧɟɜɢɡɭɚɥɶɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɸɬɫɹ ɤɨɦɩɨɧɟɧɬɵ Ttimer, TMenu, TOpenDialog, ɤɨɦɩɨɧɟɧɬɵ ɞɨɫɬɭɩɚ ɤ ɛɚɡɚɦ ɞɚɧɧɵɯ (TTable, TQuery ɢ ɬ. ɩ.). ɇɟɜɢɡɭɚɥɶɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɧɟ ɜɢɞɧɵ ɧɚ ɮɨɪɦɟ ɢ ɪɚɛɨɬɚɸɬ «ɡɚ ɤɭɥɢɫɚɦɢ» ɩɪɢɥɨɠɟɧɢɹ. ɂɯ ɩɪɟɞɤɨɦ ɧɟ ɹɜɥɹɟɬɫɹ TControl, ɚ ɧɚɫɥɟɞɨɜɚɧɢɟ ɢɞɟɬ ɨɬ Tcomponent. ȼɢɡɭɚɥɶɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɞɟɥɹɬɫɹ ɧɚ ɞɜɟ ɝɪɭɩɩɵ – ɝɪɚɮɢɱɟɫɤɢɟ ɢ ɨɤɨɧɧɵɟ. Ƚɪɚɮɢɱɟɫɤɢɟ ɤɨɦɩɨɧɟɧɬɵ ɩɨɪɨɠɞɚɸɬɫɹ ɨɬ TGraphicControl ɢ ɢɦɟɸɬ ɜɢɡɭɚɥɶɧɨɟ ɩɪɟɞɫɬɚɜɥɟɧɢɟ, ɧɨ ɧɟ ɩɨɥɭɱɚɸɬ ɮɨɤɭɫɚ ɜɜɨɞɚ. Ƚɥɚɜɧɨɣ ɨɫɨɛɟɧɧɨɫɬɶɸ ɩɨɬɨɦɤɨɜ TGraphicControl ɹɜɥɹɟɬɫɹ ɧɚɥɢɱɢɟ ɫɜɨɣɫɬɜɚ Canvas: TCanvas, ɩɨɡɜɨɥɹɸɳɟɝɨ ɜɵɜɨɞɢɬɶ ɢɡɨɛɪɚɠɟɧɢɟ ɩɪɹɦɨ ɧɚ ɩɨɜɟɪɯɧɨɫɬɢ ɤɨɦɩɨɧɟɧɬɚ. ɉɪɢɦɟɪɚɦɢ ɝɪɚɮɢɱɟɫɤɢɯ ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɸɬɫɹ TImage, TPaintBox, TLabel. Ɉɤɨɧɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɩɪɨɢɫɯɨɞɹɬ ɨɬ TWinControl. Ɉɧɢ ɢɧɤɚɩɫɭɥɢɪɭɸɬ ɮɭɧɤɰɢɢ API ɞɥɹ ɪɚɛɨɬɵ ɫ ɨɤɧɚɦɢ Windows, ɭɦɟɸɬ ɩɨɥɭɱɚɬɶ ɮɨɤɭɫ ɜɜɨɞɚ ɢ ɫɨɞɟɪɠɚɬ ɫɜɨɣɫɬɜɨ Handle: THandle. Ⱦɚɧɧɨɟ ɫɜɨɣɫɬɜɨ ɯɪɚɧɢɬ ɞɟɫɤɪɢɩɬɨɪ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɨɤɨɧɧɨɝɨ ɨɛɴɟɤɬɚ Windows ɢ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɨ ɞɥɹ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨɝɨ ɜɵɡɨɜɚ ɮɭɧɤɰɢɣ API ɢ ɨɛɦɟɧɚ ɫɨɨɛɳɟɧɢɹɦɢ Windows ɦɟɠɞɭ ɤɨɦɩɨɧɟɧɬɚɦɢ. ɂɧɨɝɞɚ ɞɥɹ ɫɨɡɞɚɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɝɨ ɷɥɟɦɟɧɬɚ ɭɩɪɚɜɥɟɧɢɹ ɢɫɩɨɥɶɡɭɟɬɫɹ ɤɥɚɫɫ TCustomControl. ɗɬɨ ɩɪɹɦɨɣ ɩɨɬɨɦɨɤ TWinControl, ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɧɚɫɥɟɞɭɸɳɢɣ ɜɫɸ ɟɝɨ ɮɭɧɤɰɢɨɧɚɥɶɧɨɫɬɶ. ɇɨ ɞɨɩɨɥɧɢɬɟɥɶɧɨ ɨɧ ɜ ɫɟɤɰɢɢ Protected ɫɨɞɟɪɠɢɬ ɫɜɨɣɫɬɜɨ Canvas, ɧɟɨɛɯɨɞɢɦɨɟ ɞɥɹ ɪɢɫɨɜɚɧɢɹ.
ɑɬɨɛɵ
ɢɫɩɨɥɶɡɨɜɚɬɶ 6
ɷɬɨ
ɫɜɨɣɫɬɜɨ
ɜ
ɩɨɬɨɦɤɚɯ
TCustomControl, ɟɝɨ ɧɚɞɨ ɩɟɪɟɧɟɫɬɢ ɜ ɫɟɤɰɢɸ Public (ɫɦ. ɫɥɟɞɭɸɳɢɣ ɪɚɡɞɟɥ).
Ɋɢɫ. 1. Ɏɪɚɝɦɟɧɬ ɢɟɪɚɪɯɢɢ ɤɥɚɫɫɨɜ ɛɢɛɥɢɨɬɟɤɢ VCL Ɋɟɤɨɦɟɧɞɚɰɢɢ ɩɨ ɢɫɩɨɥɶɡɨɜɚɧɢɸ ɪɨɞɢɬɟɥɶɫɤɢɯ ɤɥɚɫɫɨɜ ɜ ɤɚɱɟɫɬɜɟ ɩɪɟɞɤɚ ɞɥɹ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɧɨɝɨ ɤɥɚɫɫɚ ɩɪɢɜɟɞɟɧɵ ɜ ɬɚɛɥɢɰɟ 1. Ȼɚɡɨɜɵɟ ɤɥɚɫɫɵ Delphi ɞɥɹ ɫɨɡɞɚɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ ɍɧɚɫɥɟɞɨɜɚɧ ɨɬ
Ɍɚɛɥɢɰɚ 1
Ⱦɥɹ ɫɨɡɞɚɧɢɹ
ɋɭɳɟɫɬɜɭɸɳɟɝɨ ɤɨɦɩɨɧɟɧɬɚ
Ɇɨɞɢɮɢɰɢɪɨɜɚɧɧɨɣ ɜɟɪɫɢɢ ɪɚɛɨɬɚɸɳɟɝɨ ɤɨɦɩɨɧɟɧɬɚ Ƚɪɚɮɢɱɟɫɤɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɧɟ ɬɪɟ-
TGraphicControl
ɛɭɸɳɟɝɨ ɮɨɤɭɫɚ ɜɜɨɞɚ Ɉɤɨɧɧɨɝɨ
TWinControl 7
ɤɨɦɩɨɧɟɧɬɚ,
ɢɫɩɨɥɶ-
ɡɭɸɳɟɝɨ ɞɟɫɤɪɢɩɬɨɪ ɨɤɧɚ ɢɥɢ ɞɥɹ ɫɨɡɞɚɧɢɹ ɨɛɨɥɨɱɤɢ ɜɨɤɪɭɝ ɫɭɳɟɫɬɜɭɸɳɢɯ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɯ ɨɤɨɧɧɵɯ ɷɥɟɦɟɧɬɨɜ ɭɩɪɚɜɥɟɧɢɹ TCustomControl
Ɉɪɢɝɢɧɚɥɶɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ
TComponent
ɇɟɜɢɡɭɚɥɶɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ
Ɂɚɱɚɫɬɭɸ ɧɨɜɨɦɭ ɤɨɦɩɨɧɟɧɬɭ ɧɟ ɧɭɠɧɵ ɜɫɟ ɫɜɨɣɫɬɜɚ ɩɪɟɞɤɚ, ɤɨɬɨɪɵɯ ɦɨɝɭɬ ɛɵɬɶ ɦɧɨɝɢɟ ɞɟɫɹɬɤɢ. ɑɬɨɛɵ ɭɩɪɨɫɬɢɬɶ ɪɚɡɪɚɛɨɬɤɭ, ɜ ɢɟɪɚɪɯɢɢ ɤɨɦɩɨɧɟɧɬɨɜ ɢɦɟɸɬɫɹ ɤɨɦɩɨɧɟɧɬɵ, ɜ ɧɚɡɜɚɧɢɢ ɤɨɬɨɪɵɯ ɩɪɢɫɭɬɫɬɜɭɟɬ ɫɥɨɜɨ «Custom» (ɡɚɤɚɡɧɨɣ). Ɍɚɤɢɟ ɤɨɦɩɨɧɟɧɬɵ ɧɟ ɢɦɟɸɬ ɨɩɭɛɥɢɤɨɜɚɧɧɵɯ ɫɜɨɣɫɬɜ, ɬ. ɟ. ɫɜɨɣɫɬɜ, ɜɢɞɧɵɯ ɜ ɢɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ. ȼɫɟ ɫɜɨɣɫɬɜɚ, ɞɨɫɬɭɩɧɵɟ ɤ ɨɩɭɛɥɢɤɨɜɚɧɢɸ, ɩɟɪɟɱɢɫɥɟɧɵ ɜ ɫɟɤɰɢɢ protected. ɉɨɷɬɨɦɭ ɟɫɥɢ ɦɵ ɯɨɬɢɦ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ TEdit, ɧɨ ɧɟ ɜɫɟ ɫɜɨɣɫɬɜɚ TEdit ɧɚɦ ɧɭɠɧɵ, ɬɨ ɥɭɱɲɟ ɧɚɫɥɟɞɨɜɚɬɶ ɧɟ ɨɬ TEdit, ɚ ɨɬ TCustomEdit. Ⱦɚɥɟɟ, ɱɬɨɛɵ ɨɩɭɛɥɢɤɨɜɚɬɶ ɧɟɨɛɯɨɞɢɦɵɟ ɫɜɨɣɫɬɜɚ ɜ ɧɚɲɟɦ ɤɨɦɩɨɧɟɧɬɟ, ɫɥɟɞɭɟɬ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɟɝɨ ɤɥɚɫɫɚ ɩɪɨɫɬɨ ɩɟɪɟɱɢɫɥɢɬɶ ɧɚɡɜɚɧɢɹ ɫɜɨɣɫɬɜ ɜ ɫɟɤɰɢɢ published. 2. ɋɬɪɭɤɬɭɪɚ ɤɨɦɩɨɧɟɧɬɚ 2.1. Ɉɛɥɚɫɬɢ ɜɢɞɢɦɨɫɬɢ Ʉɨɦɩɨɧɟɧɬɵ ɢɦɟɸɬ ɪɚɡɧɵɟ ɩɪɨɝɪɚɦɦɧɵɟ ɢɧɬɟɪɮɟɣɫɵ: ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ (ɤɚɤ ɤɨɦɩɨɧɟɧɬ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ, ɤɚɤɢɟ ɦɟɬɨɞɵ, ɫɜɨɣɫɬɜɚ ɢ ɫɨɛɵɬɢɹ ɛɭɞɭɬ ɞɨɫɬɭɩɧɵ), ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ (ɤɚɤ ɤɨɦɩɨɧɟɧɬ ɜɟɞɺɬ ɫɟɛɹ ɜ ɫɪɟɞɟ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ), ɢɧɬɟɪɮɟɣɫ ɪɚɡɪɚɛɨɬɱɢɤɚ (ɨɛɟɫɩɟɱɢɜɚɟɬ ɞɨɫɬɭɩ ɤ ɞɟɬɚɥɹɦ ɪɟɚɥɢɡɚɰɢɢ). ɑɬɨɛɵ ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ ɪɚɡɝɪɚɧɢɱɢɬɶ ɞɨɫɬɭɩ ɤ ɩɨɥɹɦ, ɦɟɬɨɞɚɦ ɢ ɫɜɨɣɫɬɜɚɦ ɤɥɚɫɫɚ (ɬ. ɟ. ɡɚɞɚɬɶ ɨɛ8
ɥɚɫɬɶ ɜɢɞɢɦɨɫɬɢ), ɜ Delphi ɨɩɪɟɞɟɥɟɧɵ ɱɟɬɵɪɟ ɨɫɧɨɜɧɵɟ ɞɢɪɟɤɬɢɜɵ, ɩɪɢɜɟɞɟɧɧɵɟ ɜ ɬɚɛɥɢɰɟ 2. ɍɪɨɜɧɢ ɞɨɫɬɭɩɚ ɤ ɩɨɥɹɦ ɢ ɦɟɬɨɞɚɦ ɤɥɚɫɫɚ Private
Ʌɢɱɧɵɟ
Ɍɚɛɥɢɰɚ 2
ɇɟɜɢɞɢɦɵ ɜɧɟ ɦɨɞɭɥɹ ɢɥɢ ɩɪɨɝɪɚɦɦɵ, ɜ ɤɨɬɨɪɨɣ ɤɥɚɫɫ ɨɛɴɹɜɥɟɧ
Protected
Ɂɚɳɢɳɟɧ-
ȼɢɞɢɦɵ ɜɟɡɞɟ ɜ ɦɨɞɭɥɟ, ɜ ɤɨɬɨɪɨɦ ɤɥɚɫɫ ɨɛɴ-
ɧɵɟ
ɹɜɥɹɟɬɫɹ, ɢ ɢɡ ɥɸɛɨɝɨ ɤɥɚɫɫɚ ɩɨɬɨɦɤɚ, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɦɨɞɭɥɹ, ɝɞɟ ɤɥɚɫɫ ɩɨɬɨɦɤɚ ɩɨɹɜɥɹɟɬɫɹ, ɬ. ɟ. ɞɥɹ ɪɚɡɪɚɛɨɬɱɢɤɨɜ ɞɪɭɝɢɯ ɤɨɦɩɨɧɟɧɬɨɜ. ȼ ɫɟɤɰɢɹɯ Private ɢ Protected «ɫɤɪɵɜɚɸɬɫɹ» ɞɟɬɚɥɢ ɪɟɚɥɢɡɚɰɢɢ
Public
Ɉɛɳɢɟ
Ɉɛɴɟɤɬɵ ɞɚɧɧɨɝɨ ɭɪɨɜɧɹ ɜɢɞɢɦɵ ɜɟɡɞɟ, ɝɞɟ ɜɨɡɦɨɠɧɚ ɫɫɵɥɤɚ ɧɚ ɤɥɚɫɫ
Published
Ɉɩɭɛɥɢɤɨ-
ɇɟɬ ɧɢɤɚɤɢɯ ɨɝɪɚɧɢɱɟɧɢɣ ɩɨ ɞɨɫɬɭɩɭ.
ɜɚɧɧɵɟ
ɋɜɨɣɫɬɜɚ ɩɨɩɚɞɚɸɬ ɜ ɂɧɫɩɟɤɬɨɪ Ɉɛɴɟɤɬɨɜ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. Ʉɥɚɫɫ ɞɨɥɠɟɧ ɩɪɨɢɫɯɨɞɢɬɶ ɨɬ TComponent
ɉɨɪɨɠɞɟɧɧɵɣ ɤɥɚɫɫ ɦɨɠɟɬ ɩɟɪɟɦɟɫɬɢɬɶ ɫɜɨɣɫɬɜɚ ɜ ɪɚɡɞɟɥ ɫ ɦɟɧɶɲɢɦɢ ɨɝɪɚɧɢɱɟɧɢɹɦɢ (protected – ɜ public ɢɥɢ ɜ published, public – ɜ published), ɧɨ ɧɟ ɦɨɠɟɬ ɩɟɪɟɦɟɫɬɢɬɶ ɫɜɨɣɫɬɜɨ ɜ ɪɚɡɞɟɥ ɫ ɛɨɥɶɲɢɦɢ ɨɝɪɚɧɢɱɟɧɢɹɦɢ ɜ ɞɨɫɬɭɩɟ. Ɉɛɳɟɩɪɢɧɹɬɨ (ɬɚɤ ɫɥɟɞɭɟɬ ɢɡ ɩɪɢɧɰɢɩɚ ɢɧɤɚɩɫɭɥɹɰɢɢ), ɱɬɨ ɜ ɫɟɤɰɢɢ private ɪɚɫɩɨɥɚɝɚɸɬɫɹ ɨɛɴɹɜɥɟɧɢɹ ɩɨɥɟɣ ɤɥɚɫɫɚ; ɜ ɫɟɤɰɢɢ protected – ɦɟɬɨɞɵ ɱɬɟɧɢɹ-ɡɚɩɢɫɢ ɡɧɚɱɟɧɢɣ ɫɜɨɣɫɬɜ, ɨɛɴɹɜɥɟɧɢɹ ɧɟɨɩɭɛɥɢɤɨɜɚɧɧɵɯ ɫɜɨɣɫɬɜ; ɜ ɫɟɤɰɢɢ public – ɦɟɬɨɞɵ, ɜ ɬɨɦ ɱɢɫɥɟ ɤɨɧɫɬɪɭɤɬɨɪɵ ɢ ɞɟɫɬɪɭɤɬɨɪɵ, ɢ ɫɜɨɣɫɬɜɚ, ɞɨɫɬɭɩɧɵɟ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ; ɜ ɫɟɤɰɢɢ published – ɨɩɭɛɥɢɤɨɜɚɧɧɵɟ ɫɜɨɣɫɬɜɚ.
9
ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɭ ɜɧɨɜɶ ɫɨɡɞɚɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɢɦɟɟɬɫɹ ɧɨɜɨɟ ɫɜɨɣɫɬɜɨ NewProp, ɧɨɜɨɟ ɫɨɛɵɬɢɟ OnNewEvent, ɧɨɜɵɣ ɦɟɬɨɞ NewMethod. Ʉɪɨɦɟ ɬɨɝɨ, ɨɩɭɛɥɢɤɨɜɚɧɵ ɭɧɚɫɥɟɞɨɜɚɧɧɵɟ ɫɜɨɣɫɬɜɚ Height, Width ɢ ɫɨɛɵɬɢɟ OnClick. Ɍɨɝɞɚ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɨɛɥɚɫɬɟɣ ɜɢɞɢɦɨɫɬɢ ɨɛɴɹɜɥɟɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ ɫɬɪɨɢɬɫɹ ɩɨ ɫɥɟɞɭɸɳɟɣ ɫɯɟɦɟ: type TMyComponent = class( <ɉɪɟɞɨɤ> ) private // ȼɧɭɬɪɟɧɧɢɟ ɩɨɥɹ, ɫɨɞɟɪɠɚɳɢɟ ɡɧɚɱɟɧɢɟ ɧɨɜɨɝɨ ɫɜɨɣɫɬɜɚ FNewProp : TPropType; // ȼɧɭɬɪɟɧɧɢɣ ɭɤɚɡɚɬɟɥɶ ɧɚ ɦɟɬɨɞ-ɨɛɪɚɛɨɬɱɢɤ ɫɨɛɵɬɢɹ FOnNewEvent : TEventType; protected // Ɇɟɬɨɞɵ ɱɬɟɧɢɹ-ɡɚɩɢɫɢ ɫɜɨɣɫɬɜɚ function GetNewProp : TpropType; procedure SetNewProp(Value : TpropType); public // Ʉɨɧɫɬɪɭɤɬɨɪ ɢ ɞɟɫɬɪɭɤɬɨɪ constructor Create(AOwner : TComponent); override; destructor Destroy; override; // ɇɨɜɵɣ ɦɟɬɨɞ procedure NewMethod; // Ɇɟɬɨɞ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɧɨɜɨɝɨ ɫɨɛɵɬɢɹ OnNewEvent procedure NewEvent; published // Ɉɩɪɟɞɟɥɟɧɢɟ ɧɨɜɨɝɨ ɫɜɨɣɫɬɜɚ property NewProp : TPropType read GEtNewProp write SetNewProp; // Ɉɩɭɛɥɢɤɨɜɚɧɢɟ ɭɧɚɫɥɟɞɨɜɚɧɧɵɯ ɫɜɨɣɫɬɜ, // ɧɚɯɨɞɢɜɲɢɯɫɹ ɭ ɩɪɟɞɤɚ ɜ ɫɟɤɰɢɢ Protected property Height; 10
property Width; // ɇɨɜɨɟ ɫɨɛɵɬɢɟ property
OnNewEvent
:
TEventType
read
FOnNewEvent
write FOnNewEvent; // Ɉɩɭɛɥɢɤɨɜɚɧɢɟ ɭɧɚɫɥɟɞɨɜɚɧɧɨɝɨ ɫɨɛɵɬɢɹ property OnClick; end;
Ⱦɚɥɟɟ ɧɟɨɛɯɨɞɢɦɨ ɧɚɩɢɫɚɬɶ ɤɨɞ ɪɟɚɥɢɡɚɰɢɢ ɧɨɜɵɯ ɦɟɬɨɞɨɜ. ȼ ɫɥɟɞɭɸɳɢɯ ɪɚɡɞɟɥɚɯ ɫɯɟɦɚ ɤɨɦɩɨɧɟɧɬɚ ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɛɨɥɟɟ ɩɨɞɪɨɛɧɨ. 2.2. ɋɨɡɞɚɧɢɟ ɫɜɨɣɫɬɜ ɤɨɦɩɨɧɟɧɬɚ ɋɜɨɣɫɬɜɚ ɩɪɟɞɫɬɚɜɥɹɸɬ ɫɨɛɨɣ ɫɚɦɵɣ ɜɚɠɧɵɣ ɚɫɩɟɤɬ ɥɸɛɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɢ ɨɩɪɟɞɟɥɹɸɬ ɝɥɚɜɧɵɣ ɢɧɬɟɪɮɟɣɫ, ɫ ɩɨɦɨɳɶɸ ɤɨɬɨɪɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɶ ɦɚɧɢɩɭɥɢɪɭɟɬ ɤɨɦɩɨɧɟɧɬɨɦ ɢ ɧɚɫɬɪɚɢɜɚɟɬ ɟɝɨ ɩɟɪɟɞ ɜɵɩɨɥɧɟɧɢɟɦ ɩɪɢɥɨɠɟɧɢɹ. ɋɜɨɣɫɬɜɚ ɢɫɩɨɥɶɡɭɸɬɫɹ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɫɤɪɵɬɶ ɨɬ ɩɨɥɶɡɨɜɚɬɟɥɹ ɞɟɬɚɥɢ ɪɟɚɥɢɡɚɰɢɢ ɢ ɱɬɨɛɵ ɫɨɡɞɚɬɶ ɷɮɮɟɤɬ ɞɟɣɫɬɜɢɹ ɩɪɢ ɢɡɦɟɧɟɧɢɢ ɡɧɚɱɟɧɢɹ. ɇɚɩɪɢɦɟɪ, ɢɡɦɟɧɟɧɢɟ ɫɜɨɣɫɬɜɚ Height ɤɨɦɩɨɧɟɧɬɚ TPanel ɧɟ ɬɨɥɶɤɨ ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɟ Height, ɧɨ ɢ ɩɪɢɜɨɞɢɬ ɤ ɬɨɦɭ, ɱɬɨ ɤɨɦɩɨɧɟɧɬ ɩɟɪɟɪɢɫɨɜɵɜɚɟɬ ɫɟɛɹ. ɉɟɪɟɪɢɫɨɜɤɚ ɤɨɦɩɨɧɟɧɬɚ ɹɜɥɹɟɬɫɹ ɬɢɩɢɱɧɵɦ ɷɮɮɟɤɬɨɦ ɞɟɣɫɬɜɢɹ. ȼ ɩɪɨɫɬɟɣɲɟɦ ɫɥɭɱɚɟ ɫɜɨɣɫɬɜɨ ɨɩɪɟɞɟɥɹɟɬɫɹ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: property <ɢɦɹ ɫɜɨɣɫɬɜɚ>: <ɬɢɩ ɫɜɨɣɫɬɜɚ>
[read <ɦɟɬɨɞ ɱɬɟɧɢɹ
ɫɜɨɣɫɬɜɚ>] [write <ɦɟɬɨɞ ɡɚɩɢɫɢ ɫɜɨɣɫɬɜɚ>].
ɉɪɢɦɟɪ. Ⱦɨɫɬɭɩ ɤ ɩɨɥɸ FField ɬɢɩɚ integer ɪɟɚɥɢɡɭɟɬɫɹ ɱɟɪɟɡ ɫɜɨɣɫɬɜɨ PField ɬɢɩɚ string. ɉɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɢɩɨɜ ɩɪɨɢɫɯɨɞɢɬ ɩɪɢ ɜɵɡɨɜɟ ɦɟɬɨɞɨɜ ɱɬɟɧɢɹ/ɡɚɩɢɫɢ: interface type 11
TMyClass=class(TParentClass) private // ɩɨɥɟ
FField: integer; protected function GetField: string;
// ɦɟɬɨɞ
procedure SetField(AValue: string);// ɦɟɬɨɞ published // ɫɜɨɣɫɬɜɨ
property PField: string read GetField write SetField; end;
// ɷɤɡɟɦɩɥɹɪ
var MyObj: TMyClass;
implementation
function TMyClass.GetField: string; begin Result:=IntToStr(FField); end;
procedure TMyClass.SetField(AValue: string); var x,Code: integer; begin Val(AValue,x,Code); // ɩɪɨɜɟɪɤɚ ɤɨɪɪɟɤɬɧɨɫɬɢ ɧɨɜɨɝɨ ɡɧɚɱɟɧɢɹ if (x<>FField) and (Code=0) then begin FField:=x; //… // Ⱦɨɩɨɥɧɢɬɟɥɶɧɵɟ ɞɟɣɫɬɜɢɹ, ɟɫɥɢ ɨɧɢ ɧɟɨɛɯɨɞɢɦɵ end; end; end.
12
ȼ ɞɚɧɧɨɦ ɩɪɢɦɟɪɟ ɞɨɫɬɭɩ ɞɥɹ ɱɬɟɧɢɹ ɩɨɥɹ ɪɟɚɥɢɡɨɜɚɧ ɮɭɧɤɰɢɟɣ GetField. ȿɫɥɢ ɫɩɟɰɢɚɥɶɧɵɣ ɦɟɬɨɞ ɱɬɟɧɢɹ/ɨɛɧɨɜɥɟɧɢɹ ɧɟ ɬɪɟɛɭɟɬɫɹ, ɬɨ ɜɦɟɫɬɨ ɢɦɟɧɢ ɦɟɬɨɞɚ ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɢɦɹ ɩɨɥɹ (ɧɨ ɬɨɥɶɤɨ ɜ ɫɥɭɱɚɟ, ɟɫɥɢ ɬɢɩ ɫɜɨɣɫɬɜɚ ɫɨɜɩɚɞɚɟɬ ɫ ɬɢɩɨɦ ɩɨɥɹ). ɇɚɩɪɢɦɟɪ, property PField:string read FField write SetField;
ȿɫɥɢ ɫɜɨɣɫɬɜɨ ɞɨɫɬɭɩɧɨ ɬɨɥɶɤɨ ɞɥɹ ɱɬɟɧɢɹ ɢɥɢ ɞɥɹ ɡɚɩɢɫɢ, ɬɨ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɦɟɬɨɞ ɦɨɠɧɨ ɨɩɭɫɬɢɬɶ. ɉɨɞɱɟɪɤɧɟɦ,
ɱɬɨ
ɩɪɢ
ɨɛɪɚɳɟɧɢɢ
ɤ
ɫɜɨɣɫɬɜɭ
ɦɟɬɨɞɵ
ɱɬɟ-
ɧɢɹ/ɨɛɧɨɜɥɟɧɢɹ ɧɟ ɭɤɚɡɵɜɚɸɬɫɹ. Ⱦɥɹ ɩɨɥɶɡɨɜɚɬɟɥɹ ɨɛɪɚɳɟɧɢɟ ɤ ɫɜɨɣɫɬɜɭ ɬɚɤɨɟ ɠɟ, ɤɚɤ ɤ ɩɨɥɸ, ɧɚɩɪɢɦɟɪ, MyObj.PField:=’100’; …; s:=MyObj.PField;
ȼɵɡɨɜɵ
ɧɟɨɛɯɨɞɢɦɵɯ
ɦɟɬɨɞɨɜ
ɜɫɬɪɚɢɜɚɸɬɫɹ
ɤɨɦɩɢɥɹɬɨɪɨɦ
ɚɜɬɨɦɚɬɢɱɟɫɤɢ. ɋɜɨɣɫɬɜɨ ɦɨɠɟɬ ɛɵɬɶ ɜɟɤɬɨɪɧɵɦ, ɧɚɩɪɢɦɟɪ, property Items[Index: integer]: real
read
GetItems write SetItems;
ɩɪɢ ɷɬɨɦ ɦɟɬɨɞɵ ɱɬɟɧɢɹ ɢ ɨɛɧɨɜɥɟɧɢɹ ɢɦɟɸɬ ɜɢɞ function GetItems (Index: integer): real; procedure SetItems (Index: integer; NewValue: real);
Ⱦɥɹ ɩɪɨɝɪɚɦɦɢɫɬɚ ɬɚɤɨɟ ɫɜɨɣɫɬɜɨ ɜɵɝɥɹɞɢɬ ɤɚɤ ɦɚɫɫɢɜ, ɤ ɷɥɟɦɟɧɬɚɦ ɤɨɬɨɪɨɝɨ
ɦɨɠɧɨ
ɨɛɪɚɳɚɬɶɫɹ
ɨɛɵɱɧɵɦ
ɨɛɪɚɡɨɦ
(ɧɚɩɪɢɦɟɪ,
Items[i]:=1.5), ɯɨɬɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɟɦɭ ɩɨɥɟ ɜ ɞɟɣɫɬɜɢɬɟɥɶɧɨɫɬɢ ɦɨɠɟɬ ɦɚɫɫɢɜɨɦ ɢ ɧɟ ɹɜɥɹɬɶɫɹ. Ɉɞɧɨɦɭ ɢɡ ɜɟɤɬɨɪɧɵɯ ɫɜɨɣɫɬɜ ɦɨɠɧɨ ɩɪɢɩɢɫɚɬɶ ɚɬɪɢɛɭɬ default, ɬɨɝɞɚ ɩɪɢ ɨɛɪɚɳɟɧɢɢ ɤ ɧɟɦɭ ɢɦɹ ɫɜɨɣɫɬɜɚ ɦɨɠ-
13
ɧɨ ɧɟ ɭɤɚɡɵɜɚɬɶ, ɧɚɩɪɢɦɟɪ MyObject.Items[i]:=1.5 ɛɭɞɟɬ ɷɤɜɢɜɚɥɟɧɬɧɨ MyObject[i]:=1.5. 2.3. Ɍɢɩɵ ɫɜɨɣɫɬɜ ȼɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɨɩɭɛɥɢɤɨɜɚɧɧɵɟ ɫɜɨɣɫɬɜɚ ɤɨɦɩɨɧɟɧɬɚ ɩɨɹɜɥɹɸɬɫɹ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ. ɋɩɨɫɨɛɵ ɨɬɨɛɪɚɠɟɧɢɹ ɢ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɫɜɨɣɫɬɜɚ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ ɡɚɜɢɫɹɬ ɨɬ ɬɢɩɚ ɫɜɨɣɫɬɜɚ. ȼ Delphi ɫɭɳɟɫɬɜɭɟɬ ɧɟɫɤɨɥɶɤɨ ɫɬɚɧɞɚɪɬɧɵɯ ɪɟɞɚɤɬɨɪɨɜ ɫɜɨɣɫɬɜ. Ɋɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ ɨɩɪɟɞɟɥɹɸɬ ɮɭɧɤɰɢɨɧɢɪɨɜɚɧɢɟ ɩɪɚɜɨɣ ɱɚɫɬɢ ɫɬɪɚɧɢɰɵ Property ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ. ɇɚɢɛɨɥɟɟ ɪɚɫɩɪɨɫɬɪɚɧɟɧɧɵɟ ɪɟɞɚɤɬɨɪɵ ɨɩɢɫɚɧɵ ɜ ɬɚɛɥɢɰɟ 3. Ɋɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ, ɞɨɫɬɭɩɧɵɟ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ Ɍɢɩ ɫɜɨɣɫɬɜɚ ɉɪɨɫɬɨɣ
Ɉɩɢɫɚɧɢɟ ɪɟɞɚɤɬɨɪɚ ɫɜɨɣɫɬɜ
Ɍɚɛɥɢɰɚ 3 ɉɪɢɦɟɪ
ɉɪɨɫɬɵɟ ɡɧɚɱɟɧɢɹ ɫɜɨɣɫɬɜ, ɬɚɤɢɟ ɤɚɤ ɱɢɫɥɚ, Height ɫɬɪɨɤɢ ɢ ɬɨɦɭ ɩɨɞɨɛɧɨɟ, ɪɟɞɚɤɬɢɪɭɸɬɫɹ ɩɪɹɦɨ ɜ ɫɬɪɨɤɟ ɫɜɨɣɫɬɜɚ
ɉɟɪɟɱɢɫɥɢ-
ȼɫɟ ɩɟɪɟɱɢɫɥɢɦɵɟ ɡɧɚɱɟɧɢɹ, ɨɩɪɟɞɟɥɟɧɧɵɟ Color
ɦɵɣ
ɞɥɹ ɬɢɩɚ, ɨɬɨɛɪɚɠɚɸɬɫɹ ɜ ɨɤɧɟ ɤɨɦɛɢɧɢɪɨɜɚɧɧɨɝɨ ɫɩɢɫɤɚ. Ɂɧɚɱɟɧɢɹ ɜ ɩɟɪɟɱɢɫɥɟɧɢɢ ɦɨɠɧɨ ɩɟɪɟɛɢɪɚɬɶ, ɞɜɚɠɞɵ ɳɟɥɤɚɹ ɜ ɫɬɪɨɤɟ ɫɜɨɣɫɬɜɚ
Ɇɧɨɠɟɫɬɜɨ
ȼ ɫɬɪɨɤɟ ɫɜɨɣɫɬɜɚ ɬɢɩɵ ɦɧɨɠɟɫɬɜ ɩɨɹɜɥɹ- Anchors ɸɬɫɹ ɤɚɤ ɦɧɨɠɟɫɬɜɚ Pascal. Ʉɚɠɞɵɣ ɷɥɟɦɟɧɬ ɦɧɨɠɟɫɬɜɚ ɩɪɟɞɫɬɚɜɥɟɧ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ ɨɬɞɟɥɶɧɵɦ ɫɜɨɣɫɬɜɨɦ, ɩɪɢɧɢɦɚɸɳɢɦ ɡɧɚɱɟɧɢɹ True ɢ False
Ɉɛɴɟɤɬ
ɇɚɡɜɚɧɢɟ ɬɢɩɚ ɩɨɹɜɥɹɟɬɫɹ ɜ ɫɬɪɨɤɟ ɫɜɨɣɫɬɜɚ Font ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ. ȿɫɥɢ ɨɛɴɟɤɬ ɢɦɟɟɬ 14
ɩɭɛɥɢɤɭɟɦɵɟ ɫɜɨɣɫɬɜɚ, ɬɨ ɫɜɨɣɫɬɜɨ ɨɛɴɟɤɬɚ ɜ ɂɧɫɩɟɤɬɨɪɟ ɪɚɫɲɢɪɹɟɬɫɹ ɢ ɩɨɹɜɥɹɟɬɫɹ ɜɨɡɦɨɠɧɨɫɬɶ ɨɬɪɟɞɚɤɬɢɪɨɜɚɬɶ ɤɚɠɞɨɟ ɩɨɞɫɜɨɣɫɬɜɨ (ɧɚɩɪɢɦɟɪ, ɧɚɡɜɚɧɢɟ ɲɪɢɮɬɚ) ɉɨɞɱɟɪɤɧɟɦ, ɟɫɥɢ ɫɨɡɞɚɧɧɵɟ ɜ ɜɚɲɟɦ ɤɨɦɩɨɧɟɧɬɟ ɫɜɨɣɫɬɜɚ ɢɦɟɸɬ ɬɢɩɵ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɬɚɛɥɢɰɟ, ɬɨ ɩɪɢɥɚɝɚɬɶ ɞɨɩɨɥɧɢɬɟɥɶɧɵɟ ɭɫɢɥɢɹ ɢ ɫɨɡɞɚɜɚɬɶ ɪɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ ɧɟɬ ɨɫɨɛɨɣ ɧɟɨɛɯɨɞɢɦɨɫɬɢ, Delphi ɜɫɟ ɫɞɟɥɚɟɬ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ. ȼ ɱɚɫɬɧɨɫɬɢ, ɫɬɚɧɞɚɪɬɧɵɟ ɪɟɞɚɤɬɨɪɵ ɜɵɡɵɜɚɸɬɫɹ ɩɪɢ ɩɨɩɵɬɤɟ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɫɜɨɣɫɬɜ ɬɢɩɨɜ TFont, TStrings, TPicture ɢ ɧɟɤɨɬɨɪɵɯ ɞɪɭɝɢɯ. ȼ ɛɨɥɟɟ ɫɥɨɠɧɵɯ ɫɥɭɱɚɹɯ, ɧɚɩɪɢɦɟɪ, ɩɪɢ ɫɨɡɞɚɧɢɢ ɜɟɤɬɨɪɧɵɯ ɫɜɨɣɫɬɜ, ɫɥɟɞɭɟɬ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɫɨɛɫɬɜɟɧɧɵɟ ɪɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ, ɢɧɚɱɟ ɫɜɨɣɫɬɜɨ ɧɟ ɫɦɨɠɟɬ ɛɵɬɶ ɨɬɨɛɪɚɠɟɧɨ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ. ɋɬɚɧɞɚɪɬɧɵɟ ɪɟɞɚɤɬɨɪɵ ɫɜɨɣɫɬɜ ɜɵɩɨɥɧɟɧɵ ɜ ɜɢɞɟ ɤɥɚɫɫɨɜ. ɂɧɬɟɪɮɟɣɫɵ ɤ ɤɥɚɫɫɚɦ ɧɚɯɨɞɹɬɫɹ ɜ ɦɨɞɭɥɟ Dsgnintf.pas (ɫɦ. ɤɚɬɚɥɨɝ ..\Delphi7\Source\ToolsApi). ɂɡɭɱɟɧɢɟ ɷɬɨɝɨ ɦɨɞɭɥɹ – ɧɟɨɛɯɨɞɢɦɵɣ ɷɬɚɩ ɩɪɢ ɧɚɩɢɫɚɧɢɢ ɫɨɛɫɬɜɟɧɧɵɯ ɪɟɞɚɤɬɨɪɨɜ ɫɜɨɣɫɬɜ. Ȼɚɡɨɜɵɦ ɹɜɥɹɟɬɫɹ ɤɥɚɫɫ TPropertyEditor. ȿɝɨ ɩɨɬɨɦɤɚɦɢ ɹɜɥɹɸɬɫɹ ɬɢɩɢɡɢɪɨɜɚɧɧɵɟ ɤɥɚɫɫɵ TIntegerProperty, TColorProperty, TCharProperty, TstringProperty,
TSetProperty, TFontNameProperty,
TFloatProperty, TCaptionProperty,
Tfont-Property ɢ ɞɪ. ɇɚɡɧɚɱɟɧɢɹ ɤɥɚɫɫɨɜ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɧɚɡɜɚɧɢɸ. Ʉɥɚɫɫ TFloatProperty ɫɜɹɡɚɧ ɫɨ ɫɜɨɣɫɬɜɚɦɢ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɬɢɩ Float, ɤɥɚɫɫ TFontProperty ɫɜɹɡɚɧ ɫɨ ɫɜɨɣɫɬɜɚɦɢ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɬɢɩ TFont ɢ ɬ. ɩ. Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɧɨɜɨɝɨ ɪɟɞɚɤɬɨɪɚ ɫɜɨɣɫɬɜ ɧɟɨɛɯɨɞɢɦɨ ɜɵɛɪɚɬɶ ɜ ɤɚɱɟɫɬɜɟ ɩɪɟɞɤɚ ɥɢɛɨ ɨɞɢɧ ɢɡ ɬɢɩɢɡɢɪɨɜɚɧɧɵɯ ɤɥɚɫɫɨɜ, ɥɢɛɨ, ɟɫɥɢ ɩɨɞɯɨɞɹɳɟ-
15
ɝɨ ɤɥɚɫɫɚ ɧɟɬ, ɜɵɛɪɚɬɶ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ TPropertyEditor ɢ ɩɟɪɟɨɩɪɟɞɟɥɢɬɶ ɧɟɤɨɬɨɪɵɟ ɟɝɨ ɦɟɬɨɞɵ. Ȼɨɥɟɟ ɩɨɞɪɨɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɫɦ. ɜ [3, 5]. 2.4. Ɇɟɬɨɞɵ ɉɨɫɤɨɥɶɤɭ ɦɟɬɨɞɵ ɞɨɫɬɭɩɧɵ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɤɨɦɩɨɧɟɧɬ, ɜɚɠɧɨ ɩɪɚɜɢɥɶɧɨ ɢɯ ɢɦɟɧɨɜɚɬɶ. ɂɦɟɧɚ ɦɟɬɨɞɨɜ ɞɨɥɠɧɵ ɛɵɬɶ ɨɩɢɫɚɬɟɥɶɧɵɦɢ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɚɤɬɢɜɧɵɟ ɝɥɚɝɨɥɵ. ɉɨɞɨɛɧɵɦ ɠɟ ɨɛɪɚɡɨɦ ɢɦɟɧɚ ɮɭɧɤɰɢɣ ɞɨɥɠɧɵ ɭɤɚɡɵɜɚɬɶ ɧɚ ɬɨ, ɱɬɨ ɨɧɢ ɜɨɡɜɪɚɳɚɸɬ. ȼɨɬ ɧɟɫɤɨɥɶɤɨ ɩɪɢɦɟɪɨɜ ɩɨɞɯɨɞɹɳɢɯ ɢ ɧɟɩɨɞɯɨɞɹɳɢɯ ɢɦɟɧ ɦɟɬɨɞɨɜ [2]: ɉɨɞɯɨɞɹɳɢɟ
ɇɟɩɨɞɯɨɞɹɳɢɟ
Procedure CutToClipboard
procedure Cut
Procedure SetHeight
procedure SetH
function GetTextLen
function Len
Ɇɟɬɨɞɵ ɦɨɝɭɬ ɪɚɡɦɟɳɚɬɶɫɹ ɜ ɪɚɡɞɟɥɚɯ public, protected ɢɥɢ private ɨɛɴɹɜɥɟɧɢɹ ɤɨɦɩɨɧɟɧɬɚ. Ɋɟɲɟɧɢɟ ɨ ɬɨɦ, ɝɞɟ ɫɥɟɞɭɟɬ ɪɚɡɦɟɫɬɢɬɶ ɤɨɧɤɪɟɬɧɵɣ ɦɟɬɨɞ, ɡɚɜɢɫɢɬ ɨɬ ɬɨɝɨ, ɤɚɤ ɨɧ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ. Ɇɟɬɨɞɵ, ɞɨɫɬɭɩɧɵɟ ɩɨɥɶɡɨɜɚɬɟɥɸ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ, ɞɨɥɠɧɵ ɪɚɡɦɟɳɚɬɶɫɹ ɜ ɪɚɡɞɟɥɟ public (ɨɛɳɟɞɨɫɬɭɩɧɵɣ). ɋɩɟɰɢɮɢɱɟɫɤɢɟ, ɡɚɜɢɫɹɳɢɟ ɨɬ ɪɟɚɥɢɡɚɰɢɢ ɦɟɬɨɞɵ ɞɨɥɠɧɵ ɪɚɡɦɟɳɚɬɶɫɹ, ɩɨ ɦɟɧɶɲɟɣ ɦɟɪɟ, ɜ ɪɚɡɞɟɥɟ protected (ɡɚɳɢɳɟɧɧɵɣ). ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɩɨɥɶɡɨɜɚɬɟɥɶ ɤɨɦɩɨɧɟɧɬɚ ɧɟ ɫɦɨɠɟɬ ɫɥɭɱɚɣɧɨ ɜɵɩɨɥɧɢɬɶ ɦɟɬɨɞɵ, ɡɚɜɢɫɹɳɢɟ ɨɬ ɪɟɚɥɢɡɚɰɢɢ. Ɇɟɬɨɞɵ, ɤɨɬɨɪɵɟ ɞɨɥɠɧɵ ɛɵɬɶ ɞɨɫɬɭɩɧɵ ɜ ɩɨɪɨɠɞɟɧɧɵɯ ɤɥɚɫɫɚɯ, ɫɥɟɞɭɟɬ ɪɚɫɩɨɥɚɝɚɬɶ ɜ ɪɚɡɞɟɥɟ protected. Ɂɚɦɟɱɚɧɢɟ. Ɇɟɬɨɞɵ, ɨɛɴɹɜɥɟɧɧɵɟ ɭ ɩɪɟɞɤɚ ɫ ɚɬɪɢɛɭɬɚɦɢ virtual, dynamic
ɢɥɢ
abstract,
ɫɥɟɞɭɟɬ
ɩɟɪɟɨɩɪɟɞɟɥɹɬɶ
ɫ
ɚɬɪɢɛɭɬɨɦ
override. ȼɫɟ ɤɨɦɩɨɧɟɧɬɵ Delphi ɞɨɥɠɧɵ ɢɦɟɬɶ ɦɟɬɨɞ-ɤɨɧɫɬɪɭɤɬɨɪ, ɢɫɩɨɥɶɡɭɟɦɵɣ ɞɥɹ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɚ ɤɥɚɫɫɚ ɢ ɢɦɟɸɳɢɣ ɫɥɟɞɭɸɳɢɣ ɜɢɞ: constructor Create(AOwner : TComponent); override; 16
Ʉɨɧɟɱɧɨ, ɧɟ ɤɚɠɞɵɣ ɤɨɦɩɨɧɟɧɬɧɵɣ ɤɥɚɫɫ ɧɭɠɞɚɟɬɫɹ ɜ ɨɩɪɟɞɟɥɟɧɢɢ ɧɨɜɨɝɨ ɤɨɧɫɬɪɭɤɬɨɪɚ. Ɇɨɠɧɨ ɭɧɚɫɥɟɞɨɜɚɬɶ ɤɨɧɫɬɪɭɤɬɨɪ ɨɬ ɩɪɟɞɤɚ. ȼ ɥɸɛɨɦ ɫɥɭɱɚɟ, ɤɨɧɫɬɪɭɤɬɨɪ ɫɨɞɟɪɠɢɬ ɩɚɪɚɦɟɬɪ ɬɢɩɚ TComponent, ɭɤɚɡɵɜɚɸɳɢɣ ɧɚ ɜɥɚɞɟɥɶɰɚ (Owner) ɤɨɦɩɨɧɟɧɬɚ. ȼɥɚɞɟɥɟɰ – ɷɬɨ ɤɨɦɩɨɧɟɧɬ, ɜɵɡɵɜɚɸɳɢɣ ɩɪɢ ɫɜɨɟɦ ɭɧɢɱɬɨɠɟɧɢɢ ɞɟɫɬɪɭɤɬɨɪɵ ɩɨɞɱɢɧɟɧɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ. Ⱦɥɹ ɤɨɦɩɨɧɟɧɬɨɜ ɤɨɧɫɬɪɭɤɬɨɪ Create – ɷɬɨ ɢ ɟɫɬɶ ɦɟɫɬɨ, ɝɞɟ ɭɫɬɚɧɚɜɥɢɜɚɸɬɫɹ ɡɧɚɱɟɧɢɹ ɫɜɨɣɫɬɜ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɩɪɢɦɟɪ: constructor TSampleControl.Create = class(TWinControl) begin inherited Create(AOwner) ; FHeight := 25;
{ɍɫɬɚɧɨɜɤɚ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ}
FWidth := 50; FColor := clWindows; end;
ɇɟɨɛɯɨɞɢɦɨ ɩɨɫɥɟ ɨɛɴɹɜɥɟɧɢɹ ɤɨɧɫɬɪɭɤɬɨɪɚ ɡɚɞɚɬɶ ɞɢɪɟɤɬɢɜɭ override. ɉɪɨɩɭɫɤ ɷɬɨɣ ɞɢɪɟɤɬɢɜɵ ɫɢɧɬɚɤɫɢɱɟɫɤɢ ɡɚɤɨɧɟɧ, ɧɨ ɟɫɥɢ ɬɚɤ ɫɞɟɥɚɬɶ, ɬɨ ɤɨɦɩɨɧɟɧɬ ɧɟɥɶɡɹ ɛɭɞɟɬ ɩɨɦɟɫɬɢɬɶ ɧɚ ɮɨɪɦɭ. Ɉɬɦɟɬɢɦ, ɱɬɨ ɜ ɩɟɪɟɨɩɪɟɞɟɥɟɧɧɨɦ ɤɨɧɫɬɪɭɤɬɨɪɟ ɩɟɪɜɵɦ ɞɨɥɠɟɧ ɜɵɡɵɜɚɬɶɫɹ ɤɨɧɫɬɪɭɤɬɨɪ ɩɪɟɞɤɚ, ɫɧɚɛɠɟɧɧɵɣ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ inherited (ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ), ɞɚɥɟɟ ɡɚɩɢɫɵɜɚɸɬɫɹ ɨɩɟɪɚɰɢɢ, ɫɩɟɰɢɮɢɱɧɵɟ ɞɥɹ ɞɚɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ. ȿɫɥɢ ɭɞɚɥɟɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɢɡ ɩɚɦɹɬɢ ɞɨɥɠɧɨ ɫɨɩɪɨɜɨɠɞɚɬɶɫɹ ɞɨɩɨɥɧɢɬɟɥɶɧɵɦɢ ɞɟɣɫɬɜɢɹɦɢ, ɬɨ ɜ ɤɨɦɩɨɧɟɧɬɟ ɞɨɥɠɟɧ ɛɵɬɶ ɩɟɪɟɨɩɪɟɞɟɥɟɧ ɞɟɫɬɪɭɤɬɨɪ: destructor Destroy; override;
ȼ ɞɟɫɬɪɭɤɬɨɪɟ, ɭɧɢɱɬɨɠɚɸɳɟɦ ɷɤɡɟɦɩɥɹɪ ɤɨɦɩɨɧɟɧɬɚ, ɩɨɪɹɞɨɤ ɞɟɣɫɬɜɢɣ ɨɛɪɚɬɧɵɣ: destructor TSampleControl.Destroy; begin 17
// ɡɚɜɟɪɲɚɸɳɢɟ ɨɩɟɪɚɰɢɢ, // ɨɫɜɨɛɨɠɞɟɧɢɟ ɩɚɦɹɬɢ, // ɜɵɡɨɜ ɞɟɫɬɪɭɤɬɨɪɨɜ ɨɛɴɟɤɬɨɜ, ɫɨɡɞɚɧɧɵɯ // ɜ ɫɨɫɬɚɜɟ ɤɨɦɩɨɧɟɧɬɚ ɢ ɬ.ɩ. … // ɩɨɫɥɟɞɧɢɦ ɜɵɡɵɜɚɟɬɫɹ ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ ɞɟɫɬɪɭɤɬɨɪ inherited Destroy; end;
ɇɚ ɩɪɚɤɬɢɤɟ ɜɦɟɫɬɨ Destroy ɨɛɵɱɧɨ ɜɵɡɵɜɚɸɬ ɦɟɬɨɞ Free, ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ ɨɬ TObject. Ɇɟɬɨɞ Free ɛɟɡɨɩɚɫɧɟɟ, ɬɚɤ ɤɚɤ ɩɟɪɟɞ ɜɵɡɨɜɨɦ ɞɟɫɬɪɭɤɬɨɪɚ ɨɧ ɩɪɨɜɟɪɹɟɬ ɧɚɥɢɱɢɟ ɤɨɦɩɨɧɟɧɬɚ ɜ ɩɚɦɹɬɢ: if Self<>nil then Destroy;
2.5. ɋɨɛɵɬɢɹ ɋɨɛɵɬɢɟ ɤɨɦɩɨɧɟɧɬɚ – ɷɬɨ ɟɝɨ ɫɜɨɣɫɬɜɨ ɩɪɨɰɟɞɭɪɧɨɝɨ ɬɢɩɚ, ɩɪɟɞɧɚɡɧɚɱɟɧɧɨɟ ɞɥɹ ɫɨɡɞɚɧɢɹ ɪɟɚɤɰɢɢ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɬɟ ɢɥɢ ɢɧɵɟ ɜɧɟɲɧɢɟ ɜɨɡɞɟɣɫɬɜɢɹ (ɫɨ ɫɬɨɪɨɧɵ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɚɩɩɚɪɚɬɭɪɵ ɢɥɢ ɞɪɭɝɢɯ ɩɪɨɝɪɚɦɦ). Ɍɚɤɨɦɭ ɫɜɨɣɫɬɜɭ ɧɟɨɛɯɨɞɢɦɨ ɩɪɢɫɜɨɢɬɶ ɚɞɪɟɫ ɦɟɬɨɞɚ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɵɡɵɜɚɬɶɫɹ ɜ ɦɨɦɟɧɬ ɧɚɫɬɭɩɥɟɧɢɹ ɫɨɛɵɬɢɹ ɫ ɞɚɧɧɵɦ ɤɨɦɩɨɧɟɧɬɨɦ. ɋɨɛɵɬɢɹ ɢɦɟɸɬ ɪɚɡɥɢɱɧɨɟ ɱɢɫɥɨ ɩɚɪɚɦɟɬɪɨɜ. ȼ ɩɪɨɫɬɟɣɲɟɦ ɫɥɭɱɚɟ ɩɚɪɚɦɟɬɪ ɨɞɢɧ – Sender ɬɢɩɚ TObject, ɭɤɚɡɵɜɚɸɳɢɣ ɧɚ ɨɛɴɟɤɬɢɫɬɨɱɧɢɤ ɫɨɛɵɬɢɹ. ɋɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɨɛɵɬɢɹ, ɧɚɩɪɢɦɟɪ ɫɨɛɵɬɢɹ OnClick ɢ OnDblClick, ɢɦɟɸɬ ɬɢɩ type TNotifyEvent = procedure (Sender: TObject) of object;
ȼ ɨɩɢɫɚɧɢɟ ɩɪɨɰɟɞɭɪɧɨɝɨ ɬɢɩɚ ɞɥɹ ɦɟɬɨɞɚ ɧɟɨɛɯɨɞɢɦɨ ɞɨɛɚɜɥɹɬɶ ɡɚɪɟɡɟɪɜɢɪɨɜɚɧɧɵɟ ɫɥɨɜɚ of object. Ɉɛɵɱɧɵɣ ɩɪɨɰɟɞɭɪɧɵɣ ɬɢɩ ɢ ɩɪɨɰɟɞɭɪɧɵɣ ɬɢɩ ɞɥɹ ɦɟɬɨɞɚ ɧɟɫɨɜɦɟɫɬɢɦɵ, ɬɚɤ ɤɚɤ ɜ ɦɟɬɨɞ, ɤɪɨɦɟ ɨɩɢɫɚɧɧɵɯ ɩɚɪɚɦɟɬɪɨɜ, ɧɟɹɜɧɨ ɩɟɪɟɞɚɟɬɫɹ ɩɚɪɚɦɟɬɪ Self – ɭɤɚɡɚɬɟɥɶ ɧɚ ɜɵɡɜɚɜ18
ɲɢɣ ɟɝɨ ɨɛɴɟɤɬ. ɉɨɷɬɨɦɭ ɫɨɛɵɬɢɸ ɦɨɠɧɨ ɩɪɢɫɜɚɢɜɚɬɶ ɬɨɥɶɤɨ ɦɟɬɨɞɵ ɤɥɚɫɫɚ, ɫɨɜɦɟɫɬɢɦɵɟ ɩɨ ɱɢɫɥɭ ɢ ɬɢɩɭ ɩɚɪɚɦɟɬɪɨɜ. Ɍɢɩ TNotifyEvent ɢɦɟɟɬ, ɜ ɱɚɫɬɧɨɫɬɢ, ɫɨɛɵɬɢɟ ɳɟɥɱɤɚ ɦɵɲɶɸ ɧɚ ɤɨɦɩɨɧɟɧɬɟ. ȼ ɞɪɭɝɢɯ ɫɨɛɵɬɢɹɯ ɦɨɝɭɬ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɞɨɩɨɥɧɢɬɟɥɶɧɵɟ ɩɚɪɚɦɟɬɪɵ, ɧɚɩɪɢɦɟɪ, ɞɥɹ ɫɨɛɵɬɢɹ ɩɟɪɟɦɟɳɟɧɢɹ ɭɤɚɡɚɬɟɥɹ ɦɵɲɢ OnMouseMove ɨɩɢɫɚɧɢɟ ɬɢɩɚ ɫɨɛɵɬɢɹ ɢɦɟɟɬ ɜɢɞ type TMouseMoveEvent = procedure(Sender: TObject; Shift: TShiftState; X, Y:
Integer) of object;
ɝɞɟ ɩɚɪɚɦɟɬɪ Shift ɩɨɡɜɨɥɹɟɬ ɭɡɧɚɬɶ, ɛɵɥɢ ɥɢ ɧɚɠɚɬɵ ɩɪɢ ɩɟɪɟɦɟɳɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɭɩɪɚɜɥɹɸɳɢɟ ɤɥɚɜɢɲɢ ɢɥɢ ɤɧɨɩɤɢ ɦɵɲɢ; ɩɚɪɚɦɟɬɪɵ X ɢ Y ɫɨɞɟɪɠɚɬ ɧɨɜɵɟ ɤɨɨɪɞɢɧɚɬɵ ɭɤɚɡɚɬɟɥɹ ɦɵɲɢ ɜ ɤɥɢɟɧɬɫɤɨɣ ɨɛɥɚɫɬɢ ɨɛɴɟɤɬɚ Sender. ɋɨɛɵɬɢɹ
ɧɚɠɚɬɢɹ/ɨɬɩɭɫɤɚɧɢɹ
ɤɧɨɩɤɢ
ɦɵɲɢ
OnMouse-
Down/OnMouseUp ɢɦɟɸɬ ɬɢɩ type TMouseEvent = procedure(Sender: TObject; Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer) of object;
ɝɞɟ Button: TMouseButton – ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɤɧɨɩɤɢ, TMouseButton = (mbLeft, mbRight, mbMiddle).
ɇɚɠɚɬɢɟ ɢ ɨɬɩɭɫɤɚɧɢɟ ɤɥɚɜɢɲ ɤɥɚɜɢɚɬɭɪɵ OnKeyDown: type TKeyEvent = procedure(Sender: TObject; var Key: Word; Shift: TShiftState) of object;
ɝɞɟ Key – ɜɢɪɬɭɚɥɶɧɵɣ ɤɨɞ Windows ɧɚɠɚɬɨɣ ɤɥɚɜɢɲɢ. Cɨɛɵɬɢɟ OnKeyPressed ɜɨɡɧɢɤɚɟɬ ɩɪɢ ɜɜɨɞɟ ɫ ɤɥɚɜɢɚɬɭɪɵ ɫɢɦɜɨɥɚ ASCII. Ɂɞɟɫɶ Key – ASCII-ɤɨɞ ɧɚɠɚɬɨɣ ɤɥɚɜɢɲɢ ɢ ɢɦɟɟɬ ɬɢɩ
19
type TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;
Ɂɚɦɟɬɢɦ, ɱɬɨ ɫɨɨɛɳɟɧɢɹ ɨɬ ɤɥɚɜɢɚɬɭɪɵ ɩɨɫɬɭɩɚɸɬ ɬɨɦɭ ɷɥɟɦɟɧɬɭ, ɤɨɬɨɪɵɣ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɢɦɟɥ ɮɨɤɭɫ ɜɜɨɞɚ. ȿɫɥɢ ɭ ɮɨɪɦɵ, ɜɥɚɞɟɸɳɟɣ ɷɥɟɦɟɧɬɨɦ ɭɩɪɚɜɥɟɧɢɹ, ɭɫɬɚɧɨɜɢɬɶ ɡɧɚɱɟɧɢɟ ɫɜɨɣɫɬɜɚ KeyPreview ɜ True, ɬɨ ɫɨɛɵɬɢɹ ɛɭɞɭɬ ɫɧɚɱɚɥɚ ɨɛɪɚɛɨɬɚɧɵ ɮɨɪɦɨɣ. ɉɟɪɟɱɟɧɶ ɬɢɩɨɜ ɫɬɚɧɞɚɪɬɧɵɯ ɫɨɛɵɬɢɣ ɫɦ. ɜ ɮɚɣɥɟ controls.pas ɜ ɤɚɬɚɥɨɝɟ ..\Delphi7\Source\VCL. ɋɨɜɦɟɫɬɢɦɵɟ ɫɨɛɵɬɢɹ ɦɨɠɧɨ ɩɪɢɫɜɚɢɜɚɬɶ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɩɪɨɝɪɚɦɦɵ, ɧɚɩɪɢɦɟɪ, Button1.OnMouseMove:=Button2.OnMouseMove; … Button1.OnClick:=Form1.OnPaint; … Edit1.OnChange:=Button1Click;
ȼ ɪɟɡɭɥɶɬɚɬɟ ɨɞɢɧ ɨɛɴɟɤɬ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɛɪɚɛɨɬɱɢɤɢ ɫɨɜɦɟɫɬɢɦɨɝɨ
ɫɨɛɵɬɢɹ
ɞɪɭɝɨɝɨ
ɨɛɴɟɤɬɚ.
ɗɬɚ
ɜɨɡɦɨɠɧɨɫɬɶ
ɧɚɡɵɜɚɟɬɫɹ
ɞɟɥɟɝɢɪɨɜɚɧɢɟɦ. ɂɬɚɤ, ɫɨɛɵɬɢɹ ɪɟɚɥɢɡɭɸɬɫɹ ɫ ɩɨɦɨɳɶɸ ɭɤɚɡɚɬɟɥɟɣ ɧɚ ɦɟɬɨɞɵ. Ⱦɥɹ ɤɚɠɞɨɝɨ ɫɨɛɵɬɢɹ ɨɩɪɟɞɟɥɹɸɬɫɹ ɫɜɨɣɫɬɜɚ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɂɧɫɩɟɤɬɨɪ ɨɛɴɟɤɬɨɜ ɞɥɹ ɧɚɡɧɚɱɟɧɢɹ ɫɨɛɵɬɢɹɦ ɨɛɪɚɛɨɬɱɢɤɨɜ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. ɉɨɹɫɧɢɦ ɫɤɚɡɚɧɧɨɟ ɧɚ ɩɪɢɦɟɪɟ. Ɋɚɫɫɦɨɬɪɢɦ ɫɥɟɞɭɸɳɢɣ ɮɪɚɝɦɟɧɬ ɤɨɞɚ, ɩɨɤɚɡɵɜɚɸɳɢɣ ɧɟɫɤɨɥɶɤɨ ɫɬɚɧɞɚɪɬɧɵɯ ɫɨɛɵɬɢɣ, ɨɛɴɹɜɥɟɧɧɵɯ ɜ ɤɥɚɫɫɟ TControl: type TControl = class(TComponent) private FOnClick : TNotifyEvent; FOnDblClick : TNotifyEvent 20
FOnMouseDown : TMouseEvent; FOnMouseMove : TMouseMiveEvent;
… protected procedure Click; dynamic; property OnClick: TNotifyEvent read FOnClick
write FOnClick;
property OnDblClick : TNotifyEvent read FOnDblClick write
OnDblClick;
property OnMouseDown : TMouseEvent read FOnMouseDown write FOnMouseDown; property OnMouseMove : TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
… end;
ɋɨɛɵɬɢɹ ɫɨɫɬɨɹɬ ɢɡ ɞɜɭɯ ɱɚɫɬɟɣ. ȼɨ-ɩɟɪɜɵɯ, ɞɥɹ ɫɨɛɵɬɢɹ ɬɪɟɛɭɟɬɫɹ ɜɧɭɬɪɟɧɧɟɟ ɩɨɥɟ ɞɚɧɧɵɯ (ɧɚɩɪɢɦɟɪ FOnClick), ɤɨɬɨɪɨɟ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɯɪɚɧɟɧɢɹ ɭɤɚɡɚɬɟɥɹ ɧɚ ɦɟɬɨɞ. ȼɨ-ɜɬɨɪɵɯ, ɫɨɡɞɚɟɬɫɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɫɜɨɣɫɬɜɨ (ɧɚɩɪɢɦɟɪ OnClick), ɤɨɬɨɪɨɟ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɩɪɟɞɨɫɬɚɜɥɹɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɢɫɨɟɞɢɧɟɧɢɹ ɨɛɪɚɛɨɬɱɢɤɨɜ ɫɨɛɵɬɢɣ. ɋɨɛɵɬɢɹ, ɨɛɳɢɟ ɞɥɹ ɜɫɟɯ ɤɨɦɩɨɧɟɧɬɨɜ: OnClick, OnDblClick, OnMouseDown, OnMouseMove, OnMouseUp, OnDragOver, OnDragDrop, OnEndDrag. ɋɨɛɵɬɢɹ ɨɤɨɧɧɵɯ ɷɥɟɦɟɧɬɨɜ ɭɩɪɚɜɥɟɧɢɹ: OnEnter, OnExit, OnKeyDown, OnKeyUp, OnKeyPress. ɉɨ ɭɦɨɥɱɚɧɢɸ ɜɫɟ ɞɚɧɧɵɟ ɫɨɛɵɬɢɹ ɜ ɤɥɚɫɫɟ TComponent ɨɛɴɹɜɥɟɧɵ ɜ ɫɟɤɰɢɢ protected. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɫɬɚɧɞɚɪɬɧɵɟ ɫɨɛɵɬɢɹ ɧɟ ɜɢɞɧɵ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɤɨɦɩɨɧɟɧɬɚ ɢ ɞɨɥɠɧɵ ɛɵɬɶ ɩɨɜɬɨɪɧɨ ɨɛɴɹɜɥɟɧɵ (ɨɩɭɛɥɢɤɨɜɚɧɵ) ɜ ɫɟɤɰɢɢ published. type 21
TEdit=class(TCustomEdit) private … published property OnClick; end;
Ɍɚɤ ɤɚɤ ɫɨɛɵɬɢɹ Delphi ɨɯɜɚɬɵɜɚɸɬ ɛɨɥɶɲɭɸ ɱɚɫɬɶ ɫɨɨɛɳɟɧɢɣ Windows, ɬɨ ɪɚɡɪɚɛɨɬɱɢɤɭ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɤɚɤ ɩɪɚɜɢɥɨ, ɛɵɜɚɟɬ ɞɨɫɬɚɬɨɱɧɨ ɨɩɭɛɥɢɤɨɜɚɬɶ ɜ ɫɜɨɟɦ ɤɨɦɩɨɧɟɧɬɟ ɫɨɛɵɬɢɹ ɩɪɟɞɤɚ. ȼ ɛɨɥɟɟ ɫɥɨɠɧɨɣ ɫɢɬɭɚɰɢɢ, ɤɨɝɞɚ ɢɦɟɸɳɢɯɫɹ ɫɬɚɧɞɚɪɬɧɵɯ ɫɨɛɵɬɢɣ ɧɟɞɨɫɬɚɬɨɱɧɨ, ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɧɨɜɵɟ ɫɨɛɵɬɢɹ. Ɇɟɬɨɞɢɤɚ ɦɨɠɟɬ ɨɬɥɢɱɚɬɶɫɹ ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɡɚɞɚɱɢ. Ɉɛɵɱɧɨ ɜ ɞɨɩɨɥɧɟɧɢɟ ɤ ɨɛɴɹɜɥɟɧɢɸ ɩɨɥɹ ɢ ɫɜɨɣɫɬɜɚ ɞɥɹ ɫɨɛɵɬɢɹ ɜɜɨɞɹɬ ɦɟɬɨɞɵ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɫɨɛɵɬɢɣ (eventdispatching methods). ɗɬɢ ɦɟɬɨɞɵ ɧɭɠɧɵ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɨɩɪɟɞɟɥɢɬɶ, ɫɨɡɞɚɧ ɥɢ ɤɨɞ ɨɛɪɚɛɨɬɤɢ ɩɪɨɢɡɨɲɟɞɲɟɝɨ ɫɨɛɵɬɢɹ ɞɥɹ ɞɚɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɢ ɬɨɝɞɚ ɨɛɴɹɜɥɹɸɬɫɹ ɤɚɤ ɡɚɳɢɳɟɧɧɵɟ (protected). ȼ ɤɥɚɫɫɟ TControl ɛɢɛɥɢɨɬɟɤɢ VCL ɞɥɹ ɫɜɨɣɫɬɜɚ OnClick ɨɩɪɟɞɟɥɟɧ ɦɟɬɨɞ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɫ ɧɚɡɜɚɧɢɟɦ Click: type TControl = class (TComponent) private FOnClick: TNotifyEvent; protected procedure Click; dynamic; property OnClick: TNotifyEvent read FOnClick write FOnClick; end;
implementation procedure TControl.Click; begin 22
if Assigned (FOnClick) then FOnClick (Self); end;
Ɇɟɬɨɞ Click ɩɪɟɞɧɚɡɧɚɱɟɧ ɞɥɹ ɜɵɡɨɜɚ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɝɨ ɨɛɪɚɛɨɬɱɢɤɚ ɫɨɛɵɬɢɹ, ɟɫɥɢ ɨɧ ɭɤɚɡɚɧ ɜ ɩɪɨɝɪɚɦɦɟ. Ɋɚɡɭɦɟɟɬɫɹ, Click ɬɚɤɠɟ ɞɨɥɠɟɧ ɛɵɬɶ ɝɞɟ-ɬɨ ɜɵɡɜɚɧ. ȼ ɫɥɭɱɚɟ TControl ɦɟɬɨɞ Click ɜɵɡɵɜɚɟɬɫɹ ɜɧɭɬɪɢ ɨɛɪɚɛɨɬɱɢɤɚ Windows-ɫɨɨɛɳɟɧɢɹ
WM_MOUSEDOWN.
ɇɨ ɦɨɝɭɬ ɛɵɬɶ ɢ
ɞɪɭɝɢɟ ɜɚɪɢɚɧɬɵ. Ȼɨɥɟɟ ɩɨɞɪɨɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɩɪɢ ɢɡɭɱɟɧɢɢ
ɦɨɞɭɥɹ Controls.pas
ɛɢɛɥɢɨɬɟɤɢ VCL (ɝɞɟ ɨɩɢɫɚɧ ɤɥɚɫɫ
TControl), ɚ ɬɚɤɠɟ ɜ [1–4]. 3. ɉɪɨɟɤɬɢɪɨɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ 3.1. ɗɬɚɩɵ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ Ɋɚɫɫɦɨɬɪɢɦ ɩɪɨɰɟɫɫ ɫɨɡɞɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɨɱɟɧɶ ɩɪɨɫɬɨɦ ɩɪɢɦɟɪɟ ɫɨɡɞɚɧɢɹ ɩɨɬɨɦɤɚ ɤɨɦɩɨɧɟɧɬɚ Button (Ʉɧɨɩɤɚ) [2]. ɉɪɨɰɟɫɫ, ɨɩɢɫɚɧɧɵɣ ɞɚɥɟɟ, ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɥɹ ɪɚɡɪɚɛɨɬɤɢ ɥɸɛɨɝɨ ɤɨɦɩɨɧɟɧɬɚ Delphi, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɟɝɨ ɫɥɨɠɧɨɫɬɢ. 1. ɉɪɨɰɟɫɫ ɧɚɱɢɧɚɟɬɫɹ ɫ ɫɨɡɞɚɧɢɹ ɤɚɬɚɥɨɝɚ, ɜ ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɡɚɩɢɫɚɧ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ ɢ ɬɟɫɬɨɜɨɟ ɩɪɢɥɨɠɟɧɢɟ. Ʉɨɦɩɨɧɟɧɬɵ ɫɬɪɨɹɬɫɹ ɜɧɭɬɪɢ ɦɨɞɭɥɟɣ Delphi ɫɩɨɫɨɛɨɦ, ɨɱɟɧɶ ɩɨɯɨɠɢɦ ɧɚ ɦɨɞɭɥɢ ɮɨɪɦ Delphi ɫ ɨɛɴɹɜɥɟɧɢɟɦ ɤɥɚɫɫɚ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ, ɩɨɹɜɥɹɸɳɢɦɫɹ ɜ ɪɚɡɞɟɥɟ ɢɧɬɟɪɮɟɣɫɚ ɢ ɨɩɪɟɞɟɥɟɧɢɟɦ ɦɟɬɨɞɨɜ, ɤɨɬɨɪɵɟ ɩɨɹɜɥɹɸɬɫɹ ɜ ɪɚɡɞɟɥɟ ɪɟɚɥɢɡɚɰɢɢ. 2. ɋɥɟɞɭɸɳɢɣ ɲɚɝ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɨɡɞɚɧɢɢ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. ɗɬɨ ɦɨɠɧɨ ɜɵɩɨɥɧɢɬɶ ɥɢɛɨ ɜɪɭɱɧɭɸ, ɥɢɛɨ ɡɚɞɟɣɫɬɜɨɜɚɜ ɗɤɫɩɟɪɬɚ ɤɨɦɩɨɧɟɧɬɨɜ (Component Expert). 3. ɉɨɫɥɟ ɬɨɝɨ ɤɚɤ ɨɩɪɟɞɟɥɟɧɵ ɛɚɡɨɜɵɟ ɷɥɟɦɟɧɬɵ ɦɨɞɭɥɹ, ɜɵɩɨɥɧɹɟɬɫɹ ɫɥɟɞɭɸɳɢɣ ɲɚɝ, ɡɚɤɥɸɱɚɸɳɢɣɫɹ ɜ ɨɛɴɹɜɥɟɧɢɢ ɤɥɚɫɫɚ ɢ ɧɚɩɢɫɚɧɢɢ ɩɨɞɞɟɪɠɢɜɚɸɳɢɯ ɦɟɬɨɞɨɜ.
23
4. Ⱦɚɥɟɟ ɤɨɦɩɨɧɟɧɬ ɦɨɠɧɨ ɬɟɫɬɢɪɨɜɚɬɶ. Ɍɚɤ ɤɚɤ ɭ ɤɨɦɩɨɧɟɧɬɚ ɟɫɬɶ ɞɜɚ ɢɧɬɟɪɮɟɣɫɚ – ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɢ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ, ɬɨ ɢ ɬɟɫɬɢɪɨɜɚɧɢɟ ɜɵɩɨɥɧɹɟɬɫɹ ɜ ɞɜɚ ɲɚɝɚ. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɦɨɠɧɨ ɞɟɥɚɬɶ ɫɪɚɡɭ ɠɟ ɩɨɫɥɟ ɨɤɨɧɱɚɧɢɹ ɤɨɞɢɪɨɜɚɧɢɹ, ɧɨ ɬɟɫɬɢɪɨɜɚɧɢɟ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɦɨɠɧɨ ɩɪɨɜɨɞɢɬɶ ɬɨɥɶɤɨ ɩɨɫɥɟ ɬɨɝɨ, ɤɚɤ ɤɨɦɩɨɧɟɧɬ ɛɭɞɟɬ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧ ɜ Delphi ɢ ɩɨɹɜɢɬɫɹ ɧɚ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ. Ɋɚɫɫɦɨɬɪɢɦ ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɲɚɝɢ ɛɨɥɟɟ ɩɨɞɪɨɛɧɨ. ɉɨɧɚɱɚɥɭ ɤɚɬɚɥɨɝ ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɨɞɢɧ ɮɚɣɥ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ, ɧɨ ɧɚ ɫɬɚɞɢɢ ɬɟɫɬɢɪɨɜɚɧɢɹ ɜ ɤɚɬɚɥɨɝɟ ɩɨɹɜɹɬɫɹ ɜɫɟ ɮɚɣɥɵ, ɚɫɫɨɰɢɢɪɭɸɳɢɟɫɹ ɫ ɨɬɞɟɥɶɧɵɦ ɩɪɨɟɤɬɨɦ Delphi. ɂɫɯɨɞɧɵɣ ɤɨɞ ɤɨɦɩɨɧɟɧɬɚ ɪɚɫɩɨɥɚɝɚɟɬɫɹ ɜ ɦɨɞɭɥɟ. ɏɨɬɹ ɜɨɡɦɨɠɧɨ ɩɨɦɟɫɬɢɬɶ ɜ ɟɞɢɧɫɬɜɟɧɧɵɣ ɦɨɞɭɥɶ ɥɸɛɨɟ ɤɨɥɢɱɟɫɬɜɨ ɤɨɦɩɨɧɟɧɬɨɜ, ɨɛɵɱɧɨ ɜ ɦɨɞɭɥɶ ɩɨɦɟɳɚɸɬɫɹ ɬɨɥɶɤɨ ɷɥɟɦɟɧɬɵ ɭɩɪɚɜɥɟɧɢɹ, ɛɥɢɡɤɢɟ ɩɨ ɫɦɵɫɥɭ. ɋɭɳɟɫɬɜɭɟɬ ɞɜɚ ɫɩɨɫɨɛɚ ɫɨɡɞɚɧɢɹ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ – «ɪɭɱɧɨɣ» ɢ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɷɤɫɩɟɪɬɚ ɤɨɦɩɨɧɟɧɬɨɜ. ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɭɩɪɨɳɚɟɬ ɡɚɞɚɱɭ ɫɨɡɞɚɧɢɹ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. Ɉɧ ɝɟɧɟɪɢɪɭɟɬ ɫɢɧɬɚɤɫɢɱɟɫɤɢ ɤɨɪɪɟɤɬɧɵɣ ɦɨɞɭɥɶ Delphi, ɫɨɞɟɪɠɚɳɢɣ ɛɚɡɨɜɭɸ ɪɟɚɥɢɡɚɰɢɸ ɤɨɦɩɨɧɟɧɬɚ. Ɉɧ ɬɚɤɠɟ ɫɨɡɞɚɟɬ «ɡɚɝɥɭɲɤɢ», ɜɤɥɸɱɚɸɳɢɟ ɩɭɫɬɨɟ ɨɛɴɹɜɥɟɧɢɟ ɤɥɚɫɫɚ ɢ ɩɪɨɰɟɞɭɪɭ Register, ɚ ɬɚɤɠɟ ɡɚɩɨɥɧɟɧɧɵɣ ɪɚɡɞɟɥ uses ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. ɗɤɫɩɟɪɬ ɧɟ ɝɟɧɟɪɢɪɭɟɬ ɧɢɤɚɤɨɣ ɮɭɧɤɰɢɨɧɚɥɶɧɨɫɬɢ, ɨɛɟɫɩɟɱɢɜɚɹ ɬɨɥɶɤɨ ɤɚɪɤɚɫ ɢɥɢ ɮɭɧɞɚɦɟɧɬ, ɧɚ ɤɨɬɨɪɨɦ ɫɬɪɨɢɬɫɹ ɤɨɦɩɨɧɟɧɬ. ɂɦɹ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɜɵɛɪɚɧɨ ɞɥɹ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ, ɛɭɞɟɬ ɩɪɟɞɩɢɫɵɜɚɬɶ ɫɩɨɫɨɛ ɨɛɪɚɳɟɧɢɹ ɤ ɧɟɦɭ ɜ ɫɪɟɞɟ Delphi. ȿɫɥɢ ɢɦɹ ɤɥɚɫɫɚ ɧɚɱɢɧɚɟɬɫɹ ɫ Ɍ, ɬɨ Delphi ɭɛɢɪɚɟɬ ɩɟɪɜɵɣ ɫɢɦɜɨɥ ɢ ɢɫɩɨɥɶɡɭɟɬ ɨɫɬɚɜɲɭɸɫɹ ɫɬɪɨɤɭ ɤɚɤ ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ. ȿɫɥɢ ɢɦɹ ɤɥɚɫɫɚ ɧɟ ɧɚɱɢɧɚɟɬɫɹ ɫ Ɍ, ɬɨ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜɫɟ ɢɦɹ. Ɋɟɡɭɥɶɬɢɪɭɸɳɟɟ ɢɦɹ ɨɬɨɛɪɚɠɚɟɬɫɹ ɜ ɜɢɞɟ ɩɨɞɫɤɚɡɤɢ, ɟɫɥɢ ɩɨɡɢɰɢɨɧɢɪɨɜɚɬɶ ɤɭɪɫɨɪ ɧɚɞ ɤɨɦɩɨɧɟɧɬɨɦ ɜ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ. Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ Delphi ɦɨɝɥɚ ɭɫɬɚɧɨɜɢɬɶ ɤɨɦɩɨɧɟɧɬ, ɟɝɨ ɢɦɹ ɞɨɥɠɧɨ ɛɵɬɶ ɭɧɢɤɚɥɶɧɵɦ. 24
ȼ ɞɨɩɨɥɧɟɧɢɟ ɤ ɜɵɛɨɪɭ ɢɦɟɧɢ ɤɨɦɩɨɧɟɧɬɚ ɧɟɨɛɯɨɞɢɦɨ ɜɵɛɪɚɬɶ ɢ ɢɦɹ ɞɥɹ ɮɚɣɥɚ ɦɨɞɭɥɹ. ȼɨ ɢɡɛɟɠɚɧɢɟ ɧɟɨɞɧɨɡɧɚɱɧɨɫɬɢ ɥɭɱɲɟ ɜɫɟɝɨ ɢɦɟɬɶ ɭɧɢɤɚɥɶɧɵɟ ɢɦɟɧɚ ɮɚɣɥɨɜ. ɇɚɩɪɢɦɟɪ, ɧɟɪɚɡɭɦɧɨ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ, ɜɵɛɪɚɧɧɨɝɨ ɞɥɹ ɩɪɢɦɟɪɚ, ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɦɹ ɦɨɞɭɥɹ Button, ɬɚɤ ɤɚɤ ɜ Delphi ɦɨɞɭɥɶ ɫ ɬɚɤɢɦ ɢɦɟɧɟɦ ɭɠɟ ɟɫɬɶ. ɉɨɞɞɟɪɠɚɧɢɟ ɢɦɟɧ ɮɚɣɥɨɜ ɦɨɞɭɥɟɣ ɭɧɢɤɚɥɶɧɵɦɢ ɨɫɨɛɟɧɧɨ ɜɚɠɧɨ ɬɨɝɞɚ, ɤɨɝɞɚ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɦɧɨɝɨ ɤɨɦɩɨɧɟɧɬɨɜ ɢɡ ɪɚɡɧɵɯ ɢɫɬɨɱɧɢɤɨɜ. ɋɥɟɞɭɹ ɭɩɨɦɹɧɭɬɵɦ ɜɵɲɟ ɫɨɝɥɚɲɟɧɢɹɦ, ɧɚɡɨɜɺɦ ɤɥɚɫɫ ɫɨɡɞɚɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ TMyButton. Ɉɛɴɹɜɥɟɧɢɟ ɤɥɚɫɫɚ – ɷɬɨ ɬɨ ɦɟɫɬɨ, ɝɞɟ ɨɛɴɹɜɥɹɸɬɫɹ ɪɚɡɥɢɱɧɵɟ ɢɧɬɟɪɮɟɣɫɵ ɤɨɦɩɨɧɟɧɬɚ. Ʉɨɧɤɪɟɬɧɨ, ɫ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ ɬɨɱɤɢ ɡɪɟɧɢɹ, ɧɚɢɛɨɥɟɟ ɜɚɠɧɵɦɢ ɹɜɥɹɸɬɫɹ ɪɚɡɞɟɥɵ public ɢ published, ɩɨɫɤɨɥɶɤɭ ɢɦɟɧɧɨ ɨɧɢ ɨɩɪɟɞɟɥɹɸɬ ɢɧɬɟɪɮɟɣɫɵ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ (runtime) ɢ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ (design-time) ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ. ɋɜɨɣɫɬɜɚ, ɦɟɬɨɞɵ ɢ ɫɨɛɵɬɢɹ, ɨɛɴɹɜɥɟɧɧɵɟ ɜ ɪɚɡɞɟɥɟ public, ɫɨɫɬɚɜɥɹɸɬ ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. ɇɚ ɷɬɢ ɷɥɟɦɟɧɬɵ ɦɨɠɧɨ ɫɨɫɥɚɬɶɫɹ ɢɥɢ ɢɯ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɬɨɥɶɤɨ ɬɨɝɞɚ, ɤɨɝɞɚ ɩɪɢɥɨɠɟɧɢɟ, ɢɫɩɨɥɶɡɭɸɳɟɟ ɤɨɦɩɨɧɟɧɬ, ɜɵɩɨɥɧɹɟɬɫɹ. ɋɜɨɣɫɬɜɚ ɢ ɫɨɛɵɬɢɹ, ɨɛɴɹɜɥɟɧɧɵɟ ɜ ɪɚɡɞɟɥɟ published, ɬɚɤɠɟ ɞɨɫɬɭɩɧɵ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ, ɧɨ ɛɨɥɟɟ ɜɚɠɧɨ ɬɨ, ɱɬɨ ɨɧɢ ɞɨɫɬɭɩɧɵ ɜ ɂɧɫɩɟɤɬɨɪɟ ɨɛɴɟɤɬɨɜ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɧɟ ɡɚɩɨɥɧɹɟɬ ɜɫɟɯ ɪɚɡɞɟɥɨɜ ɝɟɧɟɪɢɪɭɟɦɨɝɨ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ. Ɉɧ ɨɝɪɚɧɢɱɢɜɚɟɬɫɹ ɨɛɴɹɜɥɟɧɢɟɦ ɱɟɬɵɪɟɯ ɪɚɡɥɢɱɧɵɯ ɪɚɡɞɟɥɨɜ. 3.2. ɋɨɡɞɚɧɢɟ ɮɚɣɥɚ ɦɨɞɭɥɹ ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɟɬɫɹ ɩɨɥɟɡɧɵɦ ɢɧɫɬɪɭɦɟɧɬɨɦ ɩɪɢ ɫɨɡɞɚɧɢɢ ɦɨɞɭɥɟɣ ɧɨɜɵɯ ɤɨɦɩɨɧɟɧɬɨɜ. ɇɚ ɪɢɫ. 2 ɩɪɟɞɫɬɚɜɥɟɧɨ ɞɢɚɥɨɝɨɜɨɟ ɨɤɧɨ ɗɤɫɩɟɪɬɚ
ɤɨɦɩɨɧɟɧɬɨɜ,
ɨɬɨɛɪɚɠɚɟɦɨɟ
ɩɪɢ
ɜɵɛɨɪɟ
ɩɭɧɤɬɚ
File|New|Other|Component ɢɥɢ Component|New Component. ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɬɪɟɛɭɟɬ ɜɜɟɫɬɢ ɫɥɟɞɭɸɳɭɸ ɢɧɮɨɪɦɚɰɢɸ: 25
ɦɟɧɸ
– ɢɦɹ ɤɥɚɫɫɚ-ɩɪɟɞɤɚ, ɨɬ ɤɨɬɨɪɨɝɨ ɛɭɞɟɬ ɩɨɪɨɠɞɟɧ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ; – ɢɦɹ ɤɥɚɫɫɚ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ; – ɢɦɹ ɫɬɪɚɧɢɰɵ ɩɚɥɢɬɪɵ ɤɨɦɩɨɧɟɧɬɨɜ; – ɢɦɹ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. ȿɫɥɢ ɤɚɬɚɥɨɝ ɦɨɞɭɥɹ ɧɟ ɫɨɞɟɪɠɢɬɫɹ ɜ Search path, ɨɧ ɛɭɞɟɬ ɬɭɞɚ ɞɨɛɚɜɥɟɧ; – ɩɭɬɢ ɞɥɹ ɩɨɢɫɤɚ ɮɚɣɥɨɜ.
Ɋɢɫ. 2. Ⱦɢɚɥɨɝɨɜɨɟ ɨɤɧɨ ɗɤɫɩɟɪɬɚ ɤɨɦɩɨɧɟɧɬɨɜ ɉɪɟɠɞɟ ɜɫɟɝɨ, ɜɜɟɞɢɬɟ ɢɦɹ ɞɥɹ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ. ȼɫɩɨɦɧɢɬɟ, ɱɬɨ ɢɦɟɧɚ ɤɥɚɫɫɨɜ ɤɨɦɩɨɧɟɧɬɨɜ ɞɨɥɠɧɵ ɛɵɬɶ ɭɧɢɤɚɥɶɧɵɦɢ ɢ ɱɬɨ ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɧɟ ɪɚɡɪɟɲɢɬ ɜɜɟɫɬɢ ɢɦɹ ɤɥɚɫɫɚ, ɤɨɬɨɪɨɟ ɭɠɟ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧɨ ɜ Delphi. ɉɨɫɥɟ ɜɜɨɞɚ ɢɦɟɧɢ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ, ɞɨɥɠɟɧ ɛɵɬɶ ɜɵɛɪɚɧ ɤɥɚɫɫ-ɩɪɟɞɨɤ. Ʌɢɛɨ ɧɚɛɟɪɢɬɟ ɢɦɹ ɤɥɚɫɫɚ, ɢɡ ɤɨɬɨɪɨɝɨ ɯɨɬɢɬɟ ɩɨɪɨɞɢɬɶ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ, ɥɢɛɨ ɜɵɛɟɪɢɬɟ ɟɝɨ ɢɡ ɪɚɫɤɪɵɜɚɸɳɟɝɨɫɹ ɫɩɢɫɤɚ. ɋɩɢɫɨɤ ɫɨɞɟɪɠɢɬ ɜɫɟ ɤɨɦɩɨɧɟɧɬɵ, ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧɧɵɟ ɜ ɧɚɫɬɨɹɳɢɣ ɦɨɦɟɧɬ ɜ Delphi, ɢ ɜɵɛɨɪ ɢɦɟɧɢ ɢɡ ɫɩɢɫɤɚ ɨɛɵɱɧɨ ɛɟɡɨɩɚɫɧɟɟ, ɱɟɦ ɜɜɨɞ ɟɝɨ ɜɪɭɱɧɭɸ, ɩɨɫɤɨɥɶɤɭ ɜɵ ɫɦɨɠɟɬɟ ɢɡɛɟɠɚɬɶ ɨɩɟɱɚɬɨɤ. ɋɥɟɞɭɸɳɢɣ ɲɚɝ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɜɵɛɨɪɟ ɫɬɪɚɧɢɰɵ ɩɚɥɢɬɪɵ ɤɨɦɩɨɧɟɧɬɨɜ, ɝɞɟ ɩɨɹɜɢɬɫɹ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ ɩɨɫɥɟ ɪɟɝɢɫɬɪɚɰɢɢ ɟɝɨ ɜ Delphi. Ɋɚɫ26
ɤɪɵɜɚɸɳɢɣɫɹ ɫɩɢɫɨɤ ɫɨɞɟɪɠɢɬ ɜɫɟ ɢɦɟɸɳɢɟɫɹ ɜ ɩɚɥɢɬɪɟ ɫɬɪɚɧɢɰɵ. ȿɫɥɢ ɠɟ ɜɵ ɯɨɬɢɬɟ ɫɨɡɞɚɬɶ ɧɨɜɭɸ ɫɬɪɚɧɢɰɭ, ɬɨ ɩɪɨɫɬɨ ɜɜɟɞɢɬɟ ɜ ɩɨɥɟ ɟɟ ɢɦɹ. ɑɟɬɜɺɪɬɚɹ ɫɬɪɨɤɚ ɫɨɞɟɪɠɢɬ ɩɨɥɧɨɟ ɢɦɹ ɦɨɞɭɥɹ ɨɛɴɹɜɥɹɟɦɨɝɨ ɤɥɚɫɫɚ. ɉɨɫɥɟɞɧɹɹ ɫɬɪɨɤɚ ɨɩɪɟɞɟɥɹɟɬ ɦɚɪɲɪɭɬɵ ɞɥɹ ɩɨɢɫɤɚ ɮɚɣɥɚ. ȿɫɥɢ ɜɜɟɞɟɧɧɵɣ ɜ ɩɪɟɞɵɞɭɳɟɣ ɫɬɪɨɤɟ ɩɭɬɶ ɧɟ ɫɨɞɟɪɠɢɬɫɹ ɜ Search Path, ɬɨ ɨɧ ɛɭɞɟɬ ɬɭɞɚ ɞɨɛɚɜɥɟɧ. ɉɨɫɥɟ ɜɜɨɞɚ ɜɫɟɯ ɞɚɧɧɵɯ ɧɚɠɦɢɬɟ ɤɧɨɩɤɭ ɈɄ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɩɪɟɞɩɢɫɚɬɶ Delphi ɫɨɡɞɚɬɶ ɧɨɜɵɣ ɦɨɞɭɥɶ. (Ʉɧɨɩɤɭ Install ɧɚɠɢɦɚɬɶ ɧɟ ɫɥɟɞɭɟɬ!) Ⱦɥɹ ɬɟɯ ɡɧɚɱɟɧɢɣ, ɤɨɬɨɪɵɟ ɭɤɚɡɚɧɵ ɧɚ ɪɢɫ. 2, ɗɤɫɩɟɪɬ ɤɨɦɩɨɧɟɧɬɨɜ ɫɝɟɧɟɪɢɪɭɟɬ ɫɥɟɞɭɸɳɢɣ ɲɚɛɥɨɧ ɤɨɦɩɨɧɟɧɬɚ: unit UMyButton; interface uses
Windows,
Messages,
SysUtils,
Classes,
Controls,
StdCtrls; type TMyButton = class(TButton) private { Private declarations } protected { Protected declarations } public { Public declarations } published { Published declarations } end; procedure Register; implementation procedure Register; begin RegisterComponents('MyPalette', [TMyButton]); end; end. 27
3.3. ɇɚɫɬɪɨɣɤɚ ɤɨɦɩɨɧɟɧɬɚ ɉɨɫɥɟ ɫɨɡɞɚɧɢɹ ɤɚɪɤɚɫɚ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ ɫɥɟɞɭɸɳɢɣ ɲɚɝ ɜ ɨɛɳɟɦ ɩɪɨɰɟɫɫɟ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɧɚɩɢɫɚɧɢɢ ɤɨɞɚ, ɤɨɬɨɪɵɣ ɧɚɫɬɪɨɢɬ ɤɚɪɤɚɫ ɢ, ɬɟɦ ɫɚɦɵɦ, ɩɨɥɧɨɫɬɶɸ ɨɩɪɟɞɟɥɢɬ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ. ɇɚɩɢɫɚɧɢɟ ɤɨɞɚ ɜɤɥɸɱɚɟɬ ɨɩɪɟɞɟɥɟɧɢɟ ɫɜɨɣɫɬɜ, ɦɟɬɨɞɨɜ ɢ ɫɨɛɵɬɢɣ ɤɨɦɩɨɧɟɧɬɚ, ɚ ɪɚɜɧɨ ɢ ɪɟɚɥɢɡɚɰɢɸ ɜɫɩɨɦɨɝɚɬɟɥɶɧɵɯ ɦɟɬɨɞɨɜ ɜ ɪɚɡɞɟɥɟ implementation ɦɨɞɭɥɹ. Ⱦɚɧɧɚɹ ɱɚɫɬɶ ɩɪɨɰɟɫɫɚ ɲɢɪɨɤɨ ɜɚɪɶɢɪɭɟɬɫɹ ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɬɢɩɚ ɫɨɡɞɚɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ. ȼɫɟ, ɱɬɨ ɬɪɟɛɭɟɬɫɹ ɞɥɹ ɩɪɨɫɬɨɝɨ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɹ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɷɬɨ ɤɨɧɫɬɪɭɤɬɨɪ ɞɥɹ ɩɟɪɟɨɩɪɟɞɟɥɹɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ. unit UMyButton; interface uses Classes, Controls, Forms, StdCtrls; type TMyButton = class( TButton ) public constructor Create( AOwner : TComponent ); override; published property Height default 26; property Width default 80; end; procedure Register;
implementation constructor TMyButton.Create( AOwner : TComponent ); begin // He ɡɚɛɭɞɶɬɟ ɜɵɡɜɚɬɶ ɤɨɧɫɬɪɭɤɬɨɪ ɩɪɟɞɤɚ inherited Create(AOwner); // ɍɫɬɚɧɨɜɤɚ ɧɨɜɵɯ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ Width := 80; Height := 26; end; // ɉɪɨɰɟɞɭɪɚ Register 28
procedure Register; begin RegisterComponents(‘MyPalette', [TMyButton] ); end; end.
3.4. Ɉɛɴɹɜɥɟɧɢɟ ɤɨɧɫɬɪɭɤɬɨɪɚ ȼɫɟ ɤɨɦɩɨɧɟɧɬɵ ɧɚɫɥɟɞɭɸɬ ɨɬ ɤɥɚɫɫɚ Ɍɋɨmɪɨnent ɜɢɪɬɭɚɥɶɧɵɣ ɤɨɧɫɬɪɭɤɬɨɪ Create. Ⱦɚɠɟ ɟɫɥɢ ɜ ɩɨɪɨɠɞɟɧɧɨɦ ɤɥɚɫɫɟ ɤɨɧɫɬɪɭɤɬɨɪ ɢ ɩɟɪɟɨɩɪɟɞɟɥɹɟɬɫɹ (ɬɚɤ ɤɚɤ ɨɧ ɨɛɴɹɜɥɟɧ ɜɢɪɬɭɚɥɶɧɵɦ), ɬɨ ɟɝɨ ɫɩɢɫɨɤ ɩɚɪɚɦɟɬɪɨɜ ɢɡɦɟɧɢɬɶ ɧɟɥɶɡɹ. Ʉɨɧɫɬɪɭɤɬɨɪ ɩɪɢɧɢɦɚɟɬ ɜɫɟɝɨ ɨɞɢɧ ɚɪɝɭɦɟɧɬ, ɹɜɥɹɸɳɢɣɫɹ ɫɫɵɥɤɨɣ ɧɚ ɤɨɦɩɨɧɟɧɬ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɥɚɞɟɬɶ ɧɨɜɵɦ ɤɨɦɩɨɧɟɧɬɨɦ. ȼɥɚɞɟɥɟɰ ɧɟɫɟɬ ɨɬɜɟɬɫɬɜɟɧɧɨɫɬɶ ɡɚ ɪɚɡɪɭɲɟɧɢɟ ɜɫɟɯ ɤɨɦɩɨɧɟɧɬɨɜ ɢɡ ɫɜɨɟɝɨ ɫɩɢɫɤɚ Components. Ʉɨɧɫɬɪɭɤɬɨɪ Create ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ ɜɫɟɝɞɚ ɩɨɦɟɳɚɟɬɫɹ ɜ ɪɚɡɞɟɥ public. ȼ ɞɨɩɨɥɧɟɧɢɟ ɤ ɨɛɴɹɜɥɟɧɢɸ ɤɨɧɫɬɪɭɤɬɨɪɚ ɜ ɧɭɠɧɨɦ ɪɚɡɞɟɥɟ ɧɟ ɡɚɛɭɞɶɬɟ ɞɨɛɚɜɢɬɶ ɤ ɟɝɨ ɨɛɴɹɜɥɟɧɢɸ ɞɢɪɟɤɬɢɜɭ override. ȿɫɥɢ ɞɢɪɟɤɬɢɜɚ override ɩɪɨɩɭɳɟɧɚ, ɬɨ ɤɨɦɩɨɧɟɧɬ ɛɭɞɟɬ ɤɨɦɩɢɥɢɪɨɜɚɬɶɫɹ, ɧɨ ɩɪɢ ɫɨɡɞɚɧɢɢ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɞɢɡɚɣɧɟɪɨɦ ɮɨɪɦ ɛɭɞɟɬ ɜɵɡɜɚɧ ɧɟ ɬɨɬ ɤɨɧɫɬɪɭɤɬɨɪ. ɉɪɨɩɭɫɤ ɞɢɪɟɤɬɢɜɵ override ɪɚɡɪɵɜɚɟɬ ɜɢɪɬɭɚɥɶɧɭɸ ɰɟɩɨɱɤɭ ɢɟɪɚɪɯɢɢ, ɫɭɳɟɫɬɜɭɸɳɭɸ ɦɟɠɞɭ ɤɨɧɫɬɪɭɤɬɨɪɚɦɢ. Ʉɨɝɞɚ ɞɢɡɚɣɧɟɪ ɮɨɪɦ ɫɨɡɞɚɟɬ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ, ɨɧ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɬɨɝɨ, ɤɚɤɨɣ ɤɨɧɫɬɪɭɤɬɨɪ ɧɭɠɧɨ ɜɵɡɜɚɬɶ, ɢɫɩɨɥɶɡɭɟɬ ɩɨɥɢɦɨɪɮɢɡɦ. ɇɨ ɩɨɫɤɨɥɶɤɭ ɜɢɪɬɭɚɥɶɧɚɹ ɰɟɩɨɱɤɚ ɪɚɡɨɪɜɚɧɚ, ɜɦɟɫɬɨ TMyButton.Create ɛɭɞɟɬ ɜɵɡɜɚɧ TButton.Create. ȼ ɤɨɧɫɬɪɭɤɬɨɪɟ ɨɛɵɱɧɨ ɩɟɪɟɨɩɪɟɞɟɥɹɸɬɫɹ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɉɪɢ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɢ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ ɬɚɤɠɟ ɭɞɨɛɧɨ ɩɨɜɬɨɪɧɨ ɨɛɴɹɜɢɬɶ ɩɟɪɟɨɩɪɟɞɟɥɹɟɦɵɟ ɫɜɨɣɫɬɜɚ ɢ ɭɤɚɡɚɬɶ ɧɨɜɵɟ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɢɫɩɨɥɶɡɭɹ ɞɢɪɟɤɬɢɜɭ default. 29
Ɍɚɤ ɤɚɤ ɤɨɧɫɬɪɭɤɬɨɪ ɹɜɥɹɟɬɫɹ ɜɢɪɬɭɚɥɶɧɵɦ, ɫɧɚɱɚɥɚ ɜɵɡɵɜɚɟɬɫɹ ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ ɤɨɧɫɬɪɭɤɬɨɪ. ɗɬɨ ɝɚɪɚɧɬɢɪɭɟɬ ɩɪɚɜɢɥɶɧɨɟ ɫɨɡɞɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ. ɉɪɢ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɢ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɨɫɨɛɟɧɧɨ ɜɚɠɧɨ ɜɵɡɜɚɬɶ ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ ɤɨɧɫɬɪɭɤɬɨɪ, ɩɨɫɤɨɥɶɤɭ ɨɧ ɧɟɫɟɬ ɨɬɜɟɬɫɬɜɟɧɧɨɫɬɶ ɡɚ ɭɫɬɚɧɨɜɤɭ ɢɫɯɨɞɧɵɯ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɤɨɬɨɪɵɟ ɦɵ ɩɵɬɚɟɦɫɹ ɩɟɪɟɨɩɪɟɞɟɥɢɬɶ. ȿɫɥɢ ɭɧɚɫɥɟɞɨɜɚɧɧɵɣ ɤɨɧɫɬɪɭɤɬɨɪ ɜɵɡɜɚɬɶ ɩɨɫɥɟ ɭɫɬɚɧɨɜɤɢ ɧɨɜɵɯ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɬɨ ɛɭɞɭɬ ɜɨɫɫɬɚɧɨɜɥɟɧɵ ɢɫɯɨɞɧɵɟ ɡɧɚɱɟɧɢɹ. 3.5. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ Ɍɟɫɬɢɪɨɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɧɟɫɤɨɥɶɤɨ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɬɟɫɬɢɪɨɜɚɧɢɹ ɩɨɥɧɨɝɨ ɩɪɢɥɨɠɟɧɢɹ Delphi. Ɋɚɡɥɢɱɢɟ ɩɨɪɨɠɞɚɟɬɫɹ ɬɟɦ ɮɚɤɬɨɦ, ɱɬɨ ɤɨɦɩɨɧɟɧɬɵ ɢɦɟɸɬ ɞɜɚ ɨɬɞɟɥɶɧɵɯ, ɯɨɬɹ ɢ ɫɯɨɞɧɵɯ ɢɧɬɟɪɮɟɣɫɚ, ɧɭɠɞɚɸɳɢɯɫɹ ɜ ɩɪɨɜɟɪɤɟ. Ʉɨɝɞɚ ɤɨɦɩɨɧɟɧɬ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɩɪɢɥɨɠɟɧɢɢ, ɬɨ ɟɝɨ ɩɨɜɟɞɟɧɢɟ ɨɩɪɟɞɟɥɹɟɬ ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. Ɉɞɧɚɤɨ ɤɨɝɞɚ ɤɨɦɩɨɧɟɧɬ ɩɨɦɟɳɚɟɬɫɹ ɧɚ ɮɨɪɦɭ ɜ ɫɪɟɞɟ Delphi, ɬɨ ɩɨɜɟɞɟɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɞɢɤɬɭɟɬɫɹ ɟɝɨ ɢɧɬɟɪɮɟɣɫɨɦ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɤɚɱɟɫɬɜɟɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɫɥɟɞɭɟɬ ɩɪɨɜɟɪɢɬɶ ɨɛɚ ɢɧɬɟɪɮɟɣɫɚ. ȼɩɨɥɧɟ ɜɨɡɦɨɠɧɨ ɫɨɡɞɚɬɶ ɤɨɦɩɨɧɟɧɬ, ɤɨɬɨɪɵɣ ɩɪɚɜɢɥɶɧɨ ɜɟɞɟɬ ɫɟɛɹ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ, ɧɨ ɧɟɩɪɚɜɢɥɶɧɨ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ, ɢ ɧɚɨɛɨɪɨɬ. ɉɪɟɠɞɟ ɜɫɟɝɨ ɩɪɨɬɟɫɬɢɪɭɟɦ ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. Ⱦɥɹ ɬɚɤɨɝɨ ɩɨɪɹɞɤɚ ɟɫɬɶ ɧɟɫɤɨɥɶɤɨ ɩɪɢɱɢɧ. ȼɨ-ɩɟɪɜɵɯ, ɩɪɨɬɟɫɬɢɪɨɜɚɬɶ ɩɨɜɟɞɟɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɝɨɪɚɡɞɨ ɥɟɝɱɟ, ɱɟɦ ɩɨɜɟɞɟɧɢɟ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. ȼɨ-ɜɬɨɪɵɯ, ɞɥɹ ɛɨɥɶɲɢɧɫɬɜɚ ɤɨɦɩɨɧɟɧɬɨɜ ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɜɟɫɶɦɚ ɩɨɯɨɠ ɧɚ ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɫɪɟɞɵ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɩɨɦɨɠɟɬ ɭɛɟɞɢɬɶɫɹ ɜ ɬɨɦ, ɱɬɨ ɤɨɦɩɨɧɟɧɬ ɛɭɞɟɬ ɩɪɚɜɢɥɶɧɨ ɪɚɛɨɬɚɬɶ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. ɂ, ɜ-ɬɪɟɬɶɢɯ, ɞɥɹ ɩɨɢɫɤɚ ɨɲɢɛɨɤ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɜɫɬɪɨɟɧɧɵɣ ɨɬɥɚɞɱɢɤ, ɤɨɬɨɪɵɣ ɧɟɞɨɫɬɭɩɟɧ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ.
30
Ⱦɥɹ ɩɪɨɜɟɪɤɢ ɧɚɲɟɝɨ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ MyButton ɧɭɠɧɨ ɫɨɡɞɚɬɶ ɬɟɫɬɨɜɨɟ ɩɪɢɥɨɠɟɧɢɟ. ɋɨɡɞɚɧɢɟ ɬɟɫɬɨɜɵɯ ɩɪɢɥɨɠɟɧɢɣ ɹɜɥɹɟɬɫɹ ɝɥɚɜɧɨɣ ɩɪɢɱɢɧɨɣ ɜɵɞɟɥɟɧɢɹ ɞɥɹ ɦɨɞɭɥɹ ɤɚɠɞɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɨɬɞɟɥɶɧɨɝɨ ɤɚɬɚɥɨɝɚ. Ʉɚɬɚɥɨɝ ɫɨɞɟɪɠɢɬ ɢɫɯɨɞɧɵɣ ɮɚɣɥ ɤɨɦɩɨɧɟɧɬɚ ɢ ɜɫɟ ɮɚɣɥɵ, ɚɫɫɨɰɢɢɪɭɸɳɢɟɫɹ ɫ ɬɟɫɬɨɜɵɦ ɩɪɨɟɤɬɨɦ. ɇɚɥɢɱɢɟ ɩɨɞ ɪɭɤɚɦɢ ɬɟɫɬɨɜɵɯ ɩɪɢɥɨɠɟɧɢɣ ɨɛɥɟɝɱɚɟɬ ɩɨɞɞɟɪɠɤɭ ɤɨɦɩɨɧɟɧɬɨɜ, ɬɚɤ ɤɚɤ ɤɨɝɞɚ ɤɨɦɩɨɧɟɧɬ ɩɪɢɯɨɞɢɬɫɹ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ, ɬɨ ɭ ɜɚɫ ɭɠɟ ɛɭɞɟɬ ɬɟɫɬɨɜɨɟ ɩɪɢɥɨɠɟɧɢɟ, ɩɨɫɬɪɨɟɧɧɨɟ ɜ ɬɨɦ ɠɟ ɫɚɦɨɦ ɤɚɬɚɥɨɝɟ, ɱɬɨ ɢ ɤɨɦɩɨɧɟɧɬ. ɂɬɚɤ, ɤɚɤ ɠɟ ɩɪɨɬɟɫɬɢɪɨɜɚɬɶ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ, ɟɫɥɢ ɨɧ ɞɚɠɟ ɧɟ ɩɨɹɜɥɹɟɬɫɹ ɜ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ? ȼɦɟɫɬɨ ɬɨɝɨ ɱɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɢɡɚɣɧɟɪ ɮɨɪɦ ɞɥɹ ɩɨɦɟɳɟɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɮɨɪɦɭ, ɚ ɡɚɬɟɦ ɢɫɩɨɥɶɡɨɜɚɬɶ ɂɧɫɩɟɤɬɨɪ ɨɛɴɟɤɬɨɜ ɞɥɹ ɢɡɦɟɧɟɧɢɹ ɤɚɤɢɯ-ɬɨ ɫɜɨɣɫɬɜ ɤɨɦɩɨɧɟɧɬɚ, ɦɵ ɞɢɧɚɦɢɱɟɫɤɢ ɫɨɡɞɚɞɢɦ ɷɤɡɟɦɩɥɹɪ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɫɨɡɞɚɧɢɹ ɮɨɪɦɵ ɧɚɲɟɝɨ ɩɪɢɥɨɠɟɧɢɹ. Ⱦɢɧɚɦɢɱɟɫɤɨɟ ɫɨɡɞɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɩɪɟɞɨɫɬɚɜɢɬ ɧɚɦ ɜɨɡɦɨɠɧɨɫɬɶ ɫɨɡɞɚɬɶ ɤɨɦɩɨɧɟɧɬ, ɧɟ ɪɟɝɢɫɬɪɢɪɭɹ ɟɝɨ ɜ Delphi. Ɋɟɝɢɫɬɪɚɰɢɹ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɩɪɨɰɟɫɫ, ɜ ɬɟɱɟɧɢɟ ɤɨɬɨɪɨɝɨ Delphi ɫɬɚɧɨɜɢɬɫɹ ɢɡɜɟɫɬɧɨ ɨ ɤɨɦɩɨɧɟɧɬɚɯ, ɪɚɡɦɟɳɟɧɧɵɯ ɜ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ. Ɋɟɝɢɫɬɪɚɰɢɹ ɞɨɥɠɧɚ ɜɵɩɨɥɧɹɬɶɫɹ ɞɥɹ ɩɪɨɜɟɪɤɢ ɩɨɜɟɞɟɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ, ɧɨ ɞɥɹ ɩɪɨɜɟɪɤɢ ɩɨɜɟɞɟɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɨɧɚ ɧɟɨɛɹɡɚɬɟɥɶɧɚ. ȼ ɬɨɦ ɠɟ ɫɚɦɨɦ ɤɚɬɚɥɨɝɟ, ɝɞɟ ɭɠɟ ɟɫɬɶ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ, ɫɨɡɞɚɣɬɟ ɧɨɜɵɣ ɩɪɨɟɤɬ. Ⱦɥɹ ɬɟɫɬɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɩɨɧɚɞɨɛɢɬɫɹ ɩɪɨɫɬɚɹ ɮɨɪɦɚ. Ⱦɢɧɚɦɢɱɟɫɤɨɟ ɫɨɡɞɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɫɜɨɞɢɬɫɹ ɤ ɫɥɟɞɭɸɳɢɦ ɲɚɝɚɦ. 1. Ⱦɨɛɚɜɢɬɶ ɧɨɜɵɣ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ ɜ ɪɚɡɞɟɥ uses ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. 2. Ⱦɨɛɚɜɢɬɶ ɜ ɤɥɚɫɫ ɮɨɪɦɵ ɩɨɥɟ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɫɫɵɥɤɨɣ ɧɚ ɤɨɦɩɨɧɟɧɬ. 3. ɋɨɡɞɚɬɶ ɤɨɦɩɨɧɟɧɬ ɜ ɫɨɛɵɬɢɢ ɮɨɪɦɵ OnCreate. 4. ɍɫɬɚɧɨɜɢɬɶ ɫɜɨɣɫɬɜɨ ɤɨɦɩɨɧɟɧɬɚ Parent. 5. ɍɫɬɚɧɨɜɢɬɶ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦ ɨɛɪɚɡɨɦ ɞɨɩɨɥɧɢɬɟɥɶɧɵɟ ɫɜɨɣɫɬɜɚ ɤɨɦɩɨɧɟɧɬɚ. unit Mainform; 31
interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, UMyButton; type TForml = class(TForm) procedure FormCreate(Sender: TObject); private {ɉɪɢɜɚɬɧɵɟ ɨɛɴɹɜɥɟɧɢɹ } public {Ɉɛɳɟɞɨɫɬɭɩɧɵɟ ɨɛɴɹɜɥɟɧɢɹ J BtnTest : TMyButton; end; var Forml: TForml; implementation {$R *.DFM} procedure TForml.FormCreate(Sender: TObject); begin BtnTest := TMyButton.Create( Self ) ; BtnTest.Parent := Self; BtnTest.Left := 100; BtnTest.Top := 100; BtnTest.Caption := 'MyButton'; end; end.
ɉɟɪɜɵɣ ɲɚɝ ɞɨɫɬɚɬɨɱɧɨ ɩɪɨɫɬ ɢ ɰɟɥɟɧɚɩɪɚɜɥɟɧ. Ɍɚɤ ɤɚɤ ɦɵ ɛɭɞɟɦ ɫɫɵɥɚɬɶɫɹ ɧɚ ɬɢɩ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɬɨ ɧɭɠɧɨ ɜɤɥɸɱɢɬɶ ɷɬɨɬ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ ɜ uses-ɪɚɡɞɟɥ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ. ȼɬɨɪɨɣ ɲɚɝ ɫɜɹɡɚɧ ɫ ɞɨɛɚɜɥɟɧɢɟɦ ɩɨɥɹ ɨɛɴɟɤɬɚ ɜ ɪɚɡɞɟɥ public ɤɥɚɫɫɚ ɮɨɪɦɵ. ɉɨɥɟ ɨɛɴɟɤɬɚ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɫɫɵɥɤɢ ɧɚ ɫɨɡɞɚɜɚɟɦɵɣ ɤɨɦɩɨɧɟɧɬ, ɢ, ɫɥɟɞɨɜɚɬɟɥɶɧɨ, ɬɢɩɨɦ ɩɨɥɹ ɛɭɞɟɬ ɜɧɨɜɶ ɫɨɡɞɚɧɧɵɣ ɤɨɦɩɨɧɟɧɬɧɵɣ ɤɥɚɫɫ. ȼ ɬɟɤɭɳɟɣ ɬɟɫɬɨɜɨɣ ɩɪɨɝɪɚɦɦɟ ɩɨɥɟ BtnTest ɨɛɴɹɜɥɹɟɬɫɹ ɬɢɩɨɦ TMyButton. 32
ȼɫɟ ɨɫɬɚɥɶɧɵɟ ɲɚɝɢ ɫɜɹɡɚɧɵ ɫ ɨɛɪɚɛɨɬɱɢɤɨɦ ɫɨɛɵɬɢɹ ɮɨɪɦɵ OnCreate. ɇɚɢɛɨɥɟɟ ɤɨɪɨɬɤɢɣ ɩɭɬɶ ɞɥɹ ɫɨɡɞɚɧɢɹ ɨɛɪɚɛɨɬɱɢɤɚ ɫɨɛɵɬɢɹ OnCreate ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɞɜɨɣɧɨɦ ɳɟɥɱɤɟ ɧɚ ɧɟɡɚɧɹɬɨɦ ɦɟɫɬɟ ɮɨɪɦɵ. ɗɬɨ ɫɨɡɞɚɫɬ ɦɟɬɨɞ FormCreate, ɜ ɤɨɬɨɪɨɦ ɛɭɞɟɬ ɫɨɡɞɚɧ ɤɨɦɩɨɧɟɧɬ BtnTest. ɉɟɪɜɚɹ ɡɚɞɚɱɚ, ɤɨɬɨɪɚɹ ɞɨɥɠɧɚ ɜɵɩɨɥɧɹɬɶɫɹ ɜ ɦɟɬɨɞɟ FormCreate, ɫɨɫɬɨɢɬ ɜ ɪɟɚɥɢɡɚɰɢɢ ɷɤɡɟɦɩɥɹɪɚ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɩɭɬɟɦ ɨɛɪɚɳɟɧɢɹ ɤ ɤɨɧɫɬɪɭɤɬɨɪɭ ɤɨɦɩɨɧɟɧɬɚ. ȿɞɢɧɫɬɜɟɧɧɨɟ ɪɟɲɟɧɢɟ, ɤɨɬɨɪɨɟ ɧɟɨɛɯɨɞɢɦɨ ɩɪɢɧɹɬɶ ɧɚ ɷɬɨɦ ɷɬɚɩɟ, ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɨɩɪɟɞɟɥɟɧɢɢ ɬɨɝɨ, ɱɬɨ ɡɚɞɚɬɶ ɜ ɤɚɱɟɫɬɜɟ ɜɥɚɞɟɥɶɰɚ ɤɨɦɩɨɧɟɧɬɚ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɫɢɬɭɚɰɢɣ ɜɥɚɞɟɥɶɰɟɦ ɛɭɞɟɬ ɮɨɪɦɚ. ȼ ɦɟɬɨɞɟ FormCreate ɤɨɧɫɬɪɭɤɬɨɪɭ TMyButton ɩɟɪɟɞɚɟɬɫɹ ɩɚɪɚɦɟɬɪ Self, ɭɤɚɡɵɜɚɸɳɢɣ ɧɚ ɬɨ, ɱɬɨ ɷɬɚ ɮɨɪɦɚ ɛɭɞɟɬ ɫɥɭɠɢɬɶ ɜɥɚɞɟɥɶɰɟɦ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ. ɉɨɫɥɟ ɫɨɡɞɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɜɫɟ ɟɳɟ ɧɟɨɛɯɨɞɢɦɨ ɜɵɩɨɥɧɢɬɶ ɨɞɧɭ ɢɡ ɫɚɦɵɯ ɜɚɠɧɵɯ ɡɚɞɚɱ – ɭɫɬɚɧɨɜɢɬɶ ɫɜɨɣɫɬɜɨ Parent, ɭɤɚɡɵɜɚɸɳɟɟ ɧɚ ɪɨɞɢɬɟɥɶɫɤɢɣ ɤɨɦɩɨɧɟɧɬ. Ɋɨɞɢɬɟɥɶɫɤɢɣ ɤɨɦɩɨɧɟɧɬ (ɧɟ ɫɥɟɞɭɟɬ ɩɭɬɚɬɶ ɟɝɨ ɫ ɤɨɦɩɨɧɟɧɬɨɦ-ɩɪɟɞɤɨɦ, ɨɬ ɤɨɬɨɪɨɝɨ ɢɞɟɬ ɧɚɫɥɟɞɨɜɚɧɢɟ) ɹɜɥɹɟɬɫɹ ɤɨɧɬɟɣɧɟɪɨɦ ɞɪɭɝɢɯ ɤɨɦɩɨɧɟɧɬɨɜ, ɬ. ɟ. ɫɨɞɟɪɠɢɬ ɢɯ ɜɢɡɭɚɥɶɧɨ. ɍɫɬɚɧɨɜɤɚ ɫɜɨɣɫɬɜɚ Parent ɩɨɦɟɫɬɢɬ ɬɟɤɭɳɢɣ ɤɨɦɩɨɧɟɧɬ ɜ ɫɩɢɫɨɤ Controls ɪɨɞɢɬɟɥɹ. ɋɩɢɫɨɤ Controls ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɭɫɬɚɧɨɜɥɟɧɢɹ ɩɨɪɹɞɤɚ ɨɛɯɨɞɚ ɷɥɟɦɟɧɬɨɜ ɭɩɪɚɜɥɟɧɢɹ (tab order) ɢ Z-ɭɩɨɪɹɞɨɱɢɜɚɧɢɹ ɫɪɟɞɢ ɩɪɢɧɚɞɥɟɠɚɳɢɯ ɤɨɦɩɨɧɟɧɬɭ ɷɥɟɦɟɧɬɨɜ ɭɩɪɚɜɥɟɧɢɹ. Ⱦɪɭɝɨɟ ɜɚɠɧɨɟ ɩɪɢɦɟɧɟɧɢɟ ɫɩɢɫɤɚ Controls ɩɪɨɢɫɯɨɞɢɬ ɜɨ ɜɪɟɦɹ ɪɢɫɨɜɚɧɢɹ. Ɋɨɞɢɬɟɥɶɫɤɢɣ ɤɨɦɩɨɧɟɧɬ (Parent) ɢɫɩɨɥɶɡɭɟɬ ɫɩɢɫɨɤ Controls ɞɥɹ ɜɵɞɚɱɢ ɤɨɦɚɧɞɵ ɧɚɪɢɫɨɜɚɬɶ ɫɟɛɹ ɜɫɟɦ ɞɨɱɟɪɧɢɦ ɤɨɦɩɨɧɟɧɬɚɦ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɟɫɥɢ ɷɥɟɦɟɧɬɚ ɭɩɪɚɜɥɟɧɢɹ ɜ ɪɨɞɢɬɟɥɶɫɤɨɦ ɫɩɢɫɤɟ Controls ɧɟɬ, ɬɨ ɨɧ ɢ ɧɟ ɩɨɹɜɢɬɫɹ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɩɪɢɥɨɠɟɧɢɹ. Ɍɨɥɶɤɨ ɩɨɬɨɦɤɢ TWinControl ɦɨɝɭɬ ɛɵɬɶ ɪɨɞɢɬɟɥɶɫɤɢɦɢ. ɉɪɢɦɟɪɚɦɢ ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɸɬɫɹ Panel ɢ GroupBox. Ʉɥɚɫɫ TForm ɬɚɤɠɟ ɹɜɥɹɟɬɫɹ 33
ɩɨɬɨɦɤɨɦ TWinControl ɢ, ɫɥɟɞɨɜɚɬɟɥɶɧɨ, ɬɚɤɠɟ ɦɨɠɟɬ ɛɵɬɶ ɤɨɧɬɟɣɧɟɪɨɦ. ɋ ɰɟɥɶɸ ɬɟɫɬɢɪɨɜɚɧɢɹ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɦɵ ɯɨɬɢɦ ɢɦɢɬɢɪɨɜɚɬɶ ɞɟɣɫɬɜɢɟ ɩɨɦɟɳɟɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɮɨɪɦɭ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɜ ɦɟɬɨɞɟ FormCreate ɫɜɨɣɫɬɜɨ BtnTest.Parent ɦɟɧɹɟɬɫɹ ɧɚ Self, ɫɫɵɥɚɹɫɶ ɧɚ ɮɨɪɦɭ. ɉɨɫɥɟ ɭɫɬɚɧɨɜɤɢ ɫɜɨɣɫɬɜɚ Parent ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶɫɹ ɧɟɤɨɬɨɪɚɹ ɦɨɞɢɮɢɤɚɰɢɹ ɫɜɨɣɫɬɜ ɤɨɦɩɨɧɟɧɬɚ. ɗɬɨ ɦɨɝɭɬ ɛɵɬɶ ɫɜɨɣɫɬɜɚ ɩɨɡɢɰɢɨɧɢɪɨɜɚɧɢɹ Left ɢ Ɍɨɪ. ȿɫɥɢ ɤɨɦɩɨɧɟɧɬ ɢɦɟɟɬ ɡɚɝɨɥɨɜɨɤ, ɬɨ ɱɚɫɬɨ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɢ ɫɜɨɣɫɬɜɨ Caption. Ⱦɥɹ ɬɟɫɬɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ MyButton ɭɫɬɚɧɨɜɤɢ ɬɪɟɯ ɭɩɨɦɹɧɭɬɵɯ ɫɜɨɣɫɬɜ ɞɨɫɬɚɬɨɱɧɨ, ɬɚɤ ɤɚɤ ɧɢɤɚɤɢɯ ɧɨɜɵɯ ɜɨɡɦɨɠɧɨɫɬɟɣ ɧɟ ɞɨɛɚɜɥɹɥɨɫɶ. Ⱦɥɹ ɬɟɫɬɢɪɨɜɚɧɢɹ ɛɨɥɟɟ ɫɥɨɠɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶɫɹ ɭɫɬɚɧɨɜɤɚ ɢ ɞɪɭɝɢɯ ɫɜɨɣɫɬɜ ɢ ɨɛɪɚɛɨɬɱɢɤɨɜ ɫɨɛɵɬɢɣ. ȼ ɬɟɫɬɨɜɨɦ ɩɪɢɥɨɠɟɧɢɢ ɦɨɠɧɨ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ ɥɸɛɨɟ ɞɨɫɬɭɩɧɨɟ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɫɜɨɣɫɬɜɨ. ȿɞɢɧɫɬɜɟɧɧɨɟ ɪɟɚɥɶɧɨɟ ɨɝɪɚɧɢɱɟɧɢɟ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɭɫɬɚɧɨɜɢɬɶ ɫɜɨɣɫɬɜɨ Parent, ɤɚɤ ɛɵɥɨ ɨɩɢɫɚɧɨ ɜɵɲɟ, ɞɨ ɬɨɝɨ, ɤɚɤ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɞɪɭɝɢɟ ɫɜɨɣɫɬɜɚ. ȼɵɩɨɥɧɟɧɢɟ ɩɨɞɨɛɧɨɝɨ ɬɪɟɛɨɜɚɧɢɹ ɧɟɨɛɯɨɞɢɦɨ ɩɨɬɨɦɭ, ɱɬɨ ɢɡɦɟɧɟɧɢɟ ɧɟɤɨɬɨɪɵɯ ɫɜɨɣɫɬɜ ɡɚɫɬɚɜɥɹɟɬ ɤɨɦɩɨɧɟɧɬ ɩɟɪɟɪɢɫɨɜɵɜɚɬɶ ɫɟɛɹ, ɢ ɟɫɥɢ ɤɨɦɩɨɧɟɧɬ ɧɟ ɜɤɥɸɱɟɧ ɟɳɟ ɜ ɫɩɢɫɨɤ Controls ɩɪɟɞɤɚ, ɬɨ ɢɡɦɟɧɟɧɢɟ ɨɛɫɭɠɞɚɟɦɵɯ ɫɜɨɣɫɬɜ ɧɟ ɞɚɫɬ ɠɟɥɚɟɦɨɝɨ ɷɮɮɟɤɬɚ. ɇɚ ɪɢɫ. 3 ɩɨɤɚɡɚɧ ɪɟɡɭɥɶɬɚɬ ɪɚɛɨɬɵ ɬɟɫɬɨɜɨɝɨ ɩɪɢɥɨɠɟɧɢɹ TestBtns.
Ɋɢɫ. 3. 34
Ɍɨɬ ɠɟ ɫɚɦɵɣ ɫɩɨɫɨɛ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɨɜɚɥɫɹ ɞɥɹ ɬɟɫɬɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ, ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɜ Delphi ɜɫɟɝɞɚ, ɤɨɝɞɚ ɧɟɨɛɯɨɞɢɦɨ ɞɢɧɚɦɢɱɟɫɤɨɟ ɫɨɡɞɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ. ɂ ɡɞɟɫɶ ɫɥɨɠɧɨɫɬɶ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɭɫɬɚɧɨɜɤɟ ɫɜɨɣɫɬɜɚ Parent. ȼ ɩɪɨɝɪɚɦɦɚɯ ɬɟɫɬɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ ɫɜɨɣɫɬɜɨ Parent ɨɛɵɱɧɨ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɜ Self, ɱɬɨ ɭɤɚɡɵɜɚɟɬ ɧɚ ɨɬɜɟɬɫɬɜɟɧɧɨɫɬɶ ɮɨɪɦɵ ɡɚ ɪɢɫɨɜɚɧɢɟ ɷɥɟɦɟɧɬɚ ɭɩɪɚɜɥɟɧɢɹ. Ɉɞɧɚɤɨ ɜ ɩɪɢɥɨɠɟɧɢɢ ɷɬɨ ɦɨɠɟɬ ɛɵɬɶ ɮɨɪɦɚ, ɩɚɧɟɥɶ, GroupBox ɢ ɬ. ɩ. 3.6. ɍɫɬɚɧɨɜɤɚ ɤɨɦɩɨɧɟɧɬɚ Ⱦɥɹ ɩɟɪɟɯɨɞɚ ɤ ɫɥɟɞɭɸɳɟɦɭ ɭɪɨɜɧɸ ɬɟɫɬɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬ ɞɨɥɠɟɧ ɛɵɬɶ ɜɤɥɸɱɟɧ ɜ ɩɚɥɢɬɪɭ. Ʉɨɝɞɚ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɧɨɜɵɣ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ, ɛɢɛɥɢɨɬɟɤɚ ɤɨɦɩɨɧɟɧɬɨɜ ɩɟɪɟɫɬɪɚɢɜɚɟɬɫɹ. ȼ ɫɩɢɫɨɤ ɭɫɬɚɧɨɜɥɟɧɧɵɯ ɦɨɝɭɬ ɩɨɩɚɫɬɶ ɬɨɥɶɤɨ ɬɟ ɦɨɞɭɥɢ, ɤɨɬɨɪɵɟ ɫɨɞɟɪɠɚɬ
ɩɪɨɰɟɞɭɪɭ
Register. ɇɚɩɨɦɧɢɦ, ɱɬɨ ɜ ɩɪɨɰɟɞɭɪɟ Register ɜɵɡɵɜɚɟɬɫɹ ɩɪɨɰɟɞɭɪɚ RegisterComponents, ɢɦɟɸɳɚɹ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɨɜ ɬɪɟɛɭɟɦɭɸ ɩɚɥɢɬɪɭ ɢ ɦɚɫɫɢɜ ɤɨɦɩɨɧɟɧɬɨɜ, ɠɟɥɚɸɳɢɯ ɬɚɦ ɨɬɨɛɪɚɡɢɬɶɫɹ. Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɤɨɦɩɨɧɟɧɬ ɩɨɹɜɢɥɫɹ ɜ ɩɚɥɢɬɪɟ, ɟɝɨ ɧɭɠɧɨ ɫɜɹɡɚɬɶ ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɤɨɦɩɨɧɟɧɬɨɜ Delphi, ɜɤɥɸɱɢɜ ɜ ɫɨɫɬɚɜ ɩɚɤɟɬɚ. Ɏɚɤɬɢɱɟɫɤɢ ɩɚɤɟɬ Delphi – ɷɬɨ ɞɢɧɚɦɢɱɟɫɤɢ ɡɚɝɪɭɠɚɟɦɚɹ ɛɢɛɥɢɨɬɟɤɚ ɫ ɪɚɫɲɢɪɟɧɢɟɦ .bpl. ɂɧɮɨɪɦɚɰɢɹ ɨ ɫɨɫɬɚɜɟ ɩɚɤɟɬɚ ɢ ɪɟɠɢɦɚɯ ɤɨɦɩɢɥɹɰɢɢ ɩɚɤɟɬɚ ɫɨɞɟɪɠɢɬɫɹ ɜ ɬɟɤɫɬɨɜɨɦ ɮɚɣɥɟ .dpk. Ʉɨɦɩɨɧɟɧɬɵ ɢɡ ɩɚɤɟɬɨɜ ɨɬɨɛɪɚɠɚɸɬɫɹ ɧɚ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ. Ʉɨɦɩɨɧɟɧɬɵ, ɩɨɫɬɚɜɥɹɟɦɵɟ ɫ Delphi, ɫɝɪɭɩɩɢɪɨɜɚɧɵ ɩɨ ɢɯ ɮɭɧɤɰɢɨɧɚɥɶɧɨɦɭ ɧɚɡɧɚɱɟɧɢɸ ɜ ɧɟɫɤɨɥɶɤɨ ɮɚɣɥɨɜ .bpl. Ɂɚɤɪɨɣɬɟ ɜɫɟ ɩɪɨɟɤɬɵ ɜ Delphi ɢ ɡɚɝɪɭɡɢɬɟ ɦɨɞɭɥɶ ɫ ɤɨɦɩɨɧɟɧɬɨɦ UMyButton.Pas. Ⱦɥɹ ɭɫɬɚɧɨɜɤɢ ɤɨɦɩɨɧɟɧɬɚ ɜɵɡɨɜɢɬɟ ɢɡ ɦɟɧɸ ɨɤɧɨ ɢɧɫɬɚɥɥɹɰɢɢ Component|Install Component (ɪɢɫ. 4).
35
Ɋɢɫ. 4. Ɉɤɧɨ ɭɫɬɚɧɨɜɤɢ ɤɨɦɩɨɧɟɧɬɚ ɇɨɜɵɣ ɤɨɦɩɨɧɟɧɬ ɦɨɠɟɬ ɛɵɬɶ ɭɫɬɚɧɨɜɥɟɧ ɜ ɫɭɳɟɫɬɜɭɸɳɢɣ ɩɚɤɟɬ (Into existing package) ɢɥɢ ɧɨɜɵɣ ɩɚɤɟɬ (Into new package). ɗɬɨ ɨɩɪɟɞɟɥɹɟɬɫɹ ɜɵɛɨɪɨɦ ɡɚɤɥɚɞɤɢ. ȼ ɩɟɪɜɨɣ ɫɬɪɨɤɟ ɞɨɥɠɧɨ ɛɵɬɶ ɭɤɚɡɚɧɨ ɢɦɹ ɦɨɞɭɥɹ, ɫɨɞɟɪɠɚɳɟɝɨ ɤɥɚɫɫ ɤɨɦɩɨɧɟɧɬɚ. ȼ ɧɚɲɟɦ ɫɥɭɱɚɟ ɷɬɨ UMyButton.pas. ȼɬɨɪɚɹ ɫɬɪɨɤɚ ɫɨɞɟɪɠɢɬ ɦɚɪɲɪɭɬɵ ɩɨɢɫɤɚ ɮɚɣɥɨɜ. Ɍɪɟɬɶɹ ɫɬɪɨɤɚ ɨɩɪɟɞɟɥɹɟɬ ɢɦɹ ɩɚɤɟɬɚ, ɜ ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɤɥɸɱɟɧ ɤɨɦɩɨɧɟɧɬ. Ɋɚɡɭɦɟɟɬɫɹ, ɫɨɛɫɬɜɟɧɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɩɪɚɜɢɥɶɧɟɟ ɫɨɯɪɚɧɹɬɶ ɜ ɧɨɜɵɯ, ɫɩɟɰɢɚɥɶɧɨ ɞɥɹ ɧɢɯ ɫɨɡɞɚɧɧɵɯ ɩɚɤɟɬɚɯ. ȿɫɥɢ ɤɨɦɩɨɧɟɧɬ ɞɨɛɚɜɥɹɟɬɫɹ ɜ ɫɭɳɟɫɬɜɭɸɳɢɣ ɩɚɤɟɬ, ɬɨ ɨɧ ɦɨɠɟɬ ɛɵɬɶ ɜɵɛɪɚɧ ɢɡ ɜɵɩɚɞɚɸɳɟɝɨ ɫɩɢɫɤɚ, ɢɧɚɱɟ ɜɜɨɞɢɬɫɹ ɢɦɹ ɧɨɜɨɝɨ ɩɚɤɟɬɚ. Ɋɚɫɲɢɪɟɧɢɟ .dpk ɛɭɞɟɬ ɞɨɛɚɜɥɟɧɨ ɚɜɬɨɦɚɬɢɱɟɫɤɢ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɩɚɤɟɬ ɧɚɡɵɜɚɟɬɫɹ MyButton, ɬɨ ɪɟɞɚɤɬɨɪ ɩɚɤɟɬɚ ɝɟɧɟɪɢɪɭɟɬ ɢɫɯɨɞɧɵɣ ɮɚɣɥ ɫ ɢɦɟɧɟɦ MyButton.dpk, ɤɨɦɩɢɥɹɬɨɪ ɝɟɧɟɪɢɪɭɟɬ ɜɵɩɨɥɧɹɟɦɵɣ ɮɚɣɥ (DLL) MyButton.bpl ɢ ɜɫɩɨɦɨɝɚɬɟɥɶɧɵɣ ɛɢɧɚɪɧɵɣ ɛɢɛɥɢɨɬɟɱɧɵɣ ɮɚɣɥ ɫ ɢɦɟɧɟɦ MyButton.dcp. ɉɨɫɥɟɞɧɹɹ ɫɬɪɨɤɚ ɦɨɠɟɬ ɫɨɞɟɪɠɚɬɶ ɤɪɚɬɤɨɟ ɨɩɢɫɚɧɢɟ ɩɚɤɟɬɚ. Ɂɚɩɨɥɧɢɜ ɩɨɥɹ ɢ ɧɚɠɚɜ ɤɧɨɩɤɭ OK, ɭɫɬɚɧɚɜɥɢɜɚɟɦ ɤɨɦɩɨɧɟɧɬ. ȼ ɩɨɹɜɢɜɲɟɦɫɹ ɨɤɧɟ (ɪɢɫ. 5) ɧɚɞɨ ɞɚɬɶ ɩɨɥɨɠɢɬɟɥɶɧɵɣ ɨɬɜɟɬ ɧɚ ɩɪɢɝɥɚɲɟɧɢɟ ɨɬɤɨɦɩɢɥɢɪɨɜɚɬɶ ɩɚɤɟɬ. 36
Ɋɢɫ. 5 ɋɥɟɞɭɸɳɟɟ ɨɤɧɨ ɫɜɢɞɟɬɟɥɶɫɬɜɭɟɬ ɨɛ ɭɫɩɟɲɧɨɣ ɢɧɫɬɚɥɥɹɰɢɢ ɩɚɤɟɬɚ ɢ ɪɟɝɢɫɬɪɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ.
Ɋɢɫ. 6 Ɂɚɦɟɱɚɧɢɟ. ȼ ɞɚɧɧɨɦ ɫɥɭɱɚɟ ɨɬɤɨɦɩɢɥɢɪɨɜɚɧɧɵɣ ɮɚɣɥ .bpl ɡɚɩɢɫɚɥɫɹ ɜ ɪɚɛɨɱɢɟ ɩɚɩɤɢ Delphi ɜ C:\Program Files\Borland\... ȼɨ ɦɧɨɝɢɯ ɫɥɭɱɚɹɯ ɞɨɫɬɭɩ ɜ ɷɬɢ ɩɚɩɤɢ ɡɚɤɪɵɬ ɚɞɦɢɧɢɫɬɪɚɬɨɪɨɦ. Ɍɨɝɞɚ ɫɥɟɞɭɟɬ ɹɜɧɨ ɭɤɚɡɚɬɶ ɩɚɩɤɢ ɧɚɡɧɚɱɟɧɢɹ ɜ ɩɭɧɤɬɟ ɦɟɧɸ Project | Options ɧɚ ɜɤɥɚɞɤɟ DirectoriesŇConditionals ɜ ɫɬɪɨɤɚɯ Output Directory ɢ DCP Output Directory. Ʉɚɤ ɬɨɥɶɤɨ ɤɨɦɩɨɧɟɧɬ ɭɫɬɚɧɨɜɥɟɧ ɜ ɩɚɥɢɬɪɟ, ɟɝɨ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɜ ɩɪɢɥɨɠɟɧɢɹɯ Delphi, ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɩɪɨɬɟɫɬɢɪɨɜɚɜ ɩɨɜɟɞɟɧɢɟ ɤɨɦɩɨɧɟɧɬɚ ɜɨ ɜɪɟɦɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɯɚɪɚɤɬɟɪɢɫɬɢɤ ɤɨɦɩɨɧɟɧɬɚ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɦɨɠɟɬ ɨɤɚɡɚɬɶɫɹ ɜɟɫɶɦɚ ɫɥɨɠɧɵɦ, ɨɫɨɛɟɧɧɨ ɟɫɥɢ ɤɨɦɩɨɧɟɧɬ ɢɦɟɟɬ ɦɧɨɝɨ ɫɜɨɣɫɬɜ. ȼ ɧɚɲɟɦ ɫɥɭɱɚɟ ɜɫɟ, ɱɬɨ ɧɭɠɧɨ ɫɞɟɥɚɬɶ – ɷɬɨ ɩɨɦɟɫɬɢɬɶ ɷɤɡɟɦɩɥɹɪ ɧɚ ɮɨɪɦɭ ɢ ɩɪɨɜɟɪɢɬɶ, ɞɟɣɫɬɜɭɸɬ ɥɢ ɧɨɜɵɟ ɡɧɚɱɟɧɢɹ ɪɚɡɦɟɪɚ. ȿɫɥɢ ɤɨɦɩɨɧɟɧɬ ɪɚɛɨɬɚɟɬ ɧɟɩɪɚɜɢɥɶɧɨ ɢɥɢ ɛɨɥɶɲɟ ɧɟ ɩɪɟɞɫɬɚɜɥɹɟɬ ɢɧɬɟɪɟɫ, ɟɝɨ ɦɨɠɧɨ ɭɞɚɥɢɬɶ ɢɡ IDE ɫ ɩɨɦɨɳɶɸ ɨɤɧɚ ɭɫɬɚɧɨɜɤɢ ɩɚɤɟɬɚ (ɫɦ. ɪɢɫ. 8 ɢ ɬɚɛɥ. 5). ɉɨɫɥɟ ɢɫɩɪɚɜɥɟɧɢɹ ɨɲɢɛɨɤ ɢ ɞɥɹ ɞɚɥɶɧɟɣɲɟɣ ɨɬɥɚɞɤɢ ɭɞɨɛɧɨ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɪɟɞɚɤɬɨɪɨɦ ɩɚɤɟɬɨɜ, ɜ ɤɨɬɨɪɨɦ ɦɨɠɧɨ ɞɨɛɚɜɥɹɬɶ ɢ ɭɞɚɥɹɬɶ 37
ɤɨɦɩɨɧɟɧɬɵ ɜ ɩɚɤɟɬ, ɤɨɦɩɢɥɢɪɨɜɚɬɶ ɩɚɤɟɬɵ. Ɋɟɞɚɤɬɨɪ ɩɚɤɟɬɨɜ (ɪɢɫ. 7) ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɨɹɜɢɬɫɹ, ɟɫɥɢ ɡɚɝɪɭɡɢɬɶ ɩɚɤɟɬ DPK ɜ Delphi ɱɟɪɟɡ ɦɟɧɸ File |Open Project. Ɍɚɤɠɟ ɨɧ ɛɭɞɟɬ ɜɵɡɜɚɧ, ɟɫɥɢ ɜɵɛɪɚɬɶ ɩɪɢ ɭɫɬɚɧɨɜɤɟ ɩɚɤɟɬɚ ɜɤɥɚɞɤɭ Into Existing Package.
Ɋɢɫ. 7. Ɋɟɞɚɤɬɨɪ ɩɚɤɟɬɨɜ ȼ ɨɤɧɟ ɪɟɞɚɤɬɨɪɚ ɩɪɟɞɫɬɚɜɥɟɧ ɮɚɣɥ DPK, ɤɨɬɨɪɵɣ ɜ ɧɚɲɟɦ ɩɪɢɦɟɪɟ ɢɦɟɟɬ ɜɢɞ: package my_butt_pack; {$R *.res} {Ⱦɥɹ ɫɨɤɪɚɳɟɧɢɹ ɨɛɴɟɦɚ ɨɩɰɢɢ ɤɨɦɩɢɥɹɰɢɢ ɨɩɭɳɟɧɵ} requires // ɬɪɟɛɭɟɦɵɟ ɛɢɛɥɢɨɬɟɤɢ rtl, vcl; contains // ɫɨɞɟɪɠɚɧɢɟ ɩɚɤɟɬɚ UMyButton in 'UMyButton.pas'; end.
Ʉɧɨɩɤɚ Compile ɩɨɡɜɨɥɹɟɬ ɨɬɤɨɦɩɢɥɢɪɨɜɚɬɶ ɬɟɤɭɳɢɣ ɩɚɤɟɬ (ɫɟɤɰɢɹ Contains) ɢ ɭɫɬɚɧɨɜɢɬɶ ɟɝɨ ɜ ɫɪɟɞɭ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɤɚɤ ɩɚɤɟɬ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ (ɤɧɨɩɤɚ Install). Ʉɧɨɩɤɚ Add ɞɨɛɚɜɥɹɟɬ ɦɨɞɭɥɶ ɜ ɩɚɤɟɬ, ɚ Remove ɭɞɚɥɹɟɬ ɜɵɞɟɥɟɧɧɵɣ ɦɨɞɭɥɶ ɢɡ ɩɚɤɟɬɚ. Ʉɧɨɩɤɚ Option ɨɬɨɛɪɚɠɚɟɬ ɫɬɚɧɞɚɪɬɧɨɟ ɨɤɧɨ ɨɩɰɢɣ ɩɪɨɟɤɬɨɜ ɢ ɩɚɤɟɬɨɜ. Ɋɚɡɭɦɟɟɬɫɹ, ɩɨɫɥɟɞɧɢɟ ɞɟɣɫɬɜɢɹ ɥɟɝɤɨ ɜɵɩɨɥɧɢɬɶ ɢ ɨɛɵɱɧɵɦ ɪɟɞɚɤɬɢɪɨɜɚɧɢɟɦ ɮɚɣɥɚ, ɧɚɩɪɢɦɟɪ, ɜ NotePad.
38
ɉɪɢɦɟɱɚɧɢɟ. ȿɫɥɢ ɜɵ ɪɚɫɩɪɨɫɬɪɚɧɹɟɬɟ ɤɨɦɩɨɧɟɧɬ ɛɟɡ ɢɫɯɨɞɧɵɯ ɤɨɞɨɜ, ɬɨ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɜɚɲɟɝɨ ɤɨɦɩɨɧɟɧɬɚ ɫɥɟɞɭɟɬ ɩɟɪɟɞɚɬɶ ɮɚɣɥɵ: – BPL-ɮɚɣɥ, ɫɨɞɟɪɠɚɳɢɣ ɤɨɦɩɨɧɟɧɬ (ɜ ɧɚɲɟɦ ɫɥɭɱɚɟ – MyButton.bpl); – DCU-ɮɚɣɥ ɫ ɨɬɤɨɦɩɢɥɢɪɨɜɚɧɧɵɦ ɦɨɞɭɥɟɦ (UMyButton.dcu, ɫɫɵɥɤɚ ɧɚ ɷɬɨɬ ɮɚɣɥ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɨɹɜɥɹɟɬɫɹ ɜ ɫɟɤɰɢɢ Uses ɦɨɞɭɥɹ ɩɪɢ ɪɚɡɦɟɳɟɧɢɢ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɮɨɪɦɟ). ɑɬɨɛɵ ɭɫɬɚɧɨɜɢɬɶ BPL-ɩɚɤɟɬ ɜ ɫɪɟɞɭ Delphi (ɜ ɨɬɫɭɬɫɬɜɢɟ ɞɪɭɝɢɯ ɮɚɣɥɨɜ), ɩɨɥɶɡɨɜɚɬɟɥɶ ɞɨɥɠɟɧ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɭɧɤɬ ɦɟɧɸ Component | Install Packages, ɩɪɢ ɜɵɛɨɪɟ ɤɨɬɨɪɨɝɨ ɩɨɹɜɥɹɟɬɫɹ ɞɢɚɥɨɝɨɜɨɟ ɨɤɧɨ ɭɫɬɚɧɨɜɤɢ ɩɚɤɟɬɚ (ɪɢɫ. 8).
Ɋɢɫ. 8. Ɉɤɧɨ ɭɫɬɚɧɨɜɤɢ ɩɚɤɟɬɚ ɋɩɢɫɨɤ Design Packages ɩɪɟɞɨɫɬɚɜɥɹɟɬ ɜɫɟ ɩɚɤɟɬɵ, ɜɤɥɸɱɟɧɧɵɟ ɜ IDE. Ⱦɥɹ ɪɚɛɨɬɵ ɫɨ ɫɩɢɫɤɨɦ ɢɫɩɨɥɶɡɭɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɤɧɨɩɤɢ (ɬɚɛɥ. 5).
39
Ɍɚɛɥɢɰɚ 5 ɂɧɫɬɚɥɥɢɪɭɟɬ
Add
ɩɚɤɟɬ
ɜɪɟɦɟɧɢ
ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ
(ɮɚɣɥɵ
*.bpl, ɧɚɯɨɞɹɳɢɟɫɹ ɜ ɩɚɩɤɟ ..\delphi\bin ɢɥɢ ɜ ɭɤɚɡɚɧɧɨɣ ɜɚɦɢ ɩɚɩɤɟ). ɋɨɞɟɪɠɢɦɨɟ ɩɚɤɟɬɚ ɨɬɨɛɪɚɡɢɬɫɹ ɧɚ ɩɚɥɢɬɪɟ ɤɨɦɩɨɧɟɧɬɨɜ ɢ ɛɭɞɟɬ ɞɨɫɬɭɩɧɨ ɜɨ ɜɫɟɯ ɩɪɨɟɤɬɚɯ Remove
ɍɞɚɥɹɟɬ ɢɡ IDE ɜɵɞɟɥɟɧɧɵɣ ɩɚɤɟɬ. Ⱦɥɹ ɭɞɚɥɟɧɢɹ ɦɨɠɧɨ ɭɛɪɚɬɶ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɮɥɚɠɨɤ. ȼɫɟ ɤɨɦɩɨɧɟɧɬɵ, ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧɧɵɟ ɜ ɩɚɤɟɬɟ, ɛɭɞɭɬ ɧɟɞɨɫɬɭɩɧɵ Ɉɬɤɪɵɜɚɟɬ ɜɵɛɪɚɧɧɵɣ ɩɚɤɟɬ ɜ Ɋɟɞɚɤɬɨɪɟ ɩɚɤɟɬɨɜ, ɟɫɥɢ ɞɨɫ-
Edit
ɬɭɩɟɧ ɟɝɨ ɢɫɯɨɞɧɵɣ ɬɟɤɫɬ Components ɉɨɤɚɡɵɜɚɟɬ ɤɨɦɩɨɧɟɧɬɵ, ɜɤɥɸɱɟɧɧɵɟ ɜ ɞɚɧɧɵɣ ɩɚɤɟɬ Ɏɥɚɠɨɤ Build With Runtime Packages ɭɤɚɡɵɜɚɟɬ ɧɚ ɧɟɨɛɯɨɞɢɦɨɫɬɶ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɩɚɤɟɬɨɜ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɩɪɢ ɫɨɡɞɚɧɢɢ EXE-ɮɚɣɥɚ. ɂɦɹ ɬɪɟɛɭɟɦɨɝɨ ɩɚɤɟɬɚ (DCP-ɮɚɣɥɚ) ɞɨɩɢɫɵɜɚɟɬɫɹ ɜ ɫɬɪɨɤɭ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɢɥɢ ɜɵɛɢɪɚɟɬɫɹ ɧɚɠɚɬɢɟɦ ɤɧɨɩɤɢ Add. (ɂɡ-ɡɚ ɨɝɪɚɧɢɱɟɧɧɨɫɬɢ ɨɛɴɟɦɚ ɩɨɫɨɛɢɹ ɜɨɩɪɨɫɵ ɢɫɩɨɥɶɡɨɜɚɧɢɹ Runtime Packages ɡɞɟɫɶ ɧɟ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ). ɑɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɬɟɤɭɳɭɸ ɤɨɧɮɢɝɭɪɚɰɢɸ ɩɚɤɟɬɨɜ ɜ ɤɚɱɟɫɬɜɟ ɤɨɧɮɢɝɭɪɚɰɢɢ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɜɫɟɯ ɩɪɨɟɤɬɨɜ, ɭɫɬɚɧɨɜɢɬɟ ɮɥɚɠɨɤ Default. ȿɫɥɢ ɠɟ ɤɨɦɩɨɧɟɧɬ ɪɚɫɩɪɨɫɬɪɚɧɹɟɬɫɹ ɜ ɢɫɯɨɞɧɵɯ ɤɨɞɚɯ, ɬɨ ɩɨɥɶɡɨɜɚɬɟɥɸ ɜɦɟɫɬɨ .bpl ɫɥɟɞɭɟɬ ɩɪɟɞɨɫɬɚɜɢɬɶ ɮɚɣɥɵ .pas ɢ, ɜɨɡɦɨɠɧɨ, .dpk ɢ .dcr. 3.7. Ɋɟɫɭɪɫ ɤɨɦɩɨɧɟɧɬɚ ɋɭɳɟɫɬɜɭɟɬ ɢ ɞɨɩɨɥɧɢɬɟɥɶɧɚɹ ɡɚɞɚɱɚ, ɜɵɩɨɥɧɹɟɦɚɹ ɜ ɩɪɨɰɟɫɫɟ ɭɫɬɚɧɨɜɤɢ. Ʉɨɝɞɚ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɤɨɦɩɨɧɟɧɬ, Delphi ɩɪɨɫɦɚɬɪɢɜɚɟɬ ɤɚɬɚɥɨɝɢ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɩɭɬɢ ɩɨɢɫɤɚ, ɩɵɬɚɹɫɶ ɨɛɧɚɪɭɠɢɬɶ ɮɚɣɥ, ɢɦɟɸɳɢɣ ɬɨ ɠɟ ɫɚɦɨɟ ɢɦɹ, ɱɬɨ ɢ ɦɨɞɭɥɶ, ɧɨ ɫ ɪɚɫɲɢɪɟɧɢɟɦ. dcr. Ɋɚɫɲɢɪɟɧɢɟ dcr – ɷɬɨ ɫɨɤɪɚɳɟɧɢɟ ɨɬ Delphi Component Resource, ɢ ɮɚɣɥɵ dcr ɧɚ ɫɚɦɨɦ ɞɟɥɟ ɹɜ40
ɥɹɸɬɫɹ ɮɚɣɥɚɦɢ ɪɟɫɭɪɫɨɜ Windows. ȿɫɥɢ ɮɚɣɥ ɪɟɫɭɪɫɚ ɞɥɹ ɭɫɬɚɧɚɜɥɢɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɧɚɣɞɟɧ, ɬɨ ɟɝɨ ɫɨɞɟɪɠɢɦɨɟ ɬɚɤɠɟ ɫɜɹɡɵɜɚɟɬɫɹ ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɤɨɦɩɨɧɟɧɬɨɜ. Ƚɥɚɜɧɚɹ ɰɟɥɶ ɮɚɣɥɚ ɪɟɫɭɪɫɚ ɤɨɦɩɨɧɟɧɬɚ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɡɚɞɚɧɢɢ ɪɚɫɬɪɨɜɵɯ ɢɡɨɛɪɚɠɟɧɢɣ (ɩɢɤɬɨɝɪɚɦɦ), ɩɪɟɞɫɬɚɜɥɹɸɳɢɯ ɤɨɦɩɨɧɟɧɬɵ ɜ ɩɚɥɢɬɪɟ. Ⱦɥɹ ɫɨɡɞɚɧɢɹ dcr-ɮɚɣɥɨɜ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɪɨɝɪɚɦɦɭ Image Editor, ɩɨɫɬɚɜɥɹɟɦɭɸ ɜɦɟɫɬɟ ɫ Delphi. ȼ ɦɟɧɸ ɩɪɨɝɪɚɦɦɵ ɜɵɛɟɪɢɬɟ File | New | Component Resource File (.dcr). Ⱦɚɥɟɟ ɜ ɦɟɧɸ ɜɵɛɟɪɢɬɟ Resource | New | Bitmap. ȼ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ ɩɚɪɚɦɟɬɪɨɜ ɪɚɫɬɪɨɜɨɝɨ ɢɡɨɛɪɚɠɟɧɢɹ ɡɚɞɚɣɬɟ ɪɚɡɦɟɪ 24×24 ɩɢɤɫɟɥɹ ɢ ɪɟɠɢɦ VGA (16 colors). ɇɚɪɢɫɭɣɬɟ ɬɪɟɛɭɟɦɭɸ ɩɢɤɬɨɝɪɚɦɦɭ. ɇɢɠɧɢɣ ɥɟɜɵɣ ɩɢɤɫɟɥɶ ɜ ɪɚɫɬɪɨɜɨɦ ɢɡɨɛɪɚɠɟɧɢɢ ɫɥɭɠɢɬ ɜ ɤɚɱɟɫɬɜɟ ɢɧɞɢɤɚɬɨɪɚ ɩɪɨɡɪɚɱɧɨɝɨ ɰɜɟɬɚ. ȼɫɟ ɞɪɭɝɢɟ ɩɢɤɫɟɥɢ ɛɢɬɨɜɨɝɨ ɢɡɨɛɪɚɠɟɧɢɹ ɫ ɬɚɤɢɦ ɠɟ ɰɜɟɬɨɦ ɛɭɞɭɬ ɤɚɡɚɬɶɫɹ ɩɪɨɡɪɚɱɧɵɦɢ. ɂɦɹ ɩɢɤɬɨɝɪɚɦɦɵ ɞɨɥɠɧɨ ɫɨɜɩɚɞɚɬɶ ɫ ɢɦɟɧɟɦ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ, ɧɨ ɡɚɩɢɫɚɧɨ ɩɪɨɩɢɫɧɵɦɢ ɛɭɤɜɚɦɢ. ȼ ɧɚɲɟɦ ɫɥɭɱɚɟ ɩɟɪɟɢɦɟɧɭɣɬɟ Bitmap1 ɧɚ TMYBUTTON. ɗɬɨ ɢɡɨɛɪɚɠɟɧɢɟ ɧɭɠɧɨ ɫɨɯɪɚɧɢɬɶ ɜ ɩɚɩɤɟ, ɜ ɤɨɬɨɪɨɣ ɧɚɯɨɞɢɬɫɹ ɦɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ, ɫ ɢɦɟɧɟɦ, ɫɨɜɩɚɞɚɸɳɢɦ ɫ ɢɦɟɧɟɦ ɦɨɞɭɥɹ ɤɨɦɩɨɧɟɧɬɚ ɢ c ɪɚɫɲɢɪɟɧɢɟɦ. dcr – UMYBUTTON.DCR. Ⱦɥɹ ɧɚɞɟɠɧɨɫɬɢ ɞɨɛɚɜɶɬɟ ɫɬɪɨɱɤɭ {$R 'UMYBUTTON.DCR'} ɜ ɬɟɤɫɬ dpkɮɚɣɥɚ: package my_butt_pack; {$R *.res} {$R 'UMYBUTTON.DCR'} …
ɉɨɫɥɟ ɷɬɨɝɨ ɩɨɬɪɟɛɭɟɬɫɹ ɬɨɥɶɤɨ ɭɫɬɚɧɨɜɢɬɶ ɤɨɦɩɨɧɟɧɬ ɜ ɧɭɠɧɵɣ ɩɚɤɟɬ ɧɚ ɧɭɠɧɭɸ ɩɚɥɢɬɪɭ. ȿɫɥɢ ɮɚɣɥ ɪɟɫɭɪɫɚ ɧɨɜɵɯ ɤɨɦɩɨɧɟɧɬɨɜ ɧɟ ɫɨɡɞɚɧ, ɬɨ Delphi ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɪɚɫɬɪɨɜɨɟ ɢɡɨɛɪɚɠɟɧɢɟ ɩɪɟɞɤɚ, ɟɫɥɢ ɬɚɤɨɜɨɟ ɢɦɟɟɬɫɹ, ɢɥɢ ɢɡɨɛɪɚɠɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ.
41
4. ɉɪɢɦɟɪɵ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ 4.1. Ʉɨɦɩɨɧɟɧɬ, ɧɚɫɥɟɞɧɢɤ ɤɥɚɫɫɚ TGraphicControl ȼ ɤɚɱɟɫɬɜɟ ɛɨɥɟɟ ɫɥɨɠɧɨɝɨ ɩɪɢɦɟɪɚ ɫɨɡɞɚɞɢɦ ɤɨɦɩɨɧɟɧɬ Gr, ɩɪɟɞɧɚɡɧɚɱɟɧɧɵɣ
ɞɥɹ
ɪɢɫɨɜɚɧɢɹ
ɝɪɚɮɢɤɚ
ɮɭɧɤɰɢɢ
y=a*x*x
ɜ
ɨɤɧɟ
(x1,y1,x2,y2). ȼɵɛɨɪ ɤɥɚɫɫɚ-ɩɪɟɞɤɚ ɨɩɪɟɞɟɥɹɟɬɫɹ ɬɟɦ, ɱɬɨ ɞɥɹ ɞɚɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɬɪɟɛɭɟɬɫɹ ɜɨɡɦɨɠɧɨɫɬɶ ɪɢɫɨɜɚɧɢɹ, ɧɨ ɧɟ ɧɭɠɟɧ ɮɨɤɭɫ ɜɜɨɞɚ. ɉɭɛɥɢɤɭɟɦɵɦɢ
ɫɜɨɣɫɬɜɚɦɢ
ɞɨɥɠɧɵ
ɛɵɬɶ
ɤɨɷɮɮɢɰɢɟɧɬ
a
ɢ
ɤɨɨɪɞɢɧɚɬɵ
x1,y1,x2,y2. ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ TGr, ɨɩɪɟɞɟɥɹɸɳɟɝɨ ɬɚɤɨɣ ɤɨɦɩɨɧɟɧɬ [3]. unit UGr; interface uses Windows, Messages, SysUtils, Classes, Controls,Graphics; type TGr = class(TGraphicControl) private FPen:TPen; FBrush:TBrush; Fx1,Fy1,Fx2,Fy2:real;//ɤɨɨɪɞɢɧɚɬɵ ɨɤɧɚ Fa:real;
//ɤɨɷɮɮɢɰɢɟɧɬ ɮɭɧɤɰɢɢ
function II(x:real):Integer; //ɩɟɪɟɜɨɞ ɪɟɚɥɶɧɨɣ ɤɨɨɪɞɢɧɚɬɵ x //ɜ ɷɤɪɚɧɧɭɸ function JJ(y:real):Integer; //ɩɟɪɟɜɨɞ ɪɟɚɥɶɧɨɣ ɤɨɨɪɞɢɧɚɬɵ // y ɜ ɷɤɪɚɧɧɭɸ function F(x:real):real;
//ɮɭɧɤɰɢɹ, ɨɩɪɟɞɟɥɹɸɳɚɹ ɝɪɚɮɢɤ
procedure SetX1(Value:real); //ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ x1 procedure SetY1(Value:real); //ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ y1 procedure SetX2(Value:real); //ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ x2 procedure SetY2(Value:real); //ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ y2 42
//ɭɫɬɚɧɨɜɤɚ ɤɨɷɮɮɢɰɢɟɧɬɚ a
procedure SetA(Value:real);
procedure SetBrush(Value:TBrush); //ɭɫɬɚɧɨɜɤɚ ɤɢɫɬɢ procedure SetPen(Value:TPen); //ɭɫɬɚɧɨɜɤɚ ɩɟɪɚ protected { Protected declarations } public { Public declarations } constructor Create(AOwner:TComponent);override; destructor Destroy;override; procedure Paint;override;//ɩɟɪɟɪɢɫɨɜɤɚ published procedure StyleChanged(Sender: TObject); dynamic; //ɩɭɛɥɢɤɭɟɦɵɟ ɫɜɨɣɫɬɜɚ: ɧɚɫɥɟɞɭɟɦɵɟ property Align; property Anchors; property DragCursor; property DragKind; property DragMode; property Enabled; property Constraints; property ParentShowHint; property ShowHint; property Visible; //ɩɭɛɥɢɤɭɟɦɵɟ ɫɜɨɣɫɬɜɚ: ɫɨɛɫɬɜɟɧɧɵɟ property Pen:TPen read FPen write SetPen property Brush:TBrush read FBrush write SetBrush; property x1:real read FX1 write SetX1; property x2:real read FX2 write SetX2; property Y1:real read FY1 write SetY1; property Y2:real read FY2 write SetY2; property A:real read FA write SetA; //ɩɭɛɥɢɤɭɟɦɵɟ ɧɚɫɥɟɞɭɟɦɵɟ ɫɨɛɵɬɢɹ property OnDragDrop; 43
property OnDragOver; property OnEndDrag; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnStartDock; property OnStartDrag; end;
procedure Register; implementation // ɤɨɧɫɬɪɭɤɬɨɪ constructor TGr.Create(AOwner:TComponent); begin inherited Create(AOwner); ControlStyle:= ControlStyle+[csReplicatable]; FX1:= –1; FX2:=10; FY1:= –1; FY2:=10; Fa:=1; Left:=10; Top:=10; Width:=300; Height:=200; FPen:=TPen.Create; FPen.OnChange:=StyleChanged; FBrush:=TBrush.Create; FBrush.OnChange:=StyleChanged; end; // ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ x1 procedure TGr.SetX1(Value:real); begin 44
if FX1<>Value then begin FX1:=Value; Paint; end; end; // ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ y1 procedure TGr.SetY1(Value:real); begin if FY1<>Value then begin FX1:=Value; Paint; end; end; // ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ x2 procedure TGr.SetX2(Value:real); begin if FX2<>Value then begin FX2:=Value; Paint; end; end; // ɭɫɬɚɧɨɜɤɚ ɤɨɨɪɞɢɧɚɬɵ y2 procedure TGr.SetY2(Value:real); begin if FY2<>Value then begin FY2:=Value; Paint; end; end; 45
ɭɫɬɚɧɨɜɤɚ ɤɨɷɮɮɢɰɢɟɧɬɚ a
//
procedure TGr.SetA(Value:real); begin if FA<>Value then begin FA:=Value; Paint; end; end; // ɭɫɬɚɧɨɜɤɚ ɤɢɫɬɢ procedure TGr.SetBrush(Value:TBrush); begin if FBrush<>Value then begin FBrush:=Value; Paint; end; end; //
ɭɫɬɚɧɨɜɤɚ ɩɟɪɚ
procedure TGr.SetPen(Value:TPen); begin if FPen<>Value then begin FPen:=Value; Paint; end; end; //ɜɵɱɢɫɥɟɧɢɟ ɷɤɪɚɧɧɨɣ ɤɨɨɪɞɢɧɚɬɵ II ɞɥɹ ɪɟɚɥɶɧɨɣ ɤɨɨɪɞɢɧɚɬɵ x function TGr.II(x:real):Integer; begin Result:=Round((x-FX1)*Width/(FX2-FX1)); end;
46
//ɜɵɱɢɫɥɟɧɢɟ ɷɤɪɚɧɧɨɣ ɤɨɨɪɞɢɧɚɬɵ JJ ɞɥɹ ɪɟɚɥɶɧɨɣ ɤɨɨɪɞɢɧɚɬɵ y function TGr.JJ(y:real):Integer; begin Result:=Height-Round((y-FY1)*Height/(FY2-FY1)); end; //ɮɭɧɤɰɢɹ, ɨɩɪɟɞɟɥɹɸɳɚɹ ɝɪɚɮɢɤ function TGr.F(x:real):real; begin Result:=Fa*x*x; end;
//ɞɟɫɬɪɭɤɬɨɪ destructor TGr.Destroy; begin FPen.Free; FBrush.Free; inherited destroy; end; //ɩɟɪɟɪɢɫɨɜɤɚ ɤɨɦɩɨɧɟɧɬɚ procedure TGr.Paint; const n=60;//ɤɨɥɢɱɟɫɬɜɨ ɬɨɱɟɤ, ɩɨ ɤɨɬɨɪɵɦ ɫɬɪɨɢɬɫɹ ɝɪɚɮɢɤ var I : integer; h,x : real; begin h:=(X2-X1)/n;//ɲɚɝ ɢɡɦɟɧɟɧɢɹ ɚɪɝɭɦɟɧɬɚ ɮɭɧɤɰɢɢ with Canvas do begin //ɪɢɫɨɜɚɧɢɟ ɨɫɟɣ ɤɨɨɪɞɢɧɚɬ Rectangle(0,0,Width,height); MoveTo(II(X1),JJ(0)); 47
LineTo(II(X2),JJ(0)); MoveTo(II(0),JJ(Y1)); LineTo(II(0),JJ(Y2)); //ɤɪɢɜɚɹ ɥɢɧɢɹ ɝɪɚɮɢɤɚ y=F(x) ɪɢɫɭɟɬɫɹ ɤɚɤ // ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɬɪɟɡɤɨɜ[x,F(x),x+h,F(x+h)] x := X1; MoveTo(II(x),JJ(F(x))); for I := 1 to n do begin x := x+h; LineTo(II(x),JJ(F(x))); end; end; end; {ɩɪɨɰɟɞɭɪɚ ɪɟɝɢɫɬɪɚɰɢɢ} procedure Register; begin RegisterComponents('My', [TGr]); end; end.
48
ɇɚ ɪɢɫ. 5 ɩɪɟɞɫɬɚɜɥɟɧ ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ Gr ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ.
Ɋɢɫ. 5. Ɍɟɫɬɢɪɨɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ Gr 4.2. Ʉɨɦɩɨɧɟɧɬ ɞɥɹ ɨɬɨɛɪɚɠɟɧɢɹ ɬɟɤɭɳɟɝɨ ɜɪɟɦɟɧɢ Ɍɟɤɭɳɟɟ ɜɪɟɦɹ ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ɤɨɦɩɨɧɟɧɬɟ-ɩɨɬɨɦɤɟ TCustomLabel ɩɨ ɫɨɛɵɬɢɸ OnTimer ɤɨɦɩɨɧɟɧɬɚ TTimer. ȼɵɛɟɪɢɬɟ ɩɭɧɤɬ ɦɟɧɸ Component | New Component ɢ ɜ ɨɬɤɪɵɜɲɟɦɫɹ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ ɡɚɞɚɣɬɟ ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ Class Name (TTimeLabel), ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ-ɩɪɟɞɤɚ Ancestor Type (TCustomlabel), ɧɚɡɜɚɧɢɟ ɩɚɥɢɬɪɵ (Samples)
ɢ
ɢɦɹ
ɦɨɞɭɥɹ,
ɫɨɞɟɪɠɚɳɟɝɨ
ɤɨɦɩɨɧɟɧɬ
(ɧɚɩɪɢɦɟɪ,
F:\IRA\TimeLabel.pas). Ȼɭɞɟɬ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɫɨɡɞɚɧɚ ɡɚɝɨɬɨɜɤɚ ɦɨɞɭɥɹ, ɤɨɬɨɪɭɸ ɧɟɨɛɯɨɞɢɦɨ ɨɬɪɟɞɚɤɬɢɪɨɜɚɬɶ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: 49
unit TimeLabel; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, extctrls;
type TTimeLabel = class(TCustomLabel) private FActive: Boolean; protected T: Ttimer;
// Ɍɚɣɦɟɪ
procedure GetTime (s: Tobject); procedure ChangeActive(a: boolean); public constructor Create (Owner: Tcomponent); override; destructor Destroy; override; published property Active: Boolean read FActive write ChangeActive; property Color; property Font; end;
procedure Register;
implementation
procedure TTimeLabel.GetTime; begin // ɗɬɨɬ ɦɟɬɨɞ ɨɬɨɛɪɚɠɚɟɬ ɧɚ ɡɚɝɨɥɨɜɤɟ ɜɪɟɦɹ. ɑɬɨɛɵ ɜɫɟ // ɪɚɛɨɬɚɥɨ, ɟɝɨ ɧɚɞɨ ɛɭɞɟɬ ɧɚɡɧɚɱɢɬɶ ɫɨɛɵɬɢɸ OnTimer // ɬɚɣɦɟɪɚ. if Active then 50
Caption:=TimeToStr(Time); end;
procedure TTimeLabel.ChangeActive; begin FActive:=A; T.Enabled:=A; Visible:=A; end;
constructor TTimeLabel.create; begin Inherited create(Owner); T:=TTimer.Create(Self); T.Enabled:=true; T.Interval:=1000; // ɉɪɢɫɜɚɢɜɚɟɦ ɫɨɛɵɬɢɸ ɨɛɪɚɛɨɬɱɢɤ: T.OnTimer:=GetTime; FActive:=True; Width:=50; Height:=30; end;
destructor TTimeLabel.destroy; begin T.Free; inherited Destroy; end;
procedure Register; begin RegisterComponents('Samples', [TTimeLabel]); end; end. 51
Ⱦɥɹ ɬɟɫɬɢɪɨɜɚɧɢɹ ɫɨɡɞɚɣɬɟ ɧɨɜɵɣ ɩɪɨɟɤɬ, ɞɨɛɚɜɶɬɟ ɜ ɫɟɤɰɢɸ uses ɫɫɵɥɤɭ ɧɚ ɦɨɞɭɥɶ TimeLabel ɢ ɜ ɨɛɪɚɛɨɬɱɢɤ OnCreate ɞɥɹ ɮɨɪɦɵ ɡɚɩɢɲɢɬɟ: procedure TForm1.FormCreate(Sender: TObject); var L: TTimeLabel; begin L:=TTimeLabel.Create(self); L.Parent:=Self; L.top:=0; L.Left:=Clientwidth-L.width; end;
Ⱦɥɹ ɭɫɬɚɧɨɜɤɢ ɤɨɦɩɨɧɟɧɬɚ ɜ ɫɪɟɞɭ Delphi ɜɵɛɟɪɢɬɟ ɩɭɧɤɬ ɦɟɧɸ Component | Install Component, ɭɤɚɡɚɜ ɧɚ ɫɬɪɚɧɢɰɟ Into New Package ɢɦɹ ɦɨɞɭɥɹ TimeLabel.pas ɢ ɢɦɹ ɩɚɤɟɬɚ TimeLabelPack.dpk. 4.3. Ʉɨɦɩɨɧɟɧɬ, ɜɵɩɨɥɧɹɸɳɢɣ ɜɵɱɢɫɥɟɧɢɹ ɧɚɞ ɫɨɞɟɪɠɢɦɵɦ ɨɩɭɛɥɢɤɨɜɚɧɧɨɝɨ ɫɜɨɣɫɬɜɚ TStrings ȼ ɤɚɱɟɫɬɜɟ ɩɪɟɞɤɚ ɜɵɛɟɪɟɦ TCustomGroupBox, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɞɨɩɨɥɧɢɬɟɥɶɧɨ ɫɨɞɟɪɠɚɬɶ ɤɧɨɩɤɭ btCalc ɢ ɦɟɬɤɭ lbResult ɞɥɹ ɜɵɜɨɞɚ ɪɟɡɭɥɶɬɚɬɨɜ. ɇɚ ɦɟɬɤɟ ɛɭɞɟɬ ɨɬɨɛɪɚɠɟɧɚ ɫɭɦɦɚ ɡɧɚɱɟɧɢɣ, ɜɜɟɞɟɧɧɵɯ ɜ ɦɧɨɝɨɫɬɪɨɱɧɨɦ ɫɜɨɣɫɬɜɟ Text ɬɢɩɚ TStrings (ɩɪɟɞɩɨɥɚɝɚɟɬɫɹ, ɱɬɨ ɤɚɠɞɨɟ ɡɧɚɱɟɧɢɟ ɡɚɧɢɦɚɟɬ ɨɞɧɭ ɫɬɪɨɤɭ). Ɇɨɞɭɥɶ ɤɨɦɩɨɧɟɧɬɚ ɢɦɟɟɬ ɜɢɞ: unit CalcBox; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TCalcBox = class(TCustomGroupBox) 52
private FText: TStrings; btCalc: TButton; lbResult: TLabel; protected procedure SetText(value: TStrings); procedure btCalcClick(sender: TObject); public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property Caption; property Top; property Height; property Width; // ɋɜɨɣɫɬɜɨ ɤɥɚɫɫɚ TStrings property Text: TStrings read FText write SetText; end;
procedure Register;
implementation
constructor TCalcBox.Create(AOwner: TComponent); begin inherited Create(AOwner); Width := 200; Height := 150; caption:='ɋɭɦɦɚ';
btCalc:=TButton.Create (self); with btCalc do begin Width := 75; Height := 25; 53
Parent:=Self; Top:=120; Left:=0; Caption:='Calculate'; // Ⱦɟɥɟɝɢɪɭɟɦ ɦɟɬɨɞ btCalcClick ɫɨɛɵɬɢɸ OnClick OnClick:=btCalcClick; end;
lbResult:=TLabel.Create (self); with lbResult do begin Width := 75; Height := 25; Parent:=Self; top:=60; Left:=0; Caption:=''; end; //ɋɨɡɞɚɟɦ ɩɨɬɨɦɨɤ ɚɛɫɬɪɚɤɬɧɨɝɨ ɤɥɚɫɫɚ TStrings FText:=TStringList.create; FText.clear; end;
procedure TCalcBox.SetText(Value: TStrings); begin // Ʉɨɩɢɪɭɟɦ ɜ ɩɨɥɟ FText ɫɨɞɟɪɠɢɦɨɟ TStrings FText.Assign(Value); end;
procedure TCalcBox.btCalcClick(Sender: TObject); var sum: extended; i: integer; begin // ɉɨɞɫɱɢɬɵɜɚɟɦ ɫɭɦɦɭ ɜɜɟɞɟɧɧɵɯ ɡɧɚɱɟɧɢɣ try Sum:=0; 54
for i:=0 to Text.Count-1 do Sum:=Sum+ StrToFloat(Text[i]); lbResult.Caption:=FloatToStr(Sum); except ShowMessage('Ɉɲɢɛɤɚ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ!'); end; end;
destructor TCalcBox.Destroy; begin btCalc.free; lbResult.Free; inherited Destroy; end;
procedure Register; begin RegisterComponents('Samples', [TCalcBox]); end;
end.
Ɂɚɞɚɧɢɹ ȼ ɤɚɠɞɨɦ ɡɚɞɚɧɢɢ ɬɪɟɛɭɟɬɫɹ: – ɫɨɡɞɚɬɶ ɤɥɚɫɫ ɞɥɹ ɤɨɦɩɨɧɟɧɬɚ; – ɫɨɡɞɚɬɶ ɬɟɫɬɢɪɭɸɳɟɟ ɩɪɢɥɨɠɟɧɢɟ; – ɩɪɨɜɟɪɢɬɶ ɪɚɛɨɬɭ ɤɨɦɩɨɧɟɧɬɚ ɩɪɢ ɞɢɧɚɦɢɱɟɫɤɨɦ ɫɨɡɞɚɧɢɢ; – ɭɫɬɚɧɨɜɢɬɶ ɤɨɦɩɨɧɟɧɬ; – ɩɪɨɬɟɫɬɢɪɨɜɚɬɶ ɢɧɬɟɪɮɟɣɫ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ; – ɩɨɞɝɨɬɨɜɢɬɶ ɨɬɱɟɬ. ɋɬɪɭɤɬɭɪɚ ɨɬɱɟɬɧɨɣ ɪɚɛɨɬɵ ɞɨɥɠɧɚ ɛɵɬɶ ɩɨɫɬɪɨɟɧɚ ɩɨ ɨɛɳɟɣ ɫɯɟɦɟ. ɋɥɟɞɭɟɬ ɨɛɪɚɬɢɬɶ ɜɧɢɦɚɧɢɟ ɧɚ ɨɬɪɚɠɟɧɢɟ ɫɥɟɞɭɸɳɟɣ ɢɧɮɨɪɦɚɰɢɢ. ȼ ɪɚɡ55
ɞɟɥɟ «Ⱥɧɚɥɢɡ ɡɚɞɚɱɢ» ɧɟɨɛɯɨɞɢɦɨ ɨɛɨɫɧɨɜɚɬɶ ɜɵɛɨɪ ɤɥɚɫɫɚ-ɩɪɟɞɤɚ ɤɨɦɩɨɧɟɧɬɚ, ɚ ɬɚɤɠɟ ɜɵɛɨɪ ɩɭɛɥɢɤɭɟɦɵɯ ɫɜɨɣɫɬɜ. ȼ ɪɚɡɞɟɥɟ «ɉɥɚɧ ɬɟɫɬɢɪɨɜɚɧɢɹ» ɬɪɟɛɭɟɬɫɹ ɨɩɢɫɚɧɢɟ ɤɚɤ ɢɧɬɟɪɮɟɣɫɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ, ɬɚɤ ɢ ɢɧɬɟɪɮɟɣɫɚ ɜɪɟɦɟɧɢ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ. 1. ɋɢɫɬɟɦɧɵɟ ɱɚɫɵ. 2. ɋɢɫɬɟɦɧɚɹ ɞɚɬɚ. 3. Ⱥɬɪɢɛɭɬɵ ɮɚɣɥɚ. 4. ɇɟɩɪɹɦɨɭɝɨɥɶɧɚɹ ɤɧɨɩɤɚ. 5. ɉɟɪɟɦɟɳɚɟɦɚɹ ɤɧɨɩɤɚ. 6. Ƚɪɚɮɢɤ ɮɭɧɤɰɢɢ: ɚ) ɮɭɧɤɰɢɹ ɡɚɞɚɧɚ ɬɚɛɥɢɱɧɨ; ɛ) ɡɧɚɱɟɧɢɹ ɚɪɝɭɦɟɧɬɨɜ ɢ ɮɭɧɤɰɢɢ ɡɚɞɚɧɵ ɜ ɮɚɣɥɟ; ɜ) ɮɭɧɤɰɢɹ ɜɵɛɢɪɚɟɬɫɹ ɢɡ ɡɚɞɚɧɧɨɝɨ ɤɚɤ ɩɟɪɟɱɢɫɥɢɦɵɣ ɬɢɩ ɫɩɢɫɤɚ. 7. ɇɟɩɪɹɦɨɭɝɨɥɶɧɨɟ ɢɡɨɛɪɚɠɟɧɢɟ. 8. ȼɢɡɭɚɥɶɧɨɟ ɛɢɧɚɪɧɨɟ ɞɟɪɟɜɨ. 9. ȼɢɡɭɚɥɶɧɵɣ ɝɪɚɮ. 10. ɋɬɨɥɛɢɤɨɜɚɹ ɞɢɚɝɪɚɦɦɚ. 11. Ʉɪɭɝɨɜɚɹ ɞɢɚɝɪɚɦɦɚ. 12. Ʌɢɧɟɣɱɚɬɚɹ ɞɢɚɝɪɚɦɦɚ. 13. ɉɨ ɞɚɬɟ ɨɩɪɟɞɟɥɢɬɶ ɡɧɚɤ ɡɨɞɢɚɤɚ ɢ «ɡɜɟɪɹ». 14. ɉɨ ɞɚɬɟ ɨɩɪɟɞɟɥɢɬɶ ɞɟɧɶ ɧɟɞɟɥɢ. 15. ɋɭɦɦɚ ɞɜɭɯ ɩɨɥɢɧɨɦɨɜ. 16. Ɂɧɚɱɟɧɢɟ ɩɨɥɢɧɨɦɚ. 17. ɉɪɨɢɡɜɟɞɟɧɢɟ ɞɜɭɯ ɩɨɥɢɧɨɦɨɜ. 18. ɋɤɚɥɹɪɧɨɟ ɩɪɨɢɡɜɟɞɟɧɢɟ ɞɜɭɯ ɩɨɥɢɧɨɦɨɜ. 19. Ʉɨɦɩɨɧɟɧɬ «ɉɥɨɬɧɨɫɬɢ ɪɚɫɩɪɟɞɟɥɟɧɢɹ ɜɟɪɨɹɬɧɨɫɬɟɣ». 20. Ʉɨɦɩɨɧɟɧɬ «ɋɩɥɚɣɧɵ». 21. Ʉɨɦɩɨɧɟɧɬ «StringGrid + ComboBox».
56
Ʌɢɬɟɪɚɬɭɪɚ 1. Ⱥɥɟɤɫɚɧɞɪɨɜɫɤɢɣ Ⱥ. Ⱦ. Delphi 5.0. Ɋɚɡɪɚɛɨɬɤɚ ɤɨɪɩɨɪɚɬɢɜɧɵɯ ɩɪɢɥɨɠɟɧɢɣ / Ⱥ.Ⱦ. Ⱥɥɟɤɫɚɧɞɪɨɜɫɤɢɣ. – Ɇ. : ȾɆɄ, 2000. – 512 ɫ. 2. Ʉɨɧɨɩɤɚ Ɋ. ɋɨɡɞɚɧɢɟ ɨɪɢɝɢɧɚɥɶɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ ɜ ɫɪɟɞɟ DELPHI / Ɋ. Ʉɨɧɨɩɤɚ. – Ʉɢɟɜ : ɇɂɉɎ – ȾɢɚɋɨɮɬɅɬɞ, 1996. – 512 ɫ. 3. Ɇɟɬɨɞɢɱɟɫɤɢɟ ɭɤɚɡɚɧɢɹ ɤ ɤɭɪɫɭ «ȼɢɡɭɚɥɶɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɜ Delphi». Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ. ɋɨɡɞɚɧɢɟ ɧɨɜɨɝɨ ɤɨɦɩɨɧɟɧɬɚ / ɫɨɫɬ. : ɇ.Ⱥ. Ɍɸɤɚɱɟɜ, ȼ.Ƚ. Ɋɭɞɚɥɟɜ, Ɇ.ȼ. Ȼɚɤɥɚɧɨɜ. – ȼɨɪɨɧɟɠ: ɅɈɉ ȼȽɍ, 1999. – 34 ɫ. 4. Ʉɷɧɬɭ Ɇ. Delphi 7 ɞɥɹ ɩɪɨɮɟɫɫɢɨɧɚɥɨɜ / Ɇ. Ʉɷɧɬɭ. – ɋɉɛ. : ɉɢɬɟɪ, 2004. – 1101 ɫ. 5. Ɍɟɣɤɫɟɣɪɚ ɋ. Delphi 5. Ɋɭɤɨɜɨɞɫɬɜɨ ɪɚɡɪɚɛɨɬɱɢɤɚ / ɋ. Ɍɟɣɤɫɟɣɪɚ, Ʉ. ɉɚɱɟɤɨ. – Ɇ. : ȼɢɥɶɹɦɫ, 2000. – Ɍ. 2 : Ɋɚɡɪɚɛɨɬɤɚ ɤɨɦɩɨɧɟɧɬɨɜ ɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɛɚɡ ɞɚɧɧɵɯ. – 992 ɫ.
57
ɍɱɟɛɧɨɟ ɢɡɞɚɧɢɟ ɊȺɁɊȺȻɈɌɄȺ ɄɈɆɉɈɇȿɇɌɈȼ ȼ DELPHI ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ
ɋɨɫɬɚɜɢɬɟɥɢ: ȼɨɳɢɧɫɤɚɹ Ƚɢɥɶɞɚ ɗɞɝɚɪɨɜɧɚ, Ɋɭɞɚɥɟɜ ȼɚɥɟɪɢɣ Ƚɟɧɧɚɞɶɟɜɢɱ, Ⱥɪɬɟɦɨɜ Ɇɢɯɚɢɥ Ⱥɧɚɬɨɥɶɟɜɢɱ Ɋɟɞɚɤɬɨɪ Ɉ.Ⱥ. ɂɫɚɟɜɚ
ɉɨɞɩɢɫɚɧɨ ɜ ɩɟɱɚɬɶ 27.09.07. Ɏɨɪɦɚɬ 60×84/16. ɍɫɥ. ɩɟɱ. ɥ. 3,4. Ɍɢɪɚɠ 50 ɷɤɡ. Ɂɚɤɚɡ 2003. ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɩɥ. ɢɦ. Ʌɟɧɢɧɚ, 10. Ɍɟɥ. 208-298, 598-026 (ɮɚɤɫ) http://www.ppc.vsu.ru; e-mail:
[email protected] Ɉɬɩɟɱɚɬɚɧɨ ɜ ɬɢɩɨɝɪɚɮɢɢ ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɨɝɨ ɰɟɧɬɪɚ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɭɥ. ɉɭɲɤɢɧɫɤɚɹ, 3. Ɍɟɥ. 204-133. 58