我怎么知道鼠标何时在代码创建的图片框上? [英] How do I know when the mouse is over a picture box created by code?

查看:97
本文介绍了我怎么知道鼠标何时在代码创建的图片框上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些代码,我需要创建一个上下文菜单条,当它在不同的图片框上时更改它的项目,确切地说是28。

我尝试过使用Addhandler但是它不行。

我还需要通过使用我做的Addhandler女巫来改变它们的大小和位置。

所以基本上如果按下鼠标右键就会改变大小(这是我能够做到的)如果按下鼠标左键,它应该打开上下文菜单条(我不能这样做)。

这是我上一次尝试的样本:



Hi, I am writing some code and I need to create a context menu strip that changes it's items when is over different picture boxes, 28 to be exact.
I have tried using Addhandler but it doesn't work.
I also needed to change their size and location by using the Addhandler witch i did.
So basically if the mouse right button is pressed it changes size (this I was able to do) and if the mouse left button is pressed it should open the context menu strip(this I couldn't do).
Were is a sample of my last try:

Private Sub CCTVs_Handlers()
        AddHandler CCTV1.Click, Sub(obj As Object, args As EventArgs) Drop1()
        AddHandler CCTV1.DoubleClick, Sub(obj As Object, args As EventArgs) CAM1() ' This is to resize the Picturebox , CCTV1 is the Picturebox 
End sub


Private Sub Show_CCTV()
              Me.Text = WText + " - CCTV"
        Panel3.Show()
        Panel3.Location = New Point(76, 3)
        Panel3.Width = 1260
        Panel3.Height = 665

        Dim CCTV1 As New PictureBox
        With CCTV1
            .Location = New Point(0, 0)
            .Width = 90
            .Height = 90
            .BorderStyle = BorderStyle.Fixed3D
            .BackColor = Color.Green
            Panel3.Controls.Add(CCTV1)
            Dim lb As New Label
            With lb
                .Text = "CAM1"
                .Location = New Point(0, 0)
                CCTV1.Controls.Add(lb)
            End With
        End With
End Sub

 Private Sub CAM1()
        If CCTV1.Width = 90 Then
            CCTV1.Width = 400
            CCTV1.Height = 400
            CCTV1.BringToFront()
        Else
            CCTV1.Width = 90
            CCTV1.Height = 90
        End If
    End Sub

    Private Sub Drop1()
               Try
            Dim CCTV1d1 As New ContextMenuStrip
            With CCTV1d1
                .Items.Add("Disable CAM1")
                .Items.Add("Stop Recording")
                .Items.Add("CAM1 Propreties")
                .Items.Add("CAM1 Settings")
                .Items.Add("CCTV Settings")
                .DropShadowEnabled = False
                .BackgroundImageLayout = False
                CCTV1.ContextMenuStrip = CCTV1d1
                CCTV1.Controls.Add(CCTV1d1)

            End With
        Catch ex As Exception
            MsgBox("CCTV1d1 : " & ex.Message & " - " & ex.Source)
        End Try
    End Sub





我是什么尝试过:



我尝试过使用Addhandler但它不起作用

我试图将它链接到其他子所以它可以调用上下文菜单,但这也没有用,现在我没有想法

请帮助



What I have tried:

I have tried using Addhandler but it doesn't work
I tried to link it to other sub so it could call the context menu but this also didn't work and right now i am out of ideas
Please help

推荐答案

如果我们假设在运行时可以绘制任意数量的框,为每个项目设置一组唯一的事件处理程序是不切实际的。不需要28个不同的事件处理程序:只是,例如,只是一个处理程序,知道你正在盘旋的是哪个框。



所以,你可以附加将mouseIn事件发送到图像的框。这将设置一个值 - 声明在比您的框或处理程序更高的范围内,每个框都是唯一的,这样当您单击任何特定框时,onClick(右/左/双)可以知道您单击了哪个特定框。 />


事件处理程序然后可以使用该值来确定如何响应。
If we assume an arbitrary number of boxes could be drawn at runtime, it's not practical to have a unique set of event handlers for each item. No need for 28 different event handlers: just, for example, just a handler for that is aware of which box you're hovering over.

So, you could attach a "mouseIn" event to your image's 'box'. This would set a value - declared at a higher scope than your box or handlers, unique to each box, such that when you click on any particular box the onClick (right/left/double) can know which particular box you clicked.

The event handler can then use the value to determine how to respond.


这篇关于我怎么知道鼠标何时在代码创建的图片框上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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