While describing to an ISV the MFC 8.0 support for Windows Forms, I thought about the managed class below. For a developer that knows MFC but not the managed world. I modestly think that it is more useful than CWinFormsDialog<>.

 

He or she could write:

 

void CWindowsFormsDialogTestDlg::OnBnClickedButton1() {

   WindowsFormsDialog<TestForm> dlg( this ) ;

   dlg->TheClassField = "An instance field in the form class" ;

   dlg->textBox1->Text = "Accessing a control in the form" ; // the control Modifier has to be changed from Private (default) to Public.

   if ( dlg.DoModal() == IDOK ) {

      MessageBox( L"Thanks for clicking OK" ) ;

   }

}

 

The template managed class:

 

#pragma once

 

#include <vcclr.h>

 

namespace Microsoft { namespace MCS { namespace Partner { namespace ISV {

using namespace System ;

using namespace Windows::Forms ;

 

// Inspired from http://www.acm.caltech.edu/~seanm/projects/ads/html/static__assert_8h-source.html

template<bool> struct CompileTimeError ;

template<> struct CompileTimeError<true> {} ;

#define STATIC_ASSERT(expression) { CompileTimeError<((expression) != 0)> STATIC_ASSERT_FAILURE ; (void) STATIC_ASSERT_FAILURE ; }

 

template <typename TForm>

ref class WindowsFormsDialog : IWin32Window {

   private:

      TForm ^ _form ;

      IntPtr  _parentWindow ;

 

   public:

      WindowsFormsDialog( CWnd * parentWindow ) {

         _parentWindow = static_cast<IntPtr>(parentWindow->GetSafeHwnd() ) ;

      }

 

      WindowsFormsDialog( HWND parentWindow ) {

         _parentWindow = static_cast<IntPtr>(parentWindow) ;

      }

 

      virtual property IntPtr ParentWindowHandle {

         IntPtr get () = IWin32Window::Handle::get { return _parentWindow ; }

      }

 

      INT_PTR DoModal() {

         // For the other values, all bets are off! ;-)

         STATIC_ASSERT( int(DialogResult::Abort) == IDABORT ) ;

         STATIC_ASSERT( int(DialogResult::Cancel) == IDCANCEL  ) ;

         STATIC_ASSERT( int(DialogResult::Ignore) == IDIGNORE ) ;

         STATIC_ASSERT( int(DialogResult::No) == IDNO ) ;

         STATIC_ASSERT( int(DialogResult::OK) == IDOK ) ;

         STATIC_ASSERT( int(DialogResult::Retry) == IDRETRY ) ;

         STATIC_ASSERT( int(DialogResult::Yes) == IDYES ) ;

         // "this" does expose IWin32Window!

         return static_cast<INT_PTR>( _parentWindow == IntPtr(0) ? Form->ShowDialog()

                                                                 : Form->ShowDialog(this))  ;

      }

 

     operator TForm ^ () {

        return Form ;

     }

 

     TForm ^ operator->() {

        return Form ;

     }

 

     property TForm ^ Form

     {

        TForm ^ get() {

           if ( _form == nullptr ) { // not thread-safe...

               _form = gcnew TForm() ;

           }

           return _form ;

        }

     }

} ;

}}}}

 

Thoughts?

 

(Listening to ” Hablame de ti bella señora… yo te encuentro bella como una escultura… Señora solitaria.”)