-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGS.Pixel32.Win.pas
More file actions
192 lines (168 loc) · 4.9 KB
/
Copy pathGS.Pixel32.Win.pas
File metadata and controls
192 lines (168 loc) · 4.9 KB
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
{ -----------------------------------------------------------------------------
This program is free software: Under statement of join file README - LGPL.txt
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
Unit Name : GS.Pixe32.Win
Author : Vincent Gsell (vincent dot gsell at gmail dot com)
Purpose : Pixel32's Windows DC Bridge.
Date: : 2020031
History :
20200301 - Creating unit.
Description :
Friend class helper to handle Windows's DC access.
Use it under Delphi VCL (FormPain event) or Lazarus LCL (WMBackground's Message.DC - See demo)
-----------------------------------------------------------------------------}
{$I GSCore.Inc}
unit GS.Pixel32.Win;
interface
Uses SysUtils, Classes, Types, Windows, Graphics, GS.Pixel32.Types, GS.Pixel32;
Type
TPixel32WinHelper = class helper for TPixel32
public
procedure CopyToDc(dstDc: HDC; x: Integer = 0; y: Integer = 0;
transparent: Boolean = true; bkColor: TP32 = 0);
procedure CopyFromDC(srcDc: HDC; const srcRect: Types.TRect);
procedure loadFromFile(filename : String);
procedure SaveToFile(filename : String);
end;
var
ScreenPixelsY : Integer;
DPIScale : Double;
Implementation
procedure GetDPI;
var
dc: HDC;
begin
dc := GetDC(0);
ScreenPixelsY := GetDeviceCaps(dc, LOGPIXELSY);
DpiScale := ScreenPixelsY / 96;
ReleaseDC(0, dc);
end;
function GetCompatibleMemDc(wnd: HWnd = 0): HDC;
var
dc: HDC;
begin
dc := Windows.GetDC(wnd);
try
Result := CreateCompatibleDC(dc);
finally
Windows.ReleaseDC(wnd, dc);
end;
end;
//------------------------------------------------------------------------------
function Get32bitBitmapInfoHeader(width, height: Integer): TBitmapInfoHeader;
begin
FillChar(Result, sizeof(Result), #0);
Result.biSize := sizeof(TBitmapInfoHeader);
Result.biWidth := width;
Result.biHeight := height;
Result.biPlanes := 1;
Result.biBitCount := 32;
Result.biSizeImage := width * height * SizeOf(TP32);
Result.biCompression := BI_RGB;
end;
procedure TPixel32WinHelper.CopyToDc(dstDc: HDC;
x,y: Integer; transparent: Boolean; bkColor: TP32);
var
tmp: TPixel32;
bi: TBitmapInfoHeader;
bm, oldBm: HBitmap;
dibBits: Pointer;
memDc: HDC;
begin
if (width=0) or (height=0) then
Exit;
bi := Get32bitBitmapInfoHeader(Width, Height);
tmp := TPixel32.create(fwidth,fheight);
try
copyTo(TPixel32(tmp));
TPixel32(tmp).flipVertical;
memDc := GetCompatibleMemDc;
try
bm := CreateDIBSection(memDc, PBITMAPINFO(@bi)^,
DIB_RGB_COLORS, dibBits, 0, 0);
if bm = 0 then Exit;
try
Move(tmp.getSurfacePtr^, dibBits^, Width * Height * SizeOf(TP32));
oldBm := SelectObject(memDC, bm);
BitBlt(dstDc, x,y, Width, Height, memDc, 0,0, SRCCOPY);
SelectObject(memDC, oldBm);
finally
DeleteObject(bm);
end;
finally
DeleteDc(memDc);
end;
finally
FreeAndNil(tmp);
end;
end;
procedure TPixel32WinHelper.loadFromFile(filename: String);
var b : TBitmap;
begin
b := TBitmap.Create;
try
b.LoadFromFile(filename);
CopyFromDC(b.Canvas.Handle,Types.rect(0,0,b.Width,b.Height));
finally
freeandNil(b); //Free vcl ressources.
end;
end;
procedure TPixel32WinHelper.SaveToFile(filename: String);
var b : TBitmap;
begin
b := TBitmap.Create;
try
b.PixelFormat := pf32bit;
b.SetSize(width,height);
CopyToDc(b.Canvas.Handle);
b.SaveToFile(filename);
finally
freeandNil(b); //Free vcl ressources.
end;
end;
procedure TPixel32WinHelper.CopyFromDC(srcDc: HDC; const srcRect: TRect);
var
bi: TBitmapInfoHeader;
bm, oldBm: HBitmap;
memDc: HDC;
pixels: Pointer;
w,h: integer;
begin
{$IFDEF FPC}
w := srcRect.Right - srcRect.Left;
h := srcRect.Bottom - srcRect.Top;
{$ELSE}
w := RectWidth(srcRect);
h := RectHeight(srcRect);
{$ENDIF}
resize(w, h);
bi := Get32bitBitmapInfoHeader(w, h);
memDc := GetCompatibleMemDc;
try
bm := CreateDIBSection(memDc,
PBITMAPINFO(@bi)^, DIB_RGB_COLORS, pixels, 0, 0);
if bm = 0 then Exit;
try
oldBm := SelectObject(memDc, bm);
BitBlt(memDc, 0, 0, w, h, srcDc, srcRect.Left,srcRect.Top, SRCCOPY);
Move(pixels^, getSurfacePtr^, w * h * sizeOf(TP32));
SelectObject(memDc, oldBm);
finally
DeleteObject(bm);
end;
finally
DeleteDc(memDc);
end;
AlphaLayerReset;
FlipVertical;
end;
end.