方法 : Win32 ホスト コンテナを使用してヒット テストを実行する

更新 : 2007 年 11 月

ビジュアル オブジェクトのホスト ウィンドウ コンテナを提供することで、Win32 ウィンドウにビジュアル オブジェクトを作成できます。格納されているビジュアル オブジェクト用のイベント処理機能を提供するには、ホスト ウィンドウ コンテナのメッセージ フィルタ ループに渡されるメッセージを処理します。Win32 ウィンドウでビジュアル オブジェクトをホストする方法の詳細については、「チュートリアル : Win32 アプリケーションでのビジュアル オブジェクトのホスト」を参照してください。

使用例

次のコードでは、ビジュアル オブジェクトのホスト コンテナとして使用される Win32 ウィンドウにマウス イベント ハンドラを設定する方法を示します。

// Constant values from the "winuser.h" header file.
internal const int WM_LBUTTONUP = 0x0202,
                   WM_RBUTTONUP = 0x0205;

internal static IntPtr ApplicationMessageFilter(
    IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    // Handle messages passed to the visual.
    switch (message)
    {
        // Handle the left and right mouse button up messages.
        case WM_LBUTTONUP:
        case WM_RBUTTONUP:
            System.Windows.Point pt = new System.Windows.Point();
            pt.X = (uint)lParam & (uint)0x0000ffff;  // LOWORD = x
            pt.Y = (uint)lParam >> 16;               // HIWORD = y
            MyShape.OnHitTest(pt, message);
            break;
    }

    return IntPtr.Zero;
}

トラップ固有のマウス イベントに対応してヒット テストを設定する方法を次の例に示します。

// Constant values from the "winuser.h" header file.
public const int WM_LBUTTONUP = 0x0202,
                 WM_RBUTTONUP = 0x0205;

// Respond to WM_LBUTTONUP or WM_RBUTTONUP messages by determining which visual object was clicked.
public static void OnHitTest(System.Windows.Point pt, int msg)
{
    // Clear the contents of the list used for hit test results.
    hitResultsList.Clear();

    // Determine whether to change the color of the circle or to delete the shape.
    if (msg == WM_LBUTTONUP)
    {
        MyWindow.changeColor = true;
    }
    if (msg == WM_RBUTTONUP)
    {
        MyWindow.changeColor = false;
    }

    // Set up a callback to receive the hit test results enumeration.
    VisualTreeHelper.HitTest(MyWindow.myHwndSource.RootVisual,
                             null,
                             new HitTestResultCallback(CircleHitTestResult),
                             new PointHitTestParameters(pt));

    // Perform actions on the hit test results list.
    if (hitResultsList.Count > 0)
    {
        ProcessHitTestResultsList();
    }
}

HwndSource オブジェクトは、Win32 ウィンドウ内の Windows Presentation Foundation (WPF) コンテンツを表示します。HwndSource オブジェクトの RootVisual プロパティの値は、ビジュアル ツリー階層の最上位ノードを表します。

Win32 ホスト コンテナを使用するオブジェクトのヒット テストのサンプル全体については、「Win32 相互運用によるヒット テストのサンプル」を参照してください。

参照

概念

ビジュアル層でのヒット テスト

チュートリアル : Win32 アプリケーションでのビジュアル オブジェクトのホスト

参照

HwndSource