⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enjoystickdlg.cpp

📁 手柄驱动DEMO程序源码
💻 CPP
字号:
// EnjoyStickDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EnjoyStick.h"
#include "EnjoyStickDlg.h"
#include "DIJoystick.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CDIJoystick myJoystick1;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEnjoyStickDlg dialog

CEnjoyStickDlg::CEnjoyStickDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEnjoyStickDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEnjoyStickDlg)
	m_Down = FALSE;
	m_left = FALSE;
	m_LSpeed = _T("");
	m_Right = FALSE;
	m_RSpeed = _T("");
	m_Up = FALSE;
	m_XAxis = _T("");
	m_YAxis = _T("");
	m_nPort = 3;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEnjoyStickDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEnjoyStickDlg)
	DDX_Control(pDX, IDC_JOYSTICK_NAME, m_ctlJoystickName);
	DDX_Control(pDX, IDC_BUTTON_NAMES, m_ctlButtonNames);
	DDX_Check(pDX, IDC_DOWN, m_Down);
	DDX_Check(pDX, IDC_LEFT, m_left);
	DDX_Text(pDX, IDC_LSPEED, m_LSpeed);
	DDX_Check(pDX, IDC_RIGHT, m_Right);
	DDX_Text(pDX, IDC_RSPEED, m_RSpeed);
	DDX_Check(pDX, IDC_UP, m_Up);
	DDX_Text(pDX, IDC_XAXIS, m_XAxis);
	DDX_Text(pDX, IDC_YAXIS, m_YAxis);
	DDX_Text(pDX, IDC_PORT, m_nPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEnjoyStickDlg, CDialog)
	//{{AFX_MSG_MAP(CEnjoyStickDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//ON_CBN_EDITCHANGE(IDC_COMBO1, OnSelchangeJoystickName)
	ON_CBN_EDITCHANGE(IDC_BUTTON_NAMES, OnSelchangeButtonNames)
	ON_BN_CLICKED(IDC_XPSYSDEBUG, OnXpsysdebug)
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_JOYSTICK_NAME, OnSelchangeJoystickName)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEnjoyStickDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

//DEL void CEnjoyStickDlg::OnSysCommand(UINT nID, LPARAM lParam)
//DEL {
//DEL 	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
//DEL 	{
//DEL 		CAboutDlg dlgAbout;
//DEL 		dlgAbout.DoModal();
//DEL 	}
//DEL 	else
//DEL 	{
//DEL 		CDialog::OnSysCommand(nID, lParam);
//DEL 	}
//DEL }

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEnjoyStickDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEnjoyStickDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEnjoyStickDlg::OnSelchangeJoystickName() 
{
	// TODO: Add your control notification handler code here
	OnSelchangeButtonNames();
}

void CEnjoyStickDlg::OnSelchangeButtonNames() 
{
	// TODO: Add your control notification handler code here
	CWnd*jb=this;
	myJoystick1.SetHWND(jb->m_hWnd);

	LPCDIDEVICEINSTANCE lpddi=NULL;
	
	m_ctlButtonNames.ResetContent();
	
	int x=m_ctlJoystickName.GetCurSel();
	
	lpddi=(LPCDIDEVICEINSTANCE)m_ctlJoystickName.GetItemDataPtr(x);

	GUID myguid;

	if(lpddi && ((int)lpddi!=-1))
	{

		memcpy(&myguid,&(lpddi->guidInstance),sizeof(GUID));
		
		//bool flag=myJoystick1.CreateDevice(&myguid);
		myJoystick1.SetPreferredDevice(&myguid);

		m_Buttons=myJoystick1.HowManyButtons();

		TCHAR* name=NULL;
		name=myJoystick1.GetFirstButtonName();

		while(name)
		{
			m_ctlButtonNames.AddString(name);
			name=myJoystick1.GetNextButtonName();
		}
	}

	m_ctlButtonNames.SetCurSel(0);	
}


void CEnjoyStickDlg::OnXpsysdebug() 
{
	// TODO: Add your control notification handler code here
    myJoystick1.RunControlPanel();	
}

//DEL void CEnjoyStickDlg::RunControlPanel()
//DEL {
//DEL 
//DEL }

//DEL void CEnjoyStickDlg::OnButton1()
//DEL {
//DEL     myJoystick1.RunControlPanel();
//DEL }

//DEL void CEnjoyStickDlg::OnTimer(UINT nIDEvent)
//DEL {
//DEL  
//DEL }

void CEnjoyStickDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
    myJoystick1.PollDevice();
	if(myJoystick1.IsJoystickLeft()) m_left=true; else m_left=false;
	if(myJoystick1.IsJoystickRight()) m_Right=true; else m_Right=false;
	if(myJoystick1.IsJoystickUp()) m_Up=true; else m_Up=false;
	if(myJoystick1.IsJoystickDown()) m_Down=true; else m_Down=false;
	//if(myJoystick1.IsJoystickFire()) m_Fire=true; else m_Fire=false;

	LPDIJOYSTATE2 joy=myJoystick1.GetJoystickStateInfo();

	m_XAxis.Format("%d",joy->lX);
	m_YAxis.Format("%d",joy->lY);
    lspeed=int((joy->lX-joy->lY)/4);
	rspeed=int((-joy->lY-joy->lX)/4);
	m_LSpeed.Format("%d",lspeed);
    m_RSpeed.Format("%d",rspeed);

	m_Cmd.SetBothMotorsSpeed(lspeed,rspeed);
	//m_ZAxis.Format("%d",joy->lZ);

	//m_RXAxis.Format("%d",joy->lRx);
	//m_RYAxis.Format("%d",joy->lRy);
	//m_RZAxis.Format("%d",joy->lRz);


	/*m_FireText.Empty();

	for(int i=0;i<m_Buttons;i++)
	{
		if(myJoystick1.IsJoystickFire(i))
		{
			CString temp;
			temp.Format("Fire Button : %d\r\n",i);
			m_FireText+=temp;
		}
	}*/

	UpdateData(FALSE);

	CDialog::OnTimer(nIDEvent);
}

void CEnjoyStickDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
    UpdateData();
	CString cs;
	cs.Format("端口%d已打开!",m_nPort);
	MessageBox(cs);
	m_Com.SetCmd(&m_Cmd);
	m_Com.Create(m_nPort);


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -