|
|

楼主 |
发表于 2006-1-25 13:34:00
|
显示全部楼层
Re:[Help!][Help!][Help!]帮我看看这个DirectIput方面的程序为什么
Sorry,附件好像上传不了。
我把源码粘贴在这里:
#include <stdio.h>
#include <dinput.h>
#define INPUTERROR_NODI 0x81000001
#define INPUTERROR_NOKEYBOARD 0x81000002
LPDIRECTINPUT9 pDI = NULL;
LPDIRECTINPUTDEVICE9 pKeyboard = NULL;
const char *szWndClass = "DirectInputKeyboard";
const char *szProgramName = "Direct-Input Keyboard Example";
HINSTANCE g_hInstance;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
void vCleanup(void);
HRESULT hrInitDirectInput(void);
HRESULT hrInitKeyboard(HWND hWnd);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
HRESULT hr;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MsgProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWndClass;
RegisterClass(&wc);
hWnd = CreateWindowEx(WS_EX_TOPMOST,
szWndClass,
szProgramName,
WS_OVERLAPPEDWINDOW,
0,
0,
640,
480,
NULL,
NULL,
hInstance,
NULL);
g_hInstance = hInstance;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hr = hrInitDirectInput();
if( hr == INPUTERROR_NODI ) {
MessageBox( hWnd, "DirectInput Error", "Unable to initialize Direct Input.", MB_ICONERROR );
vCleanup();
exit(1);
}
hr = hrInitKeyboard(hWnd);
if( hr == INPUTERROR_NOKEYBOARD ) {
MessageBox( hWnd, "DirectInput Error", "Unable to initialize Keyboard.", MB_ICONERROR );
vCleanup();
exit(1);
}
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else {
}
}
vCleanup();
return(msg.wParam);
};
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
void vCleanup(void)
{
if( pKeyboard ) {
pKeyboard->Unacquire();
pKeyboard->Release();
}
if( pDI ) {
pDI->Release();
}
UnregisterClass( szWndClass, g_hInstance );
}
HRESULT hrInitDirectInput(void)
{
HRESULT hReturn = 0;
if( !pDI ) {
if( FAILED( hReturn = DirectInput8Create( g_hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&pDI, NULL ) ) )
return(INPUTERROR_NODI);
}
return(hReturn);
}
HRESULT hrInitKeyboard(HWND hWnd)
{
HRESULT hReturn = 0;
DIPROPDWORD dipdw;
if( FAILED( hReturn = pDI->CreateDevice( GUID_SysKeyboard, &pKeyboard, NULL ) ) )
return(INPUTERROR_NOKEYBOARD);
ZeroMemory(&dipdw,sizeof(DIPROPDWORD));
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = 10;
if( FAILED( hReturn = pKeyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) )
return(INPUTERROR_NOKEYBOARD);
if( FAILED( hReturn = pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
return(INPUTERROR_NOKEYBOARD);
if( FAILED( hReturn = pKeyboard->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND ) ) ) {
return(INPUTERROR_NOKEYBOARD);
}
pKeyboard->Acquire();
return(0);
}
|
|