Title: Invert someones desktop for fun (has usefull code)(v2).
Question: 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:
[v2 Added DesktopCanvas unit to this text]
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.
1. It contains TDesktopCanvas where you can access your desktop through a TCanvas object. (Thanks to artical http://www.delphi3000.com/articles/article_782.asp by Erwin Molendijk)
2. 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:
TDesktopCanvas:
Please refer to artical http://www.delphi3000.com/articles/article_782.asp by Erwin Molendijk to take a look at TDesktopCanvas... [v2 update - I included it at the end of this artical] I may have modified it some, cannot remember... but you can download my useable unit. I want to make sure to give credit to Erwin.
TQuickPixel:
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.
===================================
unit QuickPixel;
interface
uses
Windows, Graphics;
type
TQuickPixel = class
private
FBitmap: TBitmap;
FScanLines: array of PRGBTriple;
function GetPixel(X, Y: Integer): TColor;
procedure SetPixel(X, Y: Integer; const Value: TColor);
function GetHeight: Integer;
function GetWidth: Integer;
public
constructor Create(const ABitmap: TBitmap);
property Pixel[X, Y: Integer]: TColor read GetPixel write SetPixel;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
end;
implementation
{ TQuickPixel }
constructor TQuickPixel.Create(const ABitmap: TBitmap);
var
I: Integer;
begin
inherited Create;
FBitmap:= ABitmap;
FBitmap.PixelFormat:= pf24bit;
SetLength(FScanLines, FBitmap.Height);
for I:= 0 to FBitmap.Height-1 do
FScanLines[I]:= FBitmap.ScanLine[I];
end;
function TQuickPixel.GetHeight: Integer;
begin
Result:= FBitmap.Height;
end;
function TQuickPixel.GetPixel(X, Y: Integer): TColor;
var
P: PRGBTriple;
begin
P:= FScanLines[Y];
Inc(P, X);
Result:= (P^.rgbtBlue shl 16) or (P^.rgbtGreen shl 8) or P^.rgbtRed;
end;
function TQuickPixel.GetWidth: Integer;
begin
Result:= FBitmap.Width;
end;
procedure TQuickPixel.SetPixel(X, Y: Integer; const Value: TColor);
var
P: PRGBTriple;
begin
P:= FScanLines[Y];
Inc(P, X);
P^.rgbtBlue:= (Value and $FF0000) shr 16;
P^.rgbtGreen:= (Value and $00FF00) shr 8;
P^.rgbtRed:= Value and $0000FF;
end;
end.
===============================================
unit DesktopCanvas;
// original aurthor is Erwin Molendijk
interface
uses
Graphics, Windows;
type
TDesktopCanvas = class(TCanvas)
private
FDC: HDC;
function GetWidth:Integer;
function GetHeight:Integer;
public
constructor Create;
destructor Destroy; override;
published
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
end;
implementation
{ TDesktopCanvas }
function TDesktopCanvas.GetWidth:Integer;
begin
Result:= GetDeviceCaps(Handle, HORZRES);
end;
function TDesktopCanvas.GetHeight:Integer;
begin
Result:= GetDeviceCaps(Handle, VERTRES);
end;
constructor TDesktopCanvas.Create;
begin
inherited Create;
FDC:= GetDC(0);
Handle:= FDC;
end;
destructor TDesktopCanvas.Destroy;
begin
Handle:= 0;
ReleaseDC(0, FDC);
inherited Destroy;
end;
end.