Hosting a C# user control Hwnd within an ATL control

Current implmentation:
Create a C# library that is COM designed. Within this library we inherit from the third party control as well as our own interface which will essentially act as a wrapper around our component.
This library will then be made COMVisible from the assembly configuration of the project. RegAsm will then be used to create a TLB from this library and sibsequently register the TLB.
Our ATL component will then import this TLB, grab the handle of the from the interface then subclass it within the ATL component.
What ideally I would like to know:
1) Is the current implementation complelty wrong / going around the houses, etc. Please bare in mind that the consuming application code can only be in ATL C++. This cannot be changed.
2) The best way to display the Hwnd obtained from the C# component within an ATL COM control. The method I am using does not seem to be correct. I would like to know what is the best way to achieve this.
Sample Code:
User Control:

Code:
1
2
3
4
5
6
7
8
9
10
namespace CSharpControl
{
    public partial class CSharpControl : UserControl
    {
        public CSharpControl()
        {
            InitializeComponent();
        }
    }
}

User Control Wrapper:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using CSharpControl;
namespace CSharpControlWrapper
{
    [Guid("0AFABFC6-B85F-4874-968D-21C3391D3678")]
    public interface IControl
    {
        IntPtr GetHandle();
    }
    [Guid("CAE265D7-BA4D-4E04-9E93-7583C0B69261"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(IControl))]
    public class CSharpControlWrapper : CSharpControl.CSharpControl, IControl
    {
        public IntPtr GetHandle()
        {
            return this.Handle;
        }
    }
}

The ATL control is configured in the following:
Hosting a C# user control Hwnd within an ATL control-atl1-png
I selected a windowed control based on a static control type:
Hosting a C# user control Hwnd within an ATL control-atl2-png
The ATL COM class is quite large so will include the key bits – the #import of the library

Code:
1
2
#import "C:\Users\Documents\Visual Studio 2010\Projects\ControlSample\CSharpControlWrapper\bin\x64\Debug\CSharpControlWrapper.tlb"
struct __declspec(uuid("cae265d7-ba4d-4e04-9e93-7583c0b69261")) LIBID_CSharpControl;

Now the best way I thought to try and get the hwnd to paint itself correctly, would be to subclass the contained window that the ATL wizard generated for you. Here is the code:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    RECT rc;
    GetWindowRect(&rc);
    rc.right -= rc.left;
    rc.bottom -= rc.top;
    rc.top = rc.left = 0;
        
    m_ctrl.CreateInstance(__uuidof(LIBID_CSharpControl));
        
    //get our control handle and subclass it
    __int64 handle = m_ctrl->GetHandle();
    HWND hWnd = (HWND)handle;
    m_ctlStatic.Create(hWnd, rc);
    m_ctlStatic.SubclassWindow(hWnd);
    return 0;
}

However, when you run a basic MFC dialog application that consumes this ATL control it just looks like this:
Hosting a C# user control Hwnd within an ATL control-mfc1-png
I then changed the code, and did this:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    RECT rc;
    GetWindowRect(&rc);
    rc.right -= rc.left;
    rc.bottom -= rc.top;
    rc.top = rc.left = 0;
    m_ctrl.CreateInstance(__uuidof(LIBID_CSharpControl));
        
    __int64 handle = m_ctrl->GetHandle();
    HWND hWnd = (HWND)handle;
    ::SetParent(hWnd, m_hWnd);
    return 0;
}

It then displays the user control correctly:
Hosting a C# user control Hwnd within an ATL control-mfc2-png
The SetParent code is horrible, and can’t be the best way of doing this. Please let me know your thoughts on this.
Thank you.

“Hosting a C# user control Hwnd within an ATL control”의 43개의 댓글

  1. I love your blog.. very nice colors & theme. Did you design this
    website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to create my own blog and would like to find
    out where u got this from. cheers

  2. Heya i’m for the primary time here. I found this board and I to
    find It really useful & it helped me out a lot.
    I am hoping to give one thing back and aid others such as you aided me.

  3. Having read this I believed it was extremely enlightening.
    I appreciate you taking the time and effort to put this content together.

    I once again find myself spending a significant amount of time both reading and posting comments.
    But so what, it was still worthwhile!

  4. Cbd oil that works 2020
    Wonderful goods from you, man. I have understand your stuff
    previous to and you are just extremely excellent. I
    really like what you’ve acquired here, certainly like what you are saying and the way in which you say it.

    You make it enjoyable and you still care for to keep it smart.
    I can not wait to read far more from you. This is actually a great website.
    best rated cbd oil http://j.mp/3drY6lE
    cbd oil that works 2020 http://j.mp/3drY6lE

  5. Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking
    for a plug-in like this for quite some time and was hoping maybe you would
    have some experience with something like this.
    Please let me know if you run into anything. I truly
    enjoy reading your blog and I look forward to your new updates.

  6. I simply want to say I’m newbie to blogging and seriously loved you’re blog site. Very likely I’m going to bookmark your site . You amazingly come with good posts. Thanks a bunch for revealing your website page.

  7. Its such as you read my mind! You appear to understand a lot about this, like you wrote the e-book in it or something. I believe that you simply could do with a few to force the message house a bit, however other than that, this is wonderful blog. A great read. I’ll certainly be back.

  8. There are some fascinating moments in this write-up however I don?t recognize if I see all of them center to heart. There is some validity however I will certainly take hold point of view until I check into it further. Great post, thanks as well as we desire extra! Added to FeedBurner also

  9. Nice post. I understand some thing very complicated on various blogs everyday. It will always be stimulating to study content off their writers and rehearse something from their website. I’d opt to apply certain while using content on my small blog whether you don’t mind. Natually I’ll provide a link with your internet blog. Thanks for sharing.

  10. Can I simply state what an alleviation to find a person who really knows what theyre discussing on the net. You certainly recognize exactly how to bring an issue to light and make it essential. More individuals need to read this and also understand this side of the story. I angle believe youre not much more popular since you definitely have the gift.

  11. A remarkable share, I simply provided this onto a colleague who was doing a little evaluation on this. As well as he as a matter of fact acquired me morning meal due to the fact that I found it for him. smile. So let me reword that: Thnx for the reward! But yeah Thnkx for investing the moment to discuss this, I feel strongly regarding it and enjoy learning more on this subject. When possible, as you become proficiency, would you mind updating your blog with more information? It is very useful for me. Big thumb up for this article!

  12. I?m pleased, I must claim. Really rarely do I experience a blog site that?s both informative as well as entertaining, as well as let me tell you, you have actually hit the nail on the head. Your concept is impressive; the issue is something that not nearly enough individuals are talking wisely about. I am extremely pleased that I came across this in my search for something connecting to this.

  13. There are some fascinating moments in this short article but I don?t understand if I see every one of them facility to heart. There is some validity however I will hold opinion till I consider it better. Excellent post, thanks and we desire more! Contributed to FeedBurner as well

  14. An interesting conversation deserves comment. I think that you ought to compose much more on this topic, it could not be a taboo subject however normally individuals are not nearly enough to speak on such topics. To the next. Cheers

  15. Can I just claim what a relief to locate someone who really recognizes what theyre discussing on the web. You absolutely understand just how to bring a problem to light and also make it crucial. Even more individuals need to read this and recognize this side of the tale. I cant believe youre not more popular because you absolutely have the gift.

  16. Hello! I simply would love to provide a substantial thumbs up for the great info you have here on this article. I will be returning to your blog for even more soon.

  17. This is the right blog for anyone that wants to discover this topic. You understand a lot its almost tough to argue with you (not that I in fact would want?HaHa). You most definitely put a brand-new spin on a topic thats been discussed for several years. Wonderful stuff, simply fantastic!

  18. Hi, I do think this is an excellent site. I stumbledupon it 😉 I may come back yet again since i have bookmarked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

  19. I?d have to get in touch with you here. Which is not something I generally do! I enjoy reading a message that will certainly make individuals assume. Likewise, many thanks for permitting me to comment!

댓글 남기기