共用方式為


HOW TO:使用 Win32 裝載容器進行點擊測試

更新:2007 年 11 月

您可以透過提供視覺物件的裝載視窗容器 (Container),在 Win32 視窗內建立視覺物件。若要為包含的視覺物件提供事件處理功能,請處理傳遞至裝載視窗容器之訊息篩選迴圈 (Loop) 的訊息。如需如何在 Win32 視窗中裝載視覺物件的詳細資訊,請參閱教學課程:在 Win32 應用程式中裝載視覺物件

範例

下列程式碼示範如何設定滑鼠事件處理常式 (Event Handler),以處理做為視覺物件容器的 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;
}

下列範例示範如何設定點擊測試 (Hit Test),以回應截獲特定滑鼠事件。

// 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 物件將 Windows Presentation Foundation (WPF) 內容呈現在 Win32 視窗內。HwndSource 物件的 RootVisual 屬性值代表視覺化樹狀結構階層中最上面的節點。

如需使用 Win32 裝載容器進行物件點擊測試的詳細資訊,請參閱使用 Win32 互通性進行點擊測試範例

請參閱

概念

視覺分層中的點擊測試

教學課程:在 Win32 應用程式中裝載視覺物件

參考

HwndSource