unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Spin, ComCtrls;

type
  TTvCardForm = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    PanelVGA: TPanel;
    Button2: TButton;
    Button3: TButton;
    CB_OutMode: TComboBox;
    SpinEditChannel: TSpinEdit;
    CB_InMode: TComboBox;
    LabelChannel: TLabel;
    SearchTV: TButton;
    LabelID: TLabel;
    PanelVid: TPanel;
    TrackBarVol: TTrackBar;
    Label4: TLabel;
    Label5: TLabel;
    CB_TVcard: TComboBox;
    Label7: TLabel;
    CB_AudioList: TComboBox;
    Label6: TLabel;
    CB_OutAudioCar: TComboBox;
    Label8: TLabel;
    EditIP: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure CB_InModeChange(Sender: TObject);
    procedure SearchTVClick(Sender: TObject);
    procedure TrackBarVolChange(Sender: TObject);
  private
    { Private declarations }
    WM_TV_CHANNEL_INT : Dword;
  public
    { Public declarations }
  protected
      procedure WndProc(var Message: TMessage); override;    
  end;

var
  TvCardForm: TTvCardForm;

implementation

{$R *.dfm}
const
  DLL_NAME ='TVCard.dll';
 
  PAL_D       = $20;
  //打开设备
  function OpenTVCardCsdelphi(CmdID:integer;VideoHwn:THandle):integer;stdcall; external DLL_NAME;

  function CloseTVCardCsdelphi():integer;stdcall;external DLL_NAME;
  //得到输入设备列表，ID=0 得到采集设备， ID=1 得到声卡设备
  function GetCaptureDeviceListCsdelphi(ID:integer;pList:Pchar):integer;stdcall;external DLL_NAME;
  //搜索台 
  function SearchTVChannelCsdelphi(Hwn:THandle;VideoHwn:THandle):integer;stdcall;external DLL_NAME;
  //设置各种参数
  function SetParamCsdelphi(TunerMode,TvCardId,sndCardId,CountryCode,ConnectType,ResolutionID:integer):integer;stdcall;external DLL_NAME;
  //选择频道
  function SetTVChannelCsdelphi(ChannelIndex:integer):integer;stdcall;external DLL_NAME;

  //设置电视卡输入设备端口
  function SelectInputLineCsdelphi(InID:integer):integer;stdcall;external DLL_NAME;

   //得到电视卡输入设备端口
  function GetCardInputDeviceCsdelphi(pList: Pchar):integer;stdcall;external DLL_NAME;

 //  输入注册码
  function SetSNCsdelphi(UserName: Pchar;ID: Pchar; pSn: Pchar):integer;stdcall;external DLL_NAME;

  //设置音量
  function SetVolumeCsdelphi(ID,Value: integer):integer;stdcall;external DLL_NAME;

  function SetBroadcastIP(Value: Pchar):integer;stdcall;external DLL_NAME;


procedure TTvCardForm.Button1Click(Sender: TObject);
begin

 SetParamCsdelphi(1,CB_TVcard.ItemIndex,CB_AudioList.ItemIndex,86, 0,PAL_D);

 SetBroadcastIP(Pchar(EditIp.Text));

 Caption:=inttostr(OpenTVCardCsdelphi(CB_OutMode.ItemIndex,PanelVGA.Handle));
 
end;

procedure TTvCardForm.Button2Click(Sender: TObject);
begin
 CloseTVCardCsdelphi();
end;

procedure TTvCardForm.Button3Click(Sender: TObject);
begin
  SetTVChannelCsdelphi(SpinEditChannel.Value);
end;

procedure TTvCardForm.FormCreate(Sender: TObject);
var
 pList : array[0..1024] of char;
 i ,ID: integer;
begin
  SetSNCsdelphi('Demo','Demo','注册码');

  WM_TV_CHANNEL_INT:=RegisterWindowMessage('WM_TV_CHANNEL_INT');

  if GetCaptureDeviceListCsdelphi(0,pList)<=0 then
   begin
      MessageBox(Handle,'没有找到电视卡，请安装！',nil,MB_ICONWARNING);
   end
  else
   begin
     CB_TVcard.Items.Text:=strpas(pList);
     CB_TVcard.ItemIndex:=0;
   end;

  if GetCaptureDeviceListCsdelphi(2,pList)<=0 then
   begin
      MessageBox(Handle,'没有找到声卡设备，请安装！',nil,MB_ICONWARNING);
   end
  else
   begin
     CB_OutAudioCar.Items.Text:=strpas(pList);
     CB_OutAudioCar.ItemIndex:=0;
   end;

   
  if GetCaptureDeviceListCsdelphi(1,pList)<=0 then
   begin
      MessageBox(Handle,'没有找到声卡设备，请安装！',nil,MB_ICONWARNING);
   end
  else
   begin
     CB_AudioList.Items.Text:=strpas(pList);
     CB_AudioList.ItemIndex:=0;
   end;

  if GetCardInputDeviceCsdelphi(pList)>0 then
   begin
     CB_InMode.Items.Text:=strpas(pList);
   end;

 for i:=0 to CB_InMode.Items.Count -1 do
  begin
    if Pos('TUNER IN',Uppercase(CB_InMode.Items[i]))>0 then
     begin
      CB_InMode.ItemIndex:=i;
      try
        ID:=StrToInt(CB_InMode.Items[CB_InMode.ItemIndex][1]);
      except
        ID:=CB_InMode.ItemIndex;
      end;  
      SelectInputLineCsdelphi(ID);
     end;
  end;  

 SetVolumeCsdelphi(CB_OutAudioCar.ItemIndex,TrackBarVol.Position);
  
end;

procedure TTvCardForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CloseTVCardCsdelphi();
end;

procedure TTvCardForm.CB_InModeChange(Sender: TObject);
var
 ID : integer;
begin

  IF CB_InMode.ItemIndex < 0 then Exit;
  
  try
    ID:=StrToInt(CB_InMode.Items[CB_InMode.ItemIndex][1]);
  except
    ID:=CB_InMode.ItemIndex;
  end;

  SelectInputLineCsdelphi(ID);
  
end;

procedure TTvCardForm.WndProc(var Message: TMessage);
begin
  inherited;

  if Message.Msg =WM_TV_CHANNEL_INT then
    begin
      if Message.WParamHi=1 then
       LabelID.Caption:=Format('信号强'+#13#10+'频道:%d  频率:%f',[Message.WParamLo,Message.LParam / 1000000])
      else
       LabelID.Caption:=Format('频道:%d 频率:%f',[Message.WParamLo,Message.LParam / 1000000]);
    end;
end;

procedure TTvCardForm.SearchTVClick(Sender: TObject);
begin
  SetParamCsdelphi(1,CB_TVcard.ItemIndex,0,86, 0,PAL_D);
  SearchTVChannelCsdelphi(Handle,PanelVid.Handle);
end;

procedure TTvCardForm.TrackBarVolChange(Sender: TObject);
begin
  SetVolumeCsdelphi(CB_OutAudioCar.ItemIndex,TrackBarVol.Position);
end;

end.
