找回密码
 加入
搜索
查看: 4513|回复: 14

求高手讲解GUIRegisterMsg注册消息后的处理函数应该如何写?

[复制链接]
发表于 2009-7-5 23:48:46 | 显示全部楼层 |阅读模式
要疯了,各种尝试都过了,但新问题总是不断出现,这些天麻烦各位大大了。
现在想找找这个问题的答案,请多多讲解。

我在GUI里内嵌了一个IE,按理这就是我们平时常用的WebBrowser控件。我现在想在点击这个内嵌IE内的链接的时候,做一些处理,于是我就想要得到发给这个内嵌IE的消息。

我找资料翻遍了天,似乎 GUIRegisterMsg 来做这件事最合适,论坛上,包括整个网上都找了,有几个这方面的例子和教程,比如:
http://www.autoitx.com/forum.php ... 5%CD%B3%CF%FB%CF%A2
http://www.autoitx.com/forum.php?mod=viewthread&tid=6238

但可惜的是我试图依葫芦画瓢时,完全不是那么回事,怎么也弄不成功。求高手讲解一下这个消息处理函数到底应该如何做,里面的东西有一些什么样的含义?现在是找资料都不知道从哪找起,基本上都没有针对autoit深入讲的。

评分

参与人数 1金钱 +10 贡献 +5 收起 理由
lynfr8 + 10 + 5 有意义的问题,继续深究!

查看全部评分

发表于 2009-7-6 19:57:09 | 显示全部楼层
本帖最后由 lynfr8 于 2009-7-6 20:07 编辑

Global Const $NM_CLICK = ($NM_FIRST - 2) ; //单击
http://www.autoitx.com/forum.php?mod=viewthread&tid=22&extra=page%3D4
键盘+鼠标快捷键例子(包括获取鼠标/键盘/窗口消息)
http://www.autoitx.com/forum.php?mod=viewthread&tid=7092&extra=&highlight=%CF%FB%CF%A2&page=1
关于截获鼠标消息的钩子耗系统资源的问题
http://www.autoitx.com/forum.php?mod=viewthread&tid=5945&highlight=%CF%FB%CF%A2
如何截获单击鼠标中键消息
http://www.autoitx.com/forum.php?mod=viewthread&tid=4664&highlight=%CF%FB%CF%A2
询问关于消息截获的问题
http://www.autoitx.com/forum.php?mod=viewthread&tid=2004&highlight=%CF%FB%CF%A2

你这个问题我也在探讨中,一起研究下
发表于 2009-7-6 20:53:57 | 显示全部楼层
留个名,等待解决
 楼主| 发表于 2009-7-7 14:23:11 | 显示全部楼层
可惜了,……

按理,在嵌入一个webBrowser控件后,要更改其打开新窗口的动作,在VB里,可以对其 newwindow 事件或者Navigate2事件编程,这种例子很多,但要在autoit3里怎么写,就没有看到示例。

说autoit使用者多,也许是针对AHK而言吧,如果有很多问题不知道怎么办,可能还是改用VB会合适一点。
 楼主| 发表于 2009-7-7 14:25:25 | 显示全部楼层
另, lynfr8  ,我今天在微软的网站上翻了一下,里面说到webBrowser控件里包含的网页点击事件,并不会发送至webBrowser控件本身,而是发送至document为止。原文转贴如下:

Handling Events in Visual Basic Applications
How do you trap events in objects contained by WebBrowser objects in a way that enables you to handle the events with native Microsoft Visual Basic code? This article explains how to create a Visual Basic-based application that consists of a class module and a form that contains a WebBrowser object. Members of the class trap events from an HTML document object contained by the WebBrowser object, then "forward" the events to a procedure in the form. Visual Basic code in the form can then execute in response to the forwarded events.

Background
Requirements
Implementation Steps
Add the WebBrowser Control to Your Toolbox
Add a Class Module to Your Project
Edit Your Visual Basic Form
Run Your Project
Click the Button
Beyond the Basics
Related Topics
Background
Applications based on Visual Basic can contain WebBrowser objects, which can contain other objects—such as HTML document objects. WebBrowser objects, however, do not provide a way to trap all of the events that can occur in the objects they can contain. The WebBrowser control, for example, does not provide a way to trap the onclick event of a button element in an HTML document object the control contains.

Events in HTML document objects are forwarded up the document object's hierarchy until the top of the hierarchy is reached (or until event forwarding is canceled). This forwarding is referred to in Understanding the Event Model as bubbling. But events do not bubble to the document object's container. The top of an HTML document object's hierarchy is the document object. The WebBrowser control does not provide a way to trap the onclick event of a button element because the event does not bubble to the WebBrowser control.

If you use Dynamic HTML (DHTML), you can use the attachEvent method to bind functions to events, ensuring that a specified function is called whenever a particular event fires. This enables you to handle events in HTML document objects with code that's also included in the document object. But this approach limits you to DHTML and script. And the code in the document object is isolated from the container (and its container).

Event sinks can be used in Visual Basic-based applications to create customized ways of responding to events that are triggered by Microsoft ActiveX objects such as WebBrowser objects. This approach often uses WithEvents variables to handle events triggered by the ActiveX objects. But if the events don't bubble to the host applications, you can't trap the events. If you can't trap the events, you can't write procedures to handle them.

Note  For more information about WithEvents variables, see the Visual Basic documentation.
Requirements
You must use Visual Basic 6.0 or later to develop this application. The CallByName function, which this application uses to bind a method at run time, is not supported by earlier versions of Visual Basic. Microsoft Internet Explorer 4.0 or later must also be installed. The innerHTML property, which this application uses to create an HTML document object inside the WebBrowser control, is not supported by earlier versions of Windows Internet Explorer.

Note  For more information about the CallByName function, see the Visual Basic documentation.
The instructions in this tutorial are written for all levels of developers who use Visual Basic. If you are familiar with the terminology of object-oriented programming—terms such as properties, methods, and events—you should be able to follow the steps in this tutorial. Familiarity with HTML, DHTML, and the document object model (DOM) is also useful.

Implementation Steps
Begin by creating a standard EXE project. This project consists of a class module and a single Visual Basic form that contains the WebBrowser control. The following sections explain how to create and debug these objects.

Add the WebBrowser Control to Your Toolbox
From the Project menu, select References and add a reference to the Microsoft Internet Controls by checking the appropriate box. The WebBrowser control icon, which looks like a globe, should appear in your toolbox. If this icon does not appear, from the Project menu, select Components and check the box for Microsoft Internet Controls on the Controls tab.

Add a Class Module to Your Project
Follow these steps to create your project's class module:

From the Project menu, select Add Class Module, and then click Open on the New tab of the Add Class Module dialog box.
By default, the Name property of your new class is set to Class1. You can enter another name in the Properties window for your class module. The class in this tutorial is named clsForward because members of the class forward events from document objects in the WebBrowser control to the host application.

In the General Declarations section of your class module, use the Dim keyword to declare three variables with local (module) scope, as follows:
Copy Code
Option Explicit

    Dim oObject as Object
    Dim sMethod as String
    Dim bInstantiated as BooleanThe purpose of each variable becomes clear as you implement the next several steps.

Note  For more information about the Option Explicit statement, see the Visual Basic documentation.
Select Class from the drop-down list on the left side of your class module's code window. An Initialize procedure stub is created automatically. Use the Initialize procedure to set the value of the Boolean variable to False.
Copy Code
Public Sub Class_Initialize()

    bInstantiated = False
  
End SubThe Initialize procedure is called when a new member of the class is created. The Boolean variable bInstantiated is a flag. When a new member of the class is instantiated, code in the instantiating procedure changes the value of bInstantiated to True.

Add a procedure to your class module by selecting Add Procedure from the Tools menu. Select the radio buttons for Sub and Public in the Type and Scope frames, respectively, of the Add Procedure dialog box, and enter a name for your procedure in the Name field. In the following example, the procedure is named Set_Destination.
Copy Code
Public Sub Set_Destination(oInObject as Object, sInMethod as String)

    Set oObject = oInObject
    sMethod = sInMethod
    bInstantiated = True
  
End SubThis procedure instantiates a new member of the class. Notice that this is where you change the value of the Boolean variable bInstantiated to True. The first parameter to this procedure is the object that handles events trapped by members of the class. The second parameter represents the procedure that handles the trapped event—the "destination" procedure.

Add one more public subprocedure to your class module. This is where the CallByName function binds the destination procedure at run time. In the next step, you'll make this procedure the default method of your class.
Copy Code
Public Sub My_Default_Method()

    If bInstantiated Then
        CallByName oObject, sMethod, vbMethod
    End if
  
End SubIt is left to you to write an error-handling procedure to deal with the case where a member of the class was not previously instantiated (bInstantiated is False).

Follow these steps to identify the procedure just added as the default method of your class. This enables you to invoke the method without explicitly calling it.
Open the Object Browser either by selecting it from the View menu or by pressing the F2 key.
Click your project's name in the Object Browser's upper drop-down list.
Click your class module's name in the Classes list on the left side of the Object Browser. The members of your class are displayed in the right side of the Object Browser.
Right-click the method of your class module that you want to be your default method, and select Properties from the context menu.
Click the Advanced button on the Procedure Attributes dialog box. From the drop-down list of Procedure IDs, select Default and then click OK.
Edit Your Visual Basic Form
Follow these steps to create your project's user interface and your form's procedure code:

Place a WebBrowser control on your form. This control represents your project's entire user interface. In the Properties window for your control, the default Name property is WebBrowser1. The Name property of the WebBrowser control in this tutorial is set to wbMyBrowser.
In the code window for your form, enter the following code for your form's Load event.
Copy Code
Private Sub Form_Load()

    'Download blank page
    wbMyBrowser.Navigate2 "about:blank"
       
End SubThis procedure uses the WebBrowser control's Navigate2 method to download a blank HTML page.

Add a public subprocedure to your form. This is the destination procedure that handles events in the WebBrowser control. Members of your class forward events from a document object in the WebBrowser control to this procedure.
Copy Code
Public Sub Some_Procedure()

    MsgBox "Some_Procedure was called."

End SubThis simple procedure displays a message box to inform you that it was called successfully.

Select your WebBrowser control's name from the left drop-down list in the code window for your form. Then select the DocumentComplete event from the right drop-down list. (You can delete the code stub for the StatusTextChange event that was created automatically.) The WebBrowser control fires the DocumentComplete event when it finishes downloading a Web page (in this case, about:blank).
Copy Code
Private Sub wbMyBrowser_DocumentComplete(ByVal pDisp as Object,
    URL As Variant)

    'Declare new member of class
    Dim cfForward As clsForward  

    'Create HTML document inside WebBrowser control
    Dim sHTML As String
    sHTML = "<P>This is some text.</P>"
    sHTML = sHTML & "<P>And here is a button.</P>"
    sHTML = sHTML & "<BUTTON ID=btnMyButton>"
    sHTML = sHTML & "Click this button.</BUTTON>"
  
    wbMyBrowser.Document.body.innerHTML = sHTML  
  
    'Instantiate new member of class
    Set cfForward = New clsForward   
    cfForward.Set_Destination Me, "Some_Procedure"
    wbMyBrowser.Document.All("btnMyButton").onclick = cfForward

End SubAfter you declare a local variable of your class type (the class is named clsForward in the example), use the New keyword to create a new member of your class. The name of the public subprocedure you created in the previous step (named Some_Procedure in the example) is the second parameter in a call to the procedure that instantiates the new member of your class (named Set_Destination in the example). Some_Procedure is the destination procedure that handles the trapped event. Then you assign the onclick event of all the elements with a given ID property that are contained by a document object in your WebBrowser control to the default method of the new member of your class.

Using the innerHTML property of the BODY object, your program creates an HTML document inside the WebBrowser control.

Run Your Project
Now that your code is complete, set a break point in the first executable line of your form's DocumentComplete event to begin debugging the project. Then compile your project by selecting Start With Full Compile from the Run menu, or by pressing the CTRL and F5 keys simultaneously.

When the WebBrowser control finishes loading about:blank, your code executes as follows:

The DocumentComplete procedure calls the Initialize procedure of the class module to create a new member of the class.
The Set and New keywords are used to instantiate a new member of the class. Your form (Me) is sent as the first parameter to the Set_Destination procedure in your class module. The form is the object that receives forwarded events. The name of the function is the second parameter to the Set_Destination procedure, so this is the procedure that executes when events are forwarded to your form. The name of the function is in quotation marks because it is sent as a string, a syntax requirement of the CallByName function. The last line of code in your form's Load event establishes the event to be forwarded&#151;in this example, the onclick event of the button element named btnMyButton.
Click the Button
When the new member of your class is instantiated in your form's DocumentComplete event, values are set in the class module for the object, string, and Boolean variables. When you click the button, all that's left is for the default method of your class to forward the event to the destination procedure specified in your form.

Beyond the Basics
This tutorial shows you how to trap and handle events in a Web page you created yourself. You have the luxury of knowing in advance what elements are on the page. And you know the events these elements can fire.

You can also trap events in random Web pages, where you do not know what elements they contain in advance. When you develop applications that trap and handle events on random Web pages, keep the following in mind:

The WebBrowser control fires a DocumentComplete event for each frame element in a frameSet element.
The all collection returns a collection if there is more than one element with the same ID property. Use all when you are sure that only one element will be returned.
Because WebBrowser objects are ActiveX objects, the WebBrowser objects can host objects other than HTML documents. Check for the type of document you are hosting.
You are not limited to trapping the onclick event of a button element. There are many other DHTML Events you can trap, and for many other elements. You can trap the onmouseover event for a elements, for example. You can trap events that bubble to the body element. And your destination procedures can do more than display a simple message box.

Related Topics

About the W3C Document Object Model
Using the WebBrowser Control
Understanding the Event Model
Reference for Visual Basic Developers
发表于 2009-7-7 14:31:55 | 显示全部楼层
4# tisiem

可惜?楼主觉得哪里可惜了,或者觉得AutoIt在哪方面不如VB?
GUICtrlCreateObj已经说得很清楚了 -- GUI 函数 GUICtrlRead 和 GUICtrlSet 不能对这个控件有任何影响(当然GUIRegisterMsg也不能截取到内嵌IE产生的事件), 这个对象只能调用它自身($ObjectVar)的"方法"( 'methods')或者属性('properties'),VB不是这样吗?
 楼主| 发表于 2009-7-7 15:22:13 | 显示全部楼层
楼上,所说可惜,就是说不明白的东西很多的时候,能在网上找到的示例和社区autoit比VB少得多,所以解决起来就慢。

比如现在我就是不明白怎么对内嵌在autoit里的的IE控件的NewWindow事件编程,老也找不到方法,在网上看到的是关于VB里如何做的示例代码。楼上帮个忙啊,我实在是没有找到,帮忙写个示例造福朋友们吧。
 楼主| 发表于 2009-7-8 17:50:39 | 显示全部楼层
顶一下,请高手给IE控件的NewWindow事件编程做段示例代码。
发表于 2009-7-9 05:00:54 | 显示全部楼层
方向错了  控制ie 用_ie系列的UDF
这是au3的强项啊

你打开ie.au3看下就明白了 一样是对webBrowser对象的操作 只是帮你封装好了
 楼主| 发表于 2009-7-9 14:01:42 | 显示全部楼层
本帖最后由 tisiem 于 2009-7-9 14:36 编辑

感谢楼上,但是现在内嵌IE里点击链接,会跑到外面去打开。我现在需要的是还在原内嵌IE窗口打开。

我在另一个贴子也提及了: http://www.autoitx.com/forum.php?mod=viewthread&tid=8144
发表于 2009-7-11 02:44:27 | 显示全部楼层
You can also trap events in random Web pages, where you do not know what elements they contain in advance. When you develop applications that trap and handle events on random Web pages

从文章可以看出是可以实现的
只是au3不知道能否办到
继续探讨。。。
 楼主| 发表于 2009-7-12 12:13:24 | 显示全部楼层
是啊,其实就是捕获WebBrowser控件的NewWindow事件,我们常用的这些IE内核的浏览器,也都是这样来实现在它自己的新标签页里打开新的页面的功能的。
但不知道在autoit3里如何对WebBrowser此类控件的事件编写回调函数,这里说的是WebBrowser这类控件,如果是button这类控件,已经有了现成的例子了,在我们翻出的例子里都有示范,只要找到相应的消息和事件的宏定义即可。但象WebBrowser或者其它第三方提供的控件,就一直没有找到示范程序。
发表于 2009-7-13 12:13:24 | 显示全部楼层
看看这个链接,不知对你有没有帮助

http://www.autoitscript.com/foru ... =1&#entry145651
发表于 2009-7-13 13:10:07 | 显示全部楼层
楼上的代码我拷贝过来了
#include <IE.au3>

$oIE = _IECreate()
_IENavigate($oIE, "http://www.google.com")

; Create an alert when the document is clicked
_IEInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")

; Prevent the right-click context menu
_IEInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu');return false;")

;===============================================================================
;
; Function Name:    _IEInsertEventScript()
; Description:      Inserts a Javascript 
; Parameter(s):     $o_object   - object variable for a InternetExplorer.Application, Window or Frame
;                   $s_htmlFor  - the HTML element for event monitoring (e.g. "document" or an element ID)
;                   $s_event    - the event to monitor (e.g. "onclick" or "oncontextmenu")
;                   $s_script   - Javascript to be executed
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - 1  and sets @ERROR = 0
;                  On Failure - 0  and sets @ERROR = 1
; Author(s):        Dale Hohm
;
;===============================================================================
;
Func _IEInsertEventScript($o_object, $s_htmlFor, $s_event, $s_script)
    If IsObj($o_object) Then
        SetError(0)
        Local $o_head = $o_object.document.all.tags("HEAD").Item(0)
        Local $o_script = $o_object.document.createElement("script")
        With $o_script
            .defer = True
            .language="jscript"
            .type = "text/javascript"
            .htmlFor = $s_htmlFor
            .event = $s_event
            .text = $s_script
        EndWith
        $o_head.appendChild($o_script)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc
问题是他只是在原网页写入"onclick", "alert('Someone clicked the document!');"
达到点击提示的目的
离解决楼主这个问题还是有差距的
发表于 2009-10-8 06:28:04 | 显示全部楼层
可以用ObjEvent创建一个事件作为接收。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-16 07:01 , Processed in 0.084385 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表