Save webpage as picture--scroll to intercept IE (WEBBROWSE)

Source: Internet
Author: User

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?
    1. Uses Comobj,shdocvw,mshtml,olectrls,jpeg,activex;
    2. Tobjectfromlresult = function (lresult:lresult; Const IID:TIID; Wparam:wparam; Out pobject): HRESULT;  stdcall;
    3. Get the window image, very useful ah, even if the window is blocked, hidden can be obtained
    4. 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?
  1. Get IE instance, Whandle is the handle of IE, this piece of code is copied online, thanks to the author!
  2. Whandle can be obtained through Windowfrompoint (GetCursorPos (PT)), access to a lot of online search.
  3. function Getiefromhwnd (Whandle:hwnd;  var ie:iwebbrowser2): HRESULT;
  4. Var
  5. Hinst:hwnd;
  6. lres:cardinal;
  7. Msg:integer;
  8. Pdoc:ihtmldocument2;
  9. Objectfromlresult:tobjectfromlresult;
  10. Begin
  11. HInst: = LoadLibrary (' Oleacc.dll ');
  12. @ObjectFromLresult: = GetProcAddress (HInst, ' Objectfromlresult ');
  13. if @ObjectFromLresult <> nil Then
  14. begin
  15. Try
  16. MSG: = RegisterWindowMessage (' wm_html_getobject ');
  17. SendMessageTimeout (WHANDLE, MSG, 0, 0, Smto_abortifhung, lres);
  18. Result: = Objectfromlresult (Lres, IHTMLDocument2, 0, PDOC);
  19. if Result = S_OK Then
  20. (PDoc. ParentWindow as IServiceProvider). QueryService (IWebBrowserApp,
  21. IWebbrowser2, IE);
  22. finally
  23. FreeLibrary (HInst);
  24. PDoc: = nil;
  25. end;
  26. end;
  27. End
  28. {Specify IE window handle, scroll to get entire page picture
  29. 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.
  30. }
  31. Procedure Savewebpic (Hiehandle:integer; webjpg:tjpegimage);
  32. Var
  33. Rect:trect;
  34. Webbmp:tbitmap;
  35. I, J, WebTop, Webleft, TLeft, ttop: integer;
  36. HT, VT: array of integers;
  37. Ie:iwebbrowser2;
  38. Idoc:ihtmldocument2;
  39. Velement:ihtmlelement2;
  40. procedure Captureweb (destbmp:tbitmap; destleft, desttop, Width, height:integer);
  41. var
  42. Tembmp:tbitmap;
  43. begin
  44. Tembmp: = Tbitmap.  Create ();
  45. Tembmp.  Height: = height+2;
  46. Tembmp.  Width: = width+2;
  47. Printwindow (Hiehandle, Tembmp. Canvas.  Handle, 0);
  48. BitBlt (Destbmp. Canvas. Handle, Destleft, Desttop, Destleft+width, Desttop+height, Tembmp. Canvas.  Handle, 2, 2, srccopy);
  49. Tembmp.  Free;
  50. end;
  51. Begin
  52. If not Assigned (webjpg) Then
  53. Exit;
  54. if Getiefromhwnd (hiehandle, IE) <> S_OK Then
  55. Exit;
  56. IDoc: = IE.  Document as IHTMLDocument2;
  57. Webbmp: = Tbitmap.  Create ();
  58. Webbmp.  PixelFormat: = Pf24bit;
  59. Try
  60. //temcanvas.handle: = GetDC (Hiehandle);
  61. IDoc. BodySetAttribute (' scroll ', ' yes ', 0);
  62. //Save original scroll bar position
  63. Velement: = (iDoc as IHTMLDocument3). documentelement as IHTMLElement2;
  64. If not Assigned (velement) Then
  65. Velement: = IDoc.  Body as IHTMLElement2;
  66. Ttop: = Velement. scrolltop;
  67. TLeft: = Velement. scrollleft;
  68. //Get the size of the page
  69. Webbmp. Height: = Velement. scrollheight;   IDoc.Body.getAttribute (' ScrollHeight ', 0);
  70. Webbmp. Width: = Velement. scrollwidth;   IDoc.Body.getAttribute (' ScrollWidth ', 0);
  71. //Get the window size of the display page
  72. Windows. GetClientRect (Hiehandle, rect);
  73. Rect. Bottom: = rect. Bottom-getsystemmetrics (Sm_cxhscroll)-GetSystemMetrics (Sm_cxdlgframe);
  74. Rect. Right: = rect. Right-getsystemmetrics (Sm_cxvscroll)-GetSystemMetrics (Sm_cxdlgframe);
  75. Rect.  Right: = Velement. clientwidth;
  76. Rect.  Bottom: = Velement. clientheight;
  77. //Calculate the height and width of the interception at each vertical and transverse
  78. //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
  79. //Intercept the wh-h part, leaving the loop by H intercept
  80. SetLength (VT, Webbmp. Height Div rect.  Bottom + 1);
  81. SetLength (HT, Webbmp. Width Div rect.  Right + 1);
  82. vt[0]: = Webbmp. Height-(Webbmp. Height Div rect. Bottom) * Rect.  Bottom;
  83. ht[0]: = Webbmp. Width-(Webbmp. Width Div rect. right) * Rect.  right;
  84. For i:=1 to Length (VT)-1 do
  85. Vt[i]: = Rect.  Bottom;
  86. For i:=1 to Length (HT)-1 does
  87. Ht[i]: = Rect.  right;
  88. Webleft: = 0;
  89. For i:=0 to Length (HT)-1 does begin
  90. WebTop: = 0;
  91. IDoc. Get_parentwindow.  Scroll (Webleft, WebTop);
  92. For j:=0 to Length (VT)-1 do begin
  93. Captureweb (Webbmp, Webleft, WebTop, Ht[i], vt[j]);
  94. Webtop: = Webtop + vt[j];
  95. IDoc. Get_parentwindow.  Scroll (Webleft, WebTop);
  96. end;
  97. Webleft: = Webleft + ht[i];
  98. end;
  99. Webjpg.  Assign (webbmp);
  100. Webjpg.  SaveToFile (' z:\demo.jpg ');
  101. finally
  102. Webbmp.  Free;
  103. SetLength (VT, 0);
  104. SetLength (HT, 0);
  105. IDoc. Get_parentwindow.  Scroll (TLeft, ttop);
  106. end;
  107. End

http://blog.csdn.net/tht2009/article/details/39736839

Save webpage as picture--scroll to intercept IE (WEBBROWSE)

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.