🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Alex Liu Thu, 01 Feb 2007 03:09
SetTimer in TForm descedant
#2681
Author: Alex Liu
Date: Thu, 01 Feb 2007 03:09
38 lines
1992 bytes
Hi,

I would like to implement SetTimer() in TForm which will update some 
controls in the form.
I try to write like this but fail:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
SetTimer(Handle, 1, 500, (TIMERPROC)(TimerFcn));
}
//---------------------------------------------------------------------------

TIMERPROC TForm1::TimerFcn(    HWND hwnd, // handle of window for timer 
messages
    UINT uMsg, // WM_TIMER message
    UINT idEvent, // timer identifier
    DWORD dwTime  // current system time
   )
{
    // update control such as captions
}

in header file:
private: // User declarations
TIMERPROC TimerFcn(    HWND hwnd,     UINT uMsg,     UINT idEvent,     DWORD 
dwTime);


when i compile it shows "E2235 member function must be called or its address 
taken"
if i remove the TimerFcn declaration in TForm1 (i.e. TIMERPROC 
TimerFcn(...), i can pass compilation but  cannot access the controls.

what should i do?

Thanks in advance.

Alex 
Re: SetTimer in TForm descedant
#2682
Author: Michel Leunen
Date: Thu, 01 Feb 2007 08:05
19 lines
1627 bytes
Alex Liu wrote:

> when i compile it shows "E2235 member function must be called or its address 
> taken"
> if i remove the TimerFcn declaration in TForm1 (i.e. TIMERPROC 
> TimerFcn(...), i can pass compilation but  cannot access the controls.

You must declare your callback function as 'static'.
Member functions and stand-alone functions are not the same. The formers 
have a 'this' pointer associated with them that do not have the seconds.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: SetTimer in TForm descedant
#2683
Author: Michel Leunen
Date: Thu, 01 Feb 2007 10:44
20 lines
1487 bytes
Michel Leunen wrote:

> You must declare your callback function as 'static'.

Besides, your callback must be declared as:

void CALLBACK TimerFcn(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

and call SetTimer() like this:

SetTimer(Handle, 1, 500, &TimerFcn);

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: SetTimer in TForm descedant
#2591
Author: Remy Lebeau (TeamB)
Date: Thu, 01 Feb 2007 18:32
57 lines
2584 bytes
"Alex Liu" <alexliu@hket.com> wrote in message
news:45c159aa@newsgroups.borland.com...

> I try to write like this but fail:

Asside from the fact that your callback is declared incorrectly to
begin with (and you should be getting additional compiler errors from
the invalid syntax), you can't use a non-static class method as an API
callback directly anyway.  The signatures do not match because of the
hidden 'this' pointer.  To do what you are asking, you shold use the
WM_TIMER message instead of a callback.  In which case, why not just
use the TTimer component instead?  It already wraps SetTimer() for
you:

    __published:
        TTimer *Timer1;
        void __fastcall Timer1Timer(TObject *Sender);


    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        Timer1->Interval = 500;
        Timer1->Enabled = true;
    }

    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
        // update controls as needed...
    }


If you must use a callback, then you will have to declare the method
as static (otherwise, do not declare it as a class method at all) and
then access the form instance via its global pointer, ie:

    private:
        static void CALLBACK TimerFcn(HWND hwnd, UINT uMsg, UINT
idEvent, DWORD dwTime);

    extern TForm1* Form1;


    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        SetTimer(Handle, 1, 500, (TIMERPROC)TimerFcn);
    }

    void CALLBACK TForm1::TimerFcn(HWND hwnd, UINT uMsg, UINT idEvent,
DWORD dwTime)
    {
        // use global Form1 pointer to update controls as needed, ie:
        // Form1->Label1->Caption = ...;
    }


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads