Debian Codegraph @

tools/castle-editor/components/mbColorLib/ScreenWin.pas

raw
1 unit ScreenWin;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls, Forms,
11 ExtCtrls, StdCtrls,
12 PalUtils;
13
14 const
15 crPickerCursor = 13;
16
17 type
18 TScreenForm = class(TForm)
19 procedure EndSelection(x, y: integer; ok: boolean);
20 procedure FormCreate(Sender: TObject);
21 procedure FormKeyDown(Sender: TObject; var Key: Word;
22 Shift: TShiftState);
23 procedure FormMouseMove(Sender: TObject;
24 {%H-}Shift: TShiftState; X, Y: Integer);
25 procedure FormMouseUp(Sender: TObject; {%H-}Button: TMouseButton;
26 {%H-}Shift: TShiftState; X, Y: Integer);
27 procedure FormShow(Sender: TObject);
28
29 private
30 FOnSelColorChange: TNotifyEvent;
31 FOnKeyDown: TKeyEvent;
32
33 protected
34 procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
35
36 public
37 FHintFormat: string;
38 SelectedColor: TColor;
39 property OnSelColorChange: TNotifyEvent read FOnSelColorChange write FOnSelColorChange;
40 property OnScreenKeyDown: TKeyEvent read FOnKeyDown write FOnKeyDown;
41 end;
42
43 var
44 ScreenForm: TScreenForm;
45
46 implementation
47
48 {$R *.lfm}
49 {$R PickCursor.res}
50
51 uses
52 HTMLColors;
53
54 function GetDesktopColor(const X, Y: Integer): TColor;
55 var
56 c: TCanvas;
57 screenDC: HDC;
58 begin
59 c := TCanvas.Create;
60 try
61 screenDC := GetDC(0);
62 c.Handle := screenDC;
63 Result := c.Pixels[X, Y];
64 finally
65 c.Free;
66 end;
67 end;
68
69
70 { TScreenForm }
71
72 procedure TScreenForm.CMHintShow(var Message: TCMHintShow);
73 begin
74 with TCMHintShow(Message) do
75 if not ShowHint then
76 Message.Result := 1
77 else
78 with HintInfo^ do
79 begin
80 Result := 0;
81 ReshowTimeout := 1;
82 HideTimeout := 5000;
83 HintPos := Point(HintPos.X + 16, HintPos.y - 16);
84 HintStr := FormatHint(FHintFormat, SelectedColor);
85 end;
86 inherited;
87 end;
88
89 procedure TScreenForm.EndSelection(x, y: integer; ok: boolean);
90 begin
91 if ok then
92 SelectedColor := GetDesktopColor(x, y)
93 else
94 SelectedColor := clNone;
95 Close;
96 if Assigned(FOnSelColorChange) then
97 FOnSelColorChange(Self);
98 end;
99
100 procedure TScreenForm.FormCreate(Sender: TObject);
101 begin
102 Brush.Style := bsClear;
103 Screen.Cursors[crPickerCursor] := LoadCursor(HInstance, 'PickerCursor');
104 Cursor := crPickerCursor;
105 SelectedColor := clNone;
106 FHintFormat := 'RGB(%r, %g, %b)'#13'Hex: %h';
107 end;
108
109 procedure TScreenForm.FormKeyDown(Sender: TObject; var Key: Word;
110 Shift: TShiftState);
111 begin
112 if (key = VK_ESCAPE) or (ssAlt in Shift) or (ssCtrl in Shift) then
113 EndSelection(0, 0, false);
114 if (key = VK_RETURN) then
115 EndSelection(Mouse.CursorPos.X, Mouse.CursorPos.Y, true);
116 if Assigned(FOnKeyDown) then
117 FOnKeyDown(Self, Key, Shift);
118 end;
119
120 procedure TScreenForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
121 Y: Integer);
122 begin
123 SelectedColor := GetDesktopColor(x, y);
124 if Assigned(FOnSelColorChange) then FOnSelColorChange(Self);
125 Application.ProcessMessages;
126 end;
127
128 procedure TScreenForm.FormMouseUp(Sender: TObject; Button: TMouseButton;
129 Shift: TShiftState; X, Y: Integer);
130 begin
131 EndSelection(x, y, true);
132 end;
133
134 procedure TScreenForm.FormShow(Sender: TObject);
135 begin
136 Width := Screen.Width;
137 Height := Screen.Height;
138 Left := 0;
139 Top := 0;
140 end;
141
142 end.