Tuesday, December 19, 2017

DELPHI - How use TIdHTTP, TTrayIcon and hotkeys for TTrayIcon

This is code for camera which can be controlled by HTTP commands.
For HTTP commands are used object TIdHTTP. Application is minimized and has own tray icon.
Hotkeys are served by WM_HOTKEY message.
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdContext, IdHTTP, IdTCPConnection,
  IdTCPClient, IdCmdTCPClient, IdBaseComponent, IdComponent, IdUDPBase,
  IdUDPClient, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    IdHTTP1: TIdHTTP;
    Button2: TButton;
    TrayIcon1: TTrayIcon;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TrayIcon1DblClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure WMHotkey( Var msg: TWMHotkey ) ; message WM_HOTKEY;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  { ..here you can see example of testing connection to idHTTP }
  
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.BasicAuthentication:= true;
  IdHTTP1.Request.Authentication.Username := 'admin';
  IdHTTP1.Request.Authentication.Password := 'xxx'; }

  idHTTP1.Connect;
  idHTTP1.Get( 'http://10.36.1.29/command/presetposition.cgi?PresetCall=1,24' );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  { -- hotkeys registration }

  RegisterHotkey( handle, 1, MOD_CONTROL, ord( '1' ) );
  RegisterHotkey( handle, 2, MOD_CONTROL, ord( '2' ) );
  RegisterHotkey( handle, 3, MOD_CONTROL, ord( '3' ) );
  RegisterHotkey( handle, 4, MOD_CONTROL, ord( '4' ) );
  RegisterHotkey( handle, 5, MOD_CONTROL, ord( '5' ) );
  RegisterHotkey( handle, 6, MOD_CONTROL, ord( '6' ) );
  RegisterHotkey( handle, 7, MOD_CONTROL, ord( '7' ) );
  RegisterHotkey( handle, 8, MOD_CONTROL, ord( '8' ) );
  RegisterHotkey( handle, 9, MOD_CONTROL, ord( '9' ) );

  { -- tray settings }

  TrayIcon1.Hint := 'Version 1.0 - click for close.';
  TrayIcon1.AnimateInterval := 200;

  { set up a hint balloon }
  TrayIcon1.BalloonTitle := 'CTRL + [1..9].';
  TrayIcon1.BalloonHint := 'Press for camera change';
  TrayIcon1.BalloonFlags := bfInfo;

  { initial calling for connection }
  idHTTP1.Get( 'http://10.36.1.29/command/presetposition.cgi?PresetCall=3,24' );

  { hide the window and set its state to wsMinimized. }
  Hide();
  WindowState := wsMinimized;

  { show animated tray icon and also a hint balloon. }
  TrayIcon1.Visible := True;
  TrayIcon1.Animate := True;
  TrayIcon1.ShowBalloonHint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotkey( Handle, 1 );
  UnRegisterHotkey( Handle, 2 );
  UnRegisterHotkey( Handle, 3 );
  UnRegisterHotkey( Handle, 4 );
  UnRegisterHotkey( Handle, 5 );
  UnRegisterHotkey( Handle, 6 );
  UnRegisterHotkey( Handle, 7 );
  UnRegisterHotkey( Handle, 8 );
  UnRegisterHotkey( Handle, 9 );
end;

procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.WMHotkey( Var msg: TWMHotkey );
begin
  { reaction for hotkey }

  idHTTP1.Get( 'http://10.36.1.29/command/presetposition.cgi?PresetCall=' + 
               IntToStr( msg.HotKey ) + ',24' );
end;

end.

No comments:

Post a Comment