CommonDialog.HookProc(IntPtr, Int32, IntPtr, IntPtr) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义要重写的通用对话框挂钩过程,以便向通用对话框添加特定功能。
protected:
virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
protected virtual IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
abstract member HookProc : nativeint * int * nativeint * nativeint -> nativeint
override this.HookProc : nativeint * int * nativeint * nativeint -> nativeint
Protected Overridable Function HookProc (hWnd As IntPtr, msg As Integer, wparam As IntPtr, lparam As IntPtr) As IntPtr
参数
- hWnd
-
IntPtr
nativeint
对话框窗口的句柄。
- msg
- Int32
正在接收的消息。
- wparam
-
IntPtr
nativeint
关于消息的附加信息。
- lparam
-
IntPtr
nativeint
关于消息的附加信息。
返回
-
IntPtr
nativeint
如果默认对话框过程处理此消息,则为零值;如果默认对话框过程忽略此消息,则为非零值。
示例
下面的代码示例演示如何重写 HookProc 方法。 该示例由继承该类的 CommonDialog 类组成。 在类的HookProc重写中,该示例根据特定Windows消息的msg
常量值评估方法的参数。 msg
如果参数等于指定的常量,则示例将写入跟踪输出,用于标识传递给HookProc该方法的Windows消息。 此示例假定声明该方法的 HookProc 类继承该 CommonDialog 类。
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...
[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.
#if defined(TRACE)
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;
}
#endif
// Always call the base class hook procedure.
return FontDialog::HookProc( hWnd, msg, wParam, lParam );
}
// 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...
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);
}
' 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
注解
挂钩过程是一种机制,函数可以在事件到达应用程序之前截获事件。 重写 HookProc 类的方法 CommonDialog 时,操作系统将调用函数的替代,以便将操作系统消息发布到窗口。
默认情况下,挂钩过程将屏幕上的对话框居中,以响应 WM_INITDIALOG
消息。
继承者说明
继承类可以重写此方法,以将特定功能添加到通用对话框。 在派生类中重写 HookProc(IntPtr, Int32, IntPtr, IntPtr) 时,请务必调用基类 HookProc(IntPtr, Int32, IntPtr, IntPtr) 的方法。