Create a VCL Button Component with sound - c++builder

I'm trying to create a custom VCL component that can have a custom sound played when clicked. The sound is accessed from a resource DLL.
Question is, the sound is played using Direct X, and for it to be created it need a valid window handle.
My naive initial test was to initialize the sound in the components constructor:
__fastcall TArrayBotButton::TArrayBotButton(TComponent* Owner)
: TBitBtn(Owner),
FSoundID("BUTTON_CLICK_1")
{
initABCoreLib();
HWND hWnd = Application->MainFormHandle;
mSound.Create(FSoundID.c_str(), hWnd);
}
but the above does not work, as the MainFormHandle at this point is NULL. Also, being a component, using the Application variable is probably not safe in the constructor(?).
Any pointers on where to initialize the sound?

Why not just use the button's own HWND instead? TBitBtn is a windowed control. Its HWND is not available in the constructor, but you can (and should) override the button's virtual CreateWnd() method to create the DirectX object, and override the virtual DestroyWnd() method to release it. This way you also account for window recreations during the button's lifetime.

Related

Firemonkey TFrame - how to fire ancestor TFrame events?

I created a base TFrame class, eg TBasicFrame
There is one button called btnTest, which do a simple task
showmessage('test');
On another TForm or TFrame, I place this TBasicFrame on it. However, when I click this btnTest on this or TForm/TFrame which has an instance of TBasicFrame on it, the showmessage does not run.
What must I do to ensure that all code in the ancestor frame will execute? That means the showmessage('test') will run even when new instances of TBasicFrame is placed anywhere.
Any advice?
I think, you overrided this event by simple double click on button (on object of your Frame) in IDE. So you dont have original call event.
Check *.fmx files of TBasicFrame and TForm with object TBasicFrame.

Assign a HWND to an OpenCV named window

I have a win32 child window handle from a win32 application and I want to draw my openCV stuff on that child window. Is there a way to set the HWND of an openCV named window. I only found a function cvGetWindowHandle to get the HWND of an already created named window, but not an equivalent one to set the HWND of a new named window.
Any pointers on how that could be done?
Thanks!

What component to derive my 'TCard' from? (game)

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.

Creating custom Hint window

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.

Is there a Delphi 'joystick' control?

I'd like to align an object in X and Y by eye using the mouse to move it into position. I can roll a solution by using various spin controls (or buttons) but it's messy and I got to wondering whether there is a control - like a joystick - that provides 2-axis control using the mouse and fires events at rates which vary depending on its position?
Afaik Jedi (jedi apilib?) had a joystick header too. It is winapi, not COM, so no TLB involved
Try NLDJoystick, an open-source component written by me and based on pure WinAPI (MMSystem.pas). It is downloadable from GitHub.
The interface:
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property AbsCenter;
property AbsMax;
property AbsMin;
property Active default False;
property Advanced default False;
property AxisCount;
property Axises;
property ButtonCount;
property HasPOV;
property ID;
property OnButtonDown;
property OnButtonUp;
property OnMove;
property OnPOVChanged;
property PollingInterval default 40;
property RepeatButtonDelay default 350;
property RepeatMoveDelay default 350;
property SuspendScreensaver default False;
property ThresholdFactor;
Maybe you can make something like that yourself.
Take a panel, and register on Mouse up, down and move events
On MouseDown, set a boolean (fButtonDown) so you know that the mousebutton is pressed and save the X and Y coordinates of the mousepointer.
On MouseMove, check if a button is down (fButtonDown) and move your object. The more your mousecursor is moved from its saved position, the faster you move.
On MouseUp, set fButtonDown to false
The above is very primitive, but should work.
I Googled for "joystick dll" and found countless. You can probably throw one of these into your project and then access its C API from Delphi by simply writing a TLIB header (or whatever it's called, haven't done this in a while) for it.
You may use a DelphiX components. They are wrappers for DirectX and one of them wraps around DirectX joystick interface as far as I remember. It wraps in a Delphi-style so it is easy to use.

Resources