//-----------------------------------------------------------------
//
// DirectInput Sample Program.
//
//-----------------------------------------------------------------
#define INITGUID
#include <stdio.h>
#include <windows.h>
#include <dinput.h>
#include <dinputex.h>
#pragma comment( lib , "dinput8.dll" )
//-----------------------------------------------------------------
// Grobal Variables.
//-----------------------------------------------------------------
LPDIRECTINPUT8 g_lpDI;
LPDIRECTINPUTDEVICE8 g_lpDIDevice;
HWND g_hWnd;
//-----------------------------------------------------------------
// Prototypes.
//-----------------------------------------------------------------
HWND InitApp( HINSTANCE , int );
BOOL InitDirectInput( HWND );
BOOL ReadInput();
BOOL CleanupDirectInput();
LRESULT CALLBACK WndProc( HWND , UINT , WPARAM , LPARAM );
//-----------------------------------------------------------------
// Main.
//-----------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInst , HINSTANCE hPrevinst , LPSTR nCmdLine , int nCmdShow )
{
MSG msg;
g_hWnd = InitApp( hInst , nCmdShow );
if ( !g_hWnd ) return FALSE;
if ( !InitDirectInput( g_hWnd ) ) return FALSE;
while( msg.message != WM_QUIT ){
if ( PeekMessage( &msg , NULL , 0 , 0 , PM_REMOVE ) ){
TranslateMessage( &msg );
DispatchMessage( &msg );
}else{
ReadInput();
}
Sleep( 1 );
}
return msg.wParam;
}
//-----------------------------------------------------------------
// Initialize Application.
//-----------------------------------------------------------------
HWND InitApp( HINSTANCE hInst , int nCmdShow )
{
WNDCLASS wc;
HWND hWnd;
char szClassName[] = "DirectInput Test";
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInst;
wc.hCursor = LoadCursor( NULL , IDC_ARROW );
wc.hIcon = LoadIcon( NULL , IDI_APPLICATION );
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszClassName = szClassName;
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
if ( !RegisterClass( &wc ) ) return FALSE;
hWnd = CreateWindow( szClassName , "Direct3D Test" , WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT , CW_USEDEFAULT , 640 , 480,
NULL , NULL , hInst , NULL );
if ( !hWnd ) return FALSE;
ShowWindow( hWnd , nCmdShow );
UpdateWindow( hWnd );
return hWnd;
}
//-----------------------------------------------------------------
// Initialize DirectInput.
//-----------------------------------------------------------------
BOOL InitDirectInput( HWND hWnd )
{
HINSTANCE hInst;
HRESULT hr;
hInst = (HINSTANCE)GetWindowLong( hWnd , GWL_HINSTANCE );
hr = DirectInput8Create( hInst , DIRECTINPUT_VERSION , IID_IDirectInput8 ,
(void**)&g_lpDI , NULL );
if ( FAILED( hr ) ){
MessageBox( hWnd , "Can't create DirectInput object." , "Error" , MB_OK );
return FALSE;
}
hr = g_lpDI->CreateDevice( GUID_SysKeyboard , &g_lpDIDevice , NULL );
if ( FAILED( hr ) ){
CleanupDirectInput();
MessageBox( hWnd , "Can't create DirectInput device." , "Error" , MB_OK );
return FALSE;
}
hr = g_lpDIDevice->SetDataFormat( &c_dfDIKeyboard );
if ( FAILED( hr ) ){
CleanupDirectInput();
MessageBox( hWnd , "Can't set data format." , "Error" , MB_OK );
return FALSE;
}
hr = g_lpDIDevice->SetCooperativeLevel( hWnd ,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
if ( FAILED( hr ) ){
CleanupDirectInput();
MessageBox( hWnd , "Can't set cooperative level." , "Error" , MB_OK );
return FALSE;
}
if ( g_lpDIDevice ) g_lpDIDevice->Acquire();
return TRUE;
}
//-----------------------------------------------------------------
// Cleanup DirectInput.
//-----------------------------------------------------------------
BOOL CleanupDirectInput()
{
g_lpDIDevice->Unacquire();
if ( g_lpDIDevice != NULL )
g_lpDIDevice->Release();
if ( g_lpDI != NULL )
g_lpDI->Release();
return TRUE;
}
//-----------------------------------------------------------------
// Window Proc.
//-----------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd , UINT msg , WPARAM wp , LPARAM lp )
{
switch( msg ){
case WM_DESTROY:
CleanupDirectInput();
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd , msg , wp , lp );
}
return 0L;
}
//-----------------------------------------------------------------
// Read Input.
//-----------------------------------------------------------------
BOOL ReadInput()
{
char buffer[256];
HRESULT hr;
char titlebar[32];
hr = g_lpDIDevice->GetDeviceState( sizeof( buffer ) , (LPVOID)&buffer );
if ( FAILED( hr ) ) return FALSE;
titlebar[0] = '\0';
if ( buffer[DIK_UP] & 0x80 ) strcat( titlebar , "上" );
if ( buffer[DIK_DOWN] & 0x80 ) strcat( titlebar , "下" );
if ( buffer[DIK_LEFT] & 0x80 ) strcat( titlebar , "左" );
if ( buffer[DIK_RIGHT] & 0x80 ) strcat( titlebar , "右" );
if ( buffer[DIK_RETURN] & 0x80 ) strcat( titlebar , "Enter" );
if ( buffer[DIK_BACK] & 0x80 ) strcat( titlebar , "Back Space" );
SetWindowText( g_hWnd , titlebar );
return TRUE;
}
|