//-----------------------------------------------------------------------------
// File: Joystick.cpp
//
// Desc: Demonstrates an application which receives immediate
// joystick data in exclusive mode via a dialog timer.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <commctrl.h>
#include <basetsd.h>
#include <dinput.h>
#include "resource.h"
//-----------------------------------------------------------------------------
// Function-prototypes
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext );
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext );
HRESULT InitDirectInput( HWND hDlg );
VOID FreeDirectInput();
HRESULT UpdateInputState( HWND hDlg );
//-----------------------------------------------------------------------------
// Defines, constants, and global variables
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
LPDIRECTINPUT8 g_pDI = NULL;
LPDIRECTINPUTDEVICE8 g_pJoystick = NULL;
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point for the application. Since we use a simple dialog for
// user interaction we don't need to pump messages.
//-----------------------------------------------------------------------------
//int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int )
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
InitCommonControls();
// Display the main dialog box.
DialogBox( hInst, MAKEINTRESOURCE(IDD_JOYST_IMM), NULL, (DLGPROC)MainDlgProc );
//hInstance, lpTemplate, hWndParent, lpDialogFunc
return TRUE;
}
//-----------------------------------------------------------------------------
// Name: MainDialogProc
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_INITDIALOG:
if( FAILED( InitDirectInput( hDlg ) ) )
{
MessageBox( NULL, TEXT("Error Initializing DirectInput"),
TEXT("DirectInput Sample"), MB_ICONERROR | MB_OK );
EndDialog( hDlg, 0 );
}
// Set a timer to go off 30 times a second. At every timer message
// the input device will be read
SetTimer( hDlg, 0, 1000 / 30, NULL );
return TRUE;
case WM_ACTIVATE:
if( WA_INACTIVE != wParam && g_pJoystick )
{
// Make sure the device is acquired, if we are gaining focus.
g_pJoystick->Acquire();
}
return TRUE;
case WM_TIMER:
// Update the input device every timer message
if( FAILED( UpdateInputState( hDlg ) ) )
{
KillTimer( hDlg, 0 );
MessageBox( NULL, TEXT("Error Reading Input State. ") \
TEXT("The sample will now exit."), TEXT("DirectInput Sample"),
MB_ICONERROR | MB_OK );
EndDialog( hDlg, TRUE );
}
return TRUE;
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDCANCEL:
EndDialog( hDlg, 0 );
return TRUE;
}
case WM_DESTROY:
// Cleanup everything
KillTimer( hDlg, 0 );
FreeDirectInput();
return TRUE;
}
return FALSE; // Message not handled
}
//-----------------------------------------------------------------------------
// Name: InitDirectInput()
// Desc: Initialize the DirectInput variables.
//-----------------------------------------------------------------------------
HRESULT InitDirectInput( HWND hDlg )
{
HRESULT hr;
// Register with the DirectInput subsystem and get a pointer
// to a IDirectInput interface we can use.
// Create a DInput object
if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) )
return hr;
// Look for a simple joystick we can use for this sample program.
if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY ) ) )
return hr;
// Make sure we got a joystick
if( NULL == g_pJoystick )
{
MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."),
TEXT("DirectInput Sample"),
MB_ICONERROR | MB_OK );
EndDialog( hDlg, 0 );
return S_OK;
}
// Set the data format to "simple joystick" - a predefined data format
//
// A data format specifies which controls on a device we are interested in,
// and how they should be reported. This tells DInput that we will be
// passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) )
return hr;
// Set the cooperative level to let DInput know how this device should
// interact with the system and with other DInput applications.
if( FAILED( hr = g_pJoystick->SetCooperativeLevel( hDlg, DISCL_EXCLUSIVE |
DISCL_FOREGROUND ) ) )
return hr;
// Enumerate the joystick objects. The callback function enabled user
// interface elements for objects that are found, and sets the min/max
// values property for discovered axes.
if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback,
(VOID*)hDlg, DIDFT_ALL ) ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: EnumJoysticksCallback()
// Desc: Called once for each enumerated joystick. If we find one, create a
// device interface on it so we can play with it.
//-----------------------------------------------------------------------------
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
VOID* pContext )
{
HRESULT hr;
// Obtain an interface to the enumerated joystick.
hr = g_pDI->CreateDevice( pdidInstance->guidInstance, &g_pJoystick, NULL );
// If it failed, then we can't use this joystick. (Maybe the user unplugged
// it while we were in the middle of enumerating it.)
if( FAILED(hr) )
return DIENUM_CONTINUE;
// Stop enumeration. Note: we're just taking the first joystick we get. You
// could store all the enumerated joysticks and let the user pick.
return DIENUM_STOP;
}
//-----------------------------------------------------------------------------
// Name: EnumObjectsCallback()
// Desc: Callback function for enumerating objects (axes, buttons, POVs) on a
// joystick. This function enables user interface
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Joystick.rar (22个子文件)
Joystick
makefile.x64 9KB
joystick.dsp 3KB
resource.h 2KB
joystick.cpp 15KB
joystick.dsw 541B
joystick.gif 5KB
joystick.manifest 681B
joystick.sln 1KB
Debug
joystick.bsc 2.23MB
vc60.pdb 100KB
joystick.sbr 0B
vc60.idb 153KB
joystick.obj 28KB
joystick.pch 5.12MB
joystick.exe 184KB
joystick.ilk 278KB
joystick.pdb 505KB
joystick.plg 930B
joystick.rc 5KB
joystick.vcproj 8KB
joystick.ncb 41KB
joystick.opt 50KB
共 22 条
- 1
资源评论
wangyangtao
- 粉丝: 20
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
- 软件漏洞生命周期管理策略.pptx
- 毕业设计-基于Python实现的的简易气温爬虫,可以爬取全国各市的近7日气温数据
- 软件系统测试报告(实用版).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功