-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
osdgui.pas
214 lines (182 loc) · 5.87 KB
/
osdgui.pas
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
unit osdgui;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, DWMApi, ActiveX, GDIPapi, GDIPObj;
type
TfrmOSD = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure DrawBubble(const text: String = '');
end;
var
frmOSD: TfrmOSD;
implementation
{$R *.dfm}
uses main;
type
TFixedStreamAdapter = class(TStreamAdapter)
public
// function Stat(out statstg: TStatStg; grfStatFlag: DWORD): HResult; override; stdcall;
function Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult; virtual; stdcall;
end;
function TFixedStreamAdapter.Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult;
begin
Result := inherited Stat(statstg, grfStatFlag);
statstg.pwcsName := nil;
end;
procedure TfrmOSD.DrawBubble(const text: String = '');
var
bmp: TBitmap;
Stream: TStream;
StreamAdapter: IStream;
PNGBitmap: TGPBitmap;
BitmapHandle: HBITMAP;
BlendFunction: TBlendFunction;
BitmapPos: TPoint;
BitmapSize: TSize;
xfont: TGPFont;
//style: Integer;
graph: TGPGraphics;
brush: TGPSolidBrush;
stringFormat: TGPStringFormat;
textval : Integer;
begin
bmp := TBitmap.Create;
try
bmp.PixelFormat := pf32bit;
textval := 1;
if Trim(text) <> '' then
textval := StrToInt(text);
if textval and 1 = 1then
Stream := TResourceStream.Create(HInstance, 'PngImage_1', RT_RCDATA)
else
Stream := TResourceStream.Create(HInstance, 'PngImage_2', RT_RCDATA);
try
StreamAdapter := TFixedStreamAdapter.Create(Stream);
try
PNGBitmap := TGPBitmap.Create(StreamAdapter);
try
PNGBitmap.GetHBITMAP(MakeColor(0, 0, 0, 0), BitmapHandle);
bmp.Handle := BitmapHandle;
finally
PNGBitmap.Free;
end;
finally
StreamAdapter := nil;
end;
finally
Stream.Free;
end;
Assert(bmp.PixelFormat = pf32bit, 'Wrong bitmap format, must be 32 bits/pixel');
//PremultiplyBitmap(bmp);
//bmp.Canvas.FillRect(Rect(0,0,Width,Height));
graph := TGPGraphics.Create(bmp.Canvas.Handle);
try
graph.SetSmoothingMode(SmoothingModeAntiAlias);
//style := FontStyleRegular;
//xfont := TGPFont.Create(font.Name, font.Size, style, UnitPoint);
xfont := TGPFont.Create(Canvas.Handle, Font.Handle);
try
brush := TGPSolidBrush.Create(MakeColor(GetRValue(Font.Color), GetGValue(Font.Color), GetBValue(Font.Color)));
try
stringFormat := TGPStringFormat.Create;
try
stringFormat.SetAlignment(StringAlignmentCenter);
stringFormat.SetLineAlignment(StringAlignmentCenter);
stringFormat.SetTrimming(StringTrimmingEllipsisCharacter);
stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);
graph.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
graph.DrawString(text,Length(text),xfont,MakePoint((bmp.Width/2 ),(bmp.Height/2 )),stringFormat,brush);
//graph.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit);
//graph.DrawString(text,Length(text),xfont,MakePoint(12.0,12.0),nil,brush);
finally
stringFormat.Free;
end;
finally
brush.Free;
end;
finally
xfont.Free;
end;
finally
graph.Free;
end;
ClientWidth := bmp.Width;
ClientHeight := bmp.Height;
// Position bitmap on form
BitmapPos := Point(0, 0);
BitmapSize.cx := bmp.Width;
BitmapSize.cy := bmp.Height;
// Setup alpha blending parameters
BlendFunction.BlendOp := AC_SRC_OVER;
BlendFunction.BlendFlags := 0;
BlendFunction.SourceConstantAlpha := 255; // Start completely transparent
BlendFunction.AlphaFormat := AC_SRC_ALPHA;
UpdateLayeredWindow(Handle, 0, nil, @BitmapSize, bmp.Canvas.Handle,
@BitmapPos, 0, @BlendFunction, ULW_ALPHA);
finally
bmp.Free;
end;
end;
procedure TfrmOSD.FormActivate(Sender: TObject);
begin
// SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
end;
{procedure PremultiplyBitmap(Bitmap: TBitmap);
var
Row, Col: integer;
p: PRGBQuad;
PreMult: array[byte, byte] of byte;
begin
// precalculate all possible values of a*b
for Row := 0 to 255 do
for Col := Row to 255 do
begin
PreMult[Row, Col] := Row*Col div 255;
if (Row <> Col) then
PreMult[Col, Row] := PreMult[Row, Col]; // a*b = b*a
end;
for Row := 0 to Bitmap.Height-1 do
begin
Col := Bitmap.Width;
p := Bitmap.ScanLine[Row];
while (Col > 0) do
begin
p.rgbBlue := PreMult[p.rgbReserved, p.rgbBlue];
p.rgbGreen := PreMult[p.rgbReserved, p.rgbGreen];
p.rgbRed := PreMult[p.rgbReserved, p.rgbRed];
inc(p);
dec(Col);
end;
end;
end;
}
procedure TfrmOSD.FormCreate(Sender: TObject);
const
DWMWA_EXCLUDED_FROM_PEEK = 12; // [set] LivePreview exclusion information
var
renderPolicy: DWMWINDOWATTRIBUTE; //integer;//DWMWINDOWATTRIBUTE;
begin
frmMain.tmrHotSpot.Enabled := True;
BorderStyle := bsNone;
FormStyle := fsStayOnTop;
Font.Size := 18;
//Font.Color := $00DBBEA6;
Font.Color := $00333333;
DwmSetWindowAttribute(Handle, DWMWA_EXCLUDED_FROM_PEEK or DWMWA_FLIP3D_POLICY, @renderpolicy, SizeOf(Integer));
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) Or WS_EX_LAYERED or WS_EX_TRANSPARENT or WS_EX_TOOLWINDOW{ and not WS_EX_APPWINDOW});
//SetLayeredWindowAttributes(Handle,0,255, LWA_ALPHA);
//SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width, Height,SWP_NOMOVE or SWP_NOACTIVATE or SWP_NOSIZE);
DrawBubble();
end;
procedure TfrmOSD.FormShow(Sender: TObject);
begin
ShowWindow(Application.Handle, SW_HIDE);
end;
end.