1 Reply Latest reply: Mar 6, 2010 9:15 AM by 843807 RSS

    about jni and mfc: how to attach a mfc dialog/window to the hwnd from jni

    843807
      Our Company has a MFC application, now we want to immigrate it to java little by little, so we want to use JNI to adapter.
      I've create a java application and a jni MFC dll, see below:
      In Java Application:
      +
      +
      +public class DialogInJava extends Canvas {+
      +static {+
      +System.loadLibrary("mfcdll2");+
      +}+
      +{color:#0000ff}*public native void paint(Graphics g);*{color}+
      +public void ShowDialog()+
      +{+
      +Frame f = new Frame();+
      +f.setBounds(0, 0, 400, 300);+
      +f.add( this );+
      +*{color:#0000ff}f.pack();*+
      +*f.setVisible(true);{color}*+
      +}+
      
      +public static void main(String [] args)+
      +{+
      +DialogInJava obj = new DialogInJava();+
      +obj.ShowDialog();+
      +}+
      +}+
      +
      +
      In C++ JNI DLL:


      +
      +
      +CDialogInJava dlg; // a Dialog Class from dialog resource template+
      
      +JNIEXPORT void JNICALL Java_test_DialogInJava_paint+
      +(JNIEnv* env, jobject canvas, jobject graphics)+
      +{+
      +JAWT awt;+
      +JAWT_DrawingSurface ds;+
      +JAWT_DrawingSurfaceInfo dsi;+
      +jawt_Win32DrawingSurfaceInfo* dsi_win;+
      +jint lock;+
      +...............................................+
      
      +dsi_win = (jawt_Win32DrawingSurfaceInfo*)dsi->platformInfo;+
      
      +CWnd wnd;+
      +wnd.Attach(dsi_win->hwnd);+
      +*{color:#0000ff}if (dlg.Create(dlg.IDD, &wnd) == TRUE)*+
      +*{*+
      +*    dlg.MoveWindow(&rc);*+
      +*    dlg.ShowWindow(SW_SHOW);*+
      +*}*+
      +*{color}*+
      +wnd.Detach();+
      
      +ds->FreeDrawingSurfaceInfo(dsi);+
      +ds->Unlock(ds);+
      +awt.FreeDrawingSurface(ds);+
      +}+
      +
      +
      but after the paint func executed, and the control is returned from jni dll to the java application, the java application become no response to any input, the cursor keep busy.
      If I just use the HDC to draw lines, the system seems ok.
      The environment is jdk 1.6 and microsoft visual studio2008.

      Does anybody have the jni and mfc experience? How to modify the code?

      Edited by: Flyblue on Jan 24, 2010 5:28 AM