How to get HWND from System::Windows::Forms::Panel^ panel [modified]
-
Hi, i'm not sure if i've chosen correct place for my post, but I'm writing an app in VC++ 2005 EE which is going to display some 3D figures on System::Windows::Forms::Panel^ panel The problem: I've written my own class which handles all OpenGL stuff. So all I have to do is: Create new 3D figure object, and draw it with panel's Paint method. Constructor needs panel's HWND, so i did something like this:
TFigure *Figure; //pointer to my base class objects Figure = new TCuboid( (HWND)panel->Handle.ToPointer() , panel->Width, panel->Height, 3, 3, 7 );
But this solution doesn't work properly. When i'm trying to initialize OpenGl machine with this code (inside TCuboid construcor):m_gHDC = GetDC( hwnd ); //identifier (window HWND) SetupPixelFormat(); m_hRC = wglCreateContext( m_gHDC ); //identifier (OpenGL graphic context)
the variable: m_hRC is NULL and it shouldn't be. I've tried my class code with other IDE and all was fine. So i think the problem is with getting panel's HWND. Can somebody help me please? -- modified at 9:53 Sunday 14th October, 2007Newbie00 wrote:
(HWND)panel->Handle.ToPointer()
Shouldn't that be just panel->Handle Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Newbie00 wrote:
(HWND)panel->Handle.ToPointer()
Shouldn't that be just panel->Handle Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
No, unfortunately this won't compile: Error 23 error C2664 : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'
I left out the cast, sorry. I meant without the ToPointer() call. I just realized where I use it without the ToPointer(), it's because the destination is looking for an IntPtr :rolleyes: Anyway, is the HWND non-NULL at that point? Is GetDC() returning non-NULL? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I left out the cast, sorry. I meant without the ToPointer() call. I just realized where I use it without the ToPointer(), it's because the destination is looking for an IntPtr :rolleyes: Anyway, is the HWND non-NULL at that point? Is GetDC() returning non-NULL? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Newbie00 wrote:
Yes GetDC() is returning non-NULL
And the HWND you pass to GetDC() is non-NULL too?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Newbie00 wrote:
Yes GetDC() is returning non-NULL
And the HWND you pass to GetDC() is non-NULL too?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Then I would assume the problem is not related to getting the panel's HWND. What does GetLastError() return after the failed
wglCreateContext
call? MarkMark Salsbery Microsoft MVP - Visual C++ :java:
-
Then I would assume the problem is not related to getting the panel's HWND. What does GetLastError() return after the failed
wglCreateContext
call? MarkMark Salsbery Microsoft MVP - Visual C++ :java:
Yeees :] hmmmmmmmm I've tried to use GetLastError() with FormatMessage() like this:
LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL );
But an exception occures: An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in Figury.exe Additional information: Cannot marshal 'parameter #7': Pointers cannot reference marshaled structures. Use ByRef instead. GetLastError() returns 2000, i don't know what to do :[ -
Yeees :] hmmmmmmmm I've tried to use GetLastError() with FormatMessage() like this:
LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL );
But an exception occures: An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in Figury.exe Additional information: Cannot marshal 'parameter #7': Pointers cannot reference marshaled structures. Use ByRef instead. GetLastError() returns 2000, i don't know what to do :[Newbie00 wrote:
GetLastError() returns 2000
...which is ERROR_INVALID_PIXEL_FORMAT. Is that a clue? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Newbie00 wrote:
GetLastError() returns 2000
...which is ERROR_INVALID_PIXEL_FORMAT. Is that a clue? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
how do you know what 2000 means? :O That could be a clue, but it's worse than i thought :O I have no idea why such strange error occures. I've tried this code with C++ Builder 6.0 and everything was ok :O There was no ERROR_INVALID_PIXEL_FORMAT
There's lots of error codes[^] listed in the documentation for GetLastError()[^] :) I'm not sure what's going on with this, or why it would work with builder. I guess this leaves SetupPixelFormat() ... what goes on in there? Is it supposed to have a DC passed to it? If so, is that DC supposed to be a memory DC or can it be a window DC? Since you can't change the format of a window DC, I would expect it requires a memory DC - or does it create one internally? I'm just guessing from what little code I've seen, but I suspect you're passing the wrong DC to wglCreateContext(). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
There's lots of error codes[^] listed in the documentation for GetLastError()[^] :) I'm not sure what's going on with this, or why it would work with builder. I guess this leaves SetupPixelFormat() ... what goes on in there? Is it supposed to have a DC passed to it? If so, is that DC supposed to be a memory DC or can it be a window DC? Since you can't change the format of a window DC, I would expect it requires a memory DC - or does it create one internally? I'm just guessing from what little code I've seen, but I suspect you're passing the wrong DC to wglCreateContext(). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Interesting. I don't have that utility either. In fact, I'm pretty sure I've never found any utilities I wanted to find in VS. Yes, there's ones there I never use... *shrug*
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
There's lots of error codes[^] listed in the documentation for GetLastError()[^] :) I'm not sure what's going on with this, or why it would work with builder. I guess this leaves SetupPixelFormat() ... what goes on in there? Is it supposed to have a DC passed to it? If so, is that DC supposed to be a memory DC or can it be a window DC? Since you can't change the format of a window DC, I would expect it requires a memory DC - or does it create one internally? I'm just guessing from what little code I've seen, but I suspect you're passing the wrong DC to wglCreateContext(). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I think i'm passing correct DC.
void TFigure::SetupPixelFormat() { int nPixelFormat = 0; PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; nPixelFormat = ChoosePixelFormat( m_gHDC, &pfd ); SetPixelFormat( m_gHDC, nPixelFormat, &pfd ); }
ChoosePixelFormat( m_gHDC, &pfd ); returns 7. -- modified at 13:35 Monday 15th October, 2007