Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to get HWND from System::Windows::Forms::Panel^ panel [modified]

How to get HWND from System::Windows::Forms::Panel^ panel [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++visual-studiographicsgame-dev
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Newbie00
    wrote on last edited by
    #1

    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, 2007

    M 1 Reply Last reply
    0
    • N Newbie00

      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, 2007

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Newbie00 wrote:

      (HWND)panel->Handle.ToPointer()

      Shouldn't that be just    panel->Handle Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      N 1 Reply Last reply
      0
      • M Mark Salsbery

        Newbie00 wrote:

        (HWND)panel->Handle.ToPointer()

        Shouldn't that be just    panel->Handle Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        N Offline
        N Offline
        Newbie00
        wrote on last edited by
        #3

        No, unfortunately this won't compile: Error 23 error C2664 : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

        M 1 Reply Last reply
        0
        • N Newbie00

          No, unfortunately this won't compile: Error 23 error C2664 : cannot convert parameter 1 from 'System::IntPtr' to 'HWND'

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          N 1 Reply Last reply
          0
          • M Mark Salsbery

            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:

            N Offline
            N Offline
            Newbie00
            wrote on last edited by
            #5

            Yes GetDC() is returning non-NULL.

            M 1 Reply Last reply
            0
            • N Newbie00

              Yes GetDC() is returning non-NULL.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              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:

              N 1 Reply Last reply
              0
              • M Mark Salsbery

                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:

                N Offline
                N Offline
                Newbie00
                wrote on last edited by
                #7

                Yes it is non-NULL too.

                M 1 Reply Last reply
                0
                • N Newbie00

                  Yes it is non-NULL too.

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Then I would assume the problem is not related to getting the panel's HWND. What does GetLastError() return after the failed wglCreateContext call? Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  N 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Then I would assume the problem is not related to getting the panel's HWND. What does GetLastError() return after the failed wglCreateContext call? Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    N Offline
                    N Offline
                    Newbie00
                    wrote on last edited by
                    #9

                    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 :[

                    M 1 Reply Last reply
                    0
                    • N Newbie00

                      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 :[

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      Newbie00 wrote:

                      GetLastError() returns 2000

                      ...which is ERROR_INVALID_PIXEL_FORMAT.  Is that a clue? Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      N 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        Newbie00 wrote:

                        GetLastError() returns 2000

                        ...which is ERROR_INVALID_PIXEL_FORMAT.  Is that a clue? Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        N Offline
                        N Offline
                        Newbie00
                        wrote on last edited by
                        #11

                        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

                        M 1 Reply Last reply
                        0
                        • N Newbie00

                          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

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #12

                          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:

                          L N 2 Replies Last reply
                          0
                          • M Mark Salsbery

                            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:

                            L Offline
                            L Offline
                            led mike
                            wrote on last edited by
                            #13

                            The old VS had a utility now in 2005 this page[^] says it's in the TOOLS menu.... I don't have it :mad:

                            M 1 Reply Last reply
                            0
                            • L led mike

                              The old VS had a utility now in 2005 this page[^] says it's in the TOOLS menu.... I don't have it :mad:

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #14

                              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:

                              1 Reply Last reply
                              0
                              • M Mark Salsbery

                                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:

                                N Offline
                                N Offline
                                Newbie00
                                wrote on last edited by
                                #15

                                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

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups