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.