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. Handling WinAPI windows (by HWND) in Managed C++

Handling WinAPI windows (by HWND) in Managed C++

Scheduled Pinned Locked Moved Managed C++/CLI
c++csharpvisual-studiodockertutorial
4 Posts 2 Posters 3 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.
  • M Offline
    M Offline
    matmus
    wrote on last edited by
    #1

    I have a window handle (HWND) obtained from a DLL (the lib creates its own dialog form and returns handle to it - /library is written using MFC - code is as follows/). What I need to do is to 'dock' this window somehow in a TabControl of an application written in Managed C++, VS 2005 (preferrably as a new Tab). So basically I need sth like conversion from HWND to a usable object in .NET (Control^ or TabPage^ perhaps?). What I tried to do, is use of System::Windows::Forms::Control::FromHandle() function with my Handle casted to IntPtr, but it didn't work (my guesses: it only handles controls already created using .NET, not 'simple' WinAPI windows). It's also possible, that I've missed something obvious - I'm a complete newbie to all this .NET stuff. My 'main' application used to also be written using MFC, so I could convert this handle back to CWnd* (using FromHandle() )and act on it as on a 'normal' MFC window (in eg.: call ShowWindow(SW_SHOW) or SetParent() for it). How to do such a thing in .NET (meaning: to get a window from HWND, place it as a child of a Windows::Forms container (like TabControl or sth else), move/resize it to client's dimensions, and then show it to the user)? A sketch of code that creates the window: HWND LoadDialog() { CMyDialog* pNewDialog = new CMyDialog(); /*MyDialog inherits from CDialog*/ pNewDialog->Create (MAKEINTRESOURCE(IDD_RESOURCE), NULL)) /*loads the window layout from DLL's resources*/ return pNewDialog->GetSafeHwnd(); } PS. I've posted a copy of this post to codeguru :-O

    M 1 Reply Last reply
    0
    • M matmus

      I have a window handle (HWND) obtained from a DLL (the lib creates its own dialog form and returns handle to it - /library is written using MFC - code is as follows/). What I need to do is to 'dock' this window somehow in a TabControl of an application written in Managed C++, VS 2005 (preferrably as a new Tab). So basically I need sth like conversion from HWND to a usable object in .NET (Control^ or TabPage^ perhaps?). What I tried to do, is use of System::Windows::Forms::Control::FromHandle() function with my Handle casted to IntPtr, but it didn't work (my guesses: it only handles controls already created using .NET, not 'simple' WinAPI windows). It's also possible, that I've missed something obvious - I'm a complete newbie to all this .NET stuff. My 'main' application used to also be written using MFC, so I could convert this handle back to CWnd* (using FromHandle() )and act on it as on a 'normal' MFC window (in eg.: call ShowWindow(SW_SHOW) or SetParent() for it). How to do such a thing in .NET (meaning: to get a window from HWND, place it as a child of a Windows::Forms container (like TabControl or sth else), move/resize it to client's dimensions, and then show it to the user)? A sketch of code that creates the window: HWND LoadDialog() { CMyDialog* pNewDialog = new CMyDialog(); /*MyDialog inherits from CDialog*/ pNewDialog->Create (MAKEINTRESOURCE(IDD_RESOURCE), NULL)) /*loads the window layout from DLL's resources*/ return pNewDialog->GetSafeHwnd(); } PS. I've posted a copy of this post to codeguru :-O

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

      matmus wrote:

      PS. I've posted a copy of this post to codeguru

      If you get a solution, please post it here :) I'm curious about this one. I do know Control::FromHandle() returns the Control already associated with an HWND, not a Control created from an HWND. I have always wondered if you could create a control from an HWND though. At first thought, there's alot of reasons it wouldn't work well, especially with an MFC HWND... Thanks, Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        matmus wrote:

        PS. I've posted a copy of this post to codeguru

        If you get a solution, please post it here :) I'm curious about this one. I do know Control::FromHandle() returns the Control already associated with an HWND, not a Control created from an HWND. I have always wondered if you could create a control from an HWND though. At first thought, there's alot of reasons it wouldn't work well, especially with an MFC HWND... Thanks, Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        M Offline
        M Offline
        matmus
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        At first thought, there's alot of reasons it wouldn't work well, especially with an MFC HWND...

        To be honest... now I'm having these thoughts (I still hope that it's possible though, my faith is just a bit weaker ;) - WinApi is not assembly language after all, and what I need is not a 'full object' (meaning ::Form) - I was thinking of it rather as some kind of a wrapper, to call functions on (thought it'll be easier to use embedded .NET/Managed C++ functions than go down to API level). BTW: why do you think, that MFC HWND is somehow special? In my opinion windows after creation are indistinguishable. Till now I was convinced that (in MFC) i can call FromHandle() on every window (even windows, which I don't own) - now I'm not so sure of that. ;) Since I've got no answer so far, I've slightly changed my approach. Basically the question still is: How to place this window (having only its HWND) as a new tab page in TabControl created via Managed C++ /I get the window by HWND and have to handle/display it somehow in my application. Creation/destruction ow this window is performed by the plugin itself/ But... I no longer care if I need to convert HWND to System::Control or do the exact opposite: convert TabPage to HWND and use API functions to bind them). I just need to get it done.. somehow ;) (if it involves mixing managed and unmanaged code - so be it :) however I still hope that this is avoidable ). I've considered even converting .NET object to MFC, still I don't even know if it's possible, not to mention the 'howto' part. Could someone point me at a usable piece of code? Thanks in advance,

        M 1 Reply Last reply
        0
        • M matmus

          Mark Salsbery wrote:

          At first thought, there's alot of reasons it wouldn't work well, especially with an MFC HWND...

          To be honest... now I'm having these thoughts (I still hope that it's possible though, my faith is just a bit weaker ;) - WinApi is not assembly language after all, and what I need is not a 'full object' (meaning ::Form) - I was thinking of it rather as some kind of a wrapper, to call functions on (thought it'll be easier to use embedded .NET/Managed C++ functions than go down to API level). BTW: why do you think, that MFC HWND is somehow special? In my opinion windows after creation are indistinguishable. Till now I was convinced that (in MFC) i can call FromHandle() on every window (even windows, which I don't own) - now I'm not so sure of that. ;) Since I've got no answer so far, I've slightly changed my approach. Basically the question still is: How to place this window (having only its HWND) as a new tab page in TabControl created via Managed C++ /I get the window by HWND and have to handle/display it somehow in my application. Creation/destruction ow this window is performed by the plugin itself/ But... I no longer care if I need to convert HWND to System::Control or do the exact opposite: convert TabPage to HWND and use API functions to bind them). I just need to get it done.. somehow ;) (if it involves mixing managed and unmanaged code - so be it :) however I still hope that this is avoidable ). I've considered even converting .NET object to MFC, still I don't even know if it's possible, not to mention the 'howto' part. Could someone point me at a usable piece of code? Thanks in advance,

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

          matmus wrote:

          BTW: why do you think, that MFC HWND is somehow special? In my opinion windows after creation are indistinguishable.

          True, HWNDs are HWNDs. The problem that comes to mind is that .NET isn't a framework specific to Windows. MFC is. The little things MFC does with message handling may not be compatible with what the Windows .NET implementation does. So to have an MFC window mixed in with .NET forms in the parent/child heirarchy may or may not work. Of course, we know that the Windows implementation of .NET is using HWNDs behind the scenes for its "forms" but the .NET framework wasn't meant to be Windows specific. For those of us using .NET specifically for Windows, I think it could/would be handy to be able to create .NET Control objects from an HWND. There's the Microsoft.Win32 namespace for Windows- specific stuff, so that would be a good place to put it IMO. We'll see what happens with the next versions of MFC. I know there's going to be some additions for mixed-mode programming and I'm anxious to see what they add, since that's what I do :) Until then, all my UI stays MFC and I'll use the .NET framework for anything else I can leverage from it that's non-UI related. Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          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