Can someone please explain me which control is better to create custom componens? What is the difference between twincontrol and tcustomcontrol?
Thank you in advance
Can someone please explain me which control is better to create custom componens?
That depends on what kind of component you are making and what its requires are.
Is it visual?
If no, use TComponent.
if yes, does it need its own HWND (input focus, window messages, etc)?
If no, use TGraphicControl.
If yes, does it need to custom paint itself?
if yes, use TCustomControl.
if no, use TWinControl.
What is the difference between twincontrol and tcustomcontrol?
TCustomControl is a TWinControl descendant that adds some additional handling for the WM_PAINT message, on top of what TWinControl does. TCustomControl exposes a public Canvas property that you can draw on. During painting, it enables the csCustomPaint flag in the ControlState property, and then calls a virtual Paint() method that your component can override. So the benefit of TCustomControl is that it makes custom painting a little easier to manage. Nothing more.
Related
I made a graphical component which ancestor has to be TGraphiccontrol because I need to paint to the canvas.
Now I want to convert this component to an activex control but that seems to be only possible when the component is a descendant from TWincontrol.
How do I solve this problem?
It is incorrect that you must only use a TGraphicControl in order to paint. Base your control on TCustomControl instead. This provides you with a canvas which you can use the same way, and is inherited from the TWinControl.
I am trying to make a TCard component for a game. What class should I derive it from?
This is for a card game like MTG or yu gi oh. The card should have a blank image, and when created it will load either a front or back view.
If it loads the front view, it will then have to have a few labels (for properties like power/cost/def/text). Cards must be clickable.
type
TCard = class(zzzzzzzzz)
private
Now once that is done, do I have to add anything to the constructor/destructor? Currently I have:
constructor TCard.Create(AOwner: Tcomponent);
begin
inherited Create(AOwner);
end;
{******************************************************************************}
{ Free any resources allocated to component }
destructor TCard.Destroy;
begin
inherited Destroy;
end;
Also I think I added the onclick parts right but not sure. In the published area I have
{Inherited properties}
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnClick;
property OnDblClick;
etc...
It depends on what you want to do, but typically there are two ancestors for visible controls:
TGraphicControl: This is a descendant of TControl that implements a Canvas for you. You can just override the Paint method to start drawing. Controls like this support mouse interactions, but cannot get keyboard focus.
TCustomControl: This a descendant of TWinControl. It also implements a Canvas and allows you to override the Paint method to draw any content. Because it descends from TWinControl, it actually has a handle and can gain keyboard focus and process keyboard messages.
An other good candidate is TPanel (or rather TCustomPanel). It inherits from TCustomControl, so it has the same graphical properties, but it also contains functionality to draw borders and align child controls. I doubt if you would need this, though.
Of course you can derive directly from TControl or TWinControl, but then you will have to take care of some of this stuff yourself.
Note that it is better to put the actual card game logic in a separate class and only create visual controls for drawing. If you do that, you can still choose whether you want to have separate controls for each card, or you can choose to draw your whole card game on a single control or even directly on the form. I doubt if Windows' card games like Free Cell and Solitaire have over 50 graphics controls.
What i am trying to accomplish is to create new touchkeyboard.
First i created buttons which i derive from speed buttons.
That done, now i need to create multiple buttons and layout them somewhere. This is were i get stuck.
I created a new component which i derive from TGraphicControl (this should be my new touchkeyboard), but i don't know how to add components to canvas. I actually don't know whether i'm supposed to add them to canvas or to some other component (eg. panel)!?!
Is my approach OK?
Thanks in advance.
If you're creating a custom visual control, you need to create the buttons and position them manually. For example:
TOnScreenKeyboard = class(TWinControl)
public
constructor Create(AOwner: TComponent);
end;
[...]
constructor TOnScreenKeyboard.Create(AOwner : TComponent)
var
TempButton : TSpeedButton;
begin
inherited;
TempButton := TSpeedButton.Create(self);
TempButton.Parent := self;
TempButton.Top := 10;
TempButton.Left := 15;
TempButton.Caption := 'A';
end;
You can put the button creation into a loop and position each one according to where it should be.
(I wrote this off the top of my head, and I don't write a lot of Pascal anymore, so there may be some minor mistakes! But it should get you started.)
Because of your wording and confusion between Panel, Canvas and custom controls in general, I assume you're a Delphi beginner. You need to learn about frames: embarcadero docwiki link on frames
Frames allow you to create re-usable portions of GUI. You use the IDE to "draw" the frame, you can then place that composite control (the frame) onto forms or other frames. It's a very powerful feature and it's conceptually very close to what other languages call "custom controls" (very close to what asp.net or WPF consider a custom control to be).
In the Delphi world, when you say "custom control", people would normally expect you to want to create an reusable control that's placed in a package and it's installed in the IDE. It's an fairly advanced subject. If that's what you want then I misunderstood the question, sorry.
I'm trying to find a way to use my 2nd form as a hint window for a component (for example a TLabel) in my 1st form.
At the moment, I'm exploring the use of THintWindow and HintWindowClass, but it is not possible to directly assign a TForm to HintWindowClass. Some examples I've seen so far use a TBitmap which is then drawn on the THintWindow.Canvas, which is not bad, but I'd still like to use some kind of integrated automatic mechanism.
Another solution that crossed my mind is to manually implement this functionality using OnMouseEnter, OnMouseMove and OnMouseLeave events of the said Tlabel.
If there actually is a way to "assign" a TForm to HintWindowClass, I'd like to ask if anyone can provide a code snippet illustrating this. Thanks.
THintWindow is a descendant of TCustomControl. TForm is not a descendant of either of those classes, so you cannot assign any TForm class to HintWindowClass. Hint windows need to descend from THintWindow. Anything you can put on a form you can also put on a THintWindow. You'll just have to instantiate it manually and assign its Parent property to make it appear.
The closest you can probably get to "visually" designing a hint window is to design a frame. Make your THintWindow descendant create an instance of the frame, and then override ActivateHint (and ActivateHintData, if you need the data) to forward the hint text and desired size to your frame.
I'm using Delphi and I'm building my own label component with class TControl.
Before I paint the text according to the properties (such as caption, font, etc.) I want to clear paint rect like there is nothing at the place of component. I mean I want to make it like a glass so that the other components behind it will be displayed; and then paint the text. What should I do to paint other components that are placed behind my label to it?
To do that, you need to do nothing. :-)
When you make a transparent label-like component, you best use the TGraphicControl base class. This is actually little more than a canvas to paint on. Whenever the content should be changed, you call the Invalidate method to repaint your control. This will call the Paint method that you can override. With every repaint, your control will be clear and transparent, except for the parts where you draw stuff in your Paint method.
Unless you override and disable the background painting, then you dont need to do anything. It depends on what base-class you go for. Although you can simply use (in the Paint() method):
Canvas.Brush.Style:=bsSolid;
Canvas.Brush.Color:=self.Color; //If you have a public color property
Canvas.FillRect(ClientRect);
You should also read up on TControlCanvas. Here is a website that deals with this topic more in depth: http://www.delphidabbler.com/tips/75