Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Selected = False wenn Click außerhalb Komponente (https://www.delphipraxis.net/86507-selected-%3D-false-wenn-click-ausserhalb-komponente.html)

owolicious 15. Feb 2007 07:37


Selected = False wenn Click außerhalb Komponente
 
Hi,
also ich habe mir eine eigene Komponente gebastelt.
Sind im groben Shapes. Ich habe nun noch eine Eigenschaft (selected mit boolean) hinzugefügt. sobald ein shape selected ist erscheinen vier kleine weise vierecke an den ecken um die größe zu verändern.
das shape wird selected sobald ich mit der maus upclicke :) also bei onmouseup (ist auch schon in der komponente definiert)
wie mache ich es jetzt am besten dass wenn ein click auf den hintergrund erfolgt das die komponente selected = false wird. mache ich das am besten im hauptprogramm oder in der komponente? (auch im hinblick darauf das später mehrere objekte mit z.b. shift gehaltener taste markiert werden sollen)

merci beaucoup

:) owolicious

sirius 15. Feb 2007 07:50

Re: Selected = False wenn Click außerhalb Komponente
 
Von wo hast du denn deine Komponente abgeleitet?
Ein TWincontrol liefert das Ergeignis onExit mit, das kannst du ja auswerten.
Oder du reagierst auf die message CM_EXIT;

owolicious 15. Feb 2007 08:46

Re: Selected = False wenn Click außerhalb Komponente
 
Delphi-Quellcode:
TShape1 = class(TShape)
d.h. doch dass ich es von tShape abegeleitet habe oder??? :?:

sirius 15. Feb 2007 09:31

Re: Selected = False wenn Click außerhalb Komponente
 
Ja, das heist es.
Und Tshape ist von TGraphiccontrol, was widerum von TControl ist, usw...
Demnach hast du kein onExit-Ereignis.

Hmm, jetzt wirds schwieriger. TGraphiccontrol ist nicht auf Benutzerinteraktion ausgelegt. Entweder du baust um auf TWincontrol, oder du musst es irgendwie anders umgehen, indem du alle Nachrichten auf das Formular auswertest.

Hawkeye219 15. Feb 2007 10:39

Re: Selected = False wenn Click außerhalb Komponente
 
Hallo owolicious,

ich würde einen anderen Weg wählen und die Verwaltung der selektierten Controls auslagern. Die Komponente TStretchHandle ist zwar schon ziemlich alt, sie funktioniert aber auch mit den aktuellen Delphi-Versionen noch zuverlässig. Vielleicht löst sie ja bereits dein Problem.

Gruß Hawkeye

owolicious 15. Feb 2007 11:24

Re: Selected = False wenn Click außerhalb Komponente
 
hmmm wie kann man den TShape auf TWinControl umbauen??

sirius 15. Feb 2007 12:38

Re: Selected = False wenn Click außerhalb Komponente
 
Auf die Schnelle fällt mir nur das ein (ist sicherlich verbesserungswürdig):
Delphi-Quellcode:
type tShape1=class(Twincontrol)
       Shape:Tshape;
       procedure Exit(var msg:TMessage);message cm_exit;
       procedure Button(var msg:TMessage);message wm_lbuttondown;
       constructor create(Aowner:Tcomponent);override;
       destructor destroy;
end;

//....

constructor Tshape1.create;
begin
  inherited create(Aowner);
  shape:=Tshape.Create(self);
  shape.Parent:=self;
  shape.Align:=alclient;
  shape.Enabled:=false;
  self.TabStop:=true;
end;

destructor Tshape1.destroy;
begin
  shape.Free;
  inherited;
end;

procedure TShape1.Exit;
begin
  showmessage('Exit');
end;

procedure TShape1.button;
begin
  self.SetFocus;
end;

owolicious 15. Feb 2007 12:50

Re: Selected = False wenn Click außerhalb Komponente
 
hmm aber da sagt er jetzt das er die methode paint nicht gefunden hat in der basis klasse


hier mal mein code:

Delphi-Quellcode:
unit Shape2;

interface

uses
  SysUtils, Classes, Controls, ExtCtrls, Graphics;

type
  TShapeType = (stRechteck, stDreieck, stProzess);

  TShape1 = class(TWinControl)
    Shape  : TShape;
  private
    { Private declarations }
    FShape   : TShapeType;
    FCaption : String;
    FSelected : Boolean;

    rx, ry,
    oH, oW,
    oL, oT   : Integer;

    procedure SetShape(Value : TShapeType);
    procedure SetCaption(Value : String);
    procedure SetSelection(Value : Boolean);
  protected
    { Protected declarations }
     protected procedure Paint();override;
     protected procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X,Y: Integer); override;
     protected procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
     protected procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X,Y: Integer); override;
  public
    { Public declarations }
  published
    { Published declarations }
    property Shape: TShapeType read FShape write SetShape;
    property Caption: String read FCaption write SetCaption;
    property Selected: Boolean read FSelected write SetSelection;

  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TShape1]);
end;

procedure TShape1.SetShape(Value : TShapeType);
begin
  FShape := Value;
end;

procedure TShape1.SetCaption(Value : String);
begin
  FCaption := Value;
end;

procedure TShape1.SetSelection(Value : Boolean);
begin
  FSelected := Value;
  Paint;
end;

procedure TShape1.Paint();
var sw, sh: Integer;
begin
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(Rect(0,0,self.Width, self.Height));
    sw := self.Width -1;
    sh := self.Height -1;
    Canvas.Brush.Color := self.Brush.Color;
    Canvas.Font.Color  := $00FFFFFF;
    Canvas.Font.Style  := [fsBold];

    if Self.Shape = stRechteck then begin
      Canvas.Polygon([Point(4, 4), Point(Self.ClientWidth-4, 4), Point(Self.ClientWidth-4, Self.ClientHeight-4), Point(4, Self.ClientHeight-4)]);
    end;

    If Self.Shape = stDreieck then begin
      Canvas.Polygon([Point(4, Trunc(Self.ClientHeight / 2)-4), Point(Self.ClientWidth-4, 4), Point(Self.ClientWidth-4, Self.ClientHeight-4)]);
    end;

    If Self.Shape = stProzess then begin
      Canvas.Polygon([Point(4,4), Point(Trunc(0.85 * Self.Width),4), Point(Self.Width-4,Trunc(Self.Height / 2)), Point(Trunc(0.85 * Self.Width), Self.Height-4), Point(4,Self.Height-4), Point(Trunc(0.15*Self.Width),Trunc(Self.Height / 2))]);
    end;

    If Self.Selected = True then begin
      Canvas.Pen.Style := psDot;
      Canvas.Pen.Color := $00999999;
      Canvas.Brush.Style:= bsClear;
      Canvas.Polyline([
        Point(0,0),
        Point(sw,0),
        Point(sw,sh),
        Point(0,sh),
        Point(0,0)
        ]);

      Canvas.Pen.Style := psSolid;
      Canvas.Pen.Color := $00000000;
      Canvas.Brush.Color := $00FFFFFF;

      //Ecken [] zeichnen
      Canvas.Rectangle(0,0,8,8);
      Canvas.Rectangle(self.ClientWidth-8,0,self.ClientWidth,8);
      Canvas.Rectangle(0,self.ClientHeight-8,8,self.ClientHeight);
      Canvas.Rectangle(self.ClientWidth-8,self.ClientHeight-8,self.ClientWidth,self.ClientHeight);

      Canvas.Rectangle(Trunc(self.Width /2)-4,0,Trunc(self.Width /2)+4,8);
      Canvas.Rectangle(Trunc(self.Width /2)-4,self.Height-8,Trunc(self.Width /2)+4,self.Height);
      Canvas.Rectangle(0,Trunc(Self.Height / 2)-4,8,Trunc(Self.Height / 2)+4);
      Canvas.Rectangle(self.Width-8,Trunc(Self.Height / 2)-4,self.Width,Trunc(Self.Height / 2)+4);
    end;

    Canvas.Brush.Style := bsClear;
    Canvas.TextOut(Trunc(self.Width / 2)-Trunc(Canvas.TextWidth(Caption) / 2), Trunc(self.Height / 2) - Trunc(Canvas.TextHeight(Caption) / 2), Self.Caption);
end;

procedure TShape1.MouseDown(Button: TMouseButton; Shift: TShiftState; X,Y: Integer);
begin
  rx := X;
  ry := Y;

  oH := self.Height;
  oW := self.Width;
  oT := self.Top;
  oL := self.Left;

  Self.Selected := True;
  Paint;
end;

procedure TShape1.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  if ((ssLeft in Shift) AND (self.Selected = True)) then begin
    //links oben
    if ((rx < 9) AND (ry < 9)) then begin
      self.Top := Mouse.CursorPos.Y -30 - Parent.Top - ry;
      self.Left := Mouse.CursorPos.X -4 - Parent.Left - rx;
      self.Height := oH+(oT - self.Top);
      self.Width := oW+(oL - self.Left);
    //rechts oben
    end else if ((rx > oW-8) AND (ry < 9)) then begin
      self.Top := Mouse.CursorPos.Y - 30 - Parent.Top - ry;
      self.Height := oH+(oT - self.Top);
      self.Width := Mouse.CursorPos.X -4 - Parent.Left - oL + (oW-rX);
    //rechts unten
    end else if ((rx > oW-8) AND (ry > oH -8)) then begin
      self.Top   := oT;
      self.Left  := oL;
      self.Height := Mouse.CursorPos.Y - 30 - Parent.Top - oT + (oH-rY);
      self.Width := Mouse.CursorPos.X - 4 - Parent.Left - oL + (oW-rX);
    //links unten
    end else if ((rx < 9) AND (ry > oH -8)) then begin
      self.Top := oT;
      self.Left := Mouse.CursorPos.X -4 - Parent.Left - rx;
      self.Height := Mouse.CursorPos.Y - 30 - Parent.Top - oT + (oH-rY);
      self.Width := oW+(oL - self.Left);
    //mitte oben
    end else if ((rx > Trunc(oW / 2)-4) AND (rx < Trunc(oW / 2)+4) AND (ry < 9)) then begin
      self.Top := Mouse.CursorPos.Y -30 - Parent.Top - ry;
      self.Height := oH+(oT - self.Top);
    //mitte links
    end else if ((rx < 9) AND (ry > Trunc(oH/2) -4) AND (ry < Trunc(oH/2)+4)) then begin
      self.Left := Mouse.CursorPos.X -4 - Parent.Left - rx;
      self.Width := oW+(oL - self.Left);
    //mitte rechts
    end else if ((rx > oW-8) AND (ry > Trunc(oH/2) -4) AND (ry < Trunc(oH/2)+4)) then begin
      self.Width := Mouse.CursorPos.X -4 - Parent.Left - oL + (oW-rX);
    //mitte unten
    end else if ((rx > Trunc(oW / 2)-4) AND (rx < Trunc(oW / 2)+4) AND (ry > oH -8)) then begin
      self.Top := oT;
      self.Height := Mouse.CursorPos.Y - 30 - Parent.Top - oT + (oH-rY);
    //sonst verschiebe nur
    end else begin
      self.Top := Mouse.CursorPos.Y - Parent.Top - ry-30;
      self.Left := Mouse.CursorPos.X - Parent.Left - rx-4;
    end;
  end;
end;

procedure TShape1.MouseUp(Button: TMouseButton; Shift: TShiftState; X,Y: Integer);
begin
end;

end.

sirius 15. Feb 2007 13:04

Re: Selected = False wenn Click außerhalb Komponente
 
Vielleicht solltest du eher ein Twincontrol um dein TShape1 bauen. Also du machst dein Tshape1 wie gehabt, veröffnetlichst dann aber Tmyshape:
Delphi-Quellcode:
type
  TShape1 = class(TShape)
  private
    { Private declarations } 
    FShape   : TShapeType;
    FCaption : String;
//...
end;


type TMyShape=class(Twincontrol)
       Shape:TShape1;
       procedure Exit(var msg:TMessage);message cm_exit;
       procedure Button(var msg:TMessage);message wm_lbuttondown;
       constructor create(Aowner:Tcomponent);override;
       destructor destroy;
end;
Aber vielleicht gibts auch noch viel bessere Ideen...

owolicious 15. Feb 2007 17:03

Re: Selected = False wenn Click außerhalb Komponente
 
also erstmal vielen dank für eure hilfe :)

also ich habs mal so gemacht aber leider passiert nix beim klicken :)

Delphi-Quellcode:
interface

uses
  SysUtils, Classes, Controls, ExtCtrls, Graphics, Messages;

type
  TShapeType = (stRechteck, stDreieck, stProzess);

  TShape1 = class(TShape)
  private
    { Private declarations }
    FShape   : TShapeType;
    FCaption : String;
    FSelected : Boolean;

    rx, ry,
    oH, oW,
    oL, oT   : Integer;

    procedure SetShape(Value : TShapeType);
    procedure SetCaption(Value : String);
    procedure SetSelection(Value : Boolean);
  protected
    { Protected declarations }
     protected procedure Paint();override;
     protected procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X,Y: Integer); override;
     protected procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
     protected procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X,Y: Integer); override;
  public
    { Public declarations }
  published
    { Published declarations }
    property Shape: TShapeType read FShape write SetShape;
    property Caption: String read FCaption write SetCaption;
    property Selected: Boolean read FSelected write SetSelection;

  end;

type TMyShape = class(TWinControl)
  Shape : TShape1;
       procedure Exit(var msg:TMessage);message cm_exit;
       procedure Button(var msg:TMessage);message wm_lbuttondown;
       constructor create(Aowner:Tcomponent);override;
       destructor destroy;
end;
procedure Register;

implementation

constructor TMyShape.create;
begin
  inherited create(Aowner);
  shape:=Tshape1.Create(self);
  shape.Parent:=self;
  shape.Enabled:=false;
  self.TabStop:=true;
end;

destructor TMyShape.destroy;
begin
  shape.Free;
  inherited;
end;

procedure TMyShape.Exit(var msg:TMessage);
begin
  self.Shape.Selected := false;
end;

procedure TMyShape.Button(var msg:TMessage);
begin
  self.Shape.Selected := true;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:01 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz