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

📄 publictype.pas

📁 语音与识别技术包含很多C语言处理模块系统
💻 PAS
字号:
unit PublicType;
                
interface

uses
  Windows, Messages, Forms, IniFiles, Classes, SysUtils, Controls, ComCtrls,
  Dialogs;

Type
  TWinVersion_Type = (NOT_FOND,     //不是 Windows 操作系统
                      WIN_32_S,     // WIN32、3.x 操作系统
                      WIN_95,       // WIN95、98、ME 操作系统
                      WIN_NT        // WINNT、2000、XP 操作系统
                      );

Type
  ExamDiagType = (ED_NEW,           //新建试题文件
                  ED_SAVE,          //保存试题文件
                  ED_OPEN           //打开试题文件
                  );

Type
  TExamRecord = Record
    AskFileName: String[225];        //答案文件
    SoundFileName: String[225];      //试题文件
    StantAnswer: Byte;               //标准答案
  end;

Type
  TSoundExamData = Record
    Str: String;                     //字符串型数据
    Data: Integer;                   //整型数据
  end;

Type
  TSoundExamType = (SET_EXEFILENAME, //取听力考试的可执行文件
                    SET_EXAMFILENAME,//取听力考试的试卷文件
                    SET_ANSTIME,     //取听力考试的答题时间
                    SET_PLAYLOOP,    //取听力考试的播放次数
                    SET_WAITPLAY,    //取听力考试的播放时间间隔
                    SET_EXAMCOMNAME  //取听力考试用的串口
                    );

const
  BookMarkMax           = 50;                   //书签的最大数量
  Type_SP               = 1;                    //以 SP 模式播放
  Type_SSP              = 3;                    //以 SSP 模式播放
  Type_SPS              = 5;                    //以 SPS 模式播放
  Type_SPSP             = 5;                    //以 SPSP 模式播放
  ExamPath              = 'SoundExam';          //听力考试文件路径
  SaveAnsPath           = 'SoundAns';           //答案文件路径
  ExamFormClass         = 'ExamSoundFormName';  //听力考试程序窗口类
  WaveSoundClass        = 'WaveSoundFormName';  //听力考试后台程序窗口类

  WM_CM_User            = WM_User + $F0;        //自定义消息
  WM_CM_RESTORE         = WM_CM_User + $01;     //换醒程序
  WM_CM_SD_BEGIN        = WM_CM_User + $02;     //开始考试
  WM_CM_SD_PLAY         = WM_CM_User + $03;     //放音
  WM_CM_SD_ANSWER       = WM_CM_User + $04;     //开始收答卷
  WM_CM_SD_END          = WM_CM_User + $05;     //结束考试
  WM_CM_WINCLOSE        = WM_CM_User + $06;     //关闭程序

Type
  TExamFile = class
  private
    { Private declarations }
    ExamRecord: Array of TExamRecord;
    FLength: Byte;
    FFileName: String;
    FChangeSave: Boolean;
    function GetExam(Index: Byte): TExamRecord;
    procedure SetFileName(const Value: String);
    function GetExamPaht: String;
  public
    { Public declarations }
    constructor Create; overload;  //构造器
    destructor Destroy; override;  //析构器
    procedure Free;
    procedure Add(const Value: TExamRecord);
    procedure Delete(const Index: Byte);
    function Open(const Name: String = ''): Boolean;
    procedure Save(const Name: String = '');
    procedure Modify(const Value: TExamRecord; const Index: Byte);
    procedure Clear;
    property Length: Byte read FLength;
    property Exam[Index: Byte]: TExamRecord read GetExam;
    property FileName: String read FFileName write SetFileName;
    property ExamPath: String read GetExamPaht;
    property ChangeSave: Boolean read FChangeSave;
  end;

var
  ApplicationPath: String;

procedure HideTitlebar(Wind: TForm);
procedure MouseDownMoveForm(Handle: HWnd);
function GetWinVersion: TWinVersion_Type;
function ReadString(const Section, Ident: string): String;
function ReadInteger(const Section, Ident: string): Integer;
procedure WriteInteger(const Section, Ident: string; Value: Integer);
procedure GetSoundExamData(var Data: TSoundExamData; const SType: TSoundExamType);
procedure SetSoundExamData(Data: TSoundExamData; const SType: TSoundExamType);
procedure CMCreateDir(const PathName: String);

implementation

const
  ApplicationINIFile = 'Phonetic.rc';
  SoundExamDataIni   = 'SoundData.ini';

procedure HideTitlebar(Wind: TForm);
Var
  Save : LongInt;
begin
  if Wind.BorderStyle = bsNone then
     Exit;
  Save := GetWindowLong(Wind.Handle,gwl_Style);
  if (Save and ws_Caption)=ws_Caption then
  begin
    Case Wind.BorderStyle of
      bsSingle, bsSizeable: SetWindowLong(Wind.Handle, gwl_Style, Save and
                (Not(ws_Caption)) or ws_border);
      bsDialog : SetWindowLong(Wind.Handle, gwl_Style, Save and
                (Not(ws_Caption)) or ds_modalframe or ws_dlgframe);
    end;

    Wind.Height := Wind.Height - getSystemMetrics(sm_cyCaption);
    Wind.Refresh;
  end;
end;

procedure MouseDownMoveForm(Handle: HWnd);
const
  sc_DragMove: longint = $F012;
begin
  ReleaseCapture;
  SendMessage(Handle,wm_SysCommand,sc_DragMove,0);
end;

function GetWinVersion: TWinVersion_Type;
var
  VersionInfo: TOSVersionInfo;
begin
  Result := NOT_FOND;
  VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
  if Windows.GetVersionEx( VersionInfo ) then
  begin
     with VersionInfo do
     begin
        case dwPlatformId of
           VER_PLATFORM_WIN32s: Result := WIN_32_S;
           VER_PLATFORM_WIN32_WINDOWS: Result := WIN_95;
           VER_PLATFORM_WIN32_NT: Result := WIN_NT;
        end; // case dwPlatformId
     end; // with VersionInfo
  end; // if GetVersionEx
end;

function ReadString(const Section, Ident: string): String;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ApplicationPath + ApplicationINIFile);
  Result := Ini.ReadString(Section, Ident, '');
  Ini.Free;
end;

function ReadInteger(const Section, Ident: string): Integer;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ApplicationPath + ApplicationINIFile);
  Result := Ini.ReadInteger(Section, Ident, 0);
  Ini.Free;
end;

procedure WriteInteger(const Section, Ident: string; Value: Integer);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ApplicationPath + ApplicationINIFile);
  Ini.WriteInteger(Section, Ident, Value);
  Ini.Free;
end;

procedure GetSoundExamData(var Data: TSoundExamData; const SType: TSoundExamType);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ApplicationPath + SoundExamDataIni);
  Case SType of
    SET_EXEFILENAME:   Data.Str  := ApplicationPath + Ini.ReadString('Application', 'ExeFileName', '');
    SET_EXAMFILENAME:  Data.Str  := Ini.ReadString('Application', 'ExamFileName', '');
    SET_ANSTIME:       Data.Data := Ini.ReadInteger('Application', 'AnsTime', 0);
    SET_PLAYLOOP:      Data.Data := Ini.ReadInteger('Application', 'PlayLoop', 0);
    SET_WAITPLAY:      Data.Data := Ini.ReadInteger('Application', 'WaitPlay', 0);
    SET_EXAMCOMNAME:   Data.Str  := Ini.ReadString('Application', 'ExamComName', '');
  end;
  Ini.Free;
  if Data.Data <= 0 then
     Data.Data := 1;
end;

procedure SetSoundExamData(Data: TSoundExamData; const SType: TSoundExamType);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ApplicationPath + SoundExamDataIni);
  Case SType of
    SET_EXEFILENAME:   Ini.WriteString('Application', 'ExeFileName', ExtractFileName(Data.Str));
    SET_EXAMFILENAME:  Ini.WriteString('Application', 'ExamFileName', Data.Str);
    SET_ANSTIME:       Ini.WriteInteger('Application', 'AnsTime', Data.Data);
    SET_PLAYLOOP:      Ini.WriteInteger('Application', 'PlayLoop', Data.Data);
    SET_WAITPLAY:      Ini.WriteInteger('Application', 'WaitPlay', Data.Data);
    SET_EXAMCOMNAME:   Ini.WriteString('Application', 'ExamComName', Data.Str);
  end;
  Ini.Free;
end;

procedure CMCreateDir(const PathName: String);
var
  Path, Path1: String;
begin
  Path := Copy(PathName, Pos('\', PathName) + 1,
          Length(PathName) - Pos('\', PathName));
  path1 := Copy(PathName, 0, Pos('\', PathName));
  While True do
  begin         //创建各级子目录
    Path1 := Path1 + Copy(Path, 0, Pos('\', Path));
    Path := Copy(Path, Pos('\', Path) + 1, Length(Path) - Pos('\', Path));
    if Not DirectoryExists(Path1) then
       CreateDir(Path1);
    if Pos('\', Path) <= 0 then
       break;
  end;
end;

{ TExamFile }
const
  HeadAskFileName = 'CMSoundExam';
  HeadSoundFileName = '2003年1月22日';

constructor TExamFile.Create;
begin
  inherited;
  Self.ExamRecord := NIL;
  Self.FChangeSave := False;
end;

destructor TExamFile.Destroy;
begin
  Self.ExamRecord := NIL;
  inherited;
end;

procedure TExamFile.Free;
begin
  if Self <> NIL then
     Self.Destroy;
end;

function TExamFile.Open(const Name: String): Boolean;
var
  EFile: File of TExamRecord;
  Red: TExamRecord;
  i: Byte;
begin
  Result := False;
  if Not FileExists(Name) then
  begin
    Application.MessageBox('找不到文件!', '文件错误', MB_OK + MB_ICONERROR);
    Exit;
  end;
  Self.Clear;
  if Name = '' then
     AssignFile(EFile, Self.FFileName)
  else
     AssignFile(EFile, Name);
  Reset(EFile);

  if Not Eof(EFile) then
  begin
    BlockRead(EFile, Red, 1);
    if (Red.AskFileName <> HeadAskFileName) or (Red.SoundFileName <> HeadSoundFileName) then
    begin
       Application.MessageBox(PChar(ExtractFileName(FileName) + '文件不是试卷文件!'),
       '文件错误', MB_OK + MB_ICONERROR);
       CloseFile(EFile);
       Exit;
    end;
    i := Red.StantAnswer;
  end
  else begin
    CloseFile(EFile);
    Exit;
  end;

  While Not EOF(EFile) do
  begin
    BlockRead(EFile, Red, 1);
    Self.Add(Red);
  end;
  CloseFile(EFile);
  if i <> Self.FLength then
  begin
     Application.MessageBox(PChar('读取文件:“' + ExtractFileName(FileName) + '”错误,可能文件已经损坏!'),
     '文件错误', MB_OK + MB_ICONERROR);
     Exit;
  end;
  Self.FChangeSave := False;
  Result := True;
end;

procedure TExamFile.Save(const Name: String);
var
  EFile: File of TExamRecord;
  i: Byte;
  Red: TExamRecord;
begin
  Red.AskFileName := HeadAskFileName;
  Red.SoundFileName := HeadSoundFileName;
  Red.StantAnswer := Self.FLength;

  if FileName = '' then
     AssignFile(EFile, Self.FFileName)
  else
     AssignFile(EFile, Name);
  ReWrite(EFile);
  BlockWrite(EFile, Red, 1);
  if Self.FLength = 0 then
  begin
     CloseFile(EFile);
     Self.FChangeSave := False;
     Exit;
  end;
  for i := 0 to Self.FLength - 1 do
  begin
    BlockWrite(EFile, Self.ExamRecord[i], 1);
  end;
  CloseFile(EFile);
  Self.FChangeSave := False;
end;

procedure TExamFile.Clear;
begin
  if Self.Length <= 0 then
     Exit;
  Self.ExamRecord := NIL;
  Self.FLength := 0;
//  Self.FChangeSave := True;
end;

function TExamFile.GetExam(Index: Byte): TExamRecord;
begin
  Result.AskFileName := '';
  Result.SoundFileName := '';
  Result.StantAnswer := 0;
  if Index >= Self.FLength then
     Exit;
  Result.AskFileName := Self.ExamRecord[Index].AskFileName;
  Result.SoundFileName := Self.ExamRecord[Index].SoundFileName;
  Result.StantAnswer := Self.ExamRecord[Index].StantAnswer;
end;

procedure TExamFile.Add(const Value: TExamRecord);
begin
  SetLength(Self.ExamRecord, Self.FLength + 1);
  Self.ExamRecord[Self.FLength].AskFileName := Value.AskFileName;
  Self.ExamRecord[Self.FLength].SoundFileName := Value.SoundFileName;
  Self.ExamRecord[Self.FLength].StantAnswer := Value.StantAnswer;
  Self.FLength := Self.FLength + 1;
  Self.FChangeSave := True;
end;

procedure TExamFile.Delete(const Index: Byte);
var
  i: Byte;
begin
  if Index >= Self.Length then
     Exit;
  if Index = (Self.Length - 1) then
  begin
     Copy(Self.ExamRecord, 0, Self.FLength - 1);
     Self.FLength := Self.FLength - 1;
     Self.FChangeSave := True;
     Exit;
  end;
  for i := Index to Self.FLength - 2 do
  begin
      Self.ExamRecord[i].AskFileName := Self.ExamRecord[i + 1].AskFileName;
      Self.ExamRecord[i].SoundFileName := Self.ExamRecord[i + 1].SoundFileName;
      Self.ExamRecord[i].StantAnswer := Self.ExamRecord[i + 1].StantAnswer;
  end;
  Copy(Self.ExamRecord, 0, Self.FLength - 1);
  Self.FLength := Self.FLength - 1;
  Self.FChangeSave := True;
end;

procedure TExamFile.SetFileName(const Value: String);
begin
  FFileName := Value;
end;

procedure TExamFile.Modify(const Value: TExamRecord; const Index: Byte);
begin
  if Index >= Self.FLength then
  begin
     Self.Add(Value);
     Exit;
  end;
  if (Self.ExamRecord[Index].AskFileName = Value.AskFileName) and
     (Self.ExamRecord[Index].SoundFileName = Value.SoundFileName) and
     (Self.ExamRecord[Index].StantAnswer = Value.StantAnswer)
  then
     Exit;
  Self.ExamRecord[Index].AskFileName := Value.AskFileName;
  Self.ExamRecord[Index].SoundFileName := Value.SoundFileName;
  Self.ExamRecord[Index].StantAnswer := Value.StantAnswer;
  Self.FChangeSave := True;
end;

function TExamFile.GetExamPaht: String;
begin
  Result := '';
  if Self.FFileName = '' then
     Exit;
  Result := ExtractFilePath(Self.FFileName) + ChangeFileExt(ExtractFileName(Self.FFileName), '') + '\';
end;

end.

⌨️ 快捷键说明

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