1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Windows.MouseManager;
interface
uses
Boolean,
Event,
Geometry._2d.Integer32.Point,
UserInterface.Control,
Windows.Control,
Windows.Gdi.Cursor;
type
TWindowsMouseTracker = class(TObject)
public
procedure Start(WindowsControl: TWindowsControl; Hover: TBoolean; Leave: TBoolean);
procedure Cancel(WindowsControl: TWindowsControl);
end;
TWindowsMouseManager = class(TMouseManager)
private
MouseTracker: TWindowsMouseTracker;
protected
procedure CaptureChanged(); override;
function LastChanceHandleEvent(RootControl: TControl; Event: TEvent): TBoolean; override;
public
constructor Create(); override;
destructor Destroy(); override;
function GetCursorPosition(): TInteger32Point; override;
procedure SetCursor(Cursor: TCursor); override;
end;
implementation
uses
Integer._16,
Integer._32.Positive,
UserInterface.Mouse.Event,
Windows.Api.UserInterface,
Windows.Api.UserInterface.Mouse;
{ TWindowsMouseManager }
constructor TWindowsMouseManager.Create();
begin
inherited Create();
MouseTracker := TWindowsMouseTracker.Create();
end;
destructor TWindowsMouseManager.Destroy();
begin
MouseTracker.Free();
inherited Destroy();
end;
function TWindowsMouseManager.GetCursorPosition(): TInteger32Point;
var
Value: TPositiveInteger32;
Top: TInteger16;
Left: TInteger16;
begin
Value := GetMessagePos();
Left := Value and $FFFF;
Top := (Value and $FFFF0000) shr 16;
Result := TInteger32Point.New(Left, Top);
end;
function TWindowsMouseManager.LastChanceHandleEvent(RootControl: TControl; Event: TEvent): TBoolean;
begin
Result := inherited LastChanceHandleEvent(RootControl, Event);
if (Event is TMouseMoveEvent) and (ControlWithCapture = nil) then
MouseTracker.Start(RootControl.RootOverlappedControl as TWindowsControl, False, True);
end;
procedure TWindowsMouseManager.SetCursor(Cursor: TCursor);
begin
Windows.Api.UserInterface.SetCursor(Cursor.Handle);
end;
procedure TWindowsMouseManager.CaptureChanged();
begin
if (ControlWithCapture <> nil) and (ControlWithCapture.RootOverlappedControl is TWindowsControl) then
Windows.Api.UserInterface.Mouse.SetCapture((ControlWithCapture.RootOverlappedControl as TWindowsControl).Handle)
else
Windows.Api.UserInterface.Mouse.ReleaseCapture();
end;
{ TWindowsMouseTracker }
procedure TWindowsMouseTracker.Cancel(WindowsControl: TWindowsControl);
var
Event: TTrackMouseEvent;
begin
Event.cbSize := SizeOf(Event);
Event.hwndTrack := WindowsControl.Handle;
Event.dwFlags := TME_CANCEL;
TrackMouseEvent(Event);
end;
procedure TWindowsMouseTracker.Start(WindowsControl: TWindowsControl; Hover, Leave: TBoolean);
var
Event: TTrackMouseEvent;
begin
Event.cbSize := SizeOf(Event);
Event.hwndTrack := WindowsControl.Handle;
Event.dwFlags := 0;
if Hover then
begin
Event.dwFlags := Event.dwFlags or TME_HOVER;
Event.dwHoverTime := HOVER_DEFAULT;
end;
if Leave then
Event.dwFlags := Event.dwFlags or TME_LEAVE;
TrackMouseEvent(Event);
end;
end.
|