建立Delphi窗体模板

来源:互联网 发布:mac pyqt sip 编辑:程序博客网 时间:2024/06/06 03:28
 窗体要实现以下功能:
<!--[if !supportLists]-->1.      <!--[endif]-->这个窗体是作为MDI子窗体使用的,Delphi默认的MDI子窗体关闭是最小化,所以需要修改实现关闭后彻底释放窗体。
<!--[if !supportLists]-->2.      <!--[endif]-->中文显示环境。
<!--[if !supportLists]-->3.      <!--[endif]-->Enter键代替Tab键实现控件切换。
<!--[if !supportLists]-->4.      <!--[endif]-->把窗体上的主要数据源的状态显示在窗体的标题(Caption)后面显示;因为这个信息管理系统有数据模块,所以需要在窗体上添加一个数据源控件(TDateSource)来实现这个功能。
<!--[if !supportLists]-->5.      <!--[endif]-->只有当数据集处于添加/编辑状态时与数据源相连的数据感知控件才可用,其他状态不可用。这个功能由ChangeEnableTrue;和ChangeEnableFalse;这两个函数实现。使用的时候只需要把控件的Tag值设置为86,就可以被这两个函数控制了
 
具体实现方法如下:
首先在工程中新建一个窗体,命名为FmExample,在窗体上放置一个标签控件,一个DateSoure控件。修改窗体和控件的属性如下设置:
FmExample:
属性名
注释
Caption
窗体模板
 
Hint
这是一个示范窗体!
可以在激活事件中显示在主窗体状态栏中
FormStyle
fsMIDChind
作为MID子窗体
Positin
poMainFormCenter
在主窗体居中显示
BorderStyle
bsSingle
边框样式
Font->Charset
GB2312_CHARSET
字符集编码
Font->Name
宋体
字符集名称
Font->Size
9
字体大小
KeyPreView
True
按键事件
TDateSource:
属性名
注释
Name
StateDS
 
TLable:
属性名
注释
Name
FrmCaptionLab
 
Caption
输入窗口标题
 
窗体单元文件如下:
{***************************************************************************}
                          软件制作人:Stosc                
                   联系方式:E-Mail:endlock@gmail.com                  
                   版权所有 (c) 2007 尊重版权 违法必究              
{***************************************************************************}
 
unit UExample;
 
interface
 
uses

 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

 Dialogs, DB, DBClient, StdCtrls, DBCtrls, Mask,UDM, Grids, DBGrids, ComCtrls,

 Buttons;
 
type
 TFmExample = class(TForm)
    StateDS: TDataSource;
    FrmCaptionLab: TLabel;
    DBGrid2: TDBGrid;
    procedure FormCreate(Sender: TObject);

    procedure DBComboBox1Enter(Sender: TObject);

    procedure ComboBox1Enter(Sender: TObject);

    procedure FormKeyPress(Sender: TObject; var Key: Char);

    procedure StateDSStateChange(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure FormActivate(Sender: TObject);
    function ChangeEnableTrue:variant; //Tag=86控件的设为可用
    function ChangeEnableFalse:variant; //Tag=86控件的设为不可用
 private
    { Private declarations }
 public
    { Public declarations }
 end;
var
    FmExample: TFmExample;
 
 
implementation
uses USellMain;
 
{$R *.dfm}
 

procedure TFmExample.FormClose(Sender: TObject; var Action: TCloseAction);

begin
//关闭窗体,释放内存
Action:=caFree;
//清空状态栏显示
USellMain.MainFm.StatusBar1.Panels.Items[1].Text:='';
end;
 

procedure TFmExample.FormCreate(Sender: TObject);

begin
 
end;
 

procedure TFmExample.FormKeyPress(Sender: TObject; var Key: Char);

                                          //在窗体中用Enter键代替Tab键切换
begin
if key=#13then   //判断是回车按执行键
 if not(ActiveControl is TDbgrid) Then //不是在TDbgrid控件内
 Begin
    key:=#0;
    perform(WM_NEXTDLGCTL,0,0); //移动到下一个控件
 end else
 if(ActiveControl is TDbgrid)Then  //是在TDbgrid控件内
 begin
    With TDbgrid(ActiveControl)Do
    if Selectedindex<(FieldCount-1) then
      Selectedindex:=Selectedindex+1    //移动到下一字段
    else Selectedindex:=0;
 end;
end;
 

procedure TFmExample.StateDSStateChange(Sender: TObject);   //显示数据集状态

var sDateState:string;

begin
 case StateDS.State of
         dsInactive:
          begin
           sDateState:='未激活';
           self.ChangeEnableFalse;
          end;
         dsBrowse:
          begin
            sDateState:='浏览';
            self.ChangeEnableFalse;
          end;
         dsEdit:
          begin
           sDateState:='修改';
           self.ChangeEnableTrue;
          end;
         dsInsert:
          begin
           sDateState:='添加新记录';
           self.ChangeEnableTrue;
          end
         else
          begin
            sDateState:='其他状态';
            self.ChangeEnableFalse;
          end;
         end;
 self.Caption:=FrmCaptionLab.Caption+'——'+sDateState;
end;
 

procedure TFmExample.FormActivate(Sender: TObject);

begin
//将提示信息显示在状态栏上
USellMain.MainFm.StatusBar1.Panels.Items[1].Text:=self.Hint;
end;
 

function TFmExample.ChangeEnableTrue:variant;   //Tag=86控件的设为可用

var I:integer;
begin
 for I:=0 to ControlCount-1 do
 begin

    if Controls[I].Tag=86 then Controls[I].Enabled:=True;

 end;
end;
 

function TFmExample.ChangeEnableFalse:variant; //Tag=86控件的设为不可用

var I:integer;
begin
 for I:=0 to ControlCount-1 do
 begin

    if Controls[I].Tag=86 then Controls[I].Enabled:=False;

 end;
end;
 
end.

使用的时候在Flie->New->Other 然后找到这个模板窗体(Delphi7在名称和工程同名的选项卡中,Delphi2006是在inheritable items 中),然后建立的窗体就会继承这个窗体的特性。日后只要修改这个模板窗体,那么所有继承自这个窗体的子窗体都会被修改。

原创粉丝点击