CommonDialog.HookProc 方法

定义要重写的通用对话框挂钩过程,以便向通用对话框添加特定功能。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Protected Overridable Function HookProc ( _
    hWnd As IntPtr, _
    msg As Integer, _
    wparam As IntPtr, _
    lparam As IntPtr _
) As IntPtr
用法
Dim hWnd As IntPtr
Dim msg As Integer
Dim wparam As IntPtr
Dim lparam As IntPtr
Dim returnValue As IntPtr

returnValue = Me.HookProc(hWnd, msg, wparam, lparam)
protected virtual IntPtr HookProc (
    IntPtr hWnd,
    int msg,
    IntPtr wparam,
    IntPtr lparam
)
protected:
virtual IntPtr HookProc (
    IntPtr hWnd, 
    int msg, 
    IntPtr wparam, 
    IntPtr lparam
)
protected IntPtr HookProc (
    IntPtr hWnd, 
    int msg, 
    IntPtr wparam, 
    IntPtr lparam
)
protected function HookProc (
    hWnd : IntPtr, 
    msg : int, 
    wparam : IntPtr, 
    lparam : IntPtr
) : IntPtr

参数

  • hWnd
    对话框窗口的句柄。
  • msg
    正在接收的消息。
  • wparam
    关于消息的附加信息。
  • lparam
    关于消息的附加信息。

返回值

如果默认对话框过程处理此消息,则为零值;如果默认对话框过程忽略此消息,则为非零值。

备注

挂钩过程是在事件到达应用程序之前,函数可通过它来截获事件的机制。重写 CommonDialog 类的 HookProc 方法时,操作系统将调用对函数的重写,以将操作系统消息发送到窗口。

默认情况下,挂钩过程在屏幕上居中显示对话框来响应 WM_INITDIALOG 消息。

提示

此方法使用 SecurityAction.LinkDemand 防止不可信代码对它进行调用;只有直接调用方才需要具有 SecurityPermissionAttribute.UnmanagedCode 权限。如果您的代码可从部分受信任的代码调用,则未经验证不要将用户输入传递给 Marshal 类方法。有关使用 LinkDemand 成员的重要限制,请参见 Demand 和 LinkDemand

该属性还使用 SecurityAction.InheritanceDemand 安全属性;要重写该成员,派生类必须具有 CustomPermission 权限。

给继承者的说明 继承类可以重写此方法以向通用对话框添加特定功能。重写派生类中的 HookProc 时,确保调用基类的 HookProc 方法。

示例

下面的代码示例演示如何重写 HookProc 方法。此示例由继承 CommonDialog 类的类组成。在该类的 HookProc 重写中,示例根据特定的 Windows 消息的常数值来计算方法的 msg 参数。如果 msg 参数等于指定的常数,则示例写入跟踪输出(它标识被传递到 HookProc 方法的 Windows 消息)。此示例假定在其中声明 HookProc 方法的类继承 CommonDialog 类。

    ' Defines the constants for Windows messages.

    Const WM_SETFOCUS = &H7
    Const WM_INITDIALOG = &H110
    Const WM_LBUTTONDOWN = &H201
    Const WM_RBUTTONDOWN = &H204
    Const WM_MOVE = &H3
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function HookProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr


        ' Evaluates the message parameter to determine the user action.

        Select Case msg

            Case WM_INITDIALOG
                System.Diagnostics.Trace.Write("The WM_INITDIALOG message was received.")
            Case WM_SETFOCUS
                System.Diagnostics.Trace.Write("The WM_SETFOCUS message was received.")
            Case WM_LBUTTONDOWN
                System.Diagnostics.Trace.Write("The WM_LBUTTONDOWN message was received.")
            Case WM_RBUTTONDOWN
                System.Diagnostics.Trace.Write("The WM_RBUTTONDOWN message was received.")
            Case WM_MOVE
                System.Diagnostics.Trace.Write("The WM_MOVE message was received.")

        End Select

        ' Always call the base class hook procedure.

        Return MyBase.HookProc(hWnd, msg, wParam, lParam)

    End Function
// Defines the constants for Windows messages.

const int WM_SETFOCUS = 0x0007;
const int WM_INITDIALOG = 0x0110;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_RBUTTONDOWN = 0x0204;
const int WM_MOVE = 0x0003;

// Overrides the base class hook procedure...
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
{

    // Evaluates the message parameter to determine the user action.

    switch(msg)
    {

        case WM_INITDIALOG:
            System.Diagnostics.Trace.Write("The WM_INITDIALOG message was received.");
            break;
        case WM_SETFOCUS:
            System.Diagnostics.Trace.Write("The WM_SETFOCUS message was received.");
            break;
        case WM_LBUTTONDOWN:
            System.Diagnostics.Trace.Write("The WM_LBUTTONDOWN message was received.");
            break;
        case WM_RBUTTONDOWN:
            System.Diagnostics.Trace.Write("The WM_RBUTTONDOWN message was received.");
            break;
        case WM_MOVE:
            System.Diagnostics.Trace.Write("The WM_MOVE message was received.");
            break;

    }

    // Always call the base class hook procedure.

    return base.HookProc(hWnd, msg, wParam, lParam);

}
   private:
      // Defines the constants for Windows messages.
      literal int WM_SETFOCUS = 0x0007;
      literal int WM_INITDIALOG = 0x0110;
      literal int WM_LBUTTONDOWN = 0x0201;
      literal int WM_RBUTTONDOWN = 0x0204;
      literal int WM_MOVE = 0x0003;

   protected:
      // Overrides the base class hook procedure...
//      [System::Security::Permissions::SecurityPermission(System::Security::Permissions::SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)] 
      [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)] 
      virtual IntPtr HookProc( IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam ) override
      {
         // Evaluates the message parameter to determine the user action.
         switch ( msg )
         {
            case WM_INITDIALOG:
               System::Diagnostics::Trace::Write( "The WM_INITDIALOG message was received." );
               break;
            case WM_SETFOCUS:
               System::Diagnostics::Trace::Write( "The WM_SETFOCUS message was received." );
               break;
            case WM_LBUTTONDOWN:
               System::Diagnostics::Trace::Write( "The WM_LBUTTONDOWN message was received." );
               break;
            case WM_RBUTTONDOWN:
               System::Diagnostics::Trace::Write( "The WM_RBUTTONDOWN message was received." );
               break;
            case WM_MOVE:
               System::Diagnostics::Trace::Write( "The WM_MOVE message was received." );
               break;
         }
         
         // Always call the base class hook procedure.
         return FontDialog::HookProc( hWnd, msg, wParam, lParam );
      }
    // Defines the constants for Windows messages.
    final int WM_SETFOCUS = 0x7;
    final int WM_INITDIALOG = 0x110;
    final int WM_LBUTTONDOWN = 0x201;
    final int WM_RBUTTONDOWN = 0x204;
    final int WM_MOVE = 0x3;

    // Overrides the base class hook procedure...
//    /** @attribute System.Security.Permissions.PermissionSet(System.Security.
//        Permissions.SecurityAction.Demand, Name = "FullTrust")
//     */
    /** @attribute SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)
     */
    protected IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, 
        IntPtr lParam)
    {
        // Evaluates the message parameter to determine the user action.
        switch (msg) {
            case WM_INITDIALOG :
                System.Diagnostics.Trace.Write(
                    "The WM_INITDIALOG message was received.");
                break;

            case WM_SETFOCUS :
                System.Diagnostics.Trace.Write(
                    "The WM_SETFOCUS message was received.");
                break;

            case WM_LBUTTONDOWN :
                System.Diagnostics.Trace.Write(
                    "The WM_LBUTTONDOWN message was received.");
                break;

            case WM_RBUTTONDOWN :
                System.Diagnostics.Trace.Write(
                    "The WM_RBUTTONDOWN message was received.");
                break;

            case WM_MOVE :
                System.Diagnostics.Trace.Write(
                    "The WM_MOVE message was received.");
                break;
        }

        // Always call the base class hook procedure.
        return super.HookProc(hWnd, msg, wParam, lParam);

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

CommonDialog 类
CommonDialog 成员
System.Windows.Forms 命名空间