unit FocusGraphicControl;
interface
uses
SysUtils, Classes, Controls, Dialogs, Messages, Graphics, Forms, Types;
type
TFocusGraphicControl =
class;
TFocusControl =
class(TWinControl)
private
FGraphicControl: TFocusGraphicControl;
protected
procedure WndProc(
var Message: TMessage);
override;
procedure WMKeyDown(
var message: TWMKeyDown);
message WM_KEYDOWN;
procedure WMKeyUp(
var message: TWMKeyUp);
message WM_KEYUP;
public
constructor Create(AOwner: TComponent; AGraphicControl: TFocusGraphicControl);
reintroduce;
property TabStop;
property TabOrder;
end;
TFocusGraphicControl =
class(TGraphicControl)
private
FFocusControl: TFocusControl;
function GetTabOrder: Integer;
procedure SetTabOrder(
const Value: Integer);
function GetTabStop: Boolean;
procedure SetTabStop(
const Value: Boolean);
function GetFocused: Boolean;
function GetCanFocus: Boolean;
procedure DestroyFocusControl;
procedure CreateFocusControl(AOwner: TComponent; AParent: TWinControl);
procedure WMEraseBkgnd(
var message: TWMEraseBkGnd);
message WM_ERASEBKGND;
protected
procedure Paint;
override;
procedure PaintShape;
virtual;
procedure SetParent(AParent: TWinControl);
override;
procedure DoKeyDown(
var Key: Word; Shift: TShiftState);
virtual;
abstract;
procedure DoKeyUp(
var Key: Word; Shift: TShiftState);
virtual;
abstract;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure SetFocus;
property CanFocus: Boolean
read GetCanFocus;
property Focused: Boolean
read GetFocused;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
override;
published
property TabStop: Boolean
read GetTabStop
write SetTabStop;
property TabOrder:Integer
read GetTabOrder
write SetTabOrder;
end;
implementation
{ TFocusGraphicControl }
constructor TFocusGraphicControl.Create(AOwner: TComponent);
begin
inherited;
FFocusControl :=
nil;
CreateFocusControl(
nil, TWinControl(AOwner));
end;
destructor TFocusGraphicControl.Destroy;
begin
DestroyFocusControl;
inherited;
end;
function TFocusGraphicControl.GetCanFocus: Boolean;
begin
if Assigned(FFocusControl)
then
result := FFocusControl.CanFocus
else
result := False;
end;
function TFocusGraphicControl.GetFocused: Boolean;
begin
if Assigned(FFocusControl)
then
result := FFocusControl.Focused
else
result := False;
end;
function TFocusGraphicControl.GetTabOrder: Integer;
begin
if Assigned(FFocusControl)
then
result := FFocusControl.TabOrder
else
result := -1;
end;
function TFocusGraphicControl.GetTabStop: Boolean;
begin
if Assigned(FFocusControl)
then
result := FFocusControl.TabStop
else
result := False;
end;
procedure TFocusGraphicControl.SetFocus;
begin
if Assigned(FFocusControl)
then
if FFocusControl.CanFocus
then
FFocusControl.SetFocus;
end;
procedure TFocusGraphicControl.SetTabOrder(
const Value: Integer);
begin
if Assigned(FFocusControl)
then
FFocusControl.TabOrder := Value;
end;
procedure TFocusGraphicControl.SetTabStop(
const Value: Boolean);
begin
if Assigned(FFocusControl)
then
FFocusControl.TabStop := Value;
end;
procedure TFocusGraphicControl.PaintShape;
begin
//!!!Nur ein Beispiel, diese Methode in Nachfolgern überschreiben
Canvas.Brush.Style := bsClear;
if not Focused
then
Canvas.Pen.Color := clBlack
else
Canvas.Pen.Color := clRed;
Canvas.Rectangle(ClientRect);
end;
procedure TFocusGraphicControl.Paint;
begin
inherited;
PaintShape;
end;
procedure TFocusGraphicControl.SetParent(AParent: TWinControl);
begin
inherited;
if Assigned(Self.Parent)
then
begin
FFocusControl.Parent := Self.Parent;
FFocusControl.Show;
end;
end;
procedure TFocusGraphicControl.CreateFocusControl(AOwner: TComponent; AParent: TWinControl);
begin
if not Assigned(FFocusControl)
then
begin
FFocusControl := TFocusControl.Create(AOwner, Self);
try
FFocusControl.TabStop := True;
FFocusControl.SetBounds(0, 0, 0, 0);
except
raise;
end;
end;
end;
procedure TFocusGraphicControl.DestroyFocusControl;
begin
if Assigned(FFocusControl)
then
begin
if Assigned(FFocusControl.Parent)
then
FreeAndNil(FFocusControl);
end;
end;
procedure TFocusGraphicControl.SetBounds(ALeft, ATop, AWidth,
AHeight: Integer);
begin
inherited;
Repaint;
end;
procedure TFocusGraphicControl.WMEraseBkgnd(
var message: TWMEraseBkGnd);
begin
message.result := 1;
end;
{ TFocusControl }
constructor TFocusControl.Create(AOwner: TComponent;
AGraphicControl: TFocusGraphicControl);
begin
inherited Create(AOwner);
Assert(Assigned(AGraphicControl), '
Cannot create a FocusControl with unassigned GraphicControl.');
FGraphicControl := AGraphicControl;
end;
procedure TFocusControl.WMKeyDown(
var message: TWMKeyDown);
var Shift: TShiftState;
begin
if Assigned(FGraphicControl)
then
begin
Shift := KeyDataToShiftState(
Message.KeyData);
FGraphicControl.DoKeyDown(
Message.CharCode, Shift);
end;
inherited;
end;
procedure TFocusControl.WMKeyUp(
var message: TWMKeyUp);
var Shift: TShiftState;
begin
if Assigned(FGraphicControl)
then
begin
Shift := KeyDataToShiftState(
Message.KeyData);
FGraphicControl.DoKeyUp(
Message.CharCode, Shift);
end;
inherited;
end;
procedure TFocusControl.WndProc(
var Message: TMessage);
begin
inherited;
case Message.Msg
of
WM_SETFOCUS, WM_KILLFOCUS:
begin
if Assigned(FGraphicControl)
then
FGraphicControl.Repaint;
end;
end;
end;
end.