
(Ebook - Pdf) Kick Ass Delphi Programming
.pdf
To access the contents, click the chapter and section titles.
Kick Ass Delphi Programming
Go!
Keyword
(Publisher: The Coriolis Group)
Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin
ISBN: 1576100448
Publication Date: 09/01/96
Search this book:
Go!
-----------
Configuration
As mentioned earlier, there are several ways to configure the screen saver. Figure 8.2 shows the configuration dialog box that is used to alter the settings. The nine radio buttons in the upper left are used to specify the origin of the laser beam. If the Random check box is checked, each time the logo is drawn, the beam will originate from a different location.
FIGURE 8.2 The configuration dialog.
Likewise, the color of the beam may be selected or chosen at random. The two track bars on the right hand side of the dialog are used to specify how fast the logo is drawn, and how long before it gets erased, respectively.
A quick note about the track bars. Although the screen captures show the screen saver running under Windows 95, the application is not a 32-bit application, and as such, does not have access to the standard Windows 95 track bar. The track bars used in this dialog box are instances of the RzTrackBar custom component. While I don’t have the space to describe how this component works, the complete description can be found in my book, Developing Custom Delphi Components, which is available from Coriolis Group Books.
Figure 8.2 also shows that this screen saver supports a password. The screen saver satisfies this requirement by using the EncryptString function which is shown in Listing 8.2. Located in the SvrUtils unit, EncryptString takes a single string parameter and encrypts it using the same technique used by Windows for encrypting screen saver passwords. This enables the laser screen saver to use the same password that is used for the standard Windows screen savers (for example,
Starfield Simulation).
Listing 8.2 SVRUTILS.PAS
unit SvrUtils;
interface
uses
Graphics;
const
Colors : array[ 0..15 ] of TColor =
( clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite );
type
TLaserOrigin = ( loUpperLeft, loUpperCenter, loUpperRight, loMidLeft, loMidCenter, loMidRight, loLowerLeft, loLowerCenter, loLowerRight );
var
LaserSpeed : Longint;
PauseDelay : Longint;
LaserColor : TColor; LaserOrigin : TLaserOrigin; Password : string; PWProtected : Boolean; RandomColors : Boolean; RandomOrigin : Boolean;
procedure LoadSettings; procedure SaveSettings;
function EncryptString( const S : string ) : string; procedure SetPassword( const S : string ); procedure WinDelay( Duration : Longint );
implementation
uses
IniFiles, SysUtils, WinProcs, Forms, MMSystem;
const
Section = 'Screen Saver.DLLaser';
procedure LoadSettings; var
IniFile : TIniFile;
TempInt : Integer; begin
IniFile := TIniFile.Create( 'CONTROL.INI' );
Password := IniFile.ReadString( 'ScreenSaver', 'Password', '' ); LaserSpeed := IniFile.ReadInteger( Section, 'LaserSpeed', 10 ); PauseDelay := IniFile.ReadInteger( Section, 'PauseDelay', 10 ); LaserColor := IniFile.ReadInteger( Section, 'LaserColor',clGreen); TempInt := IniFile.ReadInteger( Section, 'LaserOrigin',
Ord(loLowerCenter) ); LaserOrigin := TLaserOrigin( TempInt );
PWProtected := IniFile.ReadBool( Section, 'PWProtected', False ); RandomColors := IniFile.ReadBool(Section, 'RandomColors', False); RandomOrigin := IniFile.ReadBool(Section, 'RandomOrigin', False); IniFile.Free;
end; {= LoadSettings =}
procedure SaveSettings; var
IniFile : TIniFile; begin
IniFile := TIniFile.Create( 'CONTROL.INI' ); IniFile.WriteInteger( Section, 'LaserSpeed', LaserSpeed ); IniFile.WriteInteger( Section, 'PauseDelay', PauseDelay ); IniFile.WriteInteger( Section, 'LaserColor', LaserColor ); IniFile.WriteInteger( Section, 'LaserOrigin',Ord(loLowerCenter)); IniFile.WriteBool( Section, 'PWProtected', PWProtected ); IniFile.WriteBool( Section, 'RandomColors', RandomColors ); IniFile.WriteBool( Section, 'RandomOrigin', RandomOrigin ); IniFile.Free;
end;
function EncryptString( const S : string ) : string; var
I, Len : Integer; B : Byte;
Stz : array[ 0..255 ] of Char;
procedure EXor( X : Byte; var Y : Byte );
const { '[]=' - not allowed in profile string } NotAllowed = [ 0..$20, $7f..$90, $93..$9F, $3D, $5B, $5D ];
begin
if not ( ( Y xor X ) in NotAllowed ) then Y := Y xor X;
end;
begin {= EncryptString =}
{Encryption method works for null-terminated strings }
{Therefore, first copy S to a null-terminated string } StrPCopy( Stz, UpperCase( S ) );
Len := StrLen( Stz ); if Len = 0 then begin
Result := ''; Exit;
end;
for I := 0 to Len - 1 do |
{ First Pass } |
begin |
|
B := Byte( Stz[ I ] ); |
|
Exor( Len, B ); |
|
if I = 0 then |
|
Exor( $2A, B ) |
|
else |
|
begin |
|
Exor( I, B ); |
|
Exor( Byte( Stz[ I - 1 ] ), B ); |
|
end; |
|
Stz[ I ] := Char( B ); |
{ Store Encrypted Byte } |
end; |
|

if Len > 1 then |
{ Second Pass } |
begin |
|
for I := Len - 1 downto 0 do |
|
begin |
|
B := Byte( Stz[ I ] ); |
|
Exor( Len, B ); |
|
if I = Len - 1 then |
|
Exor( $2A, B ) |
|
else |
|
begin |
|
Exor( I, B ); |
|
Exor( Byte( Stz[ I + 1 ] ), B ); |
|
end; |
|
Stz[ I ] := Char( B ); |
{ Store Encrypted Byte } |
end; |
|
end; |
|
Result := StrPas( Stz ); |
{ Return a Pascal string } |
end; {= EncryptString =}
procedure SetPassword( const S : string ); var
IniFile : TIniFile; begin
IniFile := TIniFile.Create( 'CONTROL.INI' ); if S = '' then
Password := '' else
Password := EncryptString( S );
IniFile.WriteString( 'ScreenSaver', 'Password', Password ); IniFile.Free;
end; {= SetPassword =}
procedure WinDelay( Duration : Longint ); var
Start : Longint; begin
if Duration = 0 then Exit;
Start := TimeGetTime; { TimeGetTime from MMSystem Unit } repeat
Application.ProcessMessages;
until TimeGetTime - Start >= Duration; end;
end.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.
Kick Ass Delphi Programming
Go!
Keyword
(Publisher: The Coriolis Group)
Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin
ISBN: 1576100448
Publication Date: 09/01/96
Search this book:
Go!
-----------
A Color ComboBox
Take a closer look at the combo box in Figure 8.3. Rather than simply display the name of each color, this combo box displays a sample of each color next to the name. This is accomplished using the owner-draw features of the combo box.
FIGURE 8.3 The TRzColorComboBox component.
Of course, this makes a good candidate for a custom Delphi component, which is exactly what it is. Listing 8.3 shows the source code for the RzColCbx unit. The component’s declaration is roughly similar to that of the DrivesComboBox component. Both classes descend from CustomComboBox and operate as owner-draw controls. In addition, neither component surfaces any of the properties which affect owner-draw behavior. For example, the Style and ItemHeight properties are not redeclared as published.
Listing 8.3 RZCOLCBX.PAS
unit RzColCbx; interface
uses
Messages, WinTypes, WinProcs, Classes, Forms, Graphics, Controls, StdCtrls, Menus;
type
TRzColorComboBox = class( TCustomComboBox ) private
FIncludeColor : Boolean;
FShowSysColors : Boolean;
procedure SetShowSysColors( Value : Boolean ); function GetSelectedColor : TColor;
procedure SetSelectedColor( Value : TColor ); procedure DrawItem( Index : Integer; Rect : TRect;
State : TOwnerDrawState ); override;
protected
procedure CreateWnd; override;
procedure AddColor( const S : string ); virtual; public
constructor Create( AOwner : TComponent ); override; published
property SelectedColor : TColor read GetSelectedColor
write SetSelectedColor default clBlack;
property ShowSysColors : Boolean read FShowSysColors
write SetShowSysColors default True;
{ Inherited Properties & Events } property Color;
property Ctl3D; property DragMode; property DragCursor; property Enabled; property Font; property ParentColor; property ParentCtl3D; property ParentFont;
property ParentShowHint; property PopupMenu; property ShowHint; property Sorted; property TabOrder; property TabStop; property Visible; property OnChange; property OnClick; property OnDblClick; property OnDragDrop; property OnDragOver; property OnDropDown; property OnEndDrag; property OnEnter; property OnExit; property OnKeyDown; property OnKeyPress; property OnKeyUp;
end;
procedure Register;
implementation
constructor TRzColorComboBox.Create( AOwner : TComponent );
begin
inherited Create( AOwner );
Style := csOwnerDrawFixed; { Notice Style is not published } FShowSysColors := True;
end;
procedure TRzColorComboBox.CreateWnd;
begin |
|
inherited CreateWnd; |
|
Clear; |
{ Clear items from list } |
FIncludeColor := True; |
|
GetColorValues( AddColor ); |
{ Call AddColor for all Colors } |
SelectedColor := clBlack; |
|
end; |
|
procedure TRzColorComboBox.AddColor( const S : string ); var
C : Longint; begin
{GetColorValues calls AddColor for all colors, including }
{system colors. Flag is cleared to skip system colors. } if ( S = 'clScrollBar' ) and not FShowSysColors then
FIncludeColor := False;
if FIncludeColor then |
|
begin |
|
IdentToColor( S, C ); |
{ Converts string to color value } |
{Rather than a pointer to an object, the Objects property }
{is populated with the Longint value of the Color } Items.AddObject( Copy( S, 3, 20 ), TObject( C ) );
end;
end;
procedure TRzColorComboBox.SetShowSysColors( Value : Boolean ); begin
if Value <> FShowSysColors then begin
FShowSysColors := Value; RecreateWnd;
end;
end;
function TRzColorComboBox.GetSelectedColor : TColor; begin
if ItemIndex = -1 then Result := clBlack
else
Result := TColor( Items.Objects[ ItemIndex ] );
end;
procedure TRzColorComboBox.SetSelectedColor( Value : TColor );
function IndexFromColor( C : TColor ) : Integer; var
I : Integer;
begin
I := 0;
while (I < Items.Count) and (C <> TColor(Items.Objects[I])) do Inc( I );
if I = Items.Count then Result := -1
else
Result := I;
end;
begin
ItemIndex := IndexFromColor( Value ); end;
procedure TRzColorComboBox.DrawItem( Index : Integer; Rect : TRect; State : TOwnerDrawState );
var
R : TRect;
C : Longint; begin
with Canvas do begin
R := Rect;
InflateRect( R, -2, -2 );
R.Right := 20; { R represents size of color block } FillRect( Rect );
{ Color value (i.e. TColor) is stored in Objects property } Brush.Color := TColor( Items.Objects[ Index ] ); Rectangle( R.Left, R.Top, R.Right, R.Bottom );
if odSelected in State then Brush.Color := clHighlight
else
Brush.Color := Color; { Display Color Name } TextOut( Rect.Left + 24, Rect.Top + 2, Items[ Index ] );
end;
end;
procedure Register; begin
RegisterComponents( 'Raize', [ TRzColorComboBox ] ); end;
end.
The RzColorComboBox component defines two properties that make the component easier to use. First, SelectedColor is defined as a TColor property. This makes it easier to determine the currently selected color value. Likewise, the SelectedColor property can also be used to set the current color.
The second property is ShowSysColors. This property affects how many colors appear in the combo box. The GetColorValues procedure, defined in the Graphics unit, is used to populate the combo box with the available colors. The available colors include the standard 16 colors, plus the

standard system colors (for example, clScrollBar and clHighlight). If you do not want the system colors to be displayed in the combo box, then set ShowSysColors to False.
The RzColorComboBox component uses the Items list to manage the list of colors. The Strings list holds the color names while the Objects list holds the color values. Storing the color values in the Objects list makes it unnecessary to convert the selected color name to a TColor value.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.