简体   繁体   English

Visual Studio C ++控制台操纵杆

[英]Visual Studio C++ console joystick

I try to compile code from this section 我尝试从本节中编译代码

It said it working C++ console application joystick control mouse movement and mouse button click. 它说它可以在C ++控制台应用程序的操纵杆上控制鼠标的移动和鼠标的单击。 But i get C2061 error undeclared identifier. 但是我得到C2061错误未声明的标识符。

Here the code I modify in C++ console application: 这是我在C ++控制台应用程序中修改的代码:

// TestJoyConsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#ifndef D_INPUT
#define D_INPUT
#include <dinput.h>
#endif

LPDIRECTINPUT8 di;
HRESULT hr;
LPDIRECTINPUTDEVICE8 joystick;
DIDEVICEINSTANCE pdidi;
DIDEVICEINSTANCE info;

BOOL CALLBACK
enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg = (HWND)context;

DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
propRange.lMin = -50;
propRange.lMax = +50;

if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) {
    return DIENUM_STOP;
}

return DIENUM_CONTINUE;
}

BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;

hr = di->CreateDevice(instance->guidInstance, &joystick, NULL);

if (FAILED(hr)) {
    return DIENUM_CONTINUE;
}
return DIENUM_STOP;
}


int _tmain(int argc, _TCHAR* argv[])
{

int counter = 0;
while (counter++ <= 100)
{
    Joy* q1 = new Joy();
    q1 -> start();

    system("PAUSE");
    return 0;
}
}







class Joy
{
public:

HRESULT
    poll(DIJOYSTATE *js)
{
        HRESULT    hr;

        if (joystick == NULL)
        {
            return S_OK;
        }

        // Poll the device to read the current state
        hr = joystick->Poll();
        if (FAILED(hr)) {
            // DInput is telling us that the input stream has been
            // interrupted. We aren't tracking any state between polls, so
            // we don't have any special reset that needs to be done. We
            // just re-acquire and try again.
            hr = joystick->Acquire();
            while (hr == DIERR_INPUTLOST) {
                hr = joystick->Acquire();
            }

            // If we encounter a fatal error, return failure.
            if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) {
                return E_FAIL;
            }

            // If another application has control of this device, return successfully.
            // We'll just have to wait our turn to use the joystick.
            if (hr == DIERR_OTHERAPPHASPRIO) {
                return S_OK;
            }
        }
        // Get the input's device state
        if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE), js))) {
            return hr; // The device should have been acquired during the Poll()
        }

        return S_OK;
    }
void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;

    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();

    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);

    horizontal = desktop.right;
    vertical = desktop.bottom;
}

void moveMouse(int dx, int dy)
{
    POINT pt;
    int horizontal = 0;
    int vertical = 0;

    GetDesktopResolution(horizontal, vertical);

    GetCursorPos(&pt);

    pt.x += dx;
    pt.y += dy;

    if (pt.x < 0)
    {
        pt.x = 0;
    }
    if (pt.x > horizontal)
    {
        pt.x = horizontal;
    }

    if (pt.y < 0)
    {
        pt.y = 0;
    }
    if (pt.y > vertical)
    {
        pt.y = vertical;
    }

    SetCursorPos(pt.x, pt.y);
}

void clickMouse()
{
    if (GetKeyState(VK_LBUTTON) >= 0)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    }
}
void unclickMouse()
{
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

void close()
{
    if (joystick)
    {
        joystick->Unacquire();
    }
}

int start()
{
    DIJOYSTATE js;                                        // struktura stanu joysticka

    // Create a DirectInput device
    if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
        IID_IDirectInput8, (VOID**)&di, NULL))) {
        return hr;
    }

    if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,
        NULL, DIEDFL_ATTACHEDONLY))) {
        return hr;
    }

    // sprawdzenie czy jest joystick
    if (joystick == NULL) {

        std::cout << "Joystick not found.\n";
        system("pause");
        return E_FAIL;
    }

    // ustawienia
    DIDEVCAPS capabilities;

    // zdefiniowanie formatu danych urzadzenia
    if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick)))
    {
        return hr;
    }

    // Powiazanie urzadzenia z oknem aplikacji
    if (FAILED(hr = joystick->SetCooperativeLevel(GetConsoleWindow(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) {
        return hr;
    }

    // wczytanie ustawien joysticka
    capabilities.dwSize = sizeof(DIDEVCAPS);
    if (FAILED(hr = joystick->GetCapabilities(&capabilities))) {

        return hr;
    }

    // wyliczanie
    if (FAILED(hr = joystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS))) {
        return hr;
    }

    info.dwSize = sizeof(DIDEVICEINSTANCE);
    if (FAILED(hr = joystick->GetDeviceInfo(&info)))
    {
        return hr;
    }
    int i = 0;
    while (i < MAX_PATH && info.tszProductName[i] != 0)
    {
        std::cout << (char)info.tszProductName[i];
        i++;
    }
    std::cout << std::endl;
    system("pause");

    while (1)
    {
        poll(&js);

        if (js.rgbButtons[0] != 0)

            clickMouse();

        else
            unclickMouse();
        //clickMouse(js.rgbButtons[0]);

        for (int i = 0; i < 11; i++)
        {
            if (js.rgbButtons[i] != 0) std::cout << "Przycisk " << i + 1 << std::endl;
        }


        std::cout << "X: " << js.lX << std::endl;
        std::cout << "Y: " << js.lY << std::endl;
        std::cout << "Z: " << js.lZ << std::endl;

        moveMouse(js.lX, js.lY);
        printf("A\n");

        //Sleep(400);
        std::cout << std::endl;
        system("cls");
    }

    close();

    system("pause");
}
};

The operating system is windows 7 ultimate x32bit, compile with visual studio c++ 2013 操作系统是Windows 7 Ultimate x32bit,可使用Visual Studio C ++ 2013进行编译

Error message: Error 1 error C2065: 'Joy' : undeclared identifier c:\\users\\amidisglobal\\documents\\visual studio 2013\\projects\\test_joyconcolse\\testjoyconsole\\testjoyconsole\\testjoyconsole.cpp 62 1 TestJoyConsole 错误消息:错误1错误C2065:'Joy':未声明的标识符c:\\ users \\ amidisglobal \\ documents \\ visual studio 2013 \\ projects \\ test_joyconcolse \\ testjoyconsole \\ testjoyconsole \\ testjoyconsole.cpp 62 1 TestJoyConsole

Error 2 error C2065: 'q1' : undeclared identifier c:\\users\\amidisglobal\\documents\\visual studio 2013\\projects\\test_joyconcolse\\testjoyconsole\\testjoyconsole\\testjoyconsole.cpp 62 1 TestJoyConsole 错误2错误C2065:'q1':未声明的标识符c:\\ users \\ amidisglobal \\ documents \\ Visual Studio 2013 \\ projects \\ test_joyconcolse \\ testjoyconsole \\ testjoyconsole \\ testjoyconsole.cpp 62 1 TestJoyConsole

Error 3 error C2061: syntax error : identifier 'Joy' c:\\users\\amidisglobal\\documents\\visual studio 2013\\projects\\test_joyconcolse\\testjoyconsole\\testjoyconsole\\testjoyconsole.cpp 62 1 TestJoyConsole 错误3错误C2061:语法错误:标识符'Joy'c:\\ users \\ amidisglobal \\ documents \\ Visual Studio 2013 \\ projects \\ test_joyconcolse \\ testjoyconsole \\ testjoyconsole \\ testjoyconsole.cpp 62 1 TestJoyConsole

Error 4 error C2065: 'q1' : undeclared identifier c:\\users\\amidisglobal\\documents\\visual studio 2013\\projects\\test_joyconcolse\\testjoyconsole\\testjoyconsole\\testjoyconsole.cpp 63 1 TestJoyConsole 错误4错误C2065:'q1':未声明的标识符c:\\ users \\ amidisglobal \\ documents \\ Visual Studio 2013 \\ projects \\ test_joyconcolse \\ testjoyconsole \\ testjoyconsole \\ testjoyconsole.cpp 63 1 TestJoyConsole

Error 5 error C2227: left of '->start' must point to class/struct/union/generic type c:\\users\\amidisglobal\\documents\\visual studio 2013\\projects\\test_joyconcolse\\testjoyconsole\\testjoyconsole\\testjoyconsole.cpp 63 1 TestJoyConsole 错误5错误C2227:'->开始'的左侧必须指向类/结构/联合/通用类型c:\\ users \\ amidisglobal \\ documents \\ visual studio 2013 \\ projects \\ test_joyconcolse \\ testjoyconsole \\ testjoyconsole \\ testjoyconsole.cpp 63 1 TestJoyConsole

EDIT: 编辑:

// TestJoyConsole.cpp : Defines the entry point for the console application. // TestJoyConsole.cpp:定义控制台应用程序的入口点。 // //

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <InitGuid.h>
#ifndef D_INPUT
#define D_INPUT
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#endif

LPDIRECTINPUT8 di;
HRESULT hr;
LPDIRECTINPUTDEVICE8 joystick;
DIDEVICEINSTANCE pdidi;
DIDEVICEINSTANCE info;

BOOL CALLBACK
enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg = (HWND)context;

DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
propRange.lMin = -50;
propRange.lMax = +50;

if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) {
    return DIENUM_STOP;
}

return DIENUM_CONTINUE;
}

BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;

hr = di->CreateDevice(instance->guidInstance, &joystick, NULL);

if (FAILED(hr)) {
    return DIENUM_CONTINUE;
}
return DIENUM_STOP;
}



class Joy
{
public:

HRESULT
    poll(DIJOYSTATE *js)
{
        HRESULT    hr;

        if (joystick == NULL)
        {
            return S_OK;
        }

        // Poll the device to read the current state
        hr = joystick->Poll();
        if (FAILED(hr)) {
            // DInput is telling us that the input stream has been
            // interrupted. We aren't tracking any state between polls, so
            // we don't have any special reset that needs to be done. We
            // just re-acquire and try again.
            hr = joystick->Acquire();
            while (hr == DIERR_INPUTLOST) {
                hr = joystick->Acquire();
            }

            // If we encounter a fatal error, return failure.
            if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) {
                return E_FAIL;
            }

            // If another application has control of this device, return successfully.
            // We'll just have to wait our turn to use the joystick.
            if (hr == DIERR_OTHERAPPHASPRIO) {
                return S_OK;
            }
        }
        // Get the input's device state
        if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE), js))) {
            return hr; // The device should have been acquired during the Poll()
        }

        return S_OK;
    }
void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;

    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();

    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);

    horizontal = desktop.right;
    vertical = desktop.bottom;
}

void moveMouse(int dx, int dy)
{
    POINT pt;
    int horizontal = 0;
    int vertical = 0;

    GetDesktopResolution(horizontal, vertical);

    GetCursorPos(&pt);

    pt.x += dx;
    pt.y += dy;

    if (pt.x < 0)
    {
        pt.x = 0;
    }
    if (pt.x > horizontal)
    {
        pt.x = horizontal;
    }

    if (pt.y < 0)
    {
        pt.y = 0;
    }
    if (pt.y > vertical)
    {
        pt.y = vertical;
    }

    SetCursorPos(pt.x, pt.y);
}

void clickMouse()
{
    if (GetKeyState(VK_LBUTTON) >= 0)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    }
}
void unclickMouse()
{
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

void close()
{
    if (joystick)
    {
        joystick->Unacquire();
    }
}

int start()
{
    DIJOYSTATE js;                                        // struktura stanu joysticka

    // Create a DirectInput device
    if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
        IID_IDirectInput8, (VOID**)&di, NULL))) {
        return hr;
    }

    if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,
        NULL, DIEDFL_ATTACHEDONLY))) {
        return hr;
    }

    // sprawdzenie czy jest joystick
    if (joystick == NULL) {

        std::cout << "Joystick not found.\n";
        system("pause");
        return E_FAIL;
    }

    // ustawienia
    DIDEVCAPS capabilities;

    // zdefiniowanie formatu danych urzadzenia
    if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick)))
    {
        return hr;
    }

    // Powiazanie urzadzenia z oknem aplikacji
    if (FAILED(hr = joystick->SetCooperativeLevel(GetConsoleWindow(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) {
        return hr;
    }

    // wczytanie ustawien joysticka
    capabilities.dwSize = sizeof(DIDEVCAPS);
    if (FAILED(hr = joystick->GetCapabilities(&capabilities))) {

        return hr;
    }

    // wyliczanie
    if (FAILED(hr = joystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS))) {
        return hr;
    }

    info.dwSize = sizeof(DIDEVICEINSTANCE);
    if (FAILED(hr = joystick->GetDeviceInfo(&info)))
    {
        return hr;
    }
    int i = 0;
    while (i < MAX_PATH && info.tszProductName[i] != 0)
    {
        std::cout << (char)info.tszProductName[i];
        i++;
    }
    std::cout << std::endl;
    system("pause");

    while (1)
    {
        poll(&js);

        if (js.rgbButtons[0] != 0)

            clickMouse();

        else
            unclickMouse();
        //clickMouse(js.rgbButtons[0]);

        for (int i = 0; i < 11; i++)
        {
            if (js.rgbButtons[i] != 0) std::cout << "Przycisk " << i + 1 << std::endl;
        }


        std::cout << "X: " << js.lX << std::endl;
        std::cout << "Y: " << js.lY << std::endl;
        std::cout << "Z: " << js.lZ << std::endl;

        moveMouse(js.lX, js.lY);
        printf("A\n");

        //Sleep(400);
        std::cout << std::endl;
        system("cls");
    }

    close();

    system("pause");
}
};



int _tmain(int argc, _TCHAR* argv[])
{

    int counter = 0;
    while (counter++ <= 100)
    {
        Joy* q1 = new Joy();
        q1->start();
        delete q1;

        system("PAUSE");
        return 0;
    }
}

Now get Error LNK2019: 现在出现错误LNK2019:

Error 102 error LNK2019: unresolved external symbol _DirectInput8Create@20 referenced in function "public: int __thiscall Joy::start(void)" (?start@Joy@@QAEHXZ) 错误102错误LNK2019:在函数“ public:int __thiscall Joy :: start(void)”中引用了未解析的外部符号_DirectInput8Create @ 20(?start @ Joy @@ QAEHXZ)

Error 103 error LNK2001: unresolved external symbol _c_dfDIJoystick 错误103错误LNK2001:无法解析的外部符号_c_dfDIJoystick

Error 104 error LNK1120: 2 unresolved externals 错误104错误LNK1120:2个未解决的外部

You declared your class Joy after trying to use it in _tmain . 您尝试在_tmain使用类后声明了Joy类。 Either move the definition up earlier, or move the declaration into a header file, and include it at the top of your main cpp file. 可以将定义上移,或者将声明移到头文件中,并将其包括在主cpp文件的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM