I have developed two unit files (.pas) and i have 2 different components
the first one is MyPCButton and second is MyPanel
MyPCButton.pas uses MyPanel.pas
The Problem is when i try to put them in seperate packages, when i install the MyPCButton component which is using MyPanel.pas, the package installs both of them in the same package, if i install the MyPanel.pas first then MyPCButton package refuses the install and says "couldn't create output file for MyPanel.bpl in the package output directory"
I have placed MyPanel in interface section and i have placed it in implementation section but i still get the same error,
What i wanna do is install them sepeerately in their own packages
MyPCButton.pas :
unit MyPCButton;
interface
uses
Winapi.Windows,Winapi.Messages,System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls,
Vcl.Imaging.PngImage,Vcl.Graphics,Types,cxGraphics;
type
TMyPCButton=class (TCustomControl)
private
FCaption:TCaption;
FIcon:TPngImage;
FIconIndex:integer;
FIconList:TcxImageList;
FIconWidth:integer;
FIconLeftMargin,FIconRightMargin:integer;
FCloseIconList:TcxImageList;
FCloseIconWidth:integer;
FCloseIconLeftMargin,FCloseIconRightMargin:integer;
FFont:TFont;
FColorDefault,FColorDefaultFont:TColor;
FColorHover,FColorHoverFont:TColor;
FColorActive,FColorActiveFont:TColor;
FCaptionWidth:integer;
FMaximumCaptionWidth:integer;
FState:Byte;
FCurCloseIconState:integer;
FActive: Boolean;
FBuffer: TBitmap;
R3:TRect;
FOnClick,FOnCloseClick: TNotifyEvent;
FOnActivate,FOnDeactivate:TNotifyEvent;
FFocused:Boolean;
FGroupNo: integer;
procedure SetIconIndex(const Value:Integer);
procedure SetIconList(const Value:TcxImageList);
procedure SetCaption(const Value: TCaption);
procedure SetCloseIconList(const Value: TcxImageList);
procedure SetAutoSize;
procedure WndProc(var Message: TMessage); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
procedure SetActive(const Value: Boolean);
procedure SwapBuffers;
procedure CheckGroupNo;
protected
procedure Paint; override;
property Canvas;
procedure DoEnter; override;
procedure DoExit; override;
procedure KeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetTotalWidth:integer;
function GetLeftSpace:integer;
function GetRightSpace:integer;
procedure SetPositionInPanel;
procedure SetActiveAfterClose;
published
property Caption:TCaption read FCaption write SetCaption;
property IconIndex:integer Read FIconIndex write SetIconIndex;
property IconList:TcxImageList Read FIconList write SetIconList;
property CloseIconList:TcxImageList Read FCloseIconList write SetCloseIconList;
property Active:Boolean read FActive write SetActive;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property OnCloseClick: TNotifyEvent read FOnCloseClick write FOnCloseClick;
property OnActivate: TNotifyEvent read FOnActivate write FOnActivate;
property OnDeActivate: TNotifyEvent read FOnDeactivate write FOnDeactivate;
property GroupNo:integer read FGroupNo write FGroupNo;
property TabStop;
property Align;
end;
procedure Register;
implementation
uses Math,pvalues,pfunctions,MyPanel;
function IsIntInInterval(x, xmin, xmax: integer): boolean; inline;
begin
IsIntInInterval := (xmin <= x) and (x <= xmax);
end;
function PointInRect(const Point: TPoint; const Rect: TRect): boolean; inline;
begin
PointInRect := IsIntInInterval(Point.X, Rect.Left, Rect.Right) and
IsIntInInterval(Point.Y, Rect.Top, Rect.Bottom);
end;
procedure Register;
begin
RegisterComponents('MyComponents', [TMyPCButton]);
end;
procedure TMyPCButton.CheckGroupNo;
var
i:integer;
begin
for i:=0 to Parent.ControlCount-1 do
begin
if (Parent.Controls[i] is TMyPCButton) then
begin
if ((Parent.Controls[i] as TMyPCButton).Active) and
((Parent.Controls[i] as TMyPCButton).Name<>Self.Name) and
((Parent.Controls[i] as TMyPCButton).GroupNo=Self.GroupNo)
then
(Parent.Controls[i] as TMyPCButton).Active:=False;
end;
end;
end;
constructor TMyPCButton.Create(AOwner: TComponent);
begin
inherited;
if _V_RegValuesInitated=false then
_P_RegValuesInitate;
FFocused:=false;
FBuffer := TBitmap.Create;
Height:=30;
Width:=50;
FFont:=TFont.Create;
FFont.Assign(R_BtnTB.VFont);
FBuffer.Canvas.Font.Assign(FFont);
FState:=0;
FCurCloseIconState:=-1;
FIconIndex:=-1;
FIconWidth:=16;
FIconLeftMargin:=5;
FIconRightMargin:=5;
FCloseIconWidth:=17;
FCloseIconLeftMargin:=16;
FCloseIconRightMargin:=6;
FCaptionWidth:=0;
FMaximumCaptionWidth:=160;
OnKeyDown:=KeyDown;
end;
destructor TMyPCButton.Destroy;
begin
inherited;
FreeAndNil(FIcon);
FreeAndNil(FBuffer);
end;
procedure TMyPCButton.DoEnter;
begin
inherited;
if FActive=false then
FState:=1;
FFocused:=true;
paint;
end;
procedure TMyPCButton.DoExit;
begin
inherited;
if FActive=false then
FState:=0
else
FState:=2;
FFocused:=false;
paint;
end;
function TMyPCButton.GetLeftSpace: integer;
begin
Result:=Parent.Left;
end;
function TMyPCButton.GetRightSpace: integer;
begin
Result:=GetTotalWidth-Parent.Width;
end;
function TMyPCButton.GetTotalWidth: integer;
begin
Result:=Self.Left+Self.Width;
end;
procedure TMyPCButton.SetPositionInPanel;
var
TotalWidth,LeftSpace,RightSpace:integer;
begin
if (Owner is TMyPanel) then
begin
if (Owner as TMyPanel).Parent is TMyPanel then
begin
LeftSpace:=GetLeftSpace;
if LeftSpace<0 then
LeftSpace:=LeftSpace*-1;
RightSpace:=GetRightSpace;
TotalWidth:=GetTotalWidth;
if (TotalWidth-LeftSpace)<Self.Width then
Parent.Left:=Parent.Left+(((TotalWidth-LeftSpace)-Self.Width)*-1)
else
if TotalWidth-LeftSpace>(Parent).Parent.Width then
begin
Parent.Left:=Parent.Left-(TotalWidth-LeftSpace-(Parent).Parent.Width);
end;
end;
end;
end;
procedure TMyPCButton.SetActiveAfterClose;
var
VControlCount,VPosition:integer;
begin
if (Parent is TMyPanel) then
begin
VControlCount:=Parent.ControlCount;
if VControlCount>1 then
begin
for VPosition:=0 to VControlCount-1 do
begin
if (Parent.Controls[VPosition] as TMyPCButton).Name=Self.Name then
break;
end;
if VPosition+1=Parent.ControlCount then
(Parent.Controls[VPosition-1] as TMyPCButton).Active:=true
else
(Parent.Controls[VPosition+1] as TMyPCButton).Active:=true;
end;
end;
end;
procedure TMyPCButton.KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if FActive=false then
SetActive(True);
if (Key=13) and (Assigned(FOnClick)) then FOnClick(Self);
paint;
end;
procedure TMyPCButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
var
SelfWidth:integer;
begin
inherited;
if (FActive=false) and PointInRect(point(X,Y),R3)=false then
begin
FState:=2;
SetActive(True);
paint;
end;
if PointInRect(point(X,Y),R3) then
begin
if (Assigned(FOnCloseClick)) then FOnCloseClick(Self);
SelfWidth:=Self.Width;
Width:=0;
Parent.Width:=Parent.Width-SelfWidth;
if FActive then
SetActiveAfterClose;
Self.Destroy;
end
else
begin
if (Assigned(FOnClick)) then FOnClick(Self);
end;
end;
procedure TMyPCButton.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
inherited;
if PointInRect(point(X,Y),R3)=false then
begin
if FState-1<>FCurCloseIconState then
begin
FCurCloseIconState:=FState-1;
paint;
end;
end
else
begin
if FState+1<>FCurCloseIconState then
begin
FCurCloseIconState:=FState+1;
paint;
end;
end;
end;
procedure TMyPCButton.Paint;
var
R2,R4:TRect;
ColorBackground,ColorFont:TColor;
begin
inherited;
if FBuffer.Canvas.Font.Name<>R_BtnTB.VFont.Name then
begin
FBuffer.Canvas.Font.Name:=R_BtnTB.VFont.Name;
end;
if FBuffer.Canvas.Font.Size<>R_BtnTB.VFont.Size then
begin
FBuffer.Canvas.Font.Size:=R_BtnTB.VFont.Size;
end;
if FBuffer.Canvas.Font.Quality<>R_BtnTB.VFont.Quality then
begin
FBuffer.Canvas.Font.Quality:=R_BtnTB.VFont.Quality;
end;
FBuffer.SetSize(Width,Height);
if FState=0 then
begin
ColorBackground:=R_BtnTB.DefaultColor;
ColorFont:=R_BtnTB.DefaultFontColor
end;
if FState=1 then
begin
ColorBackground:=R_BtnTB.HoverColor;
ColorFont:=R_BtnTB.HoverFontColor
end;
if FState=2 then
begin
ColorBackground:=R_BtnTB.ActiveColor;
ColorFont:=R_BtnTB.ActiveFontColor
end;
FBuffer.Canvas.Brush.Color:=ColorBackground;
FBuffer.Canvas.Font.Color:=ColorFont;
FBuffer.Canvas.FillRect(ClientRect);
if ((Assigned(FIconList)) and (FIconIndex>-1)) then
begin
FIconList.Draw(FBuffer.Canvas,FIconLeftMargin,(ClientHeight div 2)-(FIconList.Height div 2),FIconIndex);
end;
R2.Top:=(ClientHeight div 2)-(FBuffer.Canvas.TextHeight(FCaption) div 2);
R2.Height:=ClientHeight;
R2.Left:=FIconLeftMargin+FIconWidth+FIconRightMargin;
R2.Width:=FCaptionWidth;
DrawText(FBuffer.Canvas.Handle, PChar(FCaption), -1, R2,DT_LEFT);
if Assigned(FCloseIconList) then
begin
R3.Top:=0;
R3.Left:=ClientWidth-FCloseIconWidth;
R3.Height:=ClientHeight;
R3.Width:=FCloseIconWidth;
FCloseIconList.Draw(FBuffer.Canvas,R3.Left+(FCloseIconList.Width div 2),(R3.Height div 2)-(FCloseIconList.Height div 2),FCurCloseIconState);
end;
if FFocused then
begin
R4.Top:=1;
R4.Left:=1;
R4.Width:=ClientWidth-2;
R4.Height:=ClientHeight-2;
DrawFocusRect(FBuffer.Canvas.Handle,R4);
end;
SwapBuffers;
end;
procedure TMyPCButton.SetActive(const Value: Boolean);
var
MyPoint:TPoint;
begin
if FActive<>Value then
begin
FActive := Value;
if FActive then
begin
CheckGroupNo;
FState:=2;
SetPositionInPanel;
if Assigned(FOnActivate) then FOnActivate(Self);
end
else
begin
MyPoint := ScreenToClient(Mouse.CursorPos);
if PtInRect(ClientRect, MyPoint) then
FState:=1
else
FState:=0;
if Assigned(FOnDeactivate) then FOnDeactivate(Self);
end;
paint;
end;
end;
procedure TMyPCButton.SetAutoSize;
begin
FCaptionWidth:=FBuffer.Canvas.TextWidth(FCaption);
if FCaptionWidth>160 then
FCaptionWidth:=FMaximumCaptionWidth;
Width:=FIconLeftMargin+FIconWidth+FIconRightMargin+FCaptionWidth+FCloseIconLeftMargin+FCloseIconWidth-12+FCloseIconRightMargin;
end;
procedure TMyPCButton.SetCaption(const Value: TCaption);
begin
inherited;
FCaption := Value;
SetAutoSize;
paint;
end;
procedure TMyPCButton.SetCloseIconList(const Value: TcxImageList);
begin
FCloseIconList := Value;
paint;
end;
procedure TMyPCButton.SetIconIndex(const Value: Integer);
begin
FIconIndex:=Value;
paint;
end;
procedure TMyPCButton.SetIconList(const Value: TcxImageList);
begin
FIconList:=Value;
paint;
end;
procedure TMyPCButton.WndProc(var Message: TMessage);
begin
inherited;
case Message.Msg of
CM_MOUSEENTER:
begin
if FActive=False then
begin
FState:=1;
Paint;
end;
end;
CM_MOUSELEAVE:
begin
if FActive=False then
begin
FState:=0;
FCurCloseIconState:=-1;
Paint;
end
else
begin
FState:=2;
FCurCloseIconState:=-1;
Paint;
end;
end;
WM_ERASEBKGND:
Message.Result := 1;
end;
end;
procedure TMyPCButton.SwapBuffers;
begin
BitBlt(Canvas.Handle, 0, 0, Width, Height, FBuffer.Canvas.Handle, 0, 0, SRCCOPY);
end;
end.
MyPanel.pas
unit MyPanel;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls;
type
TMyPanel = class(TPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TMyPanel]);
end;
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TMyPanel.Destroy;
begin
inherited;
end;
end.
There is nothing particularly special about what you are trying to do but your implementation and the way you describe installing pas files into packages suggests that you are perhaps not clear on how to go about it.
First of all, it has long been considered bad practice to combine components and IDE registration in the same package.
You should implement your components in a Runtime-only Package (or packages). You then have a Design-Time Package which includes the corresponding Runtime package in it's requires list.
The Design-Time Package also contains (typically) a single unit which uses the units containing your components and implements the Register function.
You then install the Design-Time Package into the IDE, which will load the Runtime Packages as needed to register the components.
Within that overall approach, you still have a number of options for how to organise your packages.
You could have separate runtime packages for each control and separate designtime packages to install each control separately, but it sounds like you will then have a lot of dependencies between your packages which can quickly become problematic to unravel and create dependencies in the order in which you build and install your packages.
In your case since your two components have this dependency on each other, it sounds like it would make most sense to keep those components in one single runtime package and have a single designtime package to install all the components from that runtime package.
If you really wanted to you could still have separate design-time packages to install each control individually from that single runtime package, but I really don't see any advantage in doing that in your case (unless there are further complications or consideration which are not apparent from your question).
it was my mistake
i had changed the default dcp output directory and apparently i had to add the new path in library path, so now i am able to put/install the .pas files in their own packages
Related
I’m considering switching from the abandoned TCoolTrayIcon to Delphi’s own TTrayIcon. The only thing that I’m missing is OnMouseEnter and OnMouseExit (≘OnMouseLeave) that I need in my case.
Is there an easy way to add these events to TTrayIcon?
(CoolTrayIcon does this with a timer ... I'm not sure if that's really the best solution)
Although nobody seemed to be really interested in an actual solution to this question, I guess it would be fair to post my solution anyway, in case somebody else ever searches for the same problem.
It was way less coding than I initially expected.
unit TrayIconEx;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.ExtCtrls;
type
TTrayIconEx = class(TTrayIcon)
private
CursorPosX, CursorPosY: Integer;
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
EnterLeaveTimer: TTimer;
procedure EnterLeaveEvent(Sender: TObject);
protected
procedure WindowProc(var Msg: TMessage); override;
procedure MouseEnter; virtual;
procedure MouseLeave; virtual;
public
constructor Create(AOwner: TComponent); override;
published
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Custom', [TTrayIconEx]);
end;
constructor TTrayIconEx.Create(AOwner: TComponent);
begin
inherited;
EnterLeaveTimer := TTimer.Create(Self);
EnterLeaveTimer.Enabled := False;
EnterLeaveTimer.Interval := 200;
EnterLeaveTimer.OnTimer := EnterLeaveEvent;
end;
procedure TTrayIconEx.WindowProc(var Msg: TMessage);
var
p: TPoint;
begin
if Assigned(FOnMouseEnter) or Assigned(FOnMouseLeave) then
begin
if (Msg.Msg = WM_SYSTEM_TRAY_MESSAGE) and (Msg.lParam = WM_MOUSEMOVE) then
begin
GetCursorPos(p);
CursorPosX := p.X;
CursorPosY := p.Y;
if not EnterLeaveTimer.Enabled then
MouseEnter();
end;
end;
inherited WindowProc(Msg);
end;
procedure TTrayIconEx.EnterLeaveEvent(Sender: TObject);
var
p: TPoint;
begin
//Win7+ supports Shell_NotifyIconGetRect(), but to support Vista and XP a workaround is required.
//-> If the position differs from the last captured position in MouseMove, then the cursor was moved away.
GetCursorPos(p);
if (CursorPosX <> p.X) or (CursorPosY <> p.Y) then
MouseLeave();
end;
procedure TTrayIconEx.MouseEnter;
begin
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self);
EnterLeaveTimer.Enabled := True;
end;
procedure TTrayIconEx.MouseLeave;
begin
EnterLeaveTimer.Enabled := False;
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self);
end;
end.
If all you want is a more advanced hint then you can Personalization its own hint like any other string with special character such as Enter and etc like this :
edit1.hint:='first row'+#13+'second row'+#13+#13+'last row';
I'm trying to create a VCL component, that lets you insert multiple TImages of different sizes as properties.
I was told to best use a TObjectList ( Delphi component with a variable amount of TPictures ), but now I'm struggling to make the single TPictures assignable in the Property editor.
What i have at the moment: (it compiles)
unit ImageMultiStates;
interface
uses
Vcl.Graphics, Vcl.StdCtrls, System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls, Forms, Generics.Collections;
type
TImageMultiStates = class(TImage)
private
FPictures: TObjectList<TPicture>;
procedure SetPicture(Which: Integer; APicture: TPicture);
function GetPicture(Which: Integer): TPicture;
public
Count: integer;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Activate(Which: Integer);
published
// property Pictures: TObjectList<TPicture> read GetPicture write SetPicture;
// property Pictures[Index: Integer]: TObjectList<TPicture> read GetPicture write SetPicture;
property Pictures: TObjectList<TPicture> read FPictures write FPictures;
end;
procedure Register;
implementation
constructor TImageMultiStates.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPictures := TObjectList<TPicture>.Create;
end;
destructor TImageMultiStates.Destroy;
begin
FPictures.Free;
inherited Destroy;
end;
procedure TImageMultiStates.SetPicture(Which: Integer; APicture: TPicture);
begin
FPictures[Which] := APicture;
if Which=0 then
Picture.Assign(APicture);
end;
function TImageMultiStates.GetPicture(Which: Integer): TPicture;
begin
Result := FPictures[Which];
end;
procedure TImageMultiStates.Activate(Which: Integer);
begin
Picture.Assign(FPictures[Which]);
end;
procedure Register;
begin
RegisterComponents('Standard', [TImageMultiStates]);
end;
end.
What doesn't work is the final result in the PropertyEditor. It shows one single item named "Pictures", with the value "(TObjectList)". Clicking it doesn't do anything, i don't get a proper editor. Other ideas for the line in question have been commented out, they bring other errors:
The first one throws the compiler error "E2008 Incompatible Types", The second one throws "Published property 'Pictures' can not be of type ARRAY".
The IDE has no idea how to edit a TObjectList at design-time, and the DFM streaming system has no idea how to stream a TObjectList. You would have to implement a custom property editor and custom streaming logic. While that is certainly possible, it is a LOT of work.
What you are attempting to do is better handled by using System.Classes.TCollection instead. Both the IDE and the DFM streaming system have built-in support for handling TCollection editing and streaming automatically for you.
Try something more like this:
unit ImageMultiStates;
interface
uses
System.Classes, Vcl.Controls, Vcl.ExtCtrls, Vcl.Graphics;
type
TImagePictureItem = class(TCollectionItem)
private
FPicture: TPicture;
procedure PictureChanged(Sender: TObject);
procedure SetPicture(Value: TPicture);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
published
property Picture: TPicture read FPicture write SetPicture;
end;
TImagePictureEvent = procedure(Sender: TObject; Index: Integer) of object;
TImagePictures = class(TOwnedCollection)
private
FOnPictureChange: TImagePictureEvent;
function GetPicture(Index: Integer): TImagePictureItem;
procedure SetPicture(Index: Integer; Value: TImagePictureItem);
protected
procedure Update(Item: TCollectionItem); override;
public
constructor Create(Owner: TComponent); reintroduce;
property Pictures[Index: Integer]: TImagePictureItem read GetPicture write SetPicture; default;
property OnPictureChange: TImagePictureEvent read FOnPictureChange write FOnPictureChange;
end;
TImageMultiStates = class(TImage)
private
FActivePicture: Integer;
FPictures: TImagePictures;
function GetPicture(Index: Integer): TPicture;
procedure PictureChanged(Sender: TObject; Index: Integer);
procedure SetActivePicture(Index: Integer);
procedure SetPicture(Index: Integer; Value: TPicture);
procedure SetPictures(Value: TImagePictures);
protected
procedure Loaded; override;
public
constructor Create(Owner: TComponent); override;
function Count: integer;
property Pictures[Index: Integer]: TPicture read GetPicture write SetPicture;
published
property ActivePicture: Integer read FActivePicture write SetActivePicture default -1;
property Picture stored False;
property Pictures: TImagePictures read FPictures write SetPictures;
end;
procedure Register;
implementation
{ TImagePictureItem }
constructor TImagePictureItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
end;
destructor TImagePictureItem.Destroy;
begin
FPicture.Free;
inherited;
end;
procedure TImagePictureItem.PictureChanged(Sender: TObject);
begin
Changed(False);
end;
procedure TImagePictureItem.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
end;
{ TImagePictures }
constructor TImagePictures.Create(Owner: TComponent);
begin
inherited Create(Owner, TImagePictureItem);
end;
function TImagePictures.GetPicture(Index: Integer): TImagePictureItem;
begin
Result := TImagePictureItem(inherited GetItem(Index));
end;
procedure TImagePictures.SetPicture(Index: Integer; Value: TImagePictureItem);
begin
inherited SetItem(Index, Value);
end;
procedure TImagePictures.Update(Item: TCollectionItem);
begin
if Assigned(FOnPictureChange) then
begin
if Item <> nil then
FOnPictureChange(Self, Item.Index)
else
FOnPictureChange(Self, -1);
end;
end;
{ TImageMultiStates }
constructor TImageMultiStates.Create(Owner: TComponent);
begin
inherited Create(Owner);
FPictures := TImagePictures.Create(Self);
FPictures.OnPictureChange := PictureChanged;
FActivePicture := -1;
end;
procedure TImageMultiStates.Loaded;
begin
inherited;
PictureChanged(nil, FActivePicture);
end;
function TImageMultiStates.Count: Integer;
begin
Result := FPictures.Count;
end;
procedure TImageMultiStates.PictureChanged(Sender: TObject; Index: Integer);
begin
if (FActivePicture <> -1) and ((Index = -1) or (Index = FActivePicture)) then
Picture.Assign(GetPicture(FActivePicture));
end;
function TImageMultiStates.GetPicture(Index: Integer): TPicture;
begin
Result := FPictures[Index].Picture;
end;
procedure TImageMultiStates.SetPicture(Index: Integer; Value: TPicture);
begin
FPictures[Index].Picture.Assign(Value);
end;
procedure TImageMultiStates.SetActivatePicture(Value: Integer);
begin
if FActivePicture <> Value then
begin
if ComponentState * [csLoading, csReading] = [] then
Picture.Assign(GetPicture(Value));
FActivePicture := Value;
end;
end;
procedure Register;
begin
RegisterComponents('Standard', [TImageMultiStates]);
// the inherited TImage.Picture property is published, and you cannot
// decrease the visibility of an existing property. However, if you move
// this procedure into a separate design-time package, you can then use
// DesignIntf.UnlistPublishedProperty() to hide the inherited
// Picture property at design-time, at least:
//
// UnlistPublishedProperty(TImageMultiStates, 'Picture');
//
// Thus, users are forced to use the TImageMultiStates.Pictures and
// TImageMultiStates.ActivePicture at design-time. The inherited
// Picture property will still be accessible in code at runtime, though...
end;
end.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Hi I'm trying to build an TEdit control with TButton to have Buttoned Edit but the problem is that the text clip gets under the Button and some latter doesn't appear because the button is over it. how to fix that? please note that when I call UpdateEditMargins (which is the procedure to adjust the text clip)
Here is my code:
unit YazButtonedEdit;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Buttons, Messages, Windows, Forms;
type
TYazButtonedEdit = class(TCustomEdit)
private
FEditButton: TBitBtn;
FButtonWidth: Integer;
FButtonVisible: Boolean;
procedure WMSize(var Message: TMessage); message WM_SIZE;
procedure SetButtonVisible(const Value: Boolean);
procedure GetEditButtonClick(const Value: TNotifyEvent);
function SetEditButtonClick: TNotifyEvent;
procedure SetButtonWidth(const Value: Integer);
protected
procedure RefreshButton;
procedure CreateParams(var Params: TCreateParams); override;
procedure WndProc(var Message: TMessage); override;
public
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure UpdateEditMargins;
published
property ButtonWidth: Integer read FButtonWidth write SetButtonWidth;
property ButtonVisible: Boolean read FButtonVisible write SetButtonVisible;
property OnEditButtonClick: TNotifyEvent read SetEditButtonClick write GetEditButtonClick;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('KH-Controls', [TYazButtonedEdit]);
end;
{ TYazButtonedEdit }
constructor TYazButtonedEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle - [csSetCaption];
FEditButton := TBitBtn.Create(self);
with FEditButton do begin
Parent := self;
TabStop := false;
Visible := true;
OnClick := OnEditButtonClick;
end;
end;
procedure TYazButtonedEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;
destructor TYazButtonedEdit.Destroy;
begin
FEditButton.Free;
inherited;
end;
procedure TYazButtonedEdit.GetEditButtonClick(const Value: TNotifyEvent);
begin
FEditButton.OnClick := Value;
end;
procedure TYazButtonedEdit.RefreshButton;
begin
FEditButton.Width := ButtonWidth;
FEditButton.Height := Height - 4;
FEditButton.Visible := ButtonVisible;
UpdateEditMargins;
end;
procedure TYazButtonedEdit.SetButtonVisible(const Value: Boolean);
begin
if FButtonVisible <> Value then
begin
FButtonVisible := Value;
RefreshButton;
end;
end;
procedure TYazButtonedEdit.SetButtonWidth(const Value: Integer);
begin
if FButtonWidth <> Value then
begin
FButtonWidth := Value;
RefreshButton;
end;
end;
function TYazButtonedEdit.SetEditButtonClick: TNotifyEvent;
begin
Result := FEditButton.OnClick;
end;
procedure TYazButtonedEdit.WMSize(var Message: TMessage);
begin
RefreshButton;
end;
procedure TYazButtonedEdit.WndProc(var Message: TMessage);
var
LLeft, LTop: Integer;
begin
case Message.Msg of
CN_CTLCOLORSTATIC,
CN_CTLCOLOREDIT:
if FEditButton.Visible then
begin
LLeft := FEditButton.Left;
LTop := FEditButton.Top;
ExcludeClipRect(Message.WParam, LLeft + 1, LTop + 1,
FEditButton.Width + FEditButton.Left - 1, FEditButton.Height - 1);
end;
end;
inherited;
end;
procedure TYazButtonedEdit.UpdateEditMargins;
var
LMargin, RMargin: Integer;
begin
if HandleAllocated then
begin
LMargin := 0;
RMargin := 0;
if FEditButton.Visible then
LMargin := FEditButton.Width + 2;
SendMessage(Handle, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, MakeLong(LMargin, RMargin));
Invalidate;
end;
end;
procedure TYazButtonedEdit.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
if AComponent = FEditButton then
begin
RefreshButton;
end;
end;
end;
end.
Call it after the edit window is created, ideally in CreateWnd method. So, add the following:
type
TYazButtonedEdit = class(TCustomEdit)
...
protected
procedure CreateWnd; override;
...
end;
implementation
procedure TYazButtonedEdit.CreateWnd;
begin
inherited;
UpdateEditMargins;
end;
I have created a component, TGridPaintBox, based on TPaintBox. It is basically a paintbox with added "grid functionality". It's not a data grid. More like a chess board component.
In the object explorer I can set certain properties. Most importantly I can set the grid dimensions (how many cells across/down), but also options relating to drawing. Whether the cells should be square, the color of odd/even cells etc.
My first version of this component had properties directly on the class, and when I changed a property, the designtime drawing was updated immediately. As the component grew, I wanted to organize my properties a little better, and introduced some "options properties", like drawing options, behaviour options etc. After introducing this, the designtime drawing no longer updates like before. After changing a property, I have to click on the component for it to update. Can anyone tell me why this happens?
Here's a stripped down version of the code. I hope it will explain the behaviour:
(PS: This is my first component, even though I've been using Delphi since 1997, so if anyone can spot anything stupid in the way I've done it, please feel free to tell me)
unit GridPaintBox;
interface
type
TGridDrawOption = (gdoSquareCells,gdoCenterCells,gdoDrawCellEdges,gdoDrawFocus);
TGridDrawOptions = set of TGridDrawOption;
TGridOptions = class(TPersistent)
private
FCellsX : integer;
FCellsY : integer;
FDrawOptions : TGridDrawOptions;
public
constructor Create(aGridPaintBox : TGridPaintBox);
procedure Assign(Source : TPersistent); override;
published
property CellsX : integer read FCellsX write FCellsX;
property CellsY : integer read FCellsY write FCellsY;
property DrawOptions : TGridDrawOptions read FDrawOptions write FDrawOptions;
end;
TGridPaintBox = class(TPaintBox)
private
FGridOptions : TGridOptions;
FFocusedX,
FFocusedY : integer;
FOnFocusChanged: TNotifyEvent;
procedure CalculateSizeAndPosition;
procedure DrawCell(X,Y : integer);
procedure DrawCells;
procedure SetGridOptions(const Value: TGridOptions);
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
public
constructor Create(aOwner : TComponent); override;
destructor Destroy; override;
procedure Paint; override;
procedure SetFocus(X,Y : integer);
published
property OnFocusChanged : TNotifyEvent read FOnFocusChanged write FOnFocusChanged;
property Options : TGridOptions read FGridOptions write SetGridOptions;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TGridPaintBox]);
end;
procedure TGridPaintBox.CalculateSizeAndPosition;
begin
<...>
end;
constructor TGridPaintBox.Create(aOwner: TComponent);
begin
inherited;
FGridOptions := TGridOptions.Create(self);
end;
procedure TGridPaintBox.DrawCell(X, Y: integer);
begin
<...>
end;
procedure TGridPaintBox.DrawCells;
var
X,Y : integer;
begin
CalculateSizeAndPosition;
for Y := 0 to FGridOptions.CellsY-1 do
for X := 0 to FGridOptions.CellsX-1 do
DrawCell(X,Y);
end;
procedure TGridPaintBox.Paint;
begin
Canvas.Font := Font;
Canvas.Brush.Color := Color;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(Rect(0,0,Width,Height));
DrawCells;
if Assigned(OnPaint) then
OnPaint(Self);
end;
procedure TGridPaintBox.SetGridOptions(const Value: TGridOptions);
begin
FGridOptions.Assign(Value);
invalidate;
end;
procedure TGridPaintBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
SetFocus(PixelToCellX(X),PixelToCellY(Y));
inherited;
end;
procedure TGridPaintBox.SetFocus(X, Y: integer);
begin
if (FocusedX=X) and (FocusedY=Y) then
exit;
FFocusedX := X;
FFocusedY := Y;
if assigned(OnFocusChanged) then
OnFocusChanged(self);
invalidate;
end;
constructor TGridOptions.Create(aGridPaintBox : TGridPaintBox);
begin
FCellsX := 20;
FCellsY := 8;
FDrawOptions := [gdoSquareCells,gdoCenterCells,gdoDrawCellEdges];
end;
procedure TGridOptions.Assign(Source : TPersistent);
begin
if Source is TGridOptions then
begin
FCellsX := TGridOptions(Source).CellsX;
FCellsY := TGridOptions(Source).CellsY;
FDrawOptions := TGridOptions(Source).DrawOptions;
end
else
inherited;
end;
end.
It happens because you don't have a setter for the options set which would invalidate your control which belongs to. The click in the form designer invokes the control to invalidate though, but you should handle this by your own in such options setter. So I would store the options owner for better access to the direct owner class instance and in the options setter force this owner, the control to redraw:
type
TGridPaintBox = class;
TGridDrawOption = (gdoSquareCells, gdoCenterCells, gdoDrawCellEdges, gdoDrawFocus);
TGridDrawOptions = set of TGridDrawOption;
TGridOptions = class(TPersistent)
private
FOwner: TGridPaintBox;
FCellsX: Integer;
FCellsY: Integer;
FDrawOptions: TGridDrawOptions;
procedure SetCellsX(AValue: Integer);
procedure SetCellsY(AValue: Integer);
procedure SetDrawOptions(const AValue: TGridDrawOptions);
public
constructor Create(AOwner: TGridPaintBox);
procedure Assign(ASource: TPersistent); override;
published
property CellsX: Integer read FCellsX write SetCellsX;
property CellsY: Integer read FCellsY write SetCellsY;
property DrawOptions: TGridDrawOptions read FDrawOptions write SetDrawOptions;
end;
implementation
constructor TGridOptions.Create(AOwner: TGridPaintBox);
begin
FOwner := AOwner;
FCellsX := 20;
FCellsY := 8;
FDrawOptions := [gdoSquareCells, gdoCenterCells, gdoDrawCellEdges];
end;
procedure TGridOptions.SetCellsX(AValue: Integer);
begin
if FCellsX <> AValue then
begin
FCellsX := AValue;
FOwner.Invalidate;
end;
end;
procedure TGridOptions.SetCellsY(AValue: Integer);
begin
if FCellsY <> AValue then
begin
FCellsY := AValue;
FOwner.Invalidate;
end;
end;
procedure TGridOptions.SetDrawOptions(const AValue: TGridDrawOptions);
begin
if FDrawOptions <> AValue then
begin
FDrawOptions := AValue;
FOwner.Invalidate;
end;
end;
Further notes:
If you don't explicitly need to have paint box' published properties and events like for instance Color, Font or OnPaint event, derive your control from TGraphicControl instead of from TPaintBox. You can choose what properties and events will you publish by your own.
I have created a custom control TOuterControl that is the parent for multiple TInnerControls.
Everything is working fine except that the IDE is creating icons for each the child TInnerControl's (InnerControl1 and InnerControl2 in the screenshot). How do I prevent the IDE from generating the icons?
unit TestControl;
interface
Procedure Register;
implementation
Uses
Classes,
Controls,
SysUtils,
DesignEditors,
DesignIntf,
VCLEditors;
Type
TOuterControl = Class;
TInnerControl = Class(TComponent)
Protected
FOuterControl : TOuterControl;
function GetParentComponent: TComponent; Override;
Function HasParent : Boolean; Override;
procedure SetParentComponent (Value: TComponent); Override;
End;
TOuterControl = Class(TCustomControl)
Protected
FInnerControls : TList;
Procedure Paint; Override;
Public
Constructor Create(AOwner : TComponent); Override;
Procedure AddInnerControl(AInnerControl : TInnerControl);
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
End;
TOuterControlEditor = Class(TDefaultEditor)
Public
Procedure ExecuteVerb(Index : Integer); Override;
Function GetVerb (Index : Integer) : String; Override;
Function GetVerbCount : Integer; Override;
End;
procedure TOuterControl.AddInnerControl(AInnerControl: TInnerControl);
begin
AInnerControl.FOuterControl := Self;;
FInnerControls.Add(AInnerControl);
Invalidate;
end;
constructor TOuterControl.Create(AOwner: TComponent);
begin
inherited;
FInnerControls := TList.Create;
end;
procedure TOuterControl.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
I : Integer;
begin
inherited;
For I := 0 To FInnerControls.Count - 1 Do
Proc(FInnerControls[I]);
end;
procedure TOuterControl.Paint;
begin
inherited;
Canvas.FillRect(ClientRect);
Canvas.TextOut(0,0, Format('Inner Control Count = %d', [FInnerControls.Count]));
end;
function TInnerControl.GetParentComponent: TComponent;
begin
Result := FOuterControl;
end;
function TInnerControl.HasParent: Boolean;
begin
Result := True;
end;
procedure TInnerControl.SetParentComponent(Value: TComponent);
begin
If Value Is TOuterControl Then
If FOuterControl <> Value Then
Begin
FOuterControl := TOuterControl(Value);
FOuterControl.AddInnerControl(Self);
End;
end;
procedure TOuterControlEditor.ExecuteVerb(Index: Integer);
Var
OuterControl : TOuterControl;
InnerControl : TInnerControl;
begin
inherited;
OuterControl := TOuterControl(Component);
If Index = 0 Then
Begin
InnerControl := TInnerControl.Create(OuterControl.Owner);
OuterControl.AddInnerControl(InnerControl);
End;
end;
function TOuterControlEditor.GetVerb(Index: Integer): String;
begin
Result := 'Add Inner';
end;
function TOuterControlEditor.GetVerbCount: Integer;
begin
Result := 1;
end;
Procedure Register;
Begin
RegisterComponents('AA', [TOuterControl]);
RegisterComponentEditor(TOuterControl, TOuterControlEditor);
End;
Initialization
Classes.RegisterClasses([TInnerControl]);
end.
You can prevent them from appeaing on the form with:
RegisterNoIcon([TInnerControl]);
More info on RegisterNoIcon can be found at http://docwiki.embarcadero.com/VCL/e/index.php/Classes.RegisterNoIcon
It's a little confusing having classes with a name that end with "Control" that aren't normal visual controls though.
If TInnerControl is meant to be used only inside a TOuterControl, then you should call SetSubComponent(True) during/after the TInnerControl's creation.
When you create the inner controls, you tell them that their owner is the form (the owner of the outer control). Therefore, the form draws them, just like it draws all the other components it owns. You probably want the outer control to own the inner ones.