Silverlight 窗口加载后检测鼠标位置

发布于 2024-08-23 04:39:10 字数 159 浏览 4 评论 0原文

我正在运行一个网页,其中有一个 SL“框”。

我知道如何使用 MouseEnter 和 MouseLeave 来检测鼠标是否已进入 SL 框或离开它。

我的问题是,当鼠标刚刚加载时,如何检测鼠标是在 SL 盒内部还是外部。

谢谢。

吉拉德.

I'm running a web page that has a SL 'box' in it.

I know how to use the MouseEnter and MouseLeave to detect if the mouse has entered the SL box or left it.

My question is how to detect if the mouse is inside or outside the SL box when it has just loaded.

Thanks.

Gilad.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2024-08-30 04:39:10

这是构建 Silverlight 应用程序的分步帖子,该应用程序在实例化时检测鼠标是否位于 Silverlight 控件上方。

第 1 步:使用 Visual Studio 创建示例 Silverlight 应用程序(文件/新建项目/Silverlight 应用程序)

第 2 步:编辑 MainPage.xaml,并将以下代码放入UserControl 的网格:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock x:Name="x_Text" Text="Mouse Was Not Over" />
</Grid>

第 3 步:编辑 MainPage.cs,并用以下代码替换 MainPage 类:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        base.MouseEnter += OnMouseEnter;
        base.Loaded += OnLoaded;
    }

    void OnMouseEnter(object sender, MouseEventArgs e)
    {
        x_Text.Text = "Mouse Was Over";
        base.MouseEnter -= OnMouseEnter;
    }

    void OnLoaded(object sender, EventArgs e)
    {
        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
        timer.Interval = new TimeSpan(300 * TimeSpan.TicksPerMillisecond);
        timer.Tick += delegate(object senderTick, EventArgs eTick)
        {
            base.MouseEnter -= OnMouseEnter;
            timer.Stop();
        };
        timer.Start();
    }
}

第 4 步:构建并运行!尝试将鼠标悬停在 Silverlight 控件的中心和控件外部以查看结果!

Here is a step-by-step post for building a Silverlight app that detects whether the mouse is over the Silverlight control when it is instantiated.

Step 1: Create a sample Silverlight application with Visual Studio (File / New Project / Silverlight Application)

Step 2: Edit the MainPage.xaml, and place the following code inside the UserControl's Grid:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock x:Name="x_Text" Text="Mouse Was Not Over" />
</Grid>

Step 3: Edit the MainPage.cs, and replace the MainPage class with the following code:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        base.MouseEnter += OnMouseEnter;
        base.Loaded += OnLoaded;
    }

    void OnMouseEnter(object sender, MouseEventArgs e)
    {
        x_Text.Text = "Mouse Was Over";
        base.MouseEnter -= OnMouseEnter;
    }

    void OnLoaded(object sender, EventArgs e)
    {
        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
        timer.Interval = new TimeSpan(300 * TimeSpan.TicksPerMillisecond);
        timer.Tick += delegate(object senderTick, EventArgs eTick)
        {
            base.MouseEnter -= OnMouseEnter;
            timer.Stop();
        };
        timer.Start();
    }
}

Step 4: Build and Run! Try it with the mouse over the center of the Silverlight control and outside of the control to see the results!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文