How to get the HWND of mfc DialogBox for .Net
-
Can you show you dialog class declaration ? Because, if its Cdialog derived class, then this error should not come.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Oh dear. I'm really not able to understand, why you are getting this problem. Now, pass first parameter
this
instead of m_hWnd. To avoid compile error.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Oh dear. I'm really not able to understand, why you are getting this problem. Now, pass first parameter
this
instead of m_hWnd. To avoid compile error.Prasad Notifier using ATL | Operator new[],delete[][^]
-
I change the m_hWnd with this. if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return error C2673: 'InitDirectInput' : global functions do not have 'this' pointers I got this error! Please help!
Dear,dear.
LaHaHa wrote:
error C2673: 'InitDirectInput' : global functions do not have 'this' pointers
I was insisting on complete information from start of thread. Again , this error indicates that, code you have pasted is of a global function. And obviously you could not use this pointer there. I was assuming this code is used in one of your dialog function. For using it through global function, you need to have instance of dialog class. Which you can pass as first parameter of said function. Otherwise, post code of function body of
InitDirectInput
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Dear,dear.
LaHaHa wrote:
error C2673: 'InitDirectInput' : global functions do not have 'this' pointers
I was insisting on complete information from start of thread. Again , this error indicates that, code you have pasted is of a global function. And obviously you could not use this pointer there. I was assuming this code is used in one of your dialog function. For using it through global function, you need to have instance of dialog class. Which you can pass as first parameter of said function. Otherwise, post code of function body of
InitDirectInput
.Prasad Notifier using ATL | Operator new[],delete[][^]
In fact that the function code is only like this: HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), TEXT("DirectInput Sample"), MB_ICONERROR | MB_OK ); return 0; return S_OK; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; So how to solve this problem! Please help!
-
In fact that the function code is only like this: HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), TEXT("DirectInput Sample"), MB_ICONERROR | MB_OK ); return 0; return S_OK; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; So how to solve this problem! Please help!
Come on ! Where is dialog class instance ? If dialog is already invoked then use
AfxGetMainWnd
, to get its handle.LaHaHa wrote:
if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr;
g_pJoystick->SetCooperativeLevel(AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Come on ! Where is dialog class instance ? If dialog is already invoked then use
AfxGetMainWnd
, to get its handle.LaHaHa wrote:
if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr;
g_pJoystick->SetCooperativeLevel(AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )
Prasad Notifier using ATL | Operator new[],delete[][^]
-
I got this one now! error C2664: 'IDirectInputDevice8A::SetCooperativeLevel' : cannot convert parameter 1 from 'CWnd *' to 'HWND' Please help!
You can get its handle by type-casting it. like
SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);
Prasad Notifier using ATL | Operator new[],delete[][^]
-
You can get its handle by type-casting it. like
SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);
Prasad Notifier using ATL | Operator new[],delete[][^]
-
I got this one now! error C2664: 'IDirectInputDevice8A::SetCooperativeLevel' : cannot convert parameter 1 from 'CWnd *' to 'HWND' Please help!
You need to force it to use the overloaded (HWND) casting operator... For the main window's HWND: g_pJoystick->SetCooperativeLevel(*AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) For the window's HWND (if this is called within a CWnd-derived class): g_pJoystick->SetCooperativeLevel(*this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) Note the asterisks dereferencing the CWnd pointers. You can also use this: g_pJoystick->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) or g_pJoystick->SetCooperativeLevel(GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )
-
You can get its handle by type-casting it. like
SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);
Prasad Notifier using ATL | Operator new[],delete[][^]
Try
SetCooperativeLevel((HWND)*AfxGetMainWnd(),..);
;)
-
You need to force it to use the overloaded (HWND) casting operator... For the main window's HWND: g_pJoystick->SetCooperativeLevel(*AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) For the window's HWND (if this is called within a CWnd-derived class): g_pJoystick->SetCooperativeLevel(*this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) Note the asterisks dereferencing the CWnd pointers. You can also use this: g_pJoystick->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) or g_pJoystick->SetCooperativeLevel(GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )
-
I have tried all, the result are *this and GetSafeHwnd() will get error *AfxGetMainWnd() AfxGetMainWnd()->GetSafeHwnd() both without error, but it will get a E_HANDLE after running the SetCooperativeLevel(...). Please help!
I looked back through all your posts and I didn't see where you are making the call from. Can you post the entire function that includes the call to SetCooperativeLevel()?
LaHaHa wrote:
I have tried all, the result are *this and GetSafeHwnd() will get error
Are you getting compiler error or runtime error?
-
I looked back through all your posts and I didn't see where you are making the call from. Can you post the entire function that includes the call to SetCooperativeLevel()?
LaHaHa wrote:
I have tried all, the result are *this and GetSafeHwnd() will get error
Are you getting compiler error or runtime error?
HRESULT InitDirectInput(); Void CT1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 5: KillTimer(5); if(!FAILED(InitDirectInput())) { . . . HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { // MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), // TEXT("DirectInput Sample"), // MB_ICONERROR | MB_OK ); return 0; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; HWND hDlg=AfxGetMainWnd()->GetSafeHwnd(); if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)hDlg, DIDFT_ALL ) ) ) return hr; return S_OK; } I just call it by OnTimer() function. Please help!
-
HRESULT InitDirectInput(); Void CT1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 5: KillTimer(5); if(!FAILED(InitDirectInput())) { . . . HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { // MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), // TEXT("DirectInput Sample"), // MB_ICONERROR | MB_OK ); return 0; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; HWND hDlg=AfxGetMainWnd()->GetSafeHwnd(); if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)hDlg, DIDFT_ALL ) ) ) return hr; return S_OK; } I just call it by OnTimer() function. Please help!
Thanks that helps! I would pass the HWND into your InitDirectInput() function.
Void CT1Dlg::OnTimer(UINT nIDEvent)
{
...
if(!FAILED(InitDirectInput(*this)))
{
...
}
...
}HRESULT InitDirectInput(HWND hwndMain)
{
...
if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hwndMain, DISCL_NONEXCLUSIVE |
DISCL_BACKGROUND ) ) )
return hr;
...
}If your dialog window is wrapped in (a client of) a frame window then you'd need to pass that framewnd's handle. I'm not sure what you're doing overall, but are you sure you want to initialize in response to a timer event? Generally DirectX initializations are done once, you use the functionality, and cleanup when you're done. Mark
-
Thanks that helps! I would pass the HWND into your InitDirectInput() function.
Void CT1Dlg::OnTimer(UINT nIDEvent)
{
...
if(!FAILED(InitDirectInput(*this)))
{
...
}
...
}HRESULT InitDirectInput(HWND hwndMain)
{
...
if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hwndMain, DISCL_NONEXCLUSIVE |
DISCL_BACKGROUND ) ) )
return hr;
...
}If your dialog window is wrapped in (a client of) a frame window then you'd need to pass that framewnd's handle. I'm not sure what you're doing overall, but are you sure you want to initialize in response to a timer event? Generally DirectX initializations are done once, you use the functionality, and cleanup when you're done. Mark
-
If you put a breakpoint at the SetCooperativeLevel() call, what's the value of the window handle?
-
Also, what are the properties of your dialog resource? Try setting the dialog's "Overlapped Window" property to true.
-
Also, what are the properties of your dialog resource? Try setting the dialog's "Overlapped Window" property to true.
-
Hmm something else is going wrong. The dialog doesn't have the WS_CHILD style does it? Maybe a dialog is unacceptable as a "top-level window"? Just for the heck of it, maybe try calling your InitDirectInput() from your dialog's WM_INITDIALOG handler.