dnbj2010 发表于 2010-7-15 10:43:32

一个简单的AU3多标签浏览器-学做

发一个简单的AU3多标签浏览器-学做的,有很多毛病,PUG多多。在学做过程中,接触到了更多的API和windows消息方面的东西。一个人的力量总是有限的,现在贴出来抛砖引玉,提供给如俺一样的初学者作练习用。里面引用了许多AU3论坛高手的代码,在此一并致谢。#include <ButtonConstants.au3>
#include "GUIConstantsEx.au3"
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <IE.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)
Local $Button2,$Form80,$Form,$r = 1,$i = 0 ,$t = 0, $n = 0,$b = 0,$Form2,$Text,$Text2="",$URL = "http://site.baidu.com"
$dll = DllOpen("user32.dll")   
$WebBrowser2=ObjCreate("Shell.Explorer.2")

$Form0 = GUICreate("AU3简单多标签浏览器", 623, 449, 192, 114,$WS_OVERLAPPEDWINDOW)   ;主窗体
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")             ;GUIOnEventMode 模式下添加窗口关闭
$Graphic1 = GUICtrlCreateGraphic(0, 0, @DesktopWidth, 0)   ;(左 顶 宽 高) 添加绘图控件
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 0, 30)                ; 移位 绘图控件坐标0原点,移动起点到新坐标,下移30
GUICtrlSetGraphic(-1, $GUI_GR_LINE, @DesktopWidth, 30)   ; 横线 从新起点画一横线
;GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 5)            ; 定义此点画线粗细

$Button1 = GUICtrlCreateButton("首页", 0, 36, 110, 25)   
GUICtrlSetResizing (-1, $GUI_DOCKALL)

$Button75 = GUICtrlCreateButton("停止", 0, 0, 75, 30)         ;定义主窗口基本功能按钮
GUICtrlSetResizing (-1, $GUI_DOCKALL)                         ;锁定控件位置
$Button76 = GUICtrlCreateButton("前进", 80, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button77 = GUICtrlCreateButton("后退", 160, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button78 = GUICtrlCreateButton("刷新", 240, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button79 = GUICtrlCreateButton("主页", 320, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button81 = GUICtrlCreateButton("退出", 400, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button80 = GUICtrlCreateButton("帮助", 480, 0, 75, 30)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
GUICtrlSetOnEvent($Button1, "Activate1")                           
GUICtrlSetOnEvent($Button75, "Activate75")
GUICtrlSetOnEvent($Button76, "Activate76")
GUICtrlSetOnEvent($Button77, "Activate77")
GUICtrlSetOnEvent($Button78, "Activate78")
GUICtrlSetOnEvent($Button79, "Activate79")
GUICtrlSetOnEvent($Button80, "Activate80")
GUICtrlSetOnEvent($Button81, "Activate81")                   ; 设置按钮控件单击事件

GUISetState(@SW_SHOW)
;------------------------------------------------
$Form1 = GUICreate("首窗", 1024, 650, 0, 60,$WS_POPUP)                     ;设置初载的首页窗体Form1
DllCall("user32.dll","hwnd","SetParent","hwnd",$Form1,"hwnd",$Form0)       ;设置Form1yl为Form0的子窗口.
;_WinAPI_SetParent与DllCall-"SetParent"等效。对比学习两者的关系。
;_WinAPI_SetParent($Form1,$Form0)

$WebBrowser1=ObjCreate("Shell.Explorer.2")                                 ;等效 _IECreateEmbedded()
GUICtrlCreateObj($WebBrowser1,0, 0, 1024, 750)                            ;设置WebBrowser1对象大小
$EventObject=ObjEvent($WebBrowser1,"IEEvent_","DWebBrowserEvents")       ;设置接收windows事件
if @error then exit Msgbox(0,"错误!","不可使用COM接口 'DWebBrowserEvents'. 错误代码: " & hex(@error,8))
$Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")    ;设置Timer控件
$TimerDLL = DllCall($dll, "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 10, "ptr", DllCallbackGetPtr($Timer))
;函数SetTimer,用WM_TIMER来设置定时器。每隔10毫秒自动执行一次DllCallbackRegister,创建自定义回调函数Timer
;参看后面的Timer()事件

$WebBrowser1.Navigate( $URL)             ;自定义首页
GUISetState(@SW_SHOW)
GUISwitch ($Form0)                     ;切换当前窗口到主窗口
;-------------------------------------------------------------
While 1
                Sleep (1)
                $cursor = _WinAPI_GetCursorInfo()                            ;获取鼠标状态
         If $cursor=65581 And _IsPressed("1", $dll) Then
                                WinSetOnTop($Form1,"",1)
               MsgBox(0,"IE内嵌不新建IE窗口", "0",1,WinGetHandle("Program Manager"))
                        WinSetOnTop($Form1,"",0)
                If $Text2<>"" Then
                WebBrowser1_Navigate()                            ;自定义打开新页事件
                EndIf                               
                   EndIf
                $msg = GUIGetMsg()
Wend

;-------------------------------------------------------------
Func WebBrowser1_Navigate()
          GUISwitch($Form0)
                $r = $r + 1
          $Form[$r] = GUICreate($Text2, 1024, 650, 0, 60,$WS_POPUP);设置$Form[$r]作为装载IE新页面的窗体
                _WinAPI_SetParent($Form[$r],$Form0)                     ;设置Form[$r]为Form0的子窗口.
               
                $WebBrowser2=ObjCreate("Shell.Explorer.2")
      GUICtrlCreateObj($WebBrowser2, -12, 0, 1024, 750)   
                $EventObject=ObjEvent($WebBrowser2,"IEEvent_","DWebBrowserEvents")
                if @error then exit Msgbox(0,"错误!","不可使用COM接口 'DWebBrowserEvents'. 错误代码: " & hex(@error,8))
      $Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
      $TimerDLL = DllCall($dll, "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 10, "ptr", DllCallbackGetPtr($Timer))
               
      $WebBrowser2.Navigate( $Text2 )
                GUISetState(@SW_SHOW)
EndFunc
;--------------------------------------------
Func IEEvent_TitleChange($URL)               ; 接收IE的TitleChange事件。获取网页标题。在可以获取title时发生,较晚。
If StringInStr($URL, "_") ="_" Then                ; 将$URL标题中含有_的标题剔除
$i = $i + 1
IniWrite("title.ini", "title"&$i,"", $URL)   ; 将所获得的URL标题写入配置ini文件
EndIf
wwwTab()                                    
EndFunc

;----------------------------------------
Func IEEvent_StatusTextChange ($Text)   ; 接收IE的IEEvent_StatusTextChange事件。
If $Text<>'' And StringLeft($Text,4)="http" Then $Text2=$Text

EndFunc

;====================================================================================
Func wwwTab()
        GUISwitch ($Form0)       
        $var2 = IniRead("title.ini", "title4", "", "no")
        $var3 = IniRead("title.ini", "title6", "", "no")
        $var4 = IniRead("title.ini", "title8", "", "no")
        $var5 = IniRead("title.ini", "title10", "", "no")

$Button2 = GUICtrlCreateButton($var2, 110, 36, 110, 25)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$contextmenu = GUICtrlCreateContextMenu ($Button2)         ; 创建右键菜单
$FormCLOSE2 = GUICtrlCreateMenuitem ("关闭",$contextmenu) ; 关闭此窗口
GUICtrlSetOnEvent($FormCLOSE2, "FormCLOSE2")


$Button3 = GUICtrlCreateButton($var3, 220, 36, 110, 25)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button4 = GUICtrlCreateButton($var4, 330, 36, 110, 25)
GUICtrlSetResizing (-1, $GUI_DOCKALL)
$Button5 = GUICtrlCreateButton($var5, 440, 36, 110, 25)
GUICtrlSetResizing (-1, $GUI_DOCKALL)

GUICtrlSetOnEvent($Button2, "Activate2")
GUICtrlSetOnEvent($Button3, "Activate3")
GUICtrlSetOnEvent($Button4, "Activate4")
GUICtrlSetOnEvent($Button5, "Activate5")

If $var2 = "no" Then
        GUICtrlSetState ( $Button2, $GUI_HIDE); 控件隐藏$GUI_HIDE
Else                                        ; 否则
        GUICtrlSetState ( $Button2, $GUI_SHOW); 控件显示$GUI_SHOW
EndIf
If $var3 = "no" Then
        GUICtrlSetState ( $Button3, $GUI_HIDE)
Else
        GUICtrlSetState ( $Button3, $GUI_SHOW)
EndIf
If $var4 = "no" Then
        GUICtrlSetState ( $Button4, $GUI_HIDE)
Else
        GUICtrlSetState ( $Button4, $GUI_SHOW)
EndIf
If $var5 = "no" Then
        GUICtrlSetState ( $Button5, $GUI_HIDE)
Else
        GUICtrlSetState ( $Button5, $GUI_SHOW)
EndIf

EndFunc   ;==>wwwTab()自定义的窗体按钮标签事件

;=================================================================
Func Activate1()
      _WinAPI_SetParent($Form1,$Form0)        ;用重设子窗口的方式来激活指定窗口。在SetParent之后,WinActivate,PostMessage都失效。       
EndFunc   ;==>Activate1

Func Activate2()
               _WinAPI_SetParent($Form,$Form0)
EndFunc   ;==>Activate2

Func Activate3()
         _WinAPI_SetParent($Form,$Form0)       
EndFunc   ;==>Activate3

Func Activate4()
             _WinAPI_SetParent($Form,$Form0)
EndFunc   ;==>Activate4

Func Activate5()
             _WinAPI_SetParent($Form,$Form0)
EndFunc   ;==>Activate5
       
;----------------------------------------------------------------
Func Activate75()
        _IEAction ($WebBrowser2, "stop")
EndFunc   ;==>Activate81

Func Activate76()
        _IEAction ($WebBrowser2, "forward")
EndFunc   ;==>Activate81

Func Activate77()
        _IEAction ($WebBrowser2, "back")
EndFunc   ;==>Activate81

Func Activate78()
        _IEAction ($WebBrowser2, "refresh")
EndFunc   ;==>Activate81

Func Activate79()
        _IEAction ($WebBrowser2, "home")
EndFunc   ;==>Activate81

Func Activate80()
       GUISwitch ($Form0)
   $Form80 = GUICreate("帮助", 400, 300, 250, 214)
       $Button801 = GUICtrlCreateButton("关闭", 88, 180, 150, 30)
       GUICtrlSetOnEvent($Button801, "CLOSEActivate80")
   GUISetState(@SW_SHOW)
EndFunc   ;==>Activate80

Func CLOSEActivate80()
        _WinAPI_SetParent($Form80,$Form0)
        _WinAPI_MoveWindow($Form80, 0, 0, 0, 0, True)
EndFunc   ;==>Activate81

Func FormCLOSE2()
_WinAPI_MoveWindow($Form, 0, 0, 0, 0, True)
GUICtrlSetData ($Button2,"")
GUICtrlSetState ($Button2, $GUI_HIDE)
EndFunc
;----------------------------------------------------------------
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
      If WinExists("IE内嵌不新建IE窗口") Then
                WinMove("IE内嵌不新建IE窗口","",@DesktopWidth,@DesktopHeight,0,0,1)
                Send("{Enter}")
      EndIf
EndFunc

Func CLOSE()
                                $EventObject.Stop   ; 不再接收事件
                $EventObject=0      ; 关闭Event Object
                $WebBrowser1=0            ; 清除内存
                GUIDelete ()
                DllCall($dll, "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
                DllCallbackFree($Timer)
                DllClose($dll)
                    FileDelete("title.ini");直接删除
                    ;MsgBox(0, "谢谢", "报歉!学做的东西!...",3)
                                Exit
EndFunc

Func Activate81()
        CLOSE()
EndFunc   ;==>Activate81

Func CLOSEClicked()
        CLOSE()                       
EndFunc

dnbj2010 发表于 2010-7-15 11:19:36

TitleChange ,NavigateComplete....
这些事件应该还是有用的,在做一个完善的系统的多功能多标签的浏览器时,有可能要利用到这些事件。

本例中新网页打开使用了自定义WebBrowser1_Navigate()事件,放弃了原先的NewWindow事件,代码是参考本论坛lanfengc的。原址:http://www.autoitx.com/thread-16723-1-1.html
呵呵,学习了鼠标状态获取及判断的方法,还从$Text2的获取重新认识了StatusTextChange事件,感谢。

在学做过程中,在用SetParent设置子窗口之后,卡了很久,因为没办法激活子窗口了。WinActivate,PostMessage,setMessage,FindWindow,FindWindowEx 都用了,实在没办法激活,也没法移动,关闭。奇怪。
后来只好将Form2设置成$Form[$r],然后再用SetParent重设子窗口,才达到了激活指定子窗口的目的,但是没有更直接的方法了吗?等高手指教。

kinghu318 发表于 2010-7-15 11:37:04

顶,不知道效果怎么样.....

lin0308 发表于 2010-7-16 03:40:37

我试了下,不能用啊

lxz 发表于 2010-7-16 06:55:34

谢谢分享...

wgzhi 发表于 2010-7-16 09:54:02

试用一下,还是可以的,就是在新的页面在下载时,旧的不能打开。

rikthhpgf2005 发表于 2010-7-16 10:04:04

顶下,多看帮助,对你有好处

dnbj2010 发表于 2010-7-18 15:02:52

回复 7# rikthhpgf2005

顶下,多看帮助,对你有好处
----------------------------------------------
谢谢。记住了。
页: [1]
查看完整版本: 一个简单的AU3多标签浏览器-学做