บทความนี้ จะเสนอถึงขั้นตอน และวิธีการสร้าง
คอมโพเนนท์ที่ชื่อ Fonts Combo Box โดยทำการสืบทอดคอมโพเนนท์จาก Combo Box
โดยมีขั้นตอนทั้งหมดดังนี้
1. เปิดโปรแกรม Delphi และทำการปิดโปรเจ็คที่เปิดอยู่
โดยเลือกที่ File -> Close All
2. เริ่มต้นการสร้างโดยคลิ้กที่เมนู
Component -> New Component จะปรากฏหน้าต่าง Component Wizard ดังรูปด้านล่าง
แล้วทำการ กำหนดค่าในช่องต่างๆดังนี้
Ancester
Type: TComboBox
Class Name: TMdFontBombo
Pallete Page: Md
Unit file name: MdFontBox.pas |
3. คลิกปุ่ม OK ,Component Wizard
จะทำการสร้าง code ให้เราดังรูป
4. ทำการป้อนโค้ดดังนี้
unit
MdFontCombo;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,
Forms;
type
TMdFontCombo = class(TComboBox)
private
{ Private declarations }
FChangeFormFont: Boolean;
procedure SetChangeFormFont(const Value: Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure CreateWnd; override;
procedure Change; override;
published
{ Published declarations }
property Style default csDropDownList;
property Items stored False;
property ChangeFormFont: Boolean
read FChangeFormFont write SetChangeFormFont default
True;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Md', [TMdFontCombo]);
end;
{ TMdFontCombo }
procedure TMdFontCombo.Change;
begin
if FChangeFormFont and Assigned(Owner) and
(Owner is TForm) then
TForm(Owner).Font.Name :=Text;
inherited;
end;
constructor TMdFontCombo.Create(AOwner:
TComponent);
begin
inherited Create(AOwner);
Style := csDropDownList;
FChangeFormFont :=True;
end;
procedure TMdFontCombo.CreateWnd;
begin
inherited CreateWnd;
Items.Assign(Screen.Fonts);
if FChangeFormFont and Assigned(Owner) and
(Owner is TForm) then
ItemIndex :=Items.IndexOf((Owner as TForm).Font.Name);
end;
procedure TMdFontCombo.SetChangeFormFont(const
Value: Boolean);
begin
FChangeFormFont :=Value;
if FChangeFormFont then
Change;
end;
end.
|
5. ต่อไปเป็นขั้นตอนของการติดตั้ง
Component โดยเลือกที่เมนู Component -> Install Component จะปรากฎหน้าต่าง
Install Component ให้ทำการเลือกที่แท็บ into new package
และป้อนค่าของ Package file name เป็น Md ดังแสดงในรูป แล้วคลิกปุ่ม OK
6. ที่หน้าต่าง Conferm ให้ทำการคลิกปุ่ม
Yes, จะปรากฎหน้าต่าง Package ให้ทำการคลิกปุ่ม Compile เพื่อตรวจสอบ ความถูกต้องของโค้ด
7. คลิกปุ่ม Install เพื่อติดตั้ง,
จะปรากฎหน้าต่าง Information แสดงถึง Component ใหม่ที่ได้ติดตั้งเข้าไป
8. ทำการทดสอบโปรแกรมโดยเลือกที่เมนู
File ->New -> Application, จะปรากฎหน้าต่าง Conferm ให้ทำการคลิก
ปุ่ม Yes
9. ทำการเลือก Component ที่เราได้สร้างจาก
Component Palette นำมาวางบน Form1 ตามรูป, แล้วกดปุ่ม F9
เพื่อทดสอบโปรแกรม
แล้วพบกันใหม่ในบทความครั้งต่อไปนะครับ
:-)
|