Transferred from: http://www.cnblogs.com/IceAir/archive/2011/04/11/2012744.html
IE programming has always felt that it is quite scary things, the interface, functions, events, interpretation of the chaos, the need to understand the knowledge of all aspects of the bo, let me seem to see Microsoft is the creator, because it has successfully created such chaos, the initial state of the universe ...
Recently to do a project, to intercept Web pages, try to get MHT, the beginning is successful, but do not know what software installed, it can no longer be saved into MHT, checked a pile of information, fixed a round, but also partial preservation of success, so unstable certainly can not be applied, give up! Have to praise is, net text fast capture of the plug-in is really fine, the success rate is high, I do not know how much work at the back of the author, unfortunately the author did not provide as an interface. So decided to cut the page as a picture, and also get the text saved for querying the way, after a few days of countless search data to try, in the end, before the collapse of the Jade Emperor Pity me, finally let me succeed, has been from all over the Internet to benefit a lot of articles, quickly posted out to do not know how many poor people like me.
First, the name of the file used for the fame, the function names used to get the IE Webbrowse instance, and the function name to get the window image:
[Delphi]View PlainCopy print?
- Uses Comobj,shdocvw,mshtml,olectrls,jpeg,activex;
- Tobjectfromlresult = function (lresult:lresult; Const IID:TIID; Wparam:wparam; Out pobject): HRESULT; stdcall;
- Get the window image, very useful ah, even if the window is blocked, hidden can be obtained
- function Printwindow (Sourcewindow:hwnd; DESTINATION:HDC; Nflags: Cardinal): bool; stdcall; External ' user32.dll ' name ' Printwindow ';
Get the IE instance and core code:
[Delphi]View PlainCopyprint?
- Get IE instance, Whandle is the handle of IE, this piece of code is copied online, thanks to the author!
- Whandle can be obtained through Windowfrompoint (GetCursorPos (PT)), access to a lot of online search.
- function Getiefromhwnd (Whandle:hwnd; var ie:iwebbrowser2): HRESULT;
- Var
- Hinst:hwnd;
- lres:cardinal;
- Msg:integer;
- Pdoc:ihtmldocument2;
- Objectfromlresult:tobjectfromlresult;
- Begin
- HInst: = LoadLibrary (' Oleacc.dll ');
- @ObjectFromLresult: = GetProcAddress (HInst, ' Objectfromlresult ');
- if @ObjectFromLresult <> nil Then
- begin
- Try
- MSG: = RegisterWindowMessage (' wm_html_getobject ');
- SendMessageTimeout (WHANDLE, MSG, 0, 0, Smto_abortifhung, lres);
- Result: = Objectfromlresult (Lres, IHTMLDocument2, 0, PDOC);
- if Result = S_OK Then
- (PDoc. ParentWindow as IServiceProvider). QueryService (IWebBrowserApp,
- IWebbrowser2, IE);
- finally
- FreeLibrary (HInst);
- PDoc: = nil;
- end;
- end;
- End
- {Specify IE window handle, scroll to get entire page picture
- This function after the completion of the original IE scroll bar to restore the position, it sounds very simple, find information is to find silly cloud! If the webpage has D3D this kind of thing, the online said getattribute (' scrolltop ', 0) What can not use, make very hard.
- }
- Procedure Savewebpic (Hiehandle:integer; webjpg:tjpegimage);
- Var
- Rect:trect;
- Webbmp:tbitmap;
- I, J, WebTop, Webleft, TLeft, ttop: integer;
- HT, VT: array of integers;
- Ie:iwebbrowser2;
- Idoc:ihtmldocument2;
- Velement:ihtmlelement2;
- procedure Captureweb (destbmp:tbitmap; destleft, desttop, Width, height:integer);
- var
- Tembmp:tbitmap;
- begin
- Tembmp: = Tbitmap. Create ();
- Tembmp. Height: = height+2;
- Tembmp. Width: = width+2;
- Printwindow (Hiehandle, Tembmp. Canvas. Handle, 0);
- BitBlt (Destbmp. Canvas. Handle, Destleft, Desttop, Destleft+width, Desttop+height, Tembmp. Canvas. Handle, 2, 2, srccopy);
- Tembmp. Free;
- end;
- Begin
- If not Assigned (webjpg) Then
- Exit;
- if Getiefromhwnd (hiehandle, IE) <> S_OK Then
- Exit;
- IDoc: = IE. Document as IHTMLDocument2;
- Webbmp: = Tbitmap. Create ();
- Webbmp. PixelFormat: = Pf24bit;
- Try
- //temcanvas.handle: = GetDC (Hiehandle);
- IDoc. BodySetAttribute (' scroll ', ' yes ', 0);
- //Save original scroll bar position
- Velement: = (iDoc as IHTMLDocument3). documentelement as IHTMLElement2;
- If not Assigned (velement) Then
- Velement: = IDoc. Body as IHTMLElement2;
- Ttop: = Velement. scrolltop;
- TLeft: = Velement. scrollleft;
- //Get the size of the page
- Webbmp. Height: = Velement. scrollheight; IDoc.Body.getAttribute (' ScrollHeight ', 0);
- Webbmp. Width: = Velement. scrollwidth; IDoc.Body.getAttribute (' ScrollWidth ', 0);
- //Get the window size of the display page
- Windows. GetClientRect (Hiehandle, rect);
- Rect. Bottom: = rect. Bottom-getsystemmetrics (Sm_cxhscroll)-GetSystemMetrics (Sm_cxdlgframe);
- Rect. Right: = rect. Right-getsystemmetrics (Sm_cxvscroll)-GetSystemMetrics (Sm_cxdlgframe);
- Rect. Right: = Velement. clientwidth;
- Rect. Bottom: = Velement. clientheight;
- //Calculate the height and width of the interception at each vertical and transverse
- //Page interception method is to go to 0 rounding, such as the height of the window is H, and the height of the Web page is WH, can first
- //Intercept the wh-h part, leaving the loop by H intercept
- SetLength (VT, Webbmp. Height Div rect. Bottom + 1);
- SetLength (HT, Webbmp. Width Div rect. Right + 1);
- vt[0]: = Webbmp. Height-(Webbmp. Height Div rect. Bottom) * Rect. Bottom;
- ht[0]: = Webbmp. Width-(Webbmp. Width Div rect. right) * Rect. right;
- For i:=1 to Length (VT)-1 do
- Vt[i]: = Rect. Bottom;
- For i:=1 to Length (HT)-1 does
- Ht[i]: = Rect. right;
- Webleft: = 0;
- For i:=0 to Length (HT)-1 does begin
- WebTop: = 0;
- IDoc. Get_parentwindow. Scroll (Webleft, WebTop);
- For j:=0 to Length (VT)-1 do begin
- Captureweb (Webbmp, Webleft, WebTop, Ht[i], vt[j]);
- Webtop: = Webtop + vt[j];
- IDoc. Get_parentwindow. Scroll (Webleft, WebTop);
- end;
- Webleft: = Webleft + ht[i];
- end;
- Webjpg. Assign (webbmp);
- Webjpg. SaveToFile (' z:\demo.jpg ');
- finally
- Webbmp. Free;
- SetLength (VT, 0);
- SetLength (HT, 0);
- IDoc. Get_parentwindow. Scroll (TLeft, ttop);
- end;
- End
http://blog.csdn.net/tht2009/article/details/39736839
Save webpage as picture--scroll to intercept IE (WEBBROWSE)