c# - How to unfreeze outlook2007 inspector when is invoked from other app via interop -
i'm trying make app previews mails in outlook 2007. thing when try capture inspector email, graphics aren't yet loaded , need add delay capture function. use thread.sleep. problem though inspector outlook process freezes well. i'm sure of because when use thread.sleep(15000) in app , try manually interact inspector (move, select text, resize), it's frozen. after i'm done in app (just after capture process took place) can access it. here code:
private static void handleoutlookmail(string entryidcollection) { // incoming mailitem based on id. namespace outlookns = outlookapp.getnamespace("mapi"); mapifolder mfolder = outlookapp.session.getdefaultfolder(oldefaultfolders.olfolderinbox); newmail = (mailitem) outlookns.getitemfromid(entryidcollection, mfolder.storeid); // show mail. = newmail.getinspector; //make inspector big enough show entire email i.height=2000; //activate inspector in order avoid black screen capture ((_inspector)i).activate(); // dispatch event screen capture take place onnewoutlookmail(); }
now when event fired above following handler called:
private void takescreen() { intptr hwnd = new intptr(); process[] processes = process.getprocessesbyname("outlook"); foreach (process p in processes) { if (p.mainwindowtitle == ioutlook.newmail.getinspector.caption) { hwnd = p.mainwindowhandle; debug.writeline("found " + p.mainwindowtitle); break; } } ioutlook.releaseinspector(); //give time inspector finish loading - useless since freezes , doesn't update thread.sleep(15000); //save image disk system.drawing.image img = (system.drawing.image)capturewindow(hwnd); img.save("t.png", imageformat.png); //because added releaseinspector call try unfreeze inspector can't use: //ioutlook.i.close(microsoft.office.interop.outlook.olinspectorclose.oldiscard); }
the code 2 functions used above is:
public static void releaseinspector() { marshal.releasecomobject(i); marshal.releasecomobject(newmail); = null; newmail = null; gc.collect(); gc.waitforpendingfinalizers(); debug.writeline("released?"); }
and
public system.drawing.bitmap capturewindow(intptr hwnd) { system.drawing.rectangle rctform = system.drawing.rectangle.empty; using (system.drawing.graphics grfx = system.drawing.graphics.fromhdc(getwindowdc(hwnd))) { rctform = system.drawing.rectangle.round(grfx.visibleclipbounds); } system.drawing.bitmap pimage = new system.drawing.bitmap(rctform.width, rctform.height); system.drawing.graphics graphics = system.drawing.graphics.fromimage(pimage); intptr hdc = graphics.gethdc(); //paint control onto graphics using provided options try { printwindow(hwnd, hdc, (uint)0); } { graphics.releasehdc(hdc); } return pimage; }
any ideas? appreciated. in advance.
ok, got answer msdn forums. thing outlook waits (synchronously) handler return. key start thread in handler run takescreen , put thread.sleep there. way handler return before screenshot made , inspector have time update/ load graphics.
Comments
Post a Comment