Autor Beitrag
Andy090778
Hält's aus hier
Beiträge: 10



BeitragVerfasst: So 24.07.05 13:30 
Hallo zusammen,

ich weiß nicht so recht, ob das hier das richtige Forum ist - aber nun zum Problem:

Ich möchte eine neue Klasse anlegen, welche ein WindowsHandle besitzt, welches von anderen Objekten ansprechbar ist. Darüber hinaus möchte ich in dieser Klasse benutzerdefinierte Windows Messages auswerten und diese dann ähnlich einem OnClick-Ereignis dem Nutzer zur Verfügung stellen.

Nun ist das Problem:
Wenn ich meine Klasse aus der Klasse TComponent ableite, habe ich leider kein Handle.

Wenn ich meine Klasse aus der Klasse TButton ableite, habe ich zwar ein Handle, bekomme aber beim Platzieren des Objektes im eine Form diese Fehlermeldung:
    Element '' hat kein übergeordnetes Fenster.


Mir fehlt noch irgendwas bei der Initialisierung, denn wenn ich das Objekt platziere, während es aus TComponent abgeleitet ist, und dann die Ableitung in TButton ändere funktioniert alles...

Kann mir dazu jemand helfen?

Hier der Code:

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:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
unit TaskIcon;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ShellApi, menus, stdctrls;

const
  WM_NOTIFYICON = WM_USER + 15;

type
  TTaskIconOption = (tio_TIP, tio_ICON, tio_MESSAGE);
  TTaskIconOptions = set of TTaskIconOption;

  //TTaskIcon = class(TComponent)
  TTaskIcon = class(TButton)
  private
      FWindowProc: TWndMethod;

    NID: NOTIFYICONDATA;
    //FHandle: THandle;
    FIcon: TIcon;
    FIconID: Integer;
    FIconText: String;
    FIconCallBackHandle: THandle;
    FIconActivateAppOnLClick: Boolean;
    FIconShowpopupOnRClick: Boolean;
    FIconPopupMenu: TPopupMenu;
    FIconOptions: TTaskIconOptions;
    FOnMouseDblClick: TNotifyEvent;
    function IsIconStored: Boolean;
    procedure SetIcon(Value: TIcon);
    procedure SetOptions(Value: TTaskIconOptions);
    procedure SetNID;
  published
    //property Handle: THandle read FHandle write FHandle;
    property Icon: TIcon read FIcon write SetIcon stored IsIconStored;
    property IconID: Integer read FIconID write FIconID;
    property IconText: String read FIconText write FIconText;
    property IconCallBackHandle: THandle read FIconCallBackHandle write FIconCallBackHandle;
    property IconActivateAppOnLClick: Boolean read FIconActivateAppOnLClick
      write FIconActivateAppOnLClick default True;
    property IconShowpopupOnRClick: Boolean read FIconShowpopupOnRClick
      write FIconShowpopupOnRClick default True;
    property IconPopupMenu: TPopupMenu read FIconPopupMenu write FIconPopupMenu;
    property IconOptions: TTaskIconOptions read FIconOptions write SetOptions
      default [tio_TIP, tio_ICON, tio_MESSAGE];
    property OnMouseDblClick: TNotifyEvent read FOnMouseDblClick write FOnMouseDblClick;
  public
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
    function AddIcon: Bool;
    function ModifyIcon: Bool;
    function DeleteIcon: Bool;
    property WindowProc: TWndMethod read FWindowProc write FWindowProc;
    procedure WMNI(var Mes: TMessage); message WM_NOTIFYICON;
    procedure WndProc(var Message: TMessage);
  end;

procedure Register;

implementation


procedure Register;
begin
  RegisterComponents('Sonstiges', [TTaskIcon]);
end;

function TTaskIcon.IsIconStored: Boolean;
begin
  Result := (FIcon.Handle <> 0);
end;

procedure TTaskIcon.SetIcon(Value: TIcon);
begin
  FIcon.Assign(Value);
end;

procedure TTaskIcon.SetOptions(Value: TTaskIconOptions);
begin
  if FIconOptions <> Value then
  begin
    FIconOptions := Value;
  end;
end;

procedure TTaskIcon.WMNI(var Mes: TMessage);
var
  Point: TPoint;
begin
  GetCursorPos(Point);

  if Mes.LParam = WM_LBUTTONUP then
  begin
    if Assigned(OnMouseUp) then
      OnMouseUp(Self, mbLeft, [], Point.X, Point.Y);

    if FIconActivateAppOnLClick then
    begin
      ShowWindow(Application.Handle, SW_RESTORE);
      SetForegroundWindow(Application.Handle);
    end;
  end;
  if Mes.LParam = WM_RBUTTONUP then
  begin
    if Assigned(OnMouseUp) then
      OnMouseUp(Self, mbRight, [], Point.X, Point.Y);

    if FIconShowpopupOnRClick then
    begin
      if Assigned(FIconPopupMenu) then
        FIconPopupMenu.Popup(Point.X,Point.Y);
    end;
  end;

  if Mes.LParam = WM_LBUTTONDOWN then
  begin
    if Assigned(OnMouseDown) then
      OnMouseDown(Self, mbLeft, [], Point.X, Point.Y);
  end;
  if Mes.LParam = WM_RBUTTONDOWN then
  begin
    if Assigned(OnMouseDown) then
      OnMouseDown(Self, mbRight, [], Point.X, Point.Y);
  end;

  if Mes.LParam = WM_LBUTTONDBLCLK then
  begin
    if Assigned(FOnMouseDblClick) then
      FOnMouseDblClick(Self);
  end;

end;

procedure TTaskIcon.SetNID;
begin
  NID.cbSize := SizeOf(NID);
  NID.Wnd := IconCallBackHandle;
  NID.uID := FIconID;
  NID.uCallbackMessage := WM_NOTIFYICON;
  NID.hIcon := FIcon.Handle;
  NID.uFlags := 0;
  if tio_TIP     in FIconOptions then
    NID.uFlags := NID.uFlags or NIF_TIP;
  if tio_ICON    in FIconOptions then
    NID.uFlags := NID.uFlags or NIF_ICON;
  if tio_MESSAGE in FIconOptions then
    NID.uFlags := NID.uFlags or NIF_MESSAGE;
  lstrcpyn(NID.szTip, PChar(FIconText), Length(NID.szTip));
end;

function TTaskIcon.AddIcon: Bool;
begin
  SetNID;
  Result := Shell_NotifyIcon(NIM_ADD, @NID);
end;

function TTaskIcon.ModifyIcon: Bool;
begin
  SetNID;
  Result := Shell_NotifyIcon(NIM_MODIFY, @NID);
end;

function TTaskIcon.DeleteIcon: Bool;
begin
  SetNID;
  Result := Shell_NotifyIcon(NIM_DELETE, @NID);
end;

procedure TTaskIcon.WndProc(var Message: TMessage);
begin
  //ShowMessage('');
end;

constructor TTaskIcon.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FWindowProc := WndProc;

  //Parent := TwinControl(Application);

  //ControlStyle := [csSetCaption, csOpaque, csDoubleClicks];
  //Width := 75;
  //Height := 25;
  //TabStop := True;

  //create icon
  FIcon := TIcon.Create;

  //set properties
  IconCallBackHandle := Handle;
  Visible := False;
  IconOptions := [tio_TIP,tio_ICON,tio_MESSAGE];
  FIconActivateAppOnLClick := True;
  FIconShowpopupOnRClick := True;
end;

destructor TTaskIcon.Destroy;
begin
  inherited Destroy;

  //free icon
  FIcon.Free;

  //reset properties
  IconCallBackHandle := 0;
end;

initialization

finalization

end.
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: So 24.07.05 14:10 
Ableiten von TWinControl :)

P.S.: :welcome:

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
Andy090778 Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: So 24.07.05 22:55 
Wenn ich TButton mit TWinControl ersetze ändert sich nichts an der Fehlermeldung...

Stimmt was mit meiner Ableitung an der Klasse nicht?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: So 24.07.05 22:57 
Zeile 183: Kommentar (//) weg. Fällt mir so spontan auf ;)

AXMD
andy09071978
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33

Win XP, Win 2000
D7 Prof
BeitragVerfasst: So 24.07.05 23:09 
hatte ich auch schon...

Das Problem ist ja eher, dass ich noch was registrieren muss?!

Das Ereignis Create tritt ja erst während der Laufzeit auf - und nicht wenn man versucht die Komponente auf die Form zu ziehen...

:-)
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: So 24.07.05 23:12 
user profile iconandy09071978 hat folgendes geschrieben:
Das Ereignis Create tritt ja erst während der Laufzeit auf - und nicht wenn man versucht die Komponente auf die Form zu ziehen...


Falsch. Der Konstruktor, den du überschreibst, wird sehr wohl auf in der IDE aufgerufen.

AXMD
andy09071978
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33

Win XP, Win 2000
D7 Prof
BeitragVerfasst: Mo 25.07.05 22:06 
Hm,

wenn ich den Kommentar in der Zeile wegmache, kommt eine Zugriffsverletzung beim Lesen der Adresse 0

... da muss also noch was fehlen ... :(
andy09071978
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33

Win XP, Win 2000
D7 Prof
BeitragVerfasst: Mo 25.07.05 22:40 
habs,

hatte anstatt Application.MainForm nur Application angegeben.

Danke