Muhammad Raza
Muhammad Raza

Reputation: 907

Press Button automatically?

I press button one time. And after that i want press button automatically. I try this code. But not working. Button variable name is ok.

VOID CALLBACK timerCallback(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
    {
        CkTimerDlg *box = (CkTimerDlg *)AfxGetMainWnd();
        CString test = _T("Button Not Clicked");
        box->testBox1.AddString(test);

        HWND hwdButton = ::GetDlgItem(box->ok, IDOK);
        ::PostMessage(box->ok , WM_COMMAND, MAKELONG(IDOK, BN_CLICKED), (LPARAM)
    hwdButton);

    }

    void SomeFunc()
    {
        SetTimer(NULL, 1, 1000, timerCallback);

        /*MSG msg;

        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }*/
    }


    void CkTimerDlg::OnBnClickedOk()
    {
        CString button = _T("Button Clicked");
        testBox1.AddString(button);
        SomeFunc();
    }

Any idea how to do that?

Upvotes: 2

Views: 642

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50778

Forget your timerCallback function

You need this:

In the class definition of CkTimerDlg add this méthod

void OnTimer(UINT nIDEvent);

Add this method to the CkTimerDlg class:

void CTESTDLGDlg::OnTimer(UINT nIDEvent) 
{
    OnBnClickedOk();
    CDialog::OnTimer(nIDEvent);
}

In the message map of CkTimerDlg:

BEGIN_MESSAGE_MAP(CTESTDLGDlg, CDialog)
    //{{AFX_MSG_MAP(CTESTDLGDlg)
    ...
    ON_WM_TIMER()   // <-- add this
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

In CkTimerDlg::OnInitDialog add:

SetTimer(1,2000, NULL);   // will call OnTimer after 2000 milliseconds

You don't need to call SetTimerin somefunc.

Upvotes: 3

Related Questions