自动按下按钮?

时间:2015-05-06 08:33:05

标签: c++ visual-studio-2013 mfc

我按一次按钮。之后我想自动按下按钮。 我试试这段代码。但不行。按钮变量名称没问题。

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();
    }

知道怎么做吗?

1 个答案:

答案 0 :(得分:2)

忘记timerCallback功能

你需要这个:

CkTimerDlg的类定义中添加此méthod

void OnTimer(UINT nIDEvent);

将此方法添加到CkTimerDlg类:

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

CkTimerDlg的消息地图中:

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

CkTimerDlg::OnInitDialog添加:

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

您无需在SetTimer中致电somefunc