无法使MouseDown在HwndHost派生类中工作 [英] Cannot get MouseDown to work in HwndHost derived class

查看:96
本文介绍了无法使MouseDown在HwndHost派生类中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在点击HwndHost窗口时调用MouseDown事件?



我尝试过以下方法:



在C ++ / CLI中:

How can I get the MouseDown event to be called when clicking on the HwndHost window?

I have tried the following:

In C++/CLI:

public ref class GameWindow : public HwndHost
{
    // source
};





在C#中:



In C#:

public partial class MainWindow : Window
{
    // Only important source displayed...

    private GameWindow mWindowWorld;

    private void Window_Loaded( Object sender, RoutedEventArgs e )
    {
        mWindowWorld = new GameWindow();

        // Tried this first...
        mWindowWorld.MouseDown += new MouseButtonEventHandler( Window_World_MouseDown );

        // Still does not work...
        mWindowWorld.AddHandler( FrameworkElement.MouseDownEvent, new MouseButtonEventHandler( Window_World_MouseDown ), true );

        Window_World.Child = mWindowWorld;
    }

    private void Window_World_MouseDown( Object sender, MouseButtonEventArgs e )
    {
        base.OnMouseDown( e ); // breakpoint set here

        if( e.MiddleButton == MouseButtonState.Pressed )
        {}

        if( e.RightButton == MouseButtonState.Pressed )
        {}
    }
};





在XAML中:



In XAML:

<!-- Tried this as well... -->
<Border Name="Window_World" MouseDown="Window_World_MouseDown" BorderThickness="2" BorderBrush="#666666" Margin="0,0,8,0" />





我也试过使用PreviewMouseDown类型,但这也没有用。



如果让MouseEvent到以这种方式工作是不可能的,那么这是什么原因?



I have also tried using the PreviewMouseDown types, but that also did not work.

If getting the MouseEvent to work this way is not possible, then what is the reason for this?

推荐答案

可以通过使用委托的互操作来通过C#调用鼠标的事件。 br />


在C ++ / CLI中:

The events for the mouse can be called through C# via the interop using a delegate.

In C++/CLI:
public ref class GameWindow : public HwndHost
{
    delegate void func0();

    func0^        OnLeftMouseDown;
    func0^        OnRightMouseDown;
    func0^        OnMiddleMouseDown;
    func0^        OnMouseMove;

    IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, bool% handled )
    {
        switch( msg )
        {
            case WM_IME_SETCONTEXT:
                if( LOWORD( wParam.ToInt32() ) > 0)
                    SetFocus( mHWND );

                handled = true;
                return IntPtr::Zero;

            case WM_LBUTTONDOWN:
                OnLeftMouseDown();

                handled = true;
                return IntPtr::Zero;

            case WM_RBUTTONDOWN:
                OnRightMouseDown();

                handled = true;
                return IntPtr::Zero;

            case WM_MBUTTONDOWN:
                OnMiddleMouseDown();

                handled = true;
                return IntPtr::Zero;

            case WM_MOUSEMOVE:
                OnMouseMove();

                handled = true;
                return IntPtr::Zero;
        }

        handled = false;
        return IntPtr::Zero;
    }
};





在C#中:



In C#:

public partial class MainWindow : Window
{
    // Only important source displayed...

    private GameWindow mWindowWorld;
    private Point      mMousePos;
 
    private void Window_Loaded( Object sender, RoutedEventArgs e )
    {
        mWindowWorld = new GameWindow();
 
        mWindowWorld.OnLeftMouseDown   = Window_World_OnLeftMouseDown;
        mWindowWorld.OnRightMouseDown  = Window_World_OnRightMouseDown;
        mWindowWorld.OnMiddleMouseDown = Window_World_OnMiddleMouseDown;
        mWindowWorld.OnMouseMove       = Window_World_OnMouseMove;
 
        Window_World.Child = mWindowWorld;
    }
 
    private void Window_World_OnLeftMouseDown()
    {
        // TODO: Perform desired actions here.
    }

    private void Window_World_OnRightMouseDown()
    {
        // TODO: Perform desired actions here.
    }

    private void Window_World_OnMiddleMouseDown()
    {
        // TODO: Perform desired actions here.
    }

    private void Window_World_OnMouseMove()
    {
        // Probably want to do something like this...
        Point mousePos = Mouse.GetPosition( mWindowWorld );
        mMousePos = new Point( mousePos.X, mousePos.Y );
    }
};





虽然创建代理是一个解决方案,但最终我最终创建了每个鼠标按钮功能在GameWindow类中代码清洁。然后我从该类派生,所以我可以将HwndHost与主应用程序分开。



Although creating delegates is one solution, I ultimately ended up creating each mouse button function in the GameWindow class for code cleanliness. I then derived from that class, so I could keep the HwndHost separated from the main app.


这篇关于无法使MouseDown在HwndHost派生类中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆