Share via


방법: 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 창 안에 WPF(Windows Presentation Foundation) 콘텐츠를 표시합니다. HwndSource 개체의 RootVisual 속성 값은 시각적 트리 계층 구조의 최상위 노드를 나타냅니다.

Win32 호스트 컨테이너를 사용한 적중 테스트 개체에 대한 전체 샘플을 보려면 Win32 상호 운용 적중 테스트 샘플을 참조하십시오.

참고 항목

개념

시각적 계층에서 적중 테스트

자습서: Win32 응용 프로그램에서 시각적 개체 호스팅

참조

HwndSource