Wpf and Hwnd unique name -


we have wpf application should 'piloted' legacy win32 winform application. (we don't own code ;) )

the legacy application should pilot our application (minimize, bring front, shut, etc) via windowsclass name should provide configuration parameter written ini file.

the problem cannot make work wpf since if insert classname spy++ gives us, nothing happens. point spi++ returns this

 hwndwrapper[mywpfprogram.exe;;16978ce2-3b8d-4c46-81ee-e1c6d6de4e6d] 

where guid randomly generated @ every run.

is there way solve issue?

thank you.

there no way asked. found walkaround. "simply" embedding xaml windows within windows form.

these steps followed:

1 - add windows form project.

2 - remove app.xaml , make new form entry point of application.

3 - since need hwnd of main.xaml added prop code behind

    public intptr hwnd     {         { return new windowinterophelper(this).handle; }     } 

4 - constructor of form create instance of wpf window class

    private main app;     public containerform()     {         initializecomponent();          app = new main();         elementhost.enablemodelesskeyboardinterop(app);     } 

we needed

 elementhost.enablemodelesskeyboardinterop(app); 

since want keyboard input pass the windows form xaml window

5 - want bond xpf window winform. in order need use windows api , @ onshow event of form (the reason why explicated later).

    [dllimport("user32.dll", setlasterror = true)]     private static extern long setfocus(intptr hwnd);      [dllimport("user32.dll", setlasterror = true)]     private static extern long setparent(intptr hwndchild, intptr hwndnewparent);       [dllimport("user32.dll", setlasterror = true)]     private static extern bool movewindow(intptr hwnd, int x, int y, int cx, int cy, bool repaint);      [dllimport("user32.dll", entrypoint = "setwindowlonga", setlasterror = true)]     private static extern long setwindowlong(intptr hwnd, int nindex, long dwnewlong);     private const int gwl_style = (-16);     private const int ws_visible = 0x10000000;      private void containerform_shown(object sender, eventargs e)     {         app.show();         setparent(app.hwnd, this.handle);         setwindowlong(app.hwnd, gwl_style, ws_visible);         movewindow(app.hwnd, 0, 0, this.width, this.height, true);          setfocus(app.hwnd);     } 

with

 setparent(app.hwnd, this.handle); 

wo magic, with

  setwindowlong(app.hwnd, gwl_style, ws_visible); 

we remove al chrome wpf window (there border if window defined borderless, don't ask me why)

then make wpf window fill client area of winform

  movewindow(app.hwnd, 0, 0, this.width, this.height, true); 

and focus wpf window

 setfocus(app.hwnd); 

that's reason why in show event. since if @ form's constructor, wpf window loose focus since in winform main window got focus operating system.

we didn't understand why needed add other api calls @ point, if left them @ constructor's trick didn't work.

anyway, problem solved ;)


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -