Author: William Egge
Bored? Like playing tricks on your coworkers? I tested it out on my bosses
secretary and it was fun, so I'll share it with you. BUT its just not fun, it also
contains usefull classes.
Answer:
Fun program to trick your friends, secretary or anyone with a computer :-). The
program flips your desktop upside down until you click on it.
BUT, this does have some interesting code.
It contains TDesktopCanvas where you can access your desktop through a TCanvas
object.
It contains TQuickPixel which gives you high speed pixel access, btw - it caches
the scan lines for even faster performance.
Download the source, it is fairly easy to follow. Compile it and stick it in your
friends startup folder :-) or just run it and walk away.
To end the program just click the inverted screen.
Now for the usefull part as far as coding:
A class I made so I could have fast pixel access without fumbling with scan lines.
This class caches the scan lines for faster perfomance. One drawback of this class
is that it sets your Bitmap to 24bit. If you want me to build a class that
supports all bit formats then please make a comment to do so and I can build one
without causing a performance hit (use method pointers so there is no testing of
bit format). I will also speed up the pixel setting to work without the shifts if
anyone asks for the multiple format thing. As a side note I think it would be
possible to include Line, arc and circle methods... but only if there is enough
interest. Windows is really slow about drawing.
Here is the code for TQuickPixel. You can also go to my website for working EXE and
download full source.
1 unit QuickPixel;
2
3 interface
4 uses
5 Windows, Graphics;
6
7 type
8 TQuickPixel = class
9 private
10 FBitmap: TBitmap;
11 FScanLines: array of PRGBTriple;
12 function GetPixel(X, Y: Integer): TColor;
13 procedure SetPixel(X, Y: Integer; const Value: TColor);
14 function GetHeight: Integer;
15 function GetWidth: Integer;
16 public
17 constructor Create(const ABitmap: TBitmap);
18 property Pixel[X, Y: Integer]: TColor read GetPixel write SetPixel;
19 property Width: Integer read GetWidth;
20 property Height: Integer read GetHeight;
21 end;
22
23 implementation
24
25 { TQuickPixel }
26
27 constructor TQuickPixel.Create(const ABitmap: TBitmap);
28 var
29 I: Integer;
30 begin
31 inherited Create;
32 FBitmap := ABitmap;
33 FBitmap.PixelFormat := pf24bit;
34 SetLength(FScanLines, FBitmap.Height);
35 for I := 0 to FBitmap.Height - 1 do
36 FScanLines[I] := FBitmap.ScanLine[I];
37 end;
38
39 function TQuickPixel.GetHeight: Integer;
40 begin
41 Result := FBitmap.Height;
42 end;
43
44 function TQuickPixel.GetPixel(X, Y: Integer): TColor;
45 var
46 P: PRGBTriple;
47 begin
48 P := FScanLines[Y];
49 Inc(P, X);
50 Result := (P^.rgbtBlue shl 16) or (P^.rgbtGreen shl 8) or P^.rgbtRed;
51 end;
52
53 function TQuickPixel.GetWidth: Integer;
54 begin
55 Result := FBitmap.Width;
56 end;
57
58 procedure TQuickPixel.SetPixel(X, Y: Integer; const Value: TColor);
59 var
60 P: PRGBTriple;
61 begin
62 P := FScanLines[Y];
63 Inc(P, X);
64 P^.rgbtBlue := (Value and $FF0000) shr 16;
65 P^.rgbtGreen := (Value and $00FF00) shr 8;
66 P^.rgbtRed := Value and $0000FF;
67 end;
68
69 end.
70
71 unit DesktopCanvas;
72
73 // original aurthor is Erwin Molendijk
74
75 interface
76 uses
77 Graphics, Windows;
78
79 type
80 TDesktopCanvas = class(TCanvas)
81 private
82 FDC: HDC;
83 function GetWidth: Integer;
84 function GetHeight: Integer;
85 public
86 constructor Create;
87 destructor Destroy; override;
88 published
89 property Width: Integer read GetWidth;
90 property Height: Integer read GetHeight;
91 end;
92
93 implementation
94
95 { TDesktopCanvas }
96
97 function TDesktopCanvas.GetWidth: Integer;
98 begin
99 Result := GetDeviceCaps(Handle, HORZRES);
100 end;
101
102 function TDesktopCanvas.GetHeight: Integer;
103 begin
104 Result := GetDeviceCaps(Handle, VERTRES);
105 end;
106
107 constructor TDesktopCanvas.Create;
108 begin
109 inherited Create;
110 FDC := GetDC(0);
111 Handle := FDC;
112 end;
113
114 destructor TDesktopCanvas.Destroy;
115 begin
116 Handle := 0;
117 ReleaseDC(0, FDC);
118 inherited Destroy;
119 end;
120
121 end.
|