如鵬網 大學生計算機學習社區

          CowNew開源團隊

          http://www.cownew.com 郵件請聯系 about521 at 163.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            363 隨筆 :: 2 文章 :: 808 評論 :: 0 Trackbacks
          ?

          對話框控件的制作

          Delphi中有很多對話框組件,例如TopenDialogTfontDialog等。這些控件的特點就是雖然是不可視控件,但是在運行時都有一個可視化的效果,比如TopenDialog的可視化效果就是一個打開對話框。我們這次將開發一個日期對話框控件,當我們調用控件的Execute方法(不一定非要使用Execute方法,不過大部分對話框控件都是使用這個方法,我們也就按照慣例來了)時,就會彈出一個可以選擇日期的對話框,我們選擇一個日期后,點擊“確定”則Execute返回True,點擊“取消”則Execute返回False。我們可以讀取Date屬性來得到用戶選擇的日期,也可以修改此屬性來改變對話框的初始日期。

          1、新建一個對話框。在對話框窗體上放置一個TmonthCalendar組件,命名為Cal,窗體名稱改為FormDate。在窗體上放置兩個按鈕,一個按鈕的Caption為“確定(&O)”,ModalResultmrOk,一個按鈕的Caption為“取消(&C)”,ModalResultmrCancel。設計好的窗體如下圖所示:

          delphicomdev7-1.jpg

          2、為窗體添加兩個Public訪問級的方法:

          ??? function GetSelDate: TDate;

          ??? procedure SetInitDate(AValue: TDate);

          代碼如下:

          function TFormDate.GetSelDate: TDate;

          begin

          ? result := cal.Date;

          end;

          ?

          procedure TFormDate.SetInitDate(AValue: TDate);

          begin

          ? cal.Date := AValue;

          end;

          3、新建一個控件,派生自Tcomponent

          代碼如下:

          unit DateDialog;

          ?

          interface

          ?

          uses

          ? SysUtils, Classes, Controls, frmDlg;

          ?

          type

          ? TDateDialog = class(TComponent)

          ? private

          ??? FDlg: TFormDate;

          ??? function GetDate: TDate;

          ??? procedure SetDate(AValue: TDate);

          ? protected

          ? public

          ??? constructor Create(AOwner: TComponent);override;

          ??? function Execute: Boolean;

          ? published

          ??? property Date: TDate read GetDate write SetDate;

          ? end;

          ?

          procedure Register;

          ?

          implementation

          procedure Register;

          begin

          ? RegisterComponents('Linco', [TDateDialog]);

          end;

          constructor TDateDialog.Create(AOwner: TComponent);

          begin

          ? inherited Create(AOwner);

          ? FDlg := TFormDate.Create(self);

          end;

          ?

          function TDateDialog.Execute: Boolean;

          begin

          ? result := (FDlg.ShowModal = mrOK);

          end;

          ?

          function TDateDialog.GetDate: TDate;

          begin

          ? result := FDlg.GetSelDate;

          end;

          ?

          procedure TDateDialog.SetDate(AValue: TDate);

          begin

          ? FDlg.SetInitDate(AValue);

          end;

          end.

          代碼比較簡單就不多解釋了。

          思考題:

          1、做一個模仿TcolorDialog的對話框控件。

          posted on 2005-10-21 23:37 CowNew開源團隊 閱讀(1469) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 兴文县| 禄丰县| 中宁县| 疏勒县| 包头市| 中宁县| 漠河县| 雅江县| 天等县| 北安市| 荣昌县| 宁晋县| 宜春市| 弥渡县| 乐至县| 和龙市| 伊川县| 宁晋县| 牙克石市| 那坡县| 大同市| 和龙市| 吴堡县| 广灵县| 棋牌| 武义县| 茂名市| 古丈县| 鄂州市| 台前县| 大洼县| 临江市| 巩留县| 田林县| 顺昌县| 原平市| 乌拉特后旗| 怀远县| 惠州市| 涞水县| 台中市|