为什么我不能在CALLBACK函数中使用此代码?

时间:2017-04-28 17:37:39

标签: c++ mfc

我希望改变静态文字但我不能

LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
    {
        KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
        int key = pKey->vkCode;
        cout << key << " ";
        if (key == VK_INSERT)
        {
        SetDlgItemText(IDC_STATUSTEXT, "On")
        }
        if (key == VK_DELETE)
        {
            SetDlgItemText(IDC_STATUSTEXT, "Off")
        }
    }
CallNextHookEx(KbHook, nCode, wParam, lParam);
return 0;

}

HHOOK KbHook;

BOOL CUSBDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

SetDlgItemText(IDC_STATUSTEXT, "Successfully Loaded!!!");

cout << "Successfully Loaded !!!" << endl;
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);

HMODULE hInstance = GetModuleHandle(NULL);
KbHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeybdProc, hInstance, 0);

return TRUE;

}

错误&#34;函数不带2个参数&#34;

我能解决吗?

我可以使用此功能

void CUSBDlg::Pray(byte t, int i)

在KeybdProc中?

如果我尝试使用此功能

LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
    {
        KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
        int key = pKey->vkCode;
        cout << key << " ";
        if (key == VK_INSERT)
        {
            SetDlgItemText(IDC_STATUSTEXT, "On")
            Pray(0x04, 6)
        }
        if (key == VK_DELETE)
        {
            SetDlgItemText(IDC_STATUSTEXT, "Off")
        }
    }
CallNextHookEx(KbHook, nCode, wParam, lParam);
return 0;

}

错误&#34;未声明的标识符&#34;

有人帮我解决了这个问题X(

1 个答案:

答案 0 :(得分:1)

您的回调函数不是对话框类的非静态方法,因此它没有this指针可用于调用CWnd::SetDlgItemText()CUSBDlg::Prey()方法。您正在获得一个未声明的标识符&#34; Prey()上的错误,因为该名称的范围内没有函数。你得到的&#34;功能不需要2个参数&#34;错误,因为回调试图调用Win32 API SetDlgItemText()函数,该函数在范围内,但需要3个参数作为输入。

要解决这两个问题,您需要存储一个指向对话框的全局指针,以便回调可以在需要时到达,例如:

CUSBDlg *Dlg;
HHOOK KbHook;

LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
    {
        KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
        int key = pKey->vkCode;
        cout << key << " ";
        if (key == VK_INSERT)
        {
            Dlg->SetDlgItemText(IDC_STATUSTEXT, "On")
            Dlg->Pray(0x04, 6)
        }
        if (key == VK_DELETE)
        {
            Dlg->SetDlgItemText(IDC_STATUSTEXT, "Off")
        }
    }

    CallNextHookEx(KbHook, nCode, wParam, lParam);
    return 0;
}

BOOL CUSBDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    ...

    Dlg = this;

    GetModuleHandle(NULL);
    KbHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeybdProc, hInstance, 0);

    ...
}