Question

One of the big selling points I saw when I had to buy delphi for my job was the ability to support tablet pc's. Now the client of the company where I work want to use a tablet pc. I've been trying hard to find examples of delphi with tablet pc but I don't find any. Does anybody has experience with it? Any kind of tutorials or examples?

I don't seem to be able even to bring a virtual keyboard when a component gain focus and hide it when it loses it.

Was it helpful?

Solution

Delphi 2010 introduced some nice touch and gesture support to Delphi.

To get more info about it, go to EDN website and look for CodeRage 4 replays. There is a session titled "Hands on gestures in VCL" by Seppy Bloom. Also in CodeRage 5 there is a session titled "Gesturing Capabilities for New Application and Current Projects" by Vesvolod Leonov.

Chapter 6 of Marco Cantu's "Delphi 2010 Handbook" also covers touch and gesture in Delphi.

Eventually, you can check Chris Bensen's weblog for some introductory posts and demo source code about touch and gesture support in Delphi.

I don't seem to be able even to bring a virtual keyboard when a component gain focus and hide it when it loses it.

In Delphi 2010 and newer versions a touch-enabled keyboard component is already available. To make it is visible or hide it when focus is changed, you can handle CM_FOCUSCHANGED VCL message, and make the keyboard visible when the control gaining focus is derived from a certain class or meets some special conditions. Here is a sample code:

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    TouchKeyboard1: TTouchKeyboard;
  private
    procedure ActivateVirtualKeyboard(Control: TWinControl; Keyboard: TTouchKeyboard);
    procedure CmFocusChanged(var Msg: TCMFocusChanged); message CM_FOCUSCHANGED;
  public
    { Public declarations }
  end;

/// Implementation

procedure TForm1.ActivateVirtualKeyboard(Control: TWinControl; Keyboard: TTouchKeyboard);
var
  APoint : TPoint;
begin
  if Control is TCustomEdit then
  begin
    APoint := Control.ClientToScreen(Point(0,0));
    APoint := Keyboard.Parent.ScreenToClient(APoint);
    Keyboard.Left := APoint.X;
    Keyboard.Top := APoint.Y + (Control.Height);
    Keyboard.Visible := True;
  end
  else
    Keyboard.Visible := False;
end;

procedure TForm1.CmFocusChanged(var Msg: TCMFocusChanged);
begin
  ActivateVirtualKeyboard(Msg.Sender, TouchKeyboard1);
end;

The code above calls ActivateVirtualKeyboard each time focus is changed. Msg.Sender is the control which gained focus. ActivateVirtualKeyboard checks if the control is a TCustomEdit descendant (components like TEdit or TMemo descend from this class). If the control is derived from TCustomEdit, then it places virtual keyboard right beneath the control, and makes the keyboard visible; otherwise, it hides the keyboard.

In the sample code we have an edit, a memo, and a button on Form1. The keyboard should be visible for Edit1 and Memo1, and hid when Button1 has focus.

The calculation for keyboard position on the screen isn't that clever, and keyboard might go too down if the control having focus is very close to the bottom edge of the form. Anyway, positioning a control on the screen is out of the scope of your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top