Delphi的TWebBrowser编程综述(3)

2008-04-09 04:21:21来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


        AddFav: function(Handle: THandle;
         UrlPath: PChar; UrlPathSize: Cardinal;
         Title: PChar; TitleSize: Cardinal;
         FavIDLIST: pItemIDList): Bool; stdcall;
        FDoc: IHTMLDocument2;
        UrlPath, url, title: array[0..MAX_PATH] of char;
        H: HWnd;
        pidl: pItemIDList;
        FRetOK: Bool;
       begin
        FDoc := IHTMLDocument2(IE.Document);
        if FDoc = nil then exit;
        StrPCopy(Title, FDoc.Get_title);
        StrPCopy(url, FDoc.Get_url);
        if Url <> '''''''' then
        begin
         H := LoadLibrary(PChar(''''shdocvw.dll''''));
         if H <> 0 then
         begin
          SHGetSpecialFolderLocation(0, CSIDL_FAVORITES, pidl);
          AddFav := GetProcAddress(H, PChar(''''DoAddToFavDlg''''));
          if Assigned(AddFav) then
           FRetOK :=AddFav(Handle, UrlPath, Sizeof(UrlPath),
   Title, Sizeof(Title), pidl)
         end;
         FreeLibrary(h);
         if FRetOK then
          CreateUrl(UrlPath, Url);
        end
       end;
  
  
   -----------------------------------------------------------------------------------
   6、使WebBrowser获得焦点
     TWebBrowser非常特殊,它从TWinControl继承来的SetFocus方法并不能使得它所包含的文档获得焦点,从而不能立即使用Internet
   Explorer本身具有得快捷键,解决方法如下:<
     procedure TForm1.SetFocusToDoc;
     begin
      if WebBrowser1.Document <> nil then
       with WebBrowser1.Application as IOleobject do
        DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle,
   GetClientRect);
     end;
     除此之外,我还找到一种更简单的方法,这里一并列出:
     if WebBrowser1.Document <> nil then
      IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow).focus
  
  
     刚找到了更简单的方法,也许是最简单的:
     if WebBrowser1.Document <> nil then
      IHTMLWindow4(WebBrowser1.Document).focus
     还有,需要判断文档是否获得焦点这样来做:
     if IHTMLWindow4(WebBrowser1.Document).hasfocus then
   -----------------------------------------------------------------------------------
   7、点击“提交”按钮
     如同程序里每个窗体上有一个“缺省”按钮一样,Web页面上的每个Form也有一个“缺省”按钮——即属性为“Submit”的按钮,当用户按下回车键时就相当于鼠标单击了“Submit”。但是TWebBrowser似乎并不响应回车键,并且,即使把包含TWebBrowser的窗体的KeyPreview设为True,在窗体的KeyPress事件里还是不能截获用户向TWebBrowser发出的按键。
     我的解决办法是用ApplicatinEvents构件或者自己编写TApplication对象的OnMessage事件,在其中判断消息类型,对键盘消息做出响应。至于点击“提交”按钮,可以通过分析网页源代码的方法来实现,不过我找到了更为简单快捷的方法,有两种,第一种是我自己想出来的,另一种是别人写的代码,这里都提供给大家,以做参考。
     A、用SendKeys函数向WebBrowser发送回车键
       在Delphi
   5光盘上的Info\Extras\SendKeys目录下有一个SndKey32.pas文件,其中包含了两个函数SendKeys和AppActivate,我们可以用SendKeys函数来向WebBrowser发送回车键,我现在用的就是这个方法,使用很简单,在WebBrowser获得焦点的情况下(不要求WebBrowser所包含的文档获得焦点),用一条语句即可:
      Sendkeys(''''~'''',true);// press RETURN key
      SendKeys函数的详细参数说明等,均包含在SndKey32.pas文件中。
     B、在OnMessage事件中将接受到的键盘消息传递给WebBrowser。
      procedure TForm1.ApplicationEvents1Message(var Msg: TMsg;
   var Handled: Boolean);
      {fixes the malfunction of some keys within webbrowser
   control}
      const
       StdKeys = [VK_TAB, VK_RETURN]; { standard keys }
       ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT]; {
   extended keys }
       fExtended = $01000000; { extended key flag }
      begin
       Handled := False;
       with Msg do
       if ((Message >= WM_KEYFIRST) and (Message <= WM_KEYLAST))
   and
        ((wParam in StdKeys) or
        {$IFDEF VER120}(GetKeyState(VK_CONTROL) < 0) or {$ENDIF}
        (wParam in ExtKeys) and
        ((lParam and fExtended) = fExtended)) then
       try
        if IsChild(Handle, hWnd) then { handles all browser
   related messages }
        begin
         with {$IFDEF
   VER120}Application_{$ELSE}Application{$ENDIF} as
           IOleInPlaceActiveObject do

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用break与continue语句和for与continue语句

下一篇:Deiphi编译优化导致的死循环