delphi VCL 触摸屏接受touch事件

 

Note RegisterTouchWindow must be called on every window that will be used for touch input. This means that if you have an application that has multiple windows within it, RegisterTouchWindow must be called on every window in that application that uses touch features. Also, an application can call RegisterTouchWindow any number of times for the same window if it desires to change the modifier flags. A window can be marked as no longer requiring touch input using the UnregisterTouchWindow function.

You would also have to subclass each Panel to handle the WM_TOUCH message for each panel, since it will be sent directly to each registered Panel and not to the Form.

If you want every Panel on the Form to be touch-capable, a simple interposer class will suffice:

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type

// 继承于panel 当然也可以是 form
  TPanel = class(Vcl.ExtCtrls.TPanel)
  protected
    procedure CreateWnd; override;    // 关键覆盖这两个方法
    procedure DestroyWnd; override;   //
    procedure WMTouch(var Message: TMessage); message WM_TOUCH;  // 接收touch事件
  end;

  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
  end;

var
  Form1: TForm1;

implementation

type
  TOUCHINPUT = record
    x: Integer;
    y: Integer;
    hSource: THandle;
    dwID: DWORD;
    dwFlags: DWORD;
    dwMask: DWORD;
    dwTime: DWORD;
    dwExtraInfo: ULONG_PTR;
    cxContact: DWORD;
    cyContact: DWORD;
  end;

procedure TPanel.CreateWnd;
begin
  inherited;
  RegisterTouchWindow(Handle, 0);
end;

procedure TPanel.DestroyWnd;
begin
  UnregisterTouchWindow(Handle);
  inherited;
end;

procedure TPanel.WMTouch(var Message: TMessage);
  function TouchPointToPoint(const TouchPoint: TTouchInput): TPoint;
  begin
    Result := Point(TouchPoint.X div 100, TouchPoint.Y div 100);
    PhysicalToLogicalPoint(Handle, Result);
  end;
var
  TouchInputs: array of TTouchInput;
  TouchInput: TTouchInput;
  Handled: Boolean;
  Point: TPoint;
begin
  Handled := False;
  SetLength(TouchInputs, Message.WParam);
  GetTouchInputInfo(Message.LParam, Message.WParam,
  @TouchInputs[0], SizeOf(TTouchInput));
  try
    for TouchInput in TouchInputs do
    begin
      Point := TouchPointToPoint(TouchInput);
      if PtInRect(BoundsRect, Point) then
      begin
        //labelX.Caption := 'Touch ID: ' + IntToStr(TouchInput.dwID);
        Top := Point.Y - 100;
      end;
      Handled := True;
    end;
  finally
    if Handled then
      CloseTouchInputHandle(Message.LParam)
    else
      inherited;
  end;
end;

end.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值