⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame_utilfunc.pas

📁 企业信息管理系统程序框架
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit frame_UtilFunc;

interface

uses Forms, Controls, SysUtils, Variants, Graphics, Dialogs, ComCtrls, StdCtrls,
  ExtCtrls, ADODB, IniFiles, Classes, DB, Menus, Windows,
  DBGrids,  TypInfo,DBGridEh,adoxquery;

type
  // 应用程序异常类
  EAppException = class(Exception);
function SetGridWidth(Grid1: TDbGrideh; const name1: integer): boolean;
function GetGridWidth(Grid1: TDbGrideh; const name1: integer): boolean;
// 设置属性值包括子属性
function SetPropValueIncludeSub(Instance: TObject; const PropName: string;
  const Value: Variant): Boolean;
// 获取属性值包括子属性
function GetPropValueIncludeSub(Instance: TObject; PropName: string;
  PreferStrings: Boolean = True): Variant;

// 取前月
function GetPreMonth(monthstr: string): string;

//对字符串解密(Src:源 Key:密匙)
function UncrypStr(Src, Key: string): string; overload;
function UncrypStr(Src: string): string; overload;
//对字符串加密(Src:源 Key:密匙)
function EncrypStr(Src, Key: string): string; overload;
function EncrypStr(Src: string): string; overload;


// 浮点转化为0.00
function FloatToMoney(afloat: double): string;
function FloatToMoney4(afloat: double): string;
// 获取程序路径

function GetAppPath: string;

// 读取应用程序配置文件AppCfg.ini
function ReadCfg(const Section, Ident, Default: string): string; overload;

// 写配置文件
procedure WriteCfg(const Section, Ident, Value: string);

// 显示提示确认窗口
function InfoOk(Mess: string): boolean; overload;

// 显示提示确认窗口
function InfoOk(Mess: string; Caption: string): boolean; overload;

// 显示询问窗口
function QueryDlg(Mess: string; DefaultNo: Boolean): Boolean; overload;

// 显示询问窗口
function QueryDlg(Mess: string; DefaultNo: Boolean; Caption: string): Boolean; overload;

// 增加sql条件
procedure addSQLCondition(var wheresql: string; condition: string);


// 写日志
procedure WriteLog(Str: string);

// 显示错误窗口
procedure ErrorDlg(Mess: string); overload;

// 显示错误窗口
procedure ErrorDlg(Mess: string; Caption: string); overload;

// 显示Modal窗体
function ShowModalForm(FormClass: TFormClass; var Reference): TModalResult;

const
  // 应用程序配置文件名
  AppConfigFileName = 'AppCfg.Ini';

var
  // 默认连接
  GV_Con: TCustomConnection;

  // 默认Qry
  GV_Qry: TDataSet;

implementation




 //------------------------------------------------------------------------------
 // 显示Modal窗体
 //------------------------------------------------------------------------------

function ShowModalForm(FormClass: TFormClass; var Reference): TModalResult;
begin
  Application.CreateForm(FormClass, Reference);
  try
    Result := TForm(Reference).ShowModal;
  finally
    FreeAndNil(TForm(Reference))
  end;
end;


 //------------------------------------------------------------------------------
 // 显示错误窗口
 //------------------------------------------------------------------------------

procedure ErrorDlg(Mess: string);
begin
  Application.MessageBox(PChar(Mess), PChar(Application.Title), MB_OK + MB_ICONSTOP);
end;

 //------------------------------------------------------------------------------
 // 显示错误窗口
 //------------------------------------------------------------------------------

procedure ErrorDlg(Mess: string; Caption: string);
begin
  Application.MessageBox(PChar(Mess), PChar(Caption), MB_OK + MB_ICONSTOP);
end;

 //------------------------------------------------------------------------------
 // 写日志到yyyymmdd.log文件
 //------------------------------------------------------------------------------

procedure WriteLog(Str: string);
var
  File_Name: string; // 异常文件名
  MesCaption: string; // 日志类型
  Frmhandle: hwnd; // 句柄
  FrmCaption: string; // 窗体标题
  ls_Title: array[0..254] of char; // 标题
  SaveMessage: string; // 保存的信息
  aFilehandle: integer; // 文件句柄
  F: textFile; // 处理的文本文件
begin
  // 异常文件名
  File_Name := ExtractFilePath(Application.ExeName) +
    FormatDateTime('yyyymmdd', now) +
    '.Log';

  // 日志类型
  MesCaption := '日志信息';

  // 获取所在的窗口的标题
  FrmHandle := GetactiveWindow(); // 获取触发异常模块所在的窗口
  // 句柄(活动窗口句柄)
  if FrmHandle <> NULL then
  begin
    if GetWindowText(FrmHandle, Ls_Title, 255) > 0 then
      //记录窗口的标题
    begin
      FrmCaption := StrPas(Ls_Title)
    end
    else begin
      FrmCaption := '无标题'
    end
  end;

  // 记录异常详细情况
  SaveMessage := '============================================================';
  SaveMessage := SaveMessage + #13 + #10 +
    FormatDateTime('yyyy-mm-dd hh:nn:ss ', now); // 纪录异常时间
  SaveMessage := SaveMessage + #13 + #10 + '类型:' + mesCaption;
  // 纪录异常类型
  SaveMessage := SaveMessage + #13 + #10 + '窗口:' + FrmCaption;
  // 纪录异常窗口标题
  SaveMessage := SaveMessage + #13 + #10 + '信息:' + Str + #13 + #10;
  // 记录异常原因

  // 创建保存异常信息文件
  if not FileExists(File_Name) then
  begin
    aFileHandle := FileCreate(File_Name);
    FileClose(aFileHandle);
  end;

  AssignFile(F, File_Name);

  // 写错误日志
  Append(F); // 设置增加的模式
  Writeln(F, SaveMessage); // 把异常信息追加到文件中
  CloseFile(F); // 关闭文件
end;






function GetGridWidth(Grid1: TDbGrideh; const name1: integer): boolean;
var
  path: string;
begin
  path := extractfilepath(application.exename);
  if copy(path, length(path), 1) <> '\' then
    path := path + '\';
  with grid1 do
  begin
    Columns.BeginUpdate;
    Columns.Grid.RestoreColumnsLayoutIni(path + 'grid.ini', IntToStr(name1),
      [crpColIndexEh, crpColWidthsEh, crpSortMarkerEh, crpColVisibleEh,
    crpDropDownRowsEh, crpDropDownWidthEh]);
    columns.EndUpdate;
  end;
  result := true;
end;

function SetGridWidth(Grid1: TDbGrideh; const name1: integer): boolean;
var
  path: string;
begin
  path := extractfilepath(application.exename);
  if copy(path, length(path), 1) <> '\' then
    path := path + '\';
  with grid1 do
  begin
    Columns.BeginUpdate;
    Columns.Grid.saveColumnsLayoutIni(path + 'grid.ini', IntToStr(name1),
      false);
    columns.EndUpdate;
  end;
  result := true;
end;


// 取权限

 //------------------------------------------------------------------------------
 // 显示提示确认窗口
 //------------------------------------------------------------------------------

function InfoOk(Mess: string): boolean;
begin
  Result := Application.MessageBox(PChar(Mess), PChar(Application.Title),
    MB_OK + MB_ICONINFORMATION) = idOk;
end;

 //------------------------------------------------------------------------------
 // 显示提示确认窗口
 //------------------------------------------------------------------------------

function InfoOk(Mess: string; Caption: string): boolean;
begin
  Result := Application.MessageBox(PChar(Mess), PChar(Caption),
    MB_OK + MB_ICONINFORMATION) = idOk;
end;


// 取前月

function GetPreMonth(monthstr: string): string;
begin
  Result := IntToStr(strtointdef(monthstr, 0) - 1);
  if copy(Result, 5, 2) = '00' then
  begin
    Result := IntToStr(strtointdef(copy(monthstr, 1, 4), 0) - 1) + '12';
  end;
end;
 //******************************************************************************
 // 获取程序配置文件名(应用程序目录 + AppCfg.ini)
 //******************************************************************************

function GetAppCfgFileName: string;
var
  ApplicationPath: string; // 应用程序路径
begin
  ApplicationPath := ExtractFilePath(Application.exeName);
  Result := ApplicationPath + AppConfigFileName;
end;

 //------------------------------------------------------------------------------
 // 读取配置文件
 //------------------------------------------------------------------------------

function ReadCfg(const Section, Ident, Default: string): string;
var
  AppCfg: TiniFile;
begin
  Appcfg := TiniFile.Create(GetAppcfgFileName);
  Result := AppCfg.ReadString(Section, Ident, Default);
  Appcfg.Free;
end;

 //------------------------------------------------------------------------------
 // 写配置文件
 //------------------------------------------------------------------------------

procedure WriteCfg(const Section, Ident, Value: string);
var
  AppCfg: TiniFile;
begin
  Appcfg := TiniFile.Create(GetAppcfgFileName);
  AppCfg.WriteString(Section, Ident, Value);
  Appcfg.Free;
end;
 //------------------------------------------------------------------------------
 // 获取程序路径
 //------------------------------------------------------------------------------

function GetAppPath: string;
begin
  Result := ExtractFilePath(Application.exeName);
end;

function FloatToMoney4(afloat: double): string;
begin
  Result := Format('%.4f', [aFloat]);
end;

function FloatToMoney(afloat: double): string;
begin
  Result := Format('%.2f', [aFloat]);
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -