c# - Winform showing another application inside a PictureBox -


first, state understand problem similar running application inside picturebox, application runs .exe fine instead not nest inside picturebox.

in c# winforms application attempting run calc.exe inside picturebox, 75% of time application runs calc.exe own window.

my code below.

enter code hereprivate void preview_button_click(object sender, eventargs e)     {         if (activewindows_listbox.selecteditem == null)         {             messagebox.show("there no window selected take picture of.", "insufficient data");             return;         }         process p = process.start("calc.exe");         thread.sleep(500);         p.waitforinputidle();         setparent(p.mainwindowhandle, viewingscreen_picturebox.handle);     } 

from understanding of trying starting calc.exe application native windows, telling application wait calculator initialize self tell calculator picturebox parent of calculator.

that leaves question, there missing making application doesn't set calculator inside picturebox 100% of time?

thank in advance.

p.s. have tried adjust thread.sleep give application more , less time has no change in behavior.

there several caveats in code:

  1. your call waitforinputidle should check return value. idle state reached when call returns true (see msdn).

  2. even if idle state has been reached, p.mainwindowhandle may return zero. busy-waiting loop can wait handle become non-zero. not nice, sufficient here show principle. diagnostic counter ctr shows how loop has been accessed.

  3. the following code should bind calculator reliably picture box, , additionally picbox resized calculators size, such whole calculator visible, regardless of picbox's initial size.

    [dllimport("user32.dll")] private static extern intptr setparent(intptr hwndchild, intpthwndnewparent); [dllimport("user32.dll")] private static extern bool movewindow(intptr hwnd, int x, int y, int w, int h, bool repaint); [dllimport("user32.dll")] private static extern bool getwindowrect(intptr hwnd, out rect lprect); private struct rect { public int left; public int top; public int right; public int bottom; } 

static int ctr = 0;

private void button2_click(object sender, eventargs e) {     process p = process.start("calc.exe");     intptr h = intptr.zero;      //wait calc.exe establish     while ((h = p.mainwindowhandle) == intptr.zero)         ctr++;      //get size of calculator     rect r;     getwindowrect(h, out r);      //width , height of calculator     int calcwidth = r.right - r.left;     int calcheight = r.bottom - r.top;      //bind calculator picturebox     setparent(h, picturebox1.handle);      //move calcusator upper left corner of picturebox     movewindow(h, 0, 0, calcwidth, calcheight, true);      //resize picturebox calculator size     size newpicboxsize = new size(calcwidth, calcheight);     picturebox1.size = newpicboxsize; } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -