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

📄 formsendcommand.pas

📁 控制扫描仪源码 控制扫描仪源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit FormSendCommand;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, DelphiTwain, Twain, TwainTable, ComCtrls;

type
  TSendCommandForm = class(TForm)
    Icon: TImage;
    MainTitle: TLabel;
    Container1: TGroupBox;
    Title1: TLabel;
    Combo1: TComboBox;
    Title2: TLabel;
    Combo2: TComboBox;
    Title3: TLabel;
    Combo3: TComboBox;
    Title4: TLabel;
    Combo4: TComboBox;
    GoSend: TButton;
    Results: TListBox;
    Title5: TLabel;
    Container2: TGroupBox;
    GoClose: TButton;
    SetCombo: TComboBox;
    SetPages: TNotebook;
    Panel1: TPanel;
    GoAddArray: TButton;
    GoRemoveArray: TButton;
    ArrayList: TListBox;
    GoRemoveEnumeration: TButton;
    GoAddEnumeration: TButton;
    EnumerationList: TListBox;
    Label1: TLabel;
    EnumDefault: TEdit;
    EnumDefaultValue: TUpDown;
    SetEnumDefault: TButton;
    Bevel1: TBevel;
    Label2: TLabel;
    RangeMin: TEdit;
    Label3: TLabel;
    RangeMax: TEdit;
    RangeStep: TEdit;
    Label4: TLabel;
    {User clicked to send the command}
    procedure GoSendClick(Sender: TObject);
    {Changing container, updates set page}
    procedure ComboSetChange(Sender: TObject);
    {Add an item to the array or to the enumeration}
    procedure GoAddItem(Sender: TObject);
    {Removes an item from the array or enumeration}
    procedure GoRemoveItem(Sender: TObject);
    {Changing the mode}
    procedure Combo3Change(Sender: TObject);
    {Set the current selected item in the enumeration as default}
    procedure SetEnumDefaultClick(Sender: TObject);
  private
    {Test the result, and display messages in case of error}
    function TestResult(Return: TCapabilityRet): Boolean;
    {Returns the current source}
    function CurSource: TTwainSource;
    {Returns the current selected mode}
    function CurrentMode: TRetrieveCap;
    {Returns the current item type}
    function CurrentItemType: TW_UINT16;
    {Returns the current selected capability}
    function CurrentCapability: TTableInfo;
    {Retrieve value for the selected capability}
    procedure GetValue;
    {Set value for the selected capability}
    procedure SetValue;
    {Update the set pages}
    procedure UpdateSetPages;
  public
    {Form being created}
    constructor Create(AOwner: TComponent); override;
  end;

var
  SendCommandForm: TSendCommandForm;

implementation

uses
  MainForm, DelphiTwainUtils {From twain};

{$R *.DFM}

{ TSendCommandForm }

{Form being created}
constructor TSendCommandForm.Create(AOwner: TComponent);
var
  i: Integer;
begin
  {Let inherited class create}
  inherited Create(AOwner);

  {Add all the capabilities for the capability combo}
  FOR i := 0 TO MAX_CAPABILITY - 1 DO
    Combo1.Items.Add(CapabilitySTR[i]);

  {Select the combo box items}
  Combo1.ItemIndex := Combo1.Items.IndexOf('CAP_SUPPORTEDCAPS');
  Combo2.ItemIndex := 2;   {TWON_ONEVALUE}
  Combo3.ItemIndex := 0;   {rcGet}
  Combo4.ItemIndex := 4;   {TWTY_UINT16}
  UpdateSetPages;
end;

{Returns the current item type}
function TSendCommandForm.CurrentItemType: TW_UINT16;
begin
  Result := Combo4.ItemIndex;
end;

{Returns the current selected capability}
function TSendCommandForm.CurrentCapability: TTableInfo;
var
  i: integer;
begin
  {Looks for the current capability in the list}
  FOR i := 0 TO MAX_CAPABILITY - 1 DO
    if CapabilitySTR[i] = Combo1.Items[Combo1.ItemIndex] then
      Result := CapabilityID[i];
end;

{Returns the current selected mode}
function TSendCommandForm.CurrentMode: TRetrieveCap;
begin
  Result := TRetrieveCap(Combo3.ItemIndex);
end;

{Returns the current source}
function TSendCommandForm.CurSource: TTwainSource;
begin
  Result := FormMain.Twain.Source[FormMain.LoadedSource];
end;

{Filter value to replace numbers for constants strings}
function FilterValue(const Capability: TTableInfo; ItemType: TW_UINT16;
  Value: String): String;
var
  i, j: Integer;
begin
  Result := Value; {Default return in case it didn't find a match}

  {Special value for bool type}
  if (ItemType = TWTY_BOOL) then
  begin
    {Replace 1 by TRUE and -1, 0 for FALSE (or anything else than 1)}
    if Value = '1' then Result := 'True' else Result := 'False';
    exit;
  end;

  {Capability}
  if (Capability.ID = CAP_SUPPORTEDCAPS) then
  begin
    {Look in the list}
    FOR i := 0 TO MAX_CAPABILITY - 1 DO
      if CapabilityID[i].ID = StrToIntDef(Value, -1) then
        Result := CapabilitySTR[i];
    exit;
  end;

  {Check capability table}
  FOR i := low(CapabilityTable) to high(CapabilityTable) do
    if CapabilityTable[i].Cap = Capability.ID then
    begin
      {Match found, look in the new table}
      FOR j := 0 TO CapabilityTable[i].Count - 1 DO
        if CapabilityTable[i].PTR^[j].ID = StrToIntDef(Value, -1) then
          Result := CapabilityTable[i].PTR^[j].STR;
    end {if CapabilityTable[i].Cap = Capability};

end;

{Retrieve value for the selected capability}
procedure TSendCommandForm.GetValue;
var
  Capability: TTableInfo;
  Container : TW_UINT16;
  Handle    : HGLOBAL;
  i         : Integer;

  {For all containers}
  ItemType  : TW_UINT16;
  {One value}
  Value     : String;
  {Range}
  Min, Max, Step, Default, Current: String;
  {Array and enumeration}
  List: TGetCapabilityList;
  {Enumeration}
  CurrentIndex, DefaultIndex: Integer;

begin
  {Store current capability}
  Capability := CurrentCapability();
  if TestResult(CurSource.GetCapabilityRec(Capability.ID, Handle,
    CurrentMode, Container)) then
  begin
    {Update combo boxe}
    Combo2.ItemIndex := Container - 3;
    {Clear results}
    Results.Items.Clear;
    Results.Items.Add(Combo1.Items[Combo1.ItemIndex]);
    Results.Items.Add('ItemType = ' + Combo4.Items[Combo4.ItemIndex]);

    {Test the container returned}
    case Container of

      {One value container}
      TWON_ONEVALUE:
      begin
        {Call method. Result will be filled with value}
        CurSource.GetOneValue(Capability.ID, ItemType, Value, CurrentMode,
          Handle);
        {Filter value to replace numbers for constants strings}
        Value := FilterValue(Capability, ItemType, Value);
        {Show result}
        Results.Items.Add('Value = ' + Value);
      end;

      {Range container}
      TWON_RANGE:
      begin
        {Call method to get the values}
        CurSource.GetRangeValue(Capability.ID, ItemType, Min, Max, Step,
          Default, Current, Handle);
        {Replace values with constants}
        Min := FilterValue(Capability, ItemType, Min);
        Max := FilterValue(Capability, ItemType, Max);
        Step := FilterValue(Capability, ItemType, Step);
        Default := FilterValue(Capability, ItemType, Default);
        Current := FilterValue(Capability, ItemType, Current);
        {Show results}
        Results.Items.Add('Min = ' + Min);
        Results.Items.Add('Step = ' + Step);
        Results.Items.Add('Max = ' + Max);
        Results.Items.Add('Current = ' + Current);
        Results.Items.Add('Defaukt = ' + Default);
      end;

      {Enumeration container}
      TWON_ENUMERATION:
      begin
        {Call method to get the enumeration values}
        CurSource.GetEnumerationValue(Capability.ID, ItemType, List,
          CurrentIndex, DefaultIndex, CurrentMode, Handle);
        {Replace each value with constants}
        FOR i := 0 TO Length(List) - 1 DO
          List[i] := FilterValue(Capability, ItemType, List[i]);
        {Show enumeration results}
        Results.Items.Add('Current = ' + List[CurrentIndex]);
        Results.Items.Add('Default = ' + List[DefaultIndex]);
        FOR i := 0 TO Length(List) - 1 DO
          Results.Items.Add(inttostr(i + 1) + ' = ' + List[i]);
      end;

      {Array container}
      TWON_ARRAY:
      begin
        {Call method to get the array values}
        CurSource.GetArrayValue(Capability.ID, ItemType, List, Handle);
        {Replace each value with constants}
        FOR i := 0 TO Length(List) - 1 DO
          List[i] := FilterValue(Capability, ItemType, List[i]);
        {Show array results}
        FOR i := 0 TO Length(List) - 1 DO
          Results.Items.Add(inttostr(i + 1) + ' = ' + List[i]);
      end;

      {case} else GlobalFree(Handle)
    end {case};

    {Update combo box for item type}
    Combo4.ItemIndex := ItemType;
    UpdateSetPages;

  end {if TestResult(CurSource.GetCapabilityRec...}

end;

{User clicked the button to send the command}
procedure TSendCommandForm.GoSendClick(Sender: TObject);
begin
  {Test if user is setting or retrieving values}
  if Combo3.ItemIndex = Combo3.Items.Count - 1 then
    SetValue
  else
    GetValue;
end;

{Converts string constant to capability item ID}

⌨️ 快捷键说明

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