2006. január 22., vasárnap

Invert someones desktop for fun (has usefull code)


Problem/Question/Abstract:

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.

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.

Nincsenek megjegyzések:

Megjegyzés küldése