Page 1 of 1

Capture when Maximize button is clicked

PostPosted: Sun Nov 29, 2015 6:41 am
by Marloes
How can I capture when the maximize button is clicked in FireMonkey?

Re: Capture when Maximize button is clicked

PostPosted: Mon Nov 30, 2015 3:50 pm
by rlebeau
AFAIK, FireMonkey does not provide that information. On Windows at least (not sure about other platforms), you can override the Form's virtual CreateHandle() method, calling the base class method first to create the underlying HWND and then subclass it using the Win32 API. Then you would be able to process WM_SYSCOMMAND messages looking for SC_MAXIMIZE notifications.

For example:

Code: Select all
class TMyForm : public TForm
{
    //...
#ifdef _Windows
protected:
    virtual void __fastcall CreateHandle();
private:
    static LRESULT CALLBACK SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR  uIdSubclass, DWORD_PTR dwRefData);
#endif
    //...
};


Code: Select all
#ifdef _Windows
#include <FMX.Platform.Win.hpp>

void __fastcall TMyForm::CreateHandle()
{
    TForm::CreateHandle();

    HWND hWnd = FormToHWND(this);
    // Or:
    // HWND hWnd = WindowHandleToPlatform(Handle)->Wnd;

    SetWindowSubclass(hWnd, &SubclassProc, 1, reinterpret_cast<DWORD_PTR>(this));
}

LRESULT CALLBACK TMyForm::SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR  uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
        case WM_NCDESTROY:
            RemoveWindowSubclass(hWnd, &SubclassProc, uIdSubclass);
            break;

        case WM_SYCOMMAND:
            if ((wParam & 0xFFF0) == SC_MAXIMIZE)
            {
                // do something...

                // if you need to access members of the Form:
                //
                // TMyForm *pThis = reinterpret_cast<TMyForm*>(dwRefData);
                // use pThis as needed...
            }
            break;
    }

    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
#endif