Autor Beitrag
Xelsius
Hält's aus hier
Beiträge: 3

WinXp, Ubuntu
Delphi 2005 Prof.
BeitragVerfasst: Sa 28.06.08 19:33 
Hallo liebe Delphi Gemeinde,

ich hoffe ihr könnt mir weiter helfen, da ich mir daraus keinen Reim machen kann.

folgende Klasse ist gegeben:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
unit UTWndowsFunctions;

interface     

type
  TStatus = (StandBy, LockWindowsXPStation);

type
  THotKeyFunctionsWindws = class
    public
      constructor Create(saction : TStatus);
      destructor Destroy;
      
      procedure doit();
      procedure setFunction(saction : TStatus);
      function getFunctionName() : String;
    private                             
      //Functions
      procedure goStandBy();
      procedure plockWindowsXPStation();
  end;


implementation

uses HotKeyManager, ComCtrls, Windows, Forms, Messages, Dialogs;

var
  sfunctionName : String;
  action        : TStatus;

const
  waitingms = 100;    //waiting 5 ms until next action

constructor THotKeyFunctionsWindws.Create(saction : TStatus);
begin
  Inherited Create;

  setFunction(saction);
end;

destructor THotKeyFunctionsWindws.Destroy;
begin
  inherited Destroy;
end;

procedure THotKeyFunctionsWindws.setFunction(saction : TStatus);
begin
  case action of
    StandBy                 : begin action:=saction; sfunctionName:=   'StandBy'; end;
    LockWindowsXPStation    : begin action:=saction; sfunctionName:=   'LockStation'; end;
  end;
end;

function THotKeyFunctionsWindws.getFunctionName: string;
begin
  result:=sfunctionName;
end;

procedure THotKeyFunctionsWindws.doit;
begin
  if action = StandBy               then begin goStandBy()            ; Exit; end;
  if action = LockWindowsXPStation  then begin plockWindowsXPStation() ; Exit; end;
end;

procedure THotKeyFunctionsWindws.goStandBy;
begin
  SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
  Sleep(waitingms);
end;

procedure THotKeyFunctionsWindws.plockWindowsXPStation;
begin
  LockWorkStation;
  Sleep(waitingms);
end;

end.


Jetzt füge ich in meinem Hauptformular einer listbox, Objekte der oben genannten Klasse hinzu.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TMainForm.FormCreate(Sender: TObject);
var
  hotKeyFunction : THotKeyFunctionsWindws;
begin

    listbox3.Items.AddObject('Object1 ',THotKeyFunctionsWindws.Create(StandBy));
    listbox3.Items.AddObject('Object2 ',THotKeyFunctionsWindws.Create(LockWindowsXPStation));

    hotKeyFunction:=THotKeyFunctionsWindws(listbox3.Items.Objects[0]);
    Showmessage('Object1 ' + hotKeyFunction.getFunctionName);

    hotKeyFunction:=THotKeyFunctionsWindws(listbox3.Items.Objects[1]);
    Showmessage('Object2 ' + hotKeyFunction.getFunctionName);
end;


Leider zeigt er mir:
Object1 StandBy
Object2 StandBy

aber es müsste so heissen:
Object1 StandBy
Object2 LockWindowsXPStation

das heisst doch, dass er dasselbe Objekt hinzugefügt hat? Warum er das?

Hoffe ich konnte mein Problem verständlich machen. Für jede Antwort wäre ich wirklich dankbar.

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Sa 28.06.08 20:39 
Zuallererst würde ich mal den Quelltect entflechten :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure THotKeyFunctionsWindws.setFunction(saction : TStatus);
begin
  case action of
    StandBy                 : begin action:=saction; sfunctionName:=   'StandBy'; end;
    LockWindowsXPStation    : begin action:=saction; sfunctionName:=   'LockStation'; end;
  end;
end;


Richtig einrücken, Exit wegmachen. Also das Übliche. :mrgreen: Also besser z.B. so :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure THotKeyFunctionsWindws.setFunction(saction : TStatus);
begin
  action:=saction;
  case action of
    StandBy              : sfunctionName:= 'StandBy'; 
    LockWindowsXPStation : sfunctionName:= 'LockStation'; 
  end;
end;


Zur Frage an sich : wird wirklich alles erzeugt ? Create etc. Glaube fast nicht. Ich sehe zumindest nicht mal eine Objects-Variable. Was ist das überhaupt für ein komisches Programm ? Da ist ja nicht mal ein Klassen-Var zu sehen. :shock:

_________________
Gruß
Hansa
Nostromo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33
Erhaltene Danke: 1

Win 7 Pro 64bit, Win XP SP3
Delphi 7 Ent
BeitragVerfasst: Sa 28.06.08 22:29 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure THotKeyFunctionsWindws.setFunction(saction : TStatus);  
begin  
  case action of  // ????????
    StandBy                 : begin action:=saction; sfunctionName:=   'StandBy'; end;  
    LockWindowsXPStation    : begin action:=saction; sfunctionName:=   'LockStation'; end;  
  end;  
end;


Woher kommt die Variable "action"?

Ok, gefunden.
Ich schätze action wird standardmäßig auf 0 gesetzt (also auf StandBy) und da ansonsten
keine Zuweisung stattfindet ist der Status immer StandBy.


gruß
Nostromo
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Sa 28.06.08 23:02 
Deine Objekte arbeiten beide auf den selben globalen Variablen:

ausblenden Delphi-Quelltext
1:
2:
3:
var
  sfunctionName : String;
  action        : TStatus;


Das erste Objekt trägt dort seine Daten ein und das zweite überschreibt die
Daten sofort wieder.

Am besten speicherst du die Daten nicht global, sondern als Teil deines Objektes.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
type
  THotKeyFunctionsWindws = class
    public
      constructor Create(saction : TStatus);
      destructor Destroy;
      
      procedure doit();
      procedure setFunction(saction : TStatus);
      function getFunctionName() : String;
    private    
      sfunctionName : String;
      action        : TStatus;

      //Functions
      procedure goStandBy();
      procedure plockWindowsXPStation();
  end;



und hier hast du noch einen kleinen Tippfehler:

ausblenden Delphi-Quelltext
1:
2:
3:
procedure THotKeyFunctionsWindws.setFunction(saction : TStatus);  
begin  
  case saction of
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: So 29.06.08 00:52 
user profile iconSvenAbeln hat folgendes geschrieben:
Deine Objekte arbeiten beide auf den selben globalen Variablen:

ausblenden Delphi-Quelltext
1:
2:
3:
var
  sfunctionName : String;
  action        : TStatus;



String ist ja wohl klar und TStatus ist ein Aufzählungstyp. Aber wo zum Teufel sind denn die Objects ? :shock: Von weiteren Variablen ist weit und breit nichts zu sehen !

_________________
Gruß
Hansa
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: So 29.06.08 10:08 
user profile iconhansa hat folgendes geschrieben:

String ist ja wohl klar und TStatus ist ein Aufzählungstyp. Aber wo zum Teufel sind denn die Objects ? :shock: Von weiteren Variablen ist weit und breit nichts zu sehen !


Ok, ich scheine mich nicht ganz klar ausgedrückt zu haben.
Es werden im Mainform zwei Objekte von der Klasse THotKeyFunctionsWindws erzeugt, diese
Klasse speichert ihre Daten aber nicht als Teil der Objekte, sondern auf globalen Variablen, die von jedem Objekt immer wieder überschrieben werden.

Hier mal Schritt für Schritt was passiert:
  • Erstes Objekt wird erzeugt.
    • Der Konstruktor ruft setFunction(saction); auf
    • setFunction schreibt Werte auf die globalen Variablen.

  • Zweites Objekt wird erzeugt.
    • Der Konstruktor ruft setFunction(saction); auf
    • setFunction schreibt Werte auf die selben globalen Variablen
      und überschreibt dabei die Werte vom ersten Objekt

  • Zum Schluss sollen die Funktionsnamen über Showmessage ausgegeben werden.
    • getFunctionName liest hierfür die globale Variable und liefert natürlich nur den Wert, der zu letzt dort gespeichert wurde (vom zweiten Objekt)
Xelsius Threadstarter
Hält's aus hier
Beiträge: 3

WinXp, Ubuntu
Delphi 2005 Prof.
BeitragVerfasst: So 29.06.08 16:17 
Danke für alle Antworten.

Das was SvenAbeln geschrieben hat, war die Lösung. Nochmals danke für eure Hilfe.