views:

272

answers:

0

I'm developing a plugin (C++/CLI DLL) that runs inside of an certain applications, about which I only know the HWND. My plugin shows various .NET forms that I'd like to behave as children of the main application (e.g. centered to the main window, etc). Here's what I've tried:

// MyDialog.h
public ref class MyDialog : public System::Windows::Forms::Form {
    public:
        MyDialog(const int parentHWND);

    private:
        IntPtr parent_;
};

// MyDialog.cpp
// Attempt1: Use SetParent and CenterParent
MyDialog::MyDialog(const int parentHWND) : parent_(parentHWND) {
    SetParent(reinterpret_cast<HWND>(this->Handle.ToPointer()), reinterpret_cast<HWND>(parent_.ToPointer()));

    this->StartPosition = FormStartPosition::CenterParent;
}

This appears to make MyDialog act as a child (i.e. you can't move it outside the main app, etc), but the call to CenterParent does not have the desired results.

I found several ideas online (including SO) that suggest creating a NativeWindow from the HWND and passing it to Show():

// in MyDialog.h
void Show() new;

// MyDialog.cpp
// ...
void MyDialog::Show() {
    NativeWindow^ nw = gcnew NativeWindow();
    nw->AssignHandle(parent_);
    this->Show(nw);
}

However, this appears to, in all cases I can test, crash the main application (after showing my dialog, interestingly). I will update the post with crash info if I can obtain it.

I am looking for suggestions on how to achieve the desired behavior consistently. All I need is to be able to center a modeless dialog in the main window, and have it act as a child.

Any help would be appreciated.