共用方式為


CommonDialog.HookProc(IntPtr, Int32, IntPtr, IntPtr) 方法

定義

定義為了將特定功能加入通用對話方塊中而覆寫的通用對話方塊攔截程序。

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) 方法。

適用於