🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2591

Re: SetTimer in TForm descedant

#2591
From: 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
Technical Details
Message-ID: <45c2321d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45c159aa@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,68809160c683a9df
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <45c159aa@newsgroups.borland.com>
Subject: Re: SetTimer in TForm descedant
Date: Thu, 1 Feb 2007 10:32:27 -0800
Lines: 59
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.34.171
Message-ID: <45c2321d$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1170354717 75.83.34.171 (1 Feb 2007 10:31:57 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail