Читать «Виртуальная библиотека Delphi» онлайн - страница 170

Unknown

 finally

  R.Free;

 end;

end;

function ReadFontFromRegistry(Font : TFont; SubKey : String) : boolean;

Var

 R : TRegistry;

 FontStyleInt : byte;

 FS : TFontStyles;

begin

 R:=TRegistry.Create;

 try

  result:=R.OpenKey(SubKey,false);

  if not result then exit;

  Font.Name:=R.ReadString('Font Name');

  Font.Color:=R.ReadInteger('Color');

  Font.Charset:=R.ReadInteger('CharSet');

  Font.Size:=R.ReadInteger('Size');

  FontStyleInt:=R.ReadInteger('Style');

  Move(FontStyleInt,FS,1);

  Font.Style:=FS;

 finally

  R.Free;

 end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 If FontDialog1.Execute then begin

  SaveFontToRegistry(FontDialog1.Font,'Delphi Kingdom\Fonts');

 end;

end;

procedure TForm1.Button2Click(Sender: TObject);

var NFont : TFont;

begin

 NFont:=TFont.Create;

 if ReadFontFromRegistry(NFont,'Delphi Kingdom\Fonts') then begin

  //здесь добавить проверку - существует ли шрифт

  Label1.Font.Assign(NFont);

  NFont.Free;

 end;

end;

Вопрос:

Как перемещать компонент мышкой во время работы программы "runtime"?

Ответ:

Перехватить событие OnMouseDown, запомнить x и y координты курсора мыши. Отслеживать движение мыши по событию OnMouseMove и перемещать компонент вслед за курсором мыши до тех пор пока не произойдет событие OnMouseUp. В примере показано перемещение компонента TButton. Перемещение начинается, когда пользователь "берет" TButton мышью, удерживая нажатой клавишу "Сontrol".

Пример:

type TForm1 = class(TForm)

 Button1: TButton;

 procedure Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

 procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

 procedure Button1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

private

 {Private declarations}

public

 {Public declarations}

 MouseDownSpot : TPoint;

 Capturing : bool;

end;

var Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin

 if ssCtrl in Shift then begin

  SetCapture(Button1.Handle);

  Capturing := true;

  MouseDownSpot.X := x;

  MouseDownSpot.Y := Y;

 end;

end;

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

begin

 if Capturing then begin

  Button1.Left := Button1.Left - (MouseDownSpot.x - x);

  Button1.Top := Button1.Top - (MouseDownSpot.y - y);

 end;

end;