Embed a Win32 program in the WPF program.

Source: Internet
Author: User

 

See the principle, http://msdn.microsoft.com/zh-cn/library/ms752055 (vs.90). aspx summary override buildwindowcore, return a Win32 window pointer, And the Win32 window parent window is set to buildwindowcore passed in the window handle. Note that the ws_style attribute is added to the Win32 window style. Public class embeddedapp: hwndhost <br/>{< br/> [dllimport ("user32.dll")] <br/> Private Static extern int setparent (intptr hwndchild, intptr hwndparent ); <br/> [dllimport ("user32.dll", setlasterror = true, charset = charset. auto)] <br/> Private Static extern uint setwindowlong (intptr hwnd, int nindex, uint newlong); <br/> [dllimport ("user32.dll", setlasterror = true, charset = charset. auto)] <br/> Private Static extern uint getwindowlong (intptr hwnd, int nindex); </P> <p> [dllimport ("user32.dll")] <br/> Private Static extern int enumwindows (callbackptr callptr, ref javaswinfo wndinforef); <br/> [dllimport ("user32.dll")] <br/> static extern int getwindowtext (intptr handle, stringbuilder text, int maxlen); <br/> [dllimport ("user32.dll")] <br/> Public static extern int getwindowrect (intptr hwn D, ref rect RC); <br/> internal const int <br/> gwl_wndproc = (-4), <br/> gwl_hinstance = (-6 ), <br/> gwl_hwndparent = (-8), <br/> gwl_style = (-16), <br/> gwl_exstyle = (-20 ), <br/> gwl_userdata = (-21), <br/> gwl_id = (-12); <br/> internal const uint <br/> ws_child = 0x40000000, <br/> ws_visible = 0x10000000, <br/> lbs_policy = 0x00000001, <br/> host_id = 0x00000002, <br/> listbox_id = 0x0000000 1, <br/> ws_vscroll = 0x00200000, <br/> ws_border = 0x00800000, <br/> ws_popup = 0x80000000; <br/> private const int hwnd_top = 0x0; <br/> private const int wm_command = 0x0112; <br/> private const int wm_qt_paint = 0xc2dc; <br/> private const int wm_paint = 0x000f; <br/> private const int wm_size = 0x0005; <br/> private const int swp_framechanged = 0x0020; <br/> private border wndhoster; <br/> PR Ivate double screenw, screenh; <br/> private system. diagnostics. process appproc; <br/> private uint oldstyle; <br/> private intptr hwndhost; <br/> private string apppath; <br/> Public embeddedapp (border B, double SW, double SH, string P, string f) <br/>{< br/> wndhoster = B; <br/> screenh = sh; <br/> screenw = Sw; <br/> apppath = P; <br/> wininfo = new windowinfo (); <br/> wininfo. wintitle = f; <br />}< Br/> protected override handleref buildwindowcore (handleref hwndparent) <br/>{< br/> hwndhost = findthewindow (); <br/> If (hwndhost = NULL) <br/>{< br/> appproc = new system. diagnostics. process (); <br/> appproc. startinfo. filename = apppath; <br/> appproc. start (); <br/> thread. sleep (1000); <br/> hwndhost = findthewindow (); <br/>}< br/> // The window Embedded in hwnhost must be set to ws_child <br/> oldstyle = Getwindowlong (hwndhost, gwl_style); <br/> uint newstyle = oldstyle; <br/> // ws_child and ws_popup cannot coexist. Some Win32 windows, such as QQ windows, have the ws_popup attribute, which may cause program errors during embedding. <br/> newstyle | = ws_child; <br/> newstyle & = ~ Ws_popup; <br/> setwindowlong (hwndhost, gwl_style, newstyle); <br/> // center the window. In fact, the container of the window is centered. <br/> reposwindow (wndhoster, screenw, screenh); <br/> // set the parent window of netterm to hwndhost. <br/> setparent (hwndhost, hwndparent. handle); <br/> return New handleref (this, hwndhost); <br/>}< br/> protected override void destroywindowcore (system. runtime. interopservices. handleref hwnd) <br/>{< br/> setwindowlong (hwndhost, G Wl_style, oldstyle); <br/> setparent (hwndhost, (intptr) 0); <br/>}</P> <p> [structlayout (layoutkind. sequential)] <br/> public struct rect <br/>{< br/> Public int left; <br/> Public int top; <br/> Public int right; <br/> Public int bottom; <br/>}< br/> [structlayout (layoutkind. sequential)] <br/> public struct javaswinfo <br/> {<br/> Public String wintitle; <br/> Public rect R; <br/> Public intptr Hwnd; <br/>}< br/> Public Delegate bool callbackptr (intptr hwnd, ref javaswinfo wndinforef); <br/> Private Static callbackptr; <br/> private windowinfo wininfo; <br/> Public static bool callbackproc (intptr hwnd, ref windowinfo wndinforef) <br/>{< br/> stringbuilder STR = new stringbuilder (512); <br/> getwindowtext (hwnd, STR, str. capacity); <br/> If (Str. tostring (). indexof (wndinforef. Wintitle, 0)> = 0) <br/>{< br/> wndinforef. hwnd = hwnd; <br/> getwindowrect (hwnd, ref (wndinforef. r); <br/>}</P> <p> return true; <br/>}< br/> Public intptr findthewindow () <br/>{< br/> callbackptr = new callbackptr (callbackproc); <br/> enumwindows (callbackptr, ref wininfo); <br/> return wininfo. hwnd; <br/>}< br/> Public void reposwindow (border B, double screenw, double screenh) <br/>{< br/> Double width = wininfo. r. right-wininfo. r. left; <br/> double height = wininfo. r. bottom-wininfo. r. top; <br/> double left = (screenw-width)/2; <br/> double right = (screenw-width)/2; <br/> double Top = (screenh-height)/2; <br/> double bottom = (screenh-height)/2; <br/> B. margin = New thickness (left, top, right, bottom); <br/>}< br/> example: <br/> Add a border element wndhost in XAML. Then add the following code to the WPF constructor <br/> ========================== ========< br/> This. initializecomponent (); <br/> string path; <br/> registrykey hkml = registry. localmachine; <br/> registrykey software = hkml. opensubkey ("software"); <br/> registrykey Tx = software. opensubkey ("Tencent"); <br/> registrykey qq2009 = Tx. opensubkey ("qq2009"); <br/> Path = qq2009.getvalue ("Install "). tostring () + "// bin // qq.exe"; <br/> embeddedapp Ea = new embeddedapp (wndhost, this. width, this. height, path, "qq2010"); <br/> wndhost. child = EA; <br/> embeddedapp Ea = new embeddedapp (wndhost, this. width, this. height, path, "qq2010"); <br/> wndhost. child = EA;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.